source: src/main/java/agents/anac/y2019/garavelagent/GaravelAgent.java@ 200

Last change on this file since 200 was 200, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents

  • Property svn:executable set to *
File size: 13.1 KB
Line 
1package agents.anac.y2019.garavelagent;
2
3import java.util.*;
4
5import agents.org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
6import genius.core.AgentID;
7import genius.core.Bid;
8import genius.core.actions.Accept;
9import genius.core.actions.Action;
10import genius.core.actions.Offer;
11import genius.core.issue.Issue;
12import genius.core.issue.IssueDiscrete;
13import genius.core.issue.ValueDiscrete;
14import genius.core.parties.AbstractNegotiationParty;
15import genius.core.parties.NegotiationInfo;
16import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
17import genius.core.uncertainty.BidRanking;
18import genius.core.utility.AbstractUtilitySpace;
19import javafx.util.Pair;
20
21/**
22 * This is your negotiation party.
23 */
24
25public class GaravelAgent extends AbstractNegotiationParty {
26
27 private Bid currentBid = null;
28 private Bid beforeBid = null;
29 private AbstractUtilitySpace utilitySpace = null;
30 private double[][] opponentModelValue;
31 private double[] opponentModelIssue;
32 private int numberOfIssues;
33 private ArrayList<Bid> opponentBids;
34 private int bidCount = 0;
35 double[] estimateUtilities;
36 double[] sortedUtils;
37
38 List<List<ValueDiscrete>> allIssues = new ArrayList<>();
39 int countAll;
40 double[][] omValueNormalized;
41 OLSMultipleLinearRegression regression;
42 ArrayList<Bid> allBidsList;
43 double[] utilities;
44 List<List<String>> allIssuesAsString;
45 List<List<String>> allPossibleBids;
46 List<Pair<List<String>, Double>> ourUtilityModel;
47 List<Pair<List<String>, Double>> opponentUtilityModel;
48 NegotiationInfo info_;
49
50 ArrayList<Bid> sortedList;
51 double[] allBidPredictions;
52 Bid maxBid = null;
53 List<List<String>> optimalBids;
54 Boolean isRegressionPossible = true;
55 List<Bid> bidOrder;
56
57 @Override
58 public void init(NegotiationInfo info) {
59
60 super.init(info);
61
62 try {
63 List<Issue> issues = info.getUserModel().getDomain().getIssues();
64 utils.getIssueDiscrete(issues, allIssues);
65 allIssuesAsString = issuesAsString();
66 allPossibleBids = utils.generateAllPossibleBids(allIssuesAsString, 0);
67 utils.reverse(allPossibleBids);
68 countAll = utils.getIssueCount(allIssues);
69 info_ = info;
70
71 bidOrder = info.getUserModel().getBidRanking().getBidOrder();
72 AdditiveUtilitySpaceFactory factory = new AdditiveUtilitySpaceFactory(getDomain());
73 BidRanking bidRanking = userModel.getBidRanking();
74 factory.estimateUsingBidRanks(bidRanking);
75 utilitySpace = getUtilitySpace();
76
77 double[][] oneHotEncoded = utils.encodeBids(bidOrder, countAll, allIssues);
78 double[][] oneHotEncodedAll = utils.encodeListOfStrings(allPossibleBids, countAll, allIssues);
79
80 utilities = new double[bidOrder.size()];
81 double highBid = info.getUserModel().getBidRanking().getHighUtility();
82 double lowBid = info.getUserModel().getBidRanking().getLowUtility();
83 utilities[0] = lowBid;
84 utilities[utilities.length - 1] = highBid;
85
86 double delta = highBid - lowBid;
87 double decrementAmount = delta / (utilities.length - 1);
88
89
90 for (int i = 1; i < utilities.length - 1; i++) {
91 utilities[i] = utilities[i - 1] + decrementAmount;
92 }
93
94 regression = new OLSMultipleLinearRegression();
95 regression.newSampleData(utilities, oneHotEncoded);
96
97 allBidPredictions = new double[oneHotEncodedAll.length];
98
99 for (int i = 0; i < oneHotEncodedAll.length; i++) {
100 allBidPredictions[i] = utils.predict(regression, oneHotEncodedAll[i]);
101 }
102
103 double max = Arrays.stream(allBidPredictions).max().getAsDouble();
104
105 for (int i = 0; i < oneHotEncodedAll.length; i++) {
106 if (allBidPredictions[i] > 0.9) {
107 allBidPredictions[i] = utils.scale(allBidPredictions[i], 0.9, max);
108 }
109 }
110
111 Bid sampleBid = generateRandomBid();
112 numberOfIssues = sampleBid.getIssues().size();
113 // Array to keep issueWeights we plan to sample
114 opponentModelIssue = new double[numberOfIssues];
115
116 // Array to keep valueWeights we plan to sample
117 opponentModelValue = new double[numberOfIssues][];
118 opponentBids = new ArrayList<>();
119
120 // Creating the 2d array by initializing non-fixed size rows
121 for (int i = 0; i < numberOfIssues; i++) {
122 opponentModelIssue[i] = (double) 1 / numberOfIssues;
123 IssueDiscrete issueDiscrete = (IssueDiscrete) sampleBid.getIssues().get(i);
124 opponentModelValue[i] = new double[issueDiscrete.getValues().size()];
125 }
126
127 allBidsList = new ArrayList<>();
128 for (int i = 0; i < allPossibleBids.size(); i++) {
129 allBidsList.add(utils.asBid(info.getUserModel().getDomain(), convertToStringArray(allPossibleBids.get(i).toArray())));
130 }
131
132 final List<Bid> allBidsCopy = allBidsList;
133 sortedList = new ArrayList<>(allBidsCopy);
134 sortedList.sort(Comparator.comparing(s -> allBidPredictions[allBidsCopy.indexOf(s)]));
135 sortedUtils = Arrays.stream(allBidPredictions).sorted().toArray();
136
137 ourUtilityModel = new ArrayList<>();
138 for (int i = 0; i < sortedList.size(); i++) {
139 ourUtilityModel.add(new Pair<>(utils.bidToListOfString(sortedList.get(i)), sortedUtils[i]));
140 }
141
142 } catch (Exception e) {
143 isRegressionPossible = false;
144 }
145
146 }
147
148 private static String[] convertToStringArray(Object[] array) {
149 String[] result = new String[array.length];
150 for (int i = 0; i < array.length; i++) {
151 result[i] = array[i].toString();
152 }
153 return result;
154 }
155
156 private List<List<String>> issuesAsString() {
157 List<List<String>> allIssuesAsString = new ArrayList<>();
158 for (int i = 0; i < allIssues.size(); i++) {
159 List<String> current = new ArrayList<>();
160 for (int j = 0; j < allIssues.get(i).size(); j++) {
161 current.add(allIssues.get(i).get(j).toString());
162 }
163 allIssuesAsString.add(current);
164 }
165 return allIssuesAsString;
166 }
167
168 @Override
169 public Action chooseAction(List<Class<? extends Action>> validActions) {
170
171 if (isRegressionPossible) {
172 try {
173
174 if (maxBid == null)
175 maxBid = currentBid;
176
177 if (allBidsList.indexOf(currentBid) < allBidsList.indexOf(maxBid))
178 maxBid = currentBid;
179
180 if (currentBid == null || !validActions.contains(Accept.class)) {
181 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
182 }
183
184 if (timeline.getCurrentTime() == 998) {
185 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
186 } else if (timeline.getCurrentTime() == 999) {
187 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
188 } else if (timeline.getCurrentTime() == 1000) {
189 if (0.84 <= utils.predict(regression, utils.encodeBid(maxBid, countAll, allIssues))) {
190 return new Offer(getPartyId(), maxBid);
191 } else {
192 try {
193 Random r = new Random();
194 Bid lastBid = utils.asBid(info_.getUserModel().getDomain(), toStringArray(optimalBids.get(r.nextInt(optimalBids.size()))));
195 if (0.70 <= utils.predict(regression, utils.encodeBid(lastBid, countAll, allIssues))) {
196 return new Offer(getPartyId(), lastBid);
197 } else {
198 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
199 }
200 } catch (Exception e) {
201 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
202 }
203
204 }
205 }
206
207 if (0.92 <= utils.predict(regression, utils.encodeBid(currentBid, countAll, allIssues))) {
208 System.out.println("accepted");
209 return new Accept(getPartyId(), currentBid);
210 } else {
211 try {
212 if (timeline.getCurrentTime() >= 200) {
213 Random r = new Random();
214 return new Offer(getPartyId(), utils.asBid(info_.getUserModel().getDomain(), toStringArray(optimalBids.get(r.nextInt(optimalBids.size())))));
215 }
216 } catch (Exception e) {
217 System.out.println("freq failed");
218 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
219 }
220 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
221 }
222
223 } catch (Exception e) {
224 e.printStackTrace();
225 return new Offer(getPartyId(), generateRandomBid());
226 } finally {
227 if (currentBid != null) {
228 beforeBid = currentBid;
229 }
230 }
231 } else {
232 if (timeline.getCurrentTime() == 995) {
233 return new Offer(getPartyId(), bidOrder.get(bidOrder.size() - 1));
234 }else{
235 return new Offer(getPartyId(), bidOrder.get(bidOrder.size() - 1));
236 }
237 }
238 }
239
240 private void updateWeights() {
241 for (int i = 0; i < numberOfIssues; i++) {
242 ValueDiscrete currentBidValue = (ValueDiscrete) (currentBid.getValue(i + 1));
243 int currentBidValueIndex = utils.getIndexOfValueInIssue(i, currentBidValue.getValue(), currentBid);
244
245 ValueDiscrete beforeBidValue = (ValueDiscrete) (beforeBid.getValue(i + 1));
246 int beforeBidValueIndex = utils.getIndexOfValueInIssue(i, beforeBidValue.getValue(), currentBid);
247
248 if (currentBidValueIndex == beforeBidValueIndex) {
249 opponentModelIssue[i] += ((double) 1 / numberOfIssues);
250 }
251 opponentModelValue[i][currentBidValueIndex] += 1;
252 }
253 }
254
255 // If we hold 2 bids, update the weights with it!
256 @Override
257 public void receiveMessage(AgentID sender, Action action) {
258
259 try {
260 super.receiveMessage(sender, action);
261 if (action instanceof Offer) {
262
263 currentBid = ((Offer) action).getBid();
264 opponentBids.add(currentBid);
265 if (!currentBid.equals(beforeBid) && beforeBid != null) {
266 updateWeights();
267 updateOMValues();
268
269 if (bidCount % 50 == 0) {
270 //updateOpponentModel();
271 estimateUtilities = estimateOpUtil(allPossibleBids, omValueNormalized);
272 opponentUtilityModel = utils.frequencyModelling(opponentBids, allIssuesAsString, opponentModelValue);
273
274 //optimal bids to offer are stored below
275 optimalBids = utils.getOptimalBids(allPossibleBids, utils.mostWanted, regression, info_.getUserModel().getDomain(), countAll, allIssues);
276 }
277 }
278 bidCount++;
279 }
280 } catch (Exception e) {
281 //e.printStackTrace();
282 }
283 }
284
285 private String[] toStringArray(List<String> input) {
286 String[] result = new String[input.size()];
287 for (int i = 0; i < input.size(); i++) {
288 result[i] = input.get(i);
289 }
290 return result;
291 }
292
293 private void updateOMValues() {
294
295 double sum = 0;
296 for (int i = 0; i < opponentModelValue[0].length; i++) {
297 sum += opponentModelValue[0][i];
298 }
299
300 omValueNormalized = new double[opponentModelValue.length][opponentModelValue[0].length];
301 for (int i = 0; i < omValueNormalized.length; i++)
302 omValueNormalized[i] = Arrays.copyOf(opponentModelValue[i], opponentModelValue[i].length);
303
304 for (int i = 0; i < opponentModelValue.length; i++) {
305 for (int j = 0; j < opponentModelValue[i].length; j++) {
306 omValueNormalized[i][j] = opponentModelValue[i][j] / sum;
307 }
308 }
309
310 }
311
312 private double[] estimateOpUtil(List<List<String>> allPossibleBids, double[][] omValueNormalized) {
313 double[] result = new double[allPossibleBids.size()];
314 for (int i = 0; i < allPossibleBids.size(); i++) {
315 for (int j = 0; j < allIssuesAsString.size(); j++) {
316 for (int k = 0; k < allIssuesAsString.get(j).size(); k++) {
317 if (allPossibleBids.get(i).get(j).equals(allIssuesAsString.get(j).get(k))) {
318 result[i] += omValueNormalized[j][k];
319 }
320 }
321 }
322 }
323 return result;
324 }
325
326 @Override
327 public String getDescription() {
328 return "mezgit_soft";
329 }
330
331}
Note: See TracBrowser for help on using the repository browser.