source: src/main/java/agents/anac/y2018/iqson/IQSun2018.java

Last change on this file was 345, checked in by Tim Baarslag, 4 years ago

Fixed agent 2018 descriptions

File size: 11.9 KB
RevLine 
[341]1package agents.anac.y2018.iqson;
2
[343]3import java.util.List;
[341]4
[343]5import java.util.ArrayList;
6import java.util.Comparator;
7import java.util.HashSet;
8import java.util.Random;
9import java.util.Set;
[341]10
[343]11import genius.core.AgentID;
12import genius.core.Bid;
13import genius.core.BidHistory;
14import genius.core.Domain;
15import genius.core.actions.Accept;
16import genius.core.actions.Action;
17import genius.core.actions.EndNegotiationWithAnOffer;
18import genius.core.actions.Offer;
19import genius.core.bidding.BidDetails;
20import genius.core.boaframework.OutcomeSpace;
21import genius.core.parties.AbstractNegotiationParty;
22import genius.core.parties.NegotiationInfo;
23import genius.core.timeline.TimeLineInfo;
24import genius.core.utility.UtilitySpace;
[341]25
[343]26
[341]27/*
28*
29* Crated by Seyed Mohammad Hussein Kazemi
30*
31* April 15th 2018
32*
33* University of Tehran
34*
35* Agent Lab.
36*
37* smhk1375@icloud.com
38*
39*
40* */
41
42
43public class IQSun2018 extends AbstractNegotiationParty {
44
45 private Bid lastReceivedBid, myLastBid;
46 private static double MIN_UTILITY = 0.5;
47 private double concessionFactor;//e
48 protected int k = 0;///** k \in [0, 1]. For k = 0 the agent starts with a bid of maximum utility */
49 private BidHistory currSessOppBidHistory, currSessOurBidHistory;
50 private TimeLineInfo TimeLineInfo = null;
51 private OutcomeSpace outcomeSpace;
52 private double[] weightsIfHelpingUtilIsLessThanOrEqToOne, weightsIfHelpingUtilIsMoreThanOne;
53
54 private List<BidInfo> lBids;
55
56 @Override
57 public void init(NegotiationInfo info) {
58 super.init(info);
59 concessionFactor = 0.1;
60 lastReceivedBid = null;
61 outcomeSpace = new OutcomeSpace(utilitySpace);
62 generateBidNearMyUtility(1);
63 currSessOppBidHistory = new BidHistory();
64 currSessOurBidHistory = new BidHistory();
65 TimeLineInfo = info.getTimeline();
66 lBids = new ArrayList<>(AgentTool.generateRandomBids(info.getUtilitySpace().getDomain(), 30000, this.rand, this.utilitySpace));
67 lBids.sort(new BidInfoComp().reversed());
68 determineWeights();
69
70// utilitySpace.getDiscountFactor(); <-------------------------- it gives us discount factor
71 }
72
73 private Bid generateHelpingBid(){
74 double threshold_high = 1 - 0.15 * this.timeline.getTime() * Math.abs(Math.sin(this.timeline.getTime() * 20));
75 double threshold_low = 1 - 0.21 * this.timeline.getTime() * Math.abs(Math.sin(this.timeline.getTime() * 20));
76 Bid bid = null;
77 while (bid == null) {
78 bid = AgentTool.selectBidfromList(this.lBids, threshold_high, threshold_low);
79 if (bid == null) {
80 threshold_low -= 0.0001;
81 }
82 }
83 return bid;
84 }
85
86 private void determineWeights() {
87 double domainSize = utilitySpace.getDomain().getNumberOfPossibleBids();
88 if(domainSize <= 1000){
89 weightsIfHelpingUtilIsLessThanOrEqToOne = new double[]{0.75, 0.05, 0.2};
90 weightsIfHelpingUtilIsMoreThanOne = new double[]{0.75, 0.25};
91 }else if(domainSize > 1000 && domainSize <= 2000){
92 weightsIfHelpingUtilIsLessThanOrEqToOne = new double[]{0.85, 0.05, 0.1};
93 weightsIfHelpingUtilIsMoreThanOne = new double[]{0.85, 0.15};
94 } else if(domainSize > 2000 && domainSize <= 100000) {
95 concessionFactor = 0.3;
96 weightsIfHelpingUtilIsLessThanOrEqToOne = new double[]{0.7, 0.05, 0.25};
97 weightsIfHelpingUtilIsMoreThanOne = new double[]{0.7, 0.3};
98 } else {
99 weightsIfHelpingUtilIsLessThanOrEqToOne = new double[]{0.85, 0.05, 0.1};
100 weightsIfHelpingUtilIsMoreThanOne = new double[]{0.85, 0.15};
101 }
102 }
103
104 public Action chooseAction(List<Class<? extends Action>> validActions) {
105 try {
106 if (lastReceivedBid == null) {
107 try {
108 return new Offer(getPartyId(), outcomeSpace.getBidNearUtility(1).getBid());
109 } catch (Exception e) {
110 System.out.println("here in chooseAction: ");
111 System.out.println(e.getLocalizedMessage());
112 e.printStackTrace();
113 }
114 } else {
115 try {
116 double offeredUtilFromOpponent = utilitySpace.getUtility(lastReceivedBid), time = timeline.getTime();
117 if (time < 0.17) {
118 if (offeredUtilFromOpponent >= 0.9)
119 return new Accept(getPartyId(), lastReceivedBid);
120 return (new Offer(getPartyId(), outcomeSpace.getBidNearUtility(1).getBid()));
121 }
122 if (isAcceptable(offeredUtilFromOpponent, time))
123 return new Accept(getPartyId(), lastReceivedBid);
124 double[] weights;
125 double[] values;
126 double helpingUtility = getUtility(generateHelpingBid());
127 if (helpingUtility <= 1) {
128 values = new double[]{getUtilityByTime(time), determineUtilOnlyByCurrSesHistory(), helpingUtility};
129 weights = weightsIfHelpingUtilIsLessThanOrEqToOne;
130 } else {
131 values = new double[]{getUtilityByTime(time), determineUtilOnlyByCurrSesHistory()};
132 weights = weightsIfHelpingUtilIsMoreThanOne;
133 }
134 generateBidNearMyUtility(average(values, weights));
135 return generateMyOfferAndAddItToMyCurrSessHistory();
136 } catch (Exception e) {
137 System.out.println("HERE IT IS !!");
138 System.out.println(e.getMessage());
139 }
140 }
141 generateBidNearMyUtility(1);
142 return (new Offer(getPartyId(), myLastBid));
143 }catch (Exception e){
144 return (new Offer(getPartyId(), outcomeSpace.getBidNearUtility(1).getBid()));
145 }
146 }
147
148 private Offer generateMyOfferAndAddItToMyCurrSessHistory(){
149 BidDetails myLastBidDetails = new BidDetails(myLastBid, getUtility(myLastBid), TimeLineInfo.getTime());
150 currSessOurBidHistory.add(myLastBidDetails);
151 return (new Offer(getPartyId(), myLastBid));
152 }
153
154
155
156 private void generateBidNearMyUtility(double myUtility){
157 if(myUtility > 1)
158 myUtility = 0.99;
159 if(myUtility < MIN_UTILITY)
160 myUtility = MIN_UTILITY;
161 myLastBid = outcomeSpace.getBidNearUtility(myUtility).getBid();
162 }
163
164 @Override
165 public void receiveMessage(AgentID sender, Action action) {
166 super.receiveMessage(sender, action);
167 if (action instanceof Offer) {
168 lastReceivedBid = ((Offer) action).getBid();
169 try {
170 BidDetails opponentBid = new BidDetails(lastReceivedBid, getUtility(lastReceivedBid),
171 TimeLineInfo.getTime());
172 currSessOppBidHistory.add(opponentBid);
173 } catch (Exception e) {
174 System.out.println("here in receiveMessage: ");
175 System.out.println(e.getLocalizedMessage());
176 e.printStackTrace();
177 new EndNegotiationWithAnOffer(this.getPartyId(),
178 outcomeSpace.getBidNearUtility(1).getBid());
179 }
180 }
181 }
182
183 @Override
184 public String getDescription() {
[345]185 return "ANAC2018";
[341]186 }
187
188 private double averageOfBestFiveBids(){
189 if(currSessOppBidHistory.size() > 5){
190 List<BidDetails> bestNBidsDetails = currSessOppBidHistory.getNBestBids(5);
191 double retVal = 0;
192 for(BidDetails bidDetails : bestNBidsDetails){
193 retVal += getUtility(bidDetails.getBid());
194 }
195 return retVal / 5;
196 }else {
197 return currSessOppBidHistory.getAverageUtility();
198 }
199 }
200
201 private double determineUtilOnlyByCurrSesHistory(){
202 if(currSessOppBidHistory.getHistory().isEmpty()){
203 return 1;
204 }
205 if(currSessOurBidHistory.getHistory().isEmpty())
206 return averageOfBestFiveBids() * 0.05 + 0.95;
207
208 return averageOfBestFiveBids() * 0.05 + currSessOurBidHistory.getAverageUtility() * 0.95;
209 }
210
211
212 private boolean isAcceptable(double offeredUtilFromOpponent, double time) {
213 double myLastBidUtility = utilitySpace.getUtility(myLastBid);
214 if(time >= 0.99 && partnerLastBidHasUtilMoreThanOrEqualToResVal())
215 return true;
216
217 if(time >= 0.95 && (MIN_UTILITY <= offeredUtilFromOpponent))
218 return true;
219
220 if(getUtilityByTime(time) > offeredUtilFromOpponent)
221 return false;
222 if(!partnerLastBidHasUtilMoreThanResVal()) {
223 return false;
224 }
225 if(offeredUtilFromOpponent < currSessOppBidHistory.getAverageUtility())
226 return false;
227 return myLastBidUtility <= offeredUtilFromOpponent && MIN_UTILITY <= offeredUtilFromOpponent;
228 }
229
230 private double getUtilityByTime(double t) {
231 try {
232 double pMin = utilitySpace.getUtility(utilitySpace.getMinUtilityBid());
233 double pMax = utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
234 double u = pMin + (pMax - pMin) * (1 - f(t));
235 return u >= MIN_UTILITY ? u : MIN_UTILITY;
236 } catch (Exception e) {
237 System.out.println("here in getUtilityByTime: ");
238 System.out.println(e.getLocalizedMessage());
239 e.printStackTrace();
240 return 1;
241 }
242 }
243
244 public double f(double t) throws Exception {
245 if (concessionFactor == 0)
246 return k;
247 return k + (1 - k) * Math.pow(t, 1.0 / concessionFactor);
248 }
249
250
251 private double average(double[]values, double[]weights){
252 double average = 0;
253 double sumOfWeights = 0;
254 for(int i = 0; i < values.length; i++){
255 average += values[i] * weights[i];
256 sumOfWeights += weights[i];
257 }
258
259 return average / sumOfWeights;
260 }
261
262 private boolean partnerLastBidHasUtilMoreThanResVal(){
263 return utilitySpace.getUtility(lastReceivedBid) > utilitySpace.getReservationValue();
264 }
265
266 private boolean partnerLastBidHasUtilMoreThanOrEqualToResVal(){
267 return utilitySpace.getUtility(lastReceivedBid) >= utilitySpace.getReservationValue();
268 }
269
270}
271
272class AgentTool {
273
274 private static Random random = new Random();
275
276 static Bid selectBidfromList(List<BidInfo> bidInfoList, double higerutil, double lowwerutil) {
277 List<BidInfo> bidInfos = new ArrayList<>();
278 for (BidInfo bidInfo : bidInfoList) {
279 if (bidInfo.getUtil() <= higerutil && bidInfo.getUtil() >= lowwerutil) {
280 bidInfos.add(bidInfo);
281 }
282 }
283 if (bidInfos.size() == 0) {
284 return null;
285 } else {
286 return bidInfos.get(random.nextInt(bidInfos.size())).getBid();
287 }
288 }
289
290 static Set<BidInfo> generateRandomBids(Domain d, int numberOfBids, Random random, UtilitySpace utilitySpace) {
291 Set<BidInfo> randomBids = new HashSet<>();
292 for (int i = 0; i < numberOfBids; i++) {
293 Bid b = d.getRandomBid(random);
294 randomBids.add(new BidInfo(b, utilitySpace.getUtility(b)));
295 }
296 return randomBids;
297 }
298
299}
300
301class BidInfo {
302 private Bid bid;
303 private double util;
304
305 BidInfo(Bid b, double u) {
306 this.bid = b;
307 util = u;
308 }
309
310 public Bid getBid() {
311 return bid;
312 }
313
314 public double getUtil() {
315 return util;
316 }
317
318 @Override
319 public int hashCode() {
320 return bid.hashCode();
321 }
322
323 public boolean equals(BidInfo bidInfo) {
324 return bid.equals(bidInfo.getBid());
325 }
326
327 @Override
328 public boolean equals(Object obj) {
329 return obj != null && obj instanceof BidInfo && ((BidInfo) obj).getBid().equals(bid);
330 }
331
332}
333
334final class BidInfoComp implements Comparator<BidInfo> {
335 BidInfoComp() {
336 super();
337 }
338
339 @Override
340 public int compare(BidInfo o1, BidInfo o2) {
341 return Double.compare(o1.getUtil(), o2.getUtil());
342 }
343}
Note: See TracBrowser for help on using the repository browser.