source: src/main/java/agents/ai2014/group1/Group1.java@ 127

Last change on this file since 127 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 5.4 KB
Line 
1package agents.ai2014.group1;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6
7import agents.anac.y2010.Yushu.Utility;
8import agents.anac.y2011.IAMhaggler2011.RandomBidCreator;
9import agents.anac.y2012.MetaAgent.agents.MrFriendly.OpponentModel;
10import genius.core.AgentID;
11import genius.core.Bid;
12import genius.core.DeadlineType;
13import genius.core.actions.Accept;
14import genius.core.actions.Action;
15import genius.core.actions.Offer;
16import genius.core.issue.Issue;
17import genius.core.parties.AbstractNegotiationParty;
18import genius.core.parties.NegotiationInfo;
19import genius.core.utility.AdditiveUtilitySpace;
20
21/**
22 * This is your negotiation party.
23 */
24public class Group1 extends AbstractNegotiationParty {
25
26 private Bid bid;
27 private List<String> opponents = new ArrayList<String>();
28
29 private int totalRounds = 0;
30 private int currentRound = 0;
31
32 private Bid myLastBid;
33
34 private List<String> myOpponents = new ArrayList<String>();
35 private HashMap<String, OpponentModel> opponentModels = new HashMap<String, OpponentModel>();
36 private List<Issue> issueList = new ArrayList<Issue>();
37
38 private RandomBidCreator rbc = new RandomBidCreator();
39
40 @Override
41 public void init(NegotiationInfo info) {
42 super.init(info);
43
44 // initialize issueList
45 issueList = this.utilitySpace.getDomain().getIssues();
46 }
47
48 /**
49 * Each round this method gets called and ask you to accept or offer. The
50 * first party in the first round is a bit different, it can only propose an
51 * offer.
52 *
53 * @param validActions
54 * Either a list containing both accept and offer or only offer.
55 * @return The chosen action.
56 */
57 @Override
58 public Action chooseAction(List<Class<? extends Action>> validActions) {
59 totalRounds = getDeadlines().getType() == DeadlineType.ROUND ? getDeadlines().getValue() : 0;
60 // System.out.println("totalRounds: " + totalRounds);
61 currentRound++;
62 // System.out.println("agentID: " + getPartyId().toString() +
63 // "currentRound: " + currentRound);
64 int roundsToGo = totalRounds - currentRound;
65 double threshold = 1.0;
66 Bid myBid;
67 double currentUtility = getUtility(bid);
68
69 // Depending on the deadline, adjust the threshold.
70 if ((double) roundsToGo > 0.5 * totalRounds) {
71 threshold = 0.95;
72 } else if ((double) roundsToGo > 0.3 * totalRounds) {
73 threshold = 0.9;
74 } else if (roundsToGo > 3) {
75 threshold = 0.80;
76 } else {
77 threshold = 0.6;
78 }
79
80 if (currentUtility > threshold) {
81 return new Accept(getPartyId(), bid);
82 } else { // generate bid taking others'utilities into account
83 // generate a bid with high utility for me if it's my first
84 // bid
85 // otherwise, take my last bid as base for my new bid
86 double newUtility = 0;
87 if (myLastBid != null) {
88 Bid tempBid;
89 boolean allOpponentsUtilHigh;
90 double oppThreshold = threshold - 0.2;
91 int counter = 0;
92 do {
93 if (counter == 100) {
94 oppThreshold -= 0.05;
95 counter = 0;
96 }
97 tempBid = rbc.getBid(((AdditiveUtilitySpace) utilitySpace), threshold - 0.05, threshold + 0.05);
98 newUtility = getUtility(tempBid);
99 allOpponentsUtilHigh = checkUtils(oppThreshold, tempBid);
100 counter++;
101 } while ((newUtility < threshold - 0.05 && !allOpponentsUtilHigh));
102 myBid = tempBid;
103 // System.out.println("Bid generated: " + myBid.toString() +
104 // " || utility : " + getUtility(myBid));
105 }
106 // first round
107 else {
108 try {
109 myBid = Utility.getRandomBid(((AdditiveUtilitySpace) utilitySpace));
110 } catch (Exception e1) {
111 do {
112 myBid = generateRandomBid();
113 } while ((newUtility < 0.9));
114 e1.printStackTrace();
115 }
116 newUtility = getUtility(myBid);
117 }
118 }
119 myLastBid = myBid;
120 return new Offer(getPartyId(), myBid);
121 }
122
123 /**
124 * All offers proposed by the other parties will be received as a message.
125 * You can use this information to your advantage, for example to predict
126 * their utility.
127 *
128 * @param sender
129 * The party that did the action.
130 * @param action
131 * The action that party did.
132 */
133 @Override
134 public void receiveMessage(AgentID sender, Action action) {
135 // First check if the opponent is new or not
136 // If so, create a new opponent model for that opponent
137 if (!myOpponents.contains(sender.toString())) {
138 myOpponents.add(sender.toString());
139 addOpponentModel(sender.toString());
140 }
141
142 // Check if the message received is a bid or an accept
143 if (action.toString() == "(Accept)") {
144 // Do nothing.
145 } else if (action instanceof Offer) {
146 // Get bid.
147 bid = ((Offer) action).getBid();
148
149 // if the corresponding opponentModel should exist and the current
150 // bid can be added
151 if (opponentModels.containsKey(sender)) {
152 opponentModels.get(sender).addOpponentBid(bid);
153 }
154 }
155 }
156
157 private void addOpponentModel(String opponent) {
158 if (issueList != null && !opponentModels.containsKey(opponent)) {
159 opponentModels.put(opponent, new OpponentModel(issueList, 1, timeline));
160 }
161 }
162
163 private boolean checkUtils(double threshold, Bid bid) {
164 for (String opponent : opponents) {
165 OpponentModel oppModel = opponentModels.get(opponent);
166 if (oppModel.getEstimatedUtility(bid) <= threshold) {
167 return false;
168 }
169 }
170 return true;
171 }
172
173 protected AgentID partyId = new AgentID("Group 1");
174
175 @Override
176 public String getDescription() {
177 return "ai2014 group1";
178 }
179
180}
Note: See TracBrowser for help on using the repository browser.