source: src/main/java/agents/anac/y2017/group3/Group3.java@ 316

Last change on this file since 316 was 46, checked in by Tim Baarslag, 6 years ago

agents get their getUtilitySpace() from the API, not the info object anymore

File size: 8.9 KB
Line 
1package agents.anac.y2017.group3;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.Random;
8
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.Offer;
14import genius.core.issue.Value;
15import genius.core.list.Tuple;
16import genius.core.parties.AbstractNegotiationParty;
17import genius.core.parties.NegotiationInfo;
18import genius.core.persistent.PersistentDataType;
19import genius.core.persistent.StandardInfo;
20import genius.core.persistent.StandardInfoList;
21
22/**
23 * Sample party that accepts the Nth offer, where N is the number of sessions
24 * this [agent-profile] already did.
25 */
26public class Group3 extends AbstractNegotiationParty {
27
28 private Bid lastReceivedBid = null;
29 private Bid currentBid = null;
30 private boolean isCorrectionAppliedBecauseOfHistory = false;
31 private StandardInfoList history;
32 private boolean historyAnalyzed = false;
33 private boolean counted = true;
34 private ArrayList<String> agentNames = new ArrayList<String>();
35 ArrayList<BidData> bids = new ArrayList<BidData>();
36 Random random = new Random();
37 static private double MIN_ACCEPTABLE_UTILITY = 0.7;
38
39 // init() will be called before starting negotiation
40 @Override
41 public void init(NegotiationInfo info) {
42
43 super.init(info);
44
45 System.out.println("Discount Factor is "
46 + getUtilitySpace().getDiscountFactor());
47 System.out.println("Reservation Value is "
48 + getUtilitySpace().getReservationValueUndiscounted());
49
50 if (getData().getPersistentDataType() != PersistentDataType.STANDARD) {
51 throw new IllegalStateException("need standard persistent data");
52 }
53
54 // use history to get previous negotiation utilities
55 history = (StandardInfoList) getData().get();
56
57 }
58
59 @Override
60 public Action chooseAction(List<Class<? extends Action>> validActions) {
61 // if lastReceivedBid is null --> you are starter party
62 // zamanin yarisina kadar max bid teklif ediliyor.
63 // bu arada analyze edicek bid de birikmis oluyor.
64 // kalan yarisinda analize gore bid sunuluyor.
65 try {
66 System.err.println(getUtility(lastReceivedBid));
67 if (getUtility(lastReceivedBid) > 0.85
68 && timeline.getTime() < 0.5) {
69 return new Accept(getPartyId(), lastReceivedBid);
70 } else if (getUtility(lastReceivedBid) > 0.75
71 && timeline.getTime() < 0.8) {
72 return new Accept(getPartyId(), lastReceivedBid);
73 } else if (getUtility(lastReceivedBid) > MIN_ACCEPTABLE_UTILITY
74 && timeline.getTime() < 0.9) {
75 return new Accept(getPartyId(), lastReceivedBid);
76 } else if (timeline.getTime() > 0.95) {
77 return new Accept(getPartyId(), lastReceivedBid);
78 } else if (timeline.getTime() < 0.2) {
79 Bid a = this.generateRandomBid();
80 while (this.getUtility(a) < 0.8) {
81 a = this.generateRandomBid();
82 }
83 return new Offer(getPartyId(), a);
84 } else {
85 currentBid = analyzeBids();
86 return new Offer(getPartyId(), currentBid);
87 }
88 } catch (Exception e) {
89 System.err.println(e);
90 }
91 Bid a = this.generateRandomBid();
92 while (this.getUtility(a) < 0.8) {
93 a = this.generateRandomBid();
94 }
95 return new Offer(getPartyId(), a);
96
97 }
98
99 private Bid analyzeBids() throws Exception {
100 // burada biriktirilen biddler kontrol edilip bir strateji uygulanacak
101 // Eger onceki historylerden birinde bekledigimizin ustunde bir bid
102 // kabul edilmisse onu minimumla degistiriyoruz
103 // cunku demekki olabilir
104 System.err.println("min utility " + MIN_ACCEPTABLE_UTILITY);
105 if (!isCorrectionAppliedBecauseOfHistory) {
106 System.out.println("HISTORY SIZE >>>> " + history.size());
107 for (StandardInfo si : history) {
108 System.out.println("AGREEMENT: " + si.getAgreement());
109 System.out
110 .println("UTILITIES: " + si.getUtilities().toString());
111 if (si.getAgreement().get2() >= MIN_ACCEPTABLE_UTILITY)
112 MIN_ACCEPTABLE_UTILITY = si.getAgreement().get2();
113 else {
114 // eger 0 sa sonuca ulasilamamis burada biz ustumuze duseni
115 // yapip bir sonuc elde edilebilsin diye
116 // kabul edebilecegimiz degeri dusuruyoruz. bu friendly
117 // approachimizi destekliyor.
118 // eger 0 degilde sadece mevcut minimumdan kucukse bu
119 // durumda belli ki son anda kabul ettigimiz bir offer
120 // o halde minimumu biraz dusurup son anda kabul edecegimiz
121 // daha kotu offer ihtimallerini azaltmis oluyoruz
122 // 0.5tense -> mevcut minimum utility-0.02 daha iyidir
123 MIN_ACCEPTABLE_UTILITY -= 0.02;
124 break;
125 }
126 }
127 isCorrectionAppliedBecauseOfHistory = true;
128 }
129 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
130 for (int i = 0; i < lastReceivedBid.getIssues().size(); i++) {
131 int max = 0;
132 Value value = null;
133 for (int j = 0; j < agentNames.size(); j++) {
134 // burdan elde ettigim commoni kendimle karsilastiricam
135 if (findCurrentBidData(agentNames.get(j)).assumedValues
136 .get(i) > max) {
137 max = findCurrentBidData(agentNames.get(j)).assumedValues
138 .get(i);
139 value = findCurrentBidData(agentNames.get(j)).bid
140 .getValue(i + 1);
141 }
142 }
143 values.put(i + 1, value);
144 }
145 Bid bid = new Bid(utilitySpace.getDomain(), values);
146 if (getUtility(bid) > 0.9)
147 return bid;
148 else {
149 return manupalateAssumedValues(bid);
150 }
151 }
152
153 private Bid manupalateAssumedValues(Bid bid) throws Exception {
154 // TODO Auto-generated method stub
155 HashMap<Integer, Value> values = bid.getValues();
156 while (getUtility(bid) < 0.75) {
157 int issueNum = random
158 .nextInt(lastReceivedBid.getIssues().size() - 1) + 1;
159 values.put(issueNum,
160 utilitySpace.getMaxUtilityBid().getValue(issueNum));
161 bid = new Bid(utilitySpace.getDomain(), values);
162 }
163 System.out.println("My offer: " + getUtility(bid));
164 return bid;
165 }
166
167 private void countAgentData(AgentID sender, Action action) {
168 if (agentNames.contains(sender.getName())) {
169 counted = false;
170 return;
171 } else {
172 agentNames.add(sender.getName());
173 if (action instanceof Offer) {
174 BidData bd = new BidData(sender.getName(),
175 ((Offer) action).getBid());
176 for (int i = 0; i < ((Offer) action).getBid().getIssues()
177 .size(); i++) {
178 bd.assumedValues.add(i, 0);
179 }
180 bids.add(bd);
181 return;
182 } else {
183 BidData bd = new BidData(sender.getName(),
184 ((Accept) action).getBid());
185 for (int i = 0; i < ((Accept) action).getBid().getIssues()
186 .size(); i++) {
187 bd.assumedValues.add(i, 0);
188 }
189 bids.add(bd);
190 return;
191 }
192
193 }
194 }
195
196 /*
197 * Order: init() --> receiveMessage() ---> chooseAction()
198 */
199 @Override
200 public void receiveMessage(AgentID sender, Action action) {
201 super.receiveMessage(sender, action);
202 if (counted && sender != null) {
203 countAgentData(sender, action);
204 }
205 try {
206 if (action instanceof Offer && counted) {
207 lastReceivedBid = ((Accept) action).getBid();
208 BidData bidData = new BidData(sender.getName(),
209 ((Offer) action).getBid());
210 bids.add(bidData);
211 }
212 if (action instanceof Offer) {
213 lastReceivedBid = ((Offer) action).getBid();
214 BidData currentBidData = findCurrentBidData(sender.getName());
215 if (currentBidData != null) {
216 for (int i = 0; i < lastReceivedBid.getIssues()
217 .size(); i++) {
218 if (currentBidData.bid.getValue(i + 1)
219 .equals(lastReceivedBid.getValue(i + 1))) {
220 currentBidData.assumedValues.set(i,
221 (currentBidData.assumedValues.get(i) + 1));
222 }
223 }
224 }
225 }
226 if (action instanceof Accept) {
227 lastReceivedBid = ((Accept) action).getBid();
228 BidData currentBidData = findCurrentBidData(sender.getName());
229 if (currentBidData != null) {
230 for (int i = 0; i < lastReceivedBid.getIssues()
231 .size(); i++) {
232 if (currentBidData.bid.getValue(i + 1)
233 .equals(lastReceivedBid.getValue(i + 1))) {
234 currentBidData.assumedValues.set(i,
235 (currentBidData.assumedValues.get(i) + 1));
236 }
237 }
238 }
239 }
240
241 if (!history.isEmpty() && historyAnalyzed == false) {
242 analyzeHistory();
243 isCorrectionAppliedBecauseOfHistory = false;
244 }
245
246 } catch (Exception e) {
247 System.err.println(e);
248 }
249 }
250
251 private BidData findCurrentBidData(String sender) {
252 for (BidData bid : bids) {
253 if (bid.agentName.equals(sender)) {
254 return bid;
255 }
256 }
257 return null;
258 }
259
260 public void analyzeHistory() {
261 historyAnalyzed = true;
262 // from recent to older history records
263 for (int h = history.size() - 1; h >= 0; h--) {
264
265 System.out.println("History index: " + h);
266
267 StandardInfo lastinfo = history.get(h);
268 System.out.println("historyNo: " + h + " myID: " + getPartyId());
269 int counter = 0;
270 for (Tuple<String, Double> offered : lastinfo.getUtilities()) {
271 counter++;
272
273 String party = offered.get1(); // get partyID -> example:
274 // ConcederParty@15
275 Double util = offered.get2(); // get the offer utility
276
277 System.out.println(
278 "PartyID: " + party + " utilityForMe: " + util);
279 System.out.println();
280 // just print first 3 bids, not the whole history
281 if (counter == 3)
282 break;
283 }
284 System.out.println("\n");
285
286 }
287
288 }
289
290 @Override
291 public String getDescription() {
292 return "ANAC2017";
293 }
294
295}
Note: See TracBrowser for help on using the repository browser.