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