source: src/main/java/agents/anac/y2017/gin/Gin.java@ 1

Last change on this file since 1 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

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