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