source: src/main/java/agents/anac/y2018/agreeableagent2018/AgreeableAgent2018.java@ 346

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

Fixed agent 2018 descriptions

File size: 9.0 KB
Line 
1package agents.anac.y2018.agreeableagent2018;
2
3import java.util.List;
4
5import genius.core.AgentID;
6import genius.core.Bid;
7import genius.core.actions.Accept;
8import genius.core.actions.Action;
9import genius.core.actions.Offer;
10import genius.core.bidding.BidDetails;
11import genius.core.boaframework.OutcomeSpace;
12import genius.core.issue.Issue;
13import genius.core.issue.IssueDiscrete;
14import genius.core.misc.Range;
15import genius.core.parties.AbstractNegotiationParty;
16import genius.core.parties.NegotiationInfo;
17import genius.core.timeline.TimeLineInfo;
18import genius.core.utility.AbstractUtilitySpace;
19
20/**
21 * Created by Sahar Mirzayi
22 * 2/14/2018
23 * University of Tehran
24 * Agent Lab.
25 * Sahar.Mirzayi @ gmail.com
26 */
27
28public class AgreeableAgent2018 extends AbstractNegotiationParty {
29 private int domainSize;
30 private double pMin;
31 private double pMax;
32 private AbstractUtilitySpace utilSpace = null;
33 private TimeLineInfo timeLineInfo = null;
34 private Bid bestBid = null;
35 private Bid lastReceivedOffer = null;
36 private OutcomeSpace outcomeSpace = null;
37 private FrequencyBasedOpponentModel opponentModel1;
38 private FrequencyBasedOpponentModel opponentModel2;
39 private AgentID opponent1ID = null;
40 private AgentID opponent2ID = null;
41 protected int opponent1BidCount = 0;
42 protected int opponent2BidCount = 0;
43 private double timeForUsingModel = 0.1;
44 private boolean canUseModel = false;
45
46
47 @Override
48 public void init(NegotiationInfo info) {
49 super.init(info);
50 this.utilSpace = info.getUtilitySpace();
51 outcomeSpace = new OutcomeSpace(utilSpace);
52 domainSize = outcomeSpace.getAllOutcomes().size();
53
54 timeLineInfo = info.getTimeline();
55 try {
56 bestBid = info.getUtilitySpace().getMaxUtilityBid();
57 pMin = utilitySpace.getUtility(utilitySpace.getMinUtilityBid());
58 pMax = utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
59 } catch (Exception e) {
60 e.printStackTrace();
61 }
62 if (isAllIssuesDiscrete()) {
63 canUseModel = true;
64 determineTimeForUsingModel();
65 opponentModel1 = new FrequencyBasedOpponentModel();
66 opponentModel1.init(utilitySpace.getDomain().getIssues());
67 opponentModel2 = new FrequencyBasedOpponentModel();
68 opponentModel2.init(utilitySpace.getDomain().getIssues());
69 } else {
70 canUseModel = false;
71 }
72 }
73
74 private boolean isAllIssuesDiscrete() {
75 List<Issue> issues = utilSpace.getDomain().getIssues();
76 for (Issue issue : issues) {
77 if (!(issue instanceof IssueDiscrete))
78 return false;
79 }
80 return true;
81 }
82
83 private void determineTimeForUsingModel() {
84 if (domainSize < Constants.smallDomainUpperBound)
85 timeForUsingModel = Constants.timeForUsingModelForSmallDomain;
86 if (domainSize >= Constants.smallDomainUpperBound &&
87 domainSize < Constants.midDomainUpperBound)
88 timeForUsingModel = Constants.timeForUsingModelForMidDomain;
89 if (domainSize >= Constants.midDomainUpperBound)
90 timeForUsingModel = Constants.timeForUsingModelForLargeDomain;
91 }
92
93 @Override
94 public void receiveMessage(AgentID sender, Action lastOpponentAction) {
95 super.receiveMessage(sender, lastOpponentAction);
96 if (lastOpponentAction instanceof Offer) {
97 Bid bid = ((Offer) lastOpponentAction).getBid();
98 if (canUseModel) {
99 if (opponent1ID == null) {
100 opponent1ID = sender;
101 } else if (opponent2ID == null) {
102 opponent2ID = sender;
103 }
104 if (opponent1ID == sender) {
105 opponent1BidCount++;
106 opponentModel1.updateModel(bid, opponent1BidCount);
107 }
108 if (opponent2ID == sender) {
109 opponent2BidCount++;
110 opponentModel2.updateModel(bid, opponent2BidCount);
111 }
112 }
113 lastReceivedOffer = ((Offer) lastOpponentAction).getBid();
114 }
115 }
116
117 @Override
118 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
119 if (lastReceivedOffer != null) {
120 try {
121 Bid myBid = getNextBid();
122 if (isAcceptable(getUtility(lastReceivedOffer), getUtility(myBid)))
123 return new Accept(getPartyId(), lastReceivedOffer);
124 else
125 return new Offer(getPartyId(), myBid);
126 } catch (Exception ex) {
127 return new Offer(getPartyId(), bestBid);
128 }
129 } else {
130 return new Offer(getPartyId(), bestBid);
131 }
132 }
133
134 private boolean isAcceptable(double opponentUtility, double myBidUtilByTime) {
135 if (opponentUtility >= myBidUtilByTime)
136 return true;
137
138 double time = timeLineInfo.getTime();
139 return time >= 0.99 && opponentUtility >= utilSpace.getReservationValue();
140
141 }
142
143 private Bid getNextBid() {
144
145 double time = timeLineInfo.getTime();
146 double targetUtilityByTime = getUtilityByTime(time);
147 if (targetUtilityByTime < Constants.minimumUtility)
148 targetUtilityByTime = Constants.minimumUtility;
149
150 if (isModelUsable() && canUseModel) {
151 return tuneBidByOpponentModel(targetUtilityByTime);
152 } else {
153 BidDetails bidNearUtility = outcomeSpace.getBidNearUtility(targetUtilityByTime);
154 return bidNearUtility.getBid();
155 }
156 }
157
158 private boolean isModelUsable() {
159 double time = timeLineInfo.getTime();
160 return time >= timeForUsingModel;
161 }
162
163 private Bid tuneBidByOpponentModel(double targetUtilityByTime) {
164
165 double utilityThreshold = getExplorableNeighbourhood();
166 Range range =
167 new Range(targetUtilityByTime - utilityThreshold, targetUtilityByTime + utilityThreshold);
168 List<BidDetails> bidsInRange = outcomeSpace.getBidsinRange(range);
169 if (bidsInRange.size() == 1)
170 return bidsInRange.get(0).getBid();
171 int selectedBidIndex = getBestBidByRouletteWheel(bidsInRange);
172 return bidsInRange.get(selectedBidIndex).getBid();
173 }
174
175
176 /*private int getBestBidByOpponentUtilities(List<BidDetails> bidsInRange) {
177 int size = bidsInRange.size();
178 double max = 0;
179 int maxIndex = 0;
180 for (int i = 0; i < size; i++) {
181 BidDetails bidDetails = bidsInRange.get(i);
182 double sum = opponentModel1.getUtility(bidDetails.getBid())
183 + opponentModel2.getUtility(bidDetails.getBid());
184 if (sum > max) {
185 max = sum;
186 maxIndex = i;
187 }
188 }
189 return maxIndex;
190
191 }*/
192
193 private int getBestBidByRouletteWheel(List<BidDetails> bidsInRange) {
194 int size = bidsInRange.size();
195 double[] sumOfTwoUtilitiesForBid = new double[size];
196 double totalUtility = 0;
197 for (int i = 0; i < size; i++) {
198 BidDetails bidDetails = bidsInRange.get(i);
199 double sum = opponentModel1.getUtility(bidDetails.getBid())
200 + opponentModel2.getUtility(bidDetails.getBid());
201 sumOfTwoUtilitiesForBid[i] = sum;
202 totalUtility += sum;
203 }
204 double[] normalizedSumOfTwoUtilitiesForBid = new double[size];
205 for (int i = 0; i < size; i++) {
206 normalizedSumOfTwoUtilitiesForBid[i] = sumOfTwoUtilitiesForBid[i] / totalUtility;
207 }
208 double random = Math.random();
209 double integrate = 0;
210 int selectedBidIndex = size;
211 for (int i = 0; i < size; i++) {
212 integrate += normalizedSumOfTwoUtilitiesForBid[i];
213 if (integrate >= random) {
214 selectedBidIndex = i;
215 break;
216 }
217 }
218 return selectedBidIndex;
219 }
220
221 private double getExplorableNeighbourhood() {
222 double time = timeLineInfo.getTime();
223 if (time < Constants.timeToConcede) {
224 return 0;
225 } else {
226 return Constants.neigExplorationDisFactor
227 *(1 - (pMin + (pMax - pMin) * (1 - f(time))));
228 }
229 }
230
231 @Override
232 public String getDescription() {
233 return "ANAC2018";
234 }
235
236 public double getUtilityByTime(double time) {
237 if (time < Constants.timeToConcede) {
238 return 1;
239 } else {
240 time = (time - Constants.timeToConcede) / (1 - Constants.timeToConcede);//normalization
241 return pMin + (pMax - pMin) * (1 - f(time));
242 }
243 }
244
245 public double f(double t) {
246 if (Constants.concessionFactor == 0)
247 return Constants.k;
248 return Constants.k + (1 - Constants.k) * Math.pow(t, 1.0 / Constants.concessionFactor);
249 }
250}
Note: See TracBrowser for help on using the repository browser.