[201] | 1 | package agents.anac.y2019.dandikagent;
|
---|
| 2 |
|
---|
| 3 | import java.util.*;
|
---|
| 4 | import java.util.stream.Collectors;
|
---|
| 5 | import java.util.stream.DoubleStream;
|
---|
| 6 | import java.util.stream.IntStream;
|
---|
| 7 |
|
---|
| 8 | import agents.org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
|
---|
| 9 | import genius.core.AgentID;
|
---|
| 10 | import genius.core.Bid;
|
---|
| 11 | import genius.core.actions.Accept;
|
---|
| 12 | import genius.core.actions.Action;
|
---|
| 13 | import genius.core.actions.Offer;
|
---|
| 14 | import genius.core.issue.Issue;
|
---|
| 15 | import genius.core.issue.IssueDiscrete;
|
---|
| 16 | import genius.core.issue.Value;
|
---|
| 17 | import genius.core.issue.ValueDiscrete;
|
---|
| 18 | import genius.core.parties.AbstractNegotiationParty;
|
---|
| 19 | import genius.core.parties.NegotiationInfo;
|
---|
| 20 | import genius.core.uncertainty.ExperimentalUserModel;
|
---|
| 21 | import javafx.util.Pair;
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * This is dandikAgent by Mehmet Mert Ozgun
|
---|
| 26 | */
|
---|
| 27 | public class dandikAgent extends AbstractNegotiationParty {
|
---|
| 28 |
|
---|
| 29 | private Bid currentBid = null;
|
---|
| 30 | double[] sortedUtils;
|
---|
| 31 | int oppMaxBidIndex = 0;
|
---|
| 32 | HashMap uniqueBids = new HashMap();
|
---|
| 33 | List<List<ValueDiscrete>> allIssues = new ArrayList<>();
|
---|
| 34 | int countAll;
|
---|
| 35 | OLSMultipleLinearRegression regression;
|
---|
| 36 | ArrayList<Bid> allBidsList;
|
---|
| 37 | double[] utilities;
|
---|
| 38 | List<List<String>> allIssuesAsString;
|
---|
| 39 | List<List<String>> allPossibleBids;
|
---|
| 40 | List<Pair<List<String>, Double>> ourUtilityModel;
|
---|
| 41 | ArrayList<Bid> sortedList;
|
---|
| 42 | double[] allBidPredictions;
|
---|
| 43 | boolean amIFirst = false;
|
---|
| 44 | List<Bid> bidOrder;
|
---|
| 45 | private double[][] myModel;
|
---|
| 46 | NegotiationInfo info_;
|
---|
| 47 | List<Issue> issues;
|
---|
| 48 | private int updateCount;
|
---|
| 49 | List<Bid> uniqueBids2 = new ArrayList<>();
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public void init(NegotiationInfo info) {
|
---|
| 53 |
|
---|
| 54 | super.init(info);
|
---|
| 55 | info_ = info;
|
---|
| 56 | issues = info.getUserModel().getDomain().getIssues();
|
---|
| 57 | utils.getIssueDiscrete(issues, allIssues);
|
---|
| 58 | allIssuesAsString = issuesAsString();
|
---|
| 59 | allPossibleBids = utils.generateAllPossibleBids(allIssuesAsString, 0);
|
---|
| 60 | reverse(allPossibleBids);
|
---|
| 61 | countAll = utils.getIssueCount(allIssues);
|
---|
| 62 | bidOrder = info.getUserModel().getBidRanking().getBidOrder();
|
---|
| 63 |
|
---|
| 64 | if(countAll > bidOrder.size()){
|
---|
| 65 | myModel = new double[getUtilitySpace().getDomain().getIssues().size()][];
|
---|
| 66 |
|
---|
| 67 | // Creating the 2d array by initializing non-fixed size rows
|
---|
| 68 | for (int i = 0; i < getUtilitySpace().getDomain().getIssues().size(); i++) {
|
---|
| 69 |
|
---|
| 70 | IssueDiscrete issueDiscrete = (IssueDiscrete) generateRandomBid().getIssues().get(i);
|
---|
| 71 | myModel[i] = new double[issueDiscrete.getValues().size()];
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | updateWeights();
|
---|
| 75 | System.out.println(Arrays.deepToString(myModel));
|
---|
| 76 | } else {
|
---|
| 77 |
|
---|
| 78 | double[][] oneHotEncoded = utils.encodeBids(bidOrder, countAll, allIssues);
|
---|
| 79 | utilitySpace = getUtilitySpace();
|
---|
| 80 | double[][] oneHotEncodedAll = utils.encodeListOfStrings(allPossibleBids, countAll, allIssues);
|
---|
| 81 | utilities = new double[bidOrder.size()];
|
---|
| 82 |
|
---|
| 83 | utilities[0] = 1;
|
---|
| 84 | for (int i = 1; i < utilities.length; i++) {
|
---|
| 85 | utilities[i] = utilities[i - 1] + 1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | regression = new OLSMultipleLinearRegression();
|
---|
| 89 | regression.newSampleData(utilities, oneHotEncoded);
|
---|
| 90 |
|
---|
| 91 | allBidPredictions = new double[oneHotEncodedAll.length];
|
---|
| 92 |
|
---|
| 93 | for (int i = 0; i < oneHotEncodedAll.length; i++) {
|
---|
| 94 | allBidPredictions[i] = utils.predict(regression, oneHotEncodedAll[i]);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | allBidsList = new ArrayList<>();
|
---|
| 98 | for (int i = 0; i < allPossibleBids.size(); i++) {
|
---|
| 99 | allBidsList.add(utils.asBid(info.getUserModel().getDomain(), convertToStringArray(allPossibleBids.get(i).toArray())));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | final List<Bid> allBidsCopy = allBidsList;
|
---|
| 103 | sortedList = new ArrayList<>(allBidsCopy);
|
---|
| 104 | sortedList.sort(Comparator.comparing(s -> allBidPredictions[allBidsCopy.indexOf(s)]));
|
---|
| 105 | sortedUtils = Arrays.stream(allBidPredictions).sorted().toArray();
|
---|
| 106 | ourUtilityModel = new ArrayList<>();
|
---|
| 107 | for (int i = 0; i < sortedList.size(); i++) {
|
---|
| 108 | ourUtilityModel.add(new Pair<>(utils.bidToListOfString(sortedList.get(i)), sortedUtils[i]));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | System.out.println(Arrays.toString(sortedUtils));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private int getIndexOfValueInIssue(Bid bid, int issueIndex, String value) {
|
---|
| 123 | IssueDiscrete is = (IssueDiscrete) bid.getIssues().get(issueIndex);
|
---|
| 124 | return is.getValueIndex(value);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void updateWeights() {
|
---|
| 128 | int numberOfIssues = getUtilitySpace().getDomain().getIssues().size();
|
---|
| 129 | for(int j = 0; j < bidOrder.size(); j++){
|
---|
| 130 | for (int i = 0; i < numberOfIssues; i++) {
|
---|
| 131 | ValueDiscrete currentBidValue = (ValueDiscrete) (bidOrder.get(j).getValue(i + 1));
|
---|
| 132 | int currentBidValueIndex = getIndexOfValueInIssue(bidOrder.get(j), i, currentBidValue.getValue());
|
---|
| 133 |
|
---|
| 134 | if(bidOrder.indexOf(bidOrder.get(j)) >= bidOrder.size() * 0.9){
|
---|
| 135 | myModel[i][currentBidValueIndex] += 1;
|
---|
| 136 | updateCount++;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | @Override
|
---|
| 144 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | if(bidOrder.size() > countAll){
|
---|
| 148 |
|
---|
| 149 | uniqueBids.put(currentBid, 1);
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | List<Double> sortedUtilsList = Arrays.stream(sortedUtils)
|
---|
| 153 | .boxed()
|
---|
| 154 | .collect(Collectors.toList());
|
---|
| 155 |
|
---|
| 156 | int lower = 0;
|
---|
| 157 | if (timeline.getCurrentTime() > 998) {
|
---|
| 158 | lower = (int) (sortedUtilsList.size() - 1);
|
---|
| 159 | } else if (timeline.getCurrentTime() > 975) {
|
---|
| 160 | lower = (int) (0.999 * sortedUtilsList.size());
|
---|
| 161 | } else if (timeline.getCurrentTime() > 950) {
|
---|
| 162 | lower = (int) (0.997 * sortedUtilsList.size());
|
---|
| 163 | } else if (timeline.getCurrentTime() > 900) {
|
---|
| 164 | lower = (int) (0.996 * sortedUtilsList.size());
|
---|
| 165 | } else {
|
---|
| 166 | lower = (int) (0.995 * sortedUtilsList.size());
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | int upper = sortedUtilsList.size();
|
---|
| 170 |
|
---|
| 171 | Double randomElement = sortedUtilsList.get((int) ((Math.random() * (upper - lower)) + lower));
|
---|
| 172 |
|
---|
| 173 | int indexOfRandom = sortedUtilsList.indexOf(randomElement);
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | if (oppMaxBidIndex < sortedList.indexOf(currentBid)) {
|
---|
| 178 | oppMaxBidIndex = sortedList.indexOf(currentBid);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | boolean acceptable = sortedList.indexOf(currentBid) > sortedUtilsList.size() * 0.93;
|
---|
| 182 |
|
---|
| 183 | if (timeline.getCurrentTime() >= 999) {
|
---|
| 184 | if (oppMaxBidIndex < sortedList.size() * 0.85)
|
---|
| 185 | return new Offer(getPartyId(), sortedList.get(sortedList.size() - 1));
|
---|
| 186 | else if (acceptable)
|
---|
| 187 | return new Accept(getPartyId(), currentBid);
|
---|
| 188 | else
|
---|
| 189 | return new Offer(getPartyId(), sortedList.get(oppMaxBidIndex));
|
---|
| 190 | } else if (timeline.getCurrentTime() < 600 &&
|
---|
| 191 | sortedList.indexOf(currentBid) > sortedUtilsList.size() * 0.985) {
|
---|
| 192 | return new Accept(getPartyId(), currentBid);
|
---|
| 193 | } else if (timeline.getCurrentTime() < 990 && timeline.getCurrentTime() > 600 &&
|
---|
| 194 | sortedList.indexOf(currentBid) > sortedUtilsList.size() * 0.985) {
|
---|
| 195 | return new Accept(getPartyId(), currentBid);
|
---|
| 196 | } else if (timeline.getCurrentTime() > 990 && acceptable && timeline.getCurrentTime() < 999) {
|
---|
| 197 | if (oppMaxBidIndex < sortedList.size() / 2)
|
---|
| 198 | return new Offer(getPartyId(), sortedList.get(sortedList.size() - 1));
|
---|
| 199 | else
|
---|
| 200 | return new Accept(getPartyId(), currentBid);
|
---|
| 201 | } else {
|
---|
| 202 | return new Offer(getPartyId(), sortedList.get(indexOfRandom));
|
---|
| 203 | }
|
---|
| 204 | } else {
|
---|
| 205 |
|
---|
| 206 | if(updateCount != 0){
|
---|
| 207 | HashMap<Integer, Value> values = new HashMap();
|
---|
| 208 | for(int i = 0; i < issues.size(); i++){
|
---|
| 209 | IssueDiscrete issueDiscrete = (IssueDiscrete) generateRandomBid().getIssues().get(i);
|
---|
| 210 | int rn = rand.nextInt(issueDiscrete.getValues().size()) ;
|
---|
| 211 |
|
---|
| 212 | if(myModel[i][rn] == 0)
|
---|
| 213 | i--;
|
---|
| 214 | else {
|
---|
| 215 | Value val = new ValueDiscrete(issueDiscrete.getValue(rn).toString());
|
---|
| 216 | values.put(i+1, val);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | Bid b = new Bid(info_.getUserModel().getDomain(), values);
|
---|
| 222 |
|
---|
| 223 | if(!uniqueBids2.contains(b)){
|
---|
| 224 | uniqueBids2.add(b);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | if(timeline.getCurrentTime() < 990){
|
---|
| 228 | return new Offer(getPartyId(), bidOrder.get(bidOrder.size()-1));
|
---|
| 229 | } else{
|
---|
| 230 | if(uniqueBids2.contains(currentBid)){
|
---|
| 231 | return new Accept(getPartyId(), currentBid);
|
---|
| 232 | } else if(estimateUtilitySpace().getUtility(currentBid) > 0.8){
|
---|
| 233 | return new Accept(getPartyId(), currentBid);
|
---|
| 234 | } else
|
---|
| 235 | return new Offer(getPartyId(), b);
|
---|
| 236 |
|
---|
| 237 | }
|
---|
| 238 | } else {
|
---|
| 239 | if(timeline.getCurrentTime() < 999)
|
---|
| 240 | return new Offer(getPartyId(), bidOrder.get(bidOrder.size()-1));
|
---|
| 241 | else if (timeline.getCurrentTime() < 1000)
|
---|
| 242 | return new Offer(getPartyId(), generateRandomBid());
|
---|
| 243 | else
|
---|
| 244 | return new Accept(getPartyId(), currentBid);
|
---|
| 245 |
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | // If we hold 2 bids, update the weights with it!
|
---|
| 254 | @Override
|
---|
| 255 | public void receiveMessage(AgentID sender, Action action) {
|
---|
| 256 | try {
|
---|
| 257 | if (action == null) {
|
---|
| 258 | System.out.println("Hey! I am first!");
|
---|
| 259 | amIFirst = true;
|
---|
| 260 |
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | super.receiveMessage(sender, action);
|
---|
| 264 | if (action instanceof Offer) {
|
---|
| 265 | currentBid = ((Offer) action).getBid();
|
---|
| 266 |
|
---|
| 267 | }
|
---|
| 268 | } catch (Exception e) {
|
---|
| 269 | e.printStackTrace();
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | private static String[] convertToStringArray(Object[] array) {
|
---|
| 275 | String[] result = new String[array.length];
|
---|
| 276 | for (int i = 0; i < array.length; i++) {
|
---|
| 277 | result[i] = array[i].toString();
|
---|
| 278 | }
|
---|
| 279 | return result;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | private void reverse(List<List<String>> allPossibleBids) {
|
---|
| 283 | for (List<String> sublist : allPossibleBids)
|
---|
| 284 | Collections.reverse(sublist);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | private List<List<String>> issuesAsString() {
|
---|
| 288 | List<List<String>> allIssuesAsString = new ArrayList<>();
|
---|
| 289 | for (int i = 0; i < allIssues.size(); i++) {
|
---|
| 290 | List<String> current = new ArrayList<>();
|
---|
| 291 | for (int j = 0; j < allIssues.get(i).size(); j++) {
|
---|
| 292 | current.add(allIssues.get(i).get(j).toString());
|
---|
| 293 |
|
---|
| 294 | }
|
---|
| 295 | allIssuesAsString.add(current);
|
---|
| 296 | }
|
---|
| 297 | return allIssuesAsString;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | @Override
|
---|
| 302 | public String getDescription() {
|
---|
| 303 | return "ANAC2019";
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | }
|
---|