1 | package agents.anac.y2017.gin;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map.Entry;
|
---|
10 |
|
---|
11 | import genius.core.AgentID;
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.actions.Accept;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.Offer;
|
---|
16 | import genius.core.issue.Value;
|
---|
17 | import genius.core.parties.AbstractNegotiationParty;
|
---|
18 | import genius.core.parties.NegotiationInfo;
|
---|
19 | import genius.core.persistent.StandardInfo;
|
---|
20 | import genius.core.persistent.StandardInfoList;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Agent Name: Gin. Team members: Jin Tanda. Affiliation: Nagoya Institute of
|
---|
24 | * Technology, Takayuki Ito. Laboratory Contact person: Jin Tanda. Contact
|
---|
25 | * E-mail: tanda.jin@itolab.nitech.ac.jp
|
---|
26 | */
|
---|
27 | public class Gin extends AbstractNegotiationParty {
|
---|
28 |
|
---|
29 | private Bid lastReceivedBid = null;
|
---|
30 | private NegotiationInfo info = null;
|
---|
31 | private StandardInfoList history;
|
---|
32 | private int fitToOpponent = 0;
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public void init(NegotiationInfo info) {
|
---|
36 |
|
---|
37 | super.init(info);
|
---|
38 |
|
---|
39 | System.out.println("Discount Factor is "
|
---|
40 | + getUtilitySpace().getDiscountFactor());
|
---|
41 | System.out.println("Reservation Value is "
|
---|
42 | + getUtilitySpace().getReservationValueUndiscounted());
|
---|
43 | // if you need to initialize some variables, please initialize them
|
---|
44 | // below
|
---|
45 | this.info = info;
|
---|
46 | initBidTable(); // 論点数に応じたhashmapによるデータ構造を定義
|
---|
47 | checkOpponentConcessions();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Each round this method gets called and ask you to accept or offer. The
|
---|
52 | * first party in the first round is a bit different, it can only propose an
|
---|
53 | * offer.
|
---|
54 | *
|
---|
55 | * @param validActions
|
---|
56 | * Either a list containing both accept and offer or only offer.
|
---|
57 | * @return The chosen action.
|
---|
58 | */
|
---|
59 | @Override
|
---|
60 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
61 | double myUtil = 0;
|
---|
62 | double threshold = 1;
|
---|
63 | boolean reservationPriceFlag = false;
|
---|
64 | Bid bid = null;
|
---|
65 | if (lastReceivedBid != null)
|
---|
66 | myUtil = utilitySpace.getUtility(lastReceivedBid);
|
---|
67 | threshold = concessionsFunction();
|
---|
68 | double reservationPrice = utilitySpace
|
---|
69 | .getReservationValueUndiscounted();
|
---|
70 | if (threshold < reservationPrice)
|
---|
71 | reservationPriceFlag = true;
|
---|
72 | else
|
---|
73 | reservationPriceFlag = false;
|
---|
74 | // if we are the first party, offer.
|
---|
75 | if ((lastReceivedBid == null || !validActions.contains(Accept.class)
|
---|
76 | || myUtil < threshold) && reservationPriceFlag == false) {
|
---|
77 | if (lastReceivedBid == null || info.getTimeline().getTime() < 0.3) {
|
---|
78 | while (myUtil < concessionsFunction()) {
|
---|
79 | bid = generateRandomBid();
|
---|
80 | myUtil = utilitySpace.getUtility(bid);
|
---|
81 | }
|
---|
82 | } else
|
---|
83 | bid = generateMyBidFromTable();
|
---|
84 | return new Offer(getPartyId(), bid);
|
---|
85 | } else {
|
---|
86 | // printTable();
|
---|
87 | return new Accept(getPartyId(), lastReceivedBid);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void checkOpponentConcessions() {
|
---|
92 | history = (StandardInfoList) getData().get();
|
---|
93 | if (!history.isEmpty()) {
|
---|
94 | int cnt = 0;
|
---|
95 | for (StandardInfo si : history) {
|
---|
96 | if (si.getAgreement().get1() == null) {
|
---|
97 | cnt++;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (((float) cnt / (float) history.size()) > 0.4) {
|
---|
101 | // 3者間の交渉が過去情報から参照してうまくいっていない時
|
---|
102 | // 自身の譲歩関数を定義し直す
|
---|
103 | fitToOpponent = 2;
|
---|
104 | } else if (((float) cnt / (float) history.size()) > 0.2) {
|
---|
105 | fitToOpponent = 1;
|
---|
106 | } else
|
---|
107 | fitToOpponent = 0;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | private Bid generateMyBidFromTable() {
|
---|
112 | Bid myBid = generateRandomBid();
|
---|
113 | // 各論点において、bidを頻出順にソート
|
---|
114 | List<List<Entry<Value, Integer>>> lists = new ArrayList<List<Entry<Value, Integer>>>();
|
---|
115 | for (int i = 0; i < bidTable.length; i++) {
|
---|
116 | List<Entry<Value, Integer>> list_entries = new ArrayList<Entry<Value, Integer>>(
|
---|
117 | bidTable[i].entrySet());
|
---|
118 | // 比較関数Comparatorを使用してMap.Entryの値を比較する(降順)
|
---|
119 | Collections.sort(list_entries,
|
---|
120 | new Comparator<Entry<Value, Integer>>() {
|
---|
121 | // compareを使用して値を比較する
|
---|
122 | @Override
|
---|
123 | public int compare(Entry<Value, Integer> obj1,
|
---|
124 | Entry<Value, Integer> obj2) {
|
---|
125 | // 降順
|
---|
126 | return obj2.getValue().compareTo(obj1.getValue());
|
---|
127 | }
|
---|
128 | });
|
---|
129 | lists.add(list_entries);
|
---|
130 | }
|
---|
131 | // 3者間における最良のものを寄せ集めたbid候補でbidを初期化
|
---|
132 | for (int i = 0; i < lists.size(); i++) {
|
---|
133 | Value candidate = lists.get(i).get(0).getKey();
|
---|
134 | myBid = myBid.putValue(i + 1, candidate);
|
---|
135 | }
|
---|
136 | // 各論点をtableを元に一つずつ変更し、myUtilの値に応じてループ
|
---|
137 | int maxLoop = 1000;
|
---|
138 | int cnt = 0;
|
---|
139 | double permissionRange;
|
---|
140 | while (utilitySpace.getUtility(myBid) < concessionsFunction()) {
|
---|
141 | if (cnt > maxLoop) {
|
---|
142 | double myUtil = utilitySpace.getUtility(myBid);
|
---|
143 | while (myUtil < concessionsFunction()) {
|
---|
144 | myBid = generateRandomBid();
|
---|
145 | myUtil = utilitySpace.getUtility(myBid);
|
---|
146 | }
|
---|
147 | // System.out.println("強制的にbidを決定します");
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | for (int i = 0; i < lists.size(); i++) {
|
---|
151 | List<Entry<Value, Integer>> issue = lists.get(i);
|
---|
152 | permissionRange = ((double) cnt / (double) (maxLoop + 1))
|
---|
153 | * (issue.size());
|
---|
154 | // System.out.println("cnt:issue.size() = " + cnt + " " +
|
---|
155 | // issue.size());
|
---|
156 | // System.out.println("permissionRange = " + permissionRange);
|
---|
157 | Value value = issue.get((int) (Math.random() * permissionRange))
|
---|
158 | .getKey();
|
---|
159 | myBid = myBid.putValue(i + 1, value);
|
---|
160 | // System.out.println("myBid = " + myBid);
|
---|
161 | // System.out.println("Utility = " +
|
---|
162 | // utilitySpace.getUtility(myBid));
|
---|
163 | if (utilitySpace.getUtility(myBid) > concessionsFunction())
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | cnt++;
|
---|
167 | }
|
---|
168 | // System.out.println("Gin's Bid = " + myBid);
|
---|
169 | double time = info.getTimeline().getTime();
|
---|
170 | if (time > 0.98) {
|
---|
171 | for (StandardInfo si : history) {
|
---|
172 | if (si.getAgreement() != null) {
|
---|
173 | myBid = si.getAgreement().get1();
|
---|
174 | // System.out.println("getAgreementBid = " + myBid);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | return myBid;
|
---|
179 | }
|
---|
180 |
|
---|
181 | private double concessionsFunction() {
|
---|
182 | double time = info.getTimeline().getTime(); // 全体時間(round)に対する正規化された時間:0-1
|
---|
183 | double fx = 0;
|
---|
184 | // System.out.println("fitToOpponent = " + fitToOpponent);
|
---|
185 | if (fitToOpponent == 0) {
|
---|
186 | if (time < 0.85)
|
---|
187 | fx = 0.9;
|
---|
188 | else if (time < 0.95)
|
---|
189 | fx = 0.88;
|
---|
190 | else
|
---|
191 | fx = 0.80;
|
---|
192 | } else if (fitToOpponent == 1) {
|
---|
193 | if (time < 0.85)
|
---|
194 | fx = 0.95 - (time / 3.0);
|
---|
195 | else if (time < 0.95)
|
---|
196 | fx = 0.8;
|
---|
197 | else
|
---|
198 | fx = 0.7;
|
---|
199 | } else {
|
---|
200 | if (time < 0.7) {
|
---|
201 | fx = 0.95 - (time / 3.0);
|
---|
202 | } else {
|
---|
203 | fx = 0.5;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | return fx;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // 三者間における頻出bid格納テーブルを表示
|
---|
210 | private void printTable() {
|
---|
211 | for (int i = 0; i < bidTable.length; i++) {
|
---|
212 | System.out.println("bidTable[" + i + "]" + ":" + bidTable[i]);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | // 三者間における頻出bid格納テーブルを初期化
|
---|
217 | private HashMap[] bidTable;
|
---|
218 |
|
---|
219 | private void initBidTable() {
|
---|
220 | int issueNum = generateRandomBid().getIssues().size();
|
---|
221 | bidTable = new HashMap[issueNum];
|
---|
222 | for (int i = 0; i < issueNum; i++) {
|
---|
223 | bidTable[i] = new HashMap<Value, Integer>();
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | // 三者間における頻出bid格納テーブルを更新
|
---|
228 | private void updateOurTable(Bid bid) {
|
---|
229 | Value key = null;
|
---|
230 | int value = 1;
|
---|
231 | for (int i = 0; i < bid.getIssues().size(); i++) {
|
---|
232 | key = bid.getValue(i + 1);
|
---|
233 | if (bidTable[i].get(key) != null) {
|
---|
234 | value = (int) bidTable[i].get(key) + 1;
|
---|
235 | }
|
---|
236 | bidTable[i].put(key, value);
|
---|
237 | }
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * All offers proposed by the other parties will be received as a message.
|
---|
243 | * You can use this information to your advantage, for example to predict
|
---|
244 | * their utility.
|
---|
245 | *
|
---|
246 | * @param sender
|
---|
247 | * The party that did the action. Can be null.
|
---|
248 | * @param action
|
---|
249 | * The action that party did.
|
---|
250 | */
|
---|
251 | @Override
|
---|
252 | public void receiveMessage(AgentID sender, Action action) {
|
---|
253 | super.receiveMessage(sender, action);
|
---|
254 | if (action instanceof Offer) {
|
---|
255 | lastReceivedBid = ((Offer) action).getBid();
|
---|
256 | updateOurTable(lastReceivedBid);
|
---|
257 | // System.out.println("getBid,name = " + sender + " , "+
|
---|
258 | // lastReceivedBid);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public String getDescription() {
|
---|
264 | return "ANAC2017";
|
---|
265 | }
|
---|
266 |
|
---|
267 | }
|
---|