source: src/main/java/agents/ai2014/group2/Group2.java@ 61

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

Initial import : Genius 9.0.0

File size: 8.0 KB
Line 
1package agents.ai2014.group2;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8import java.util.Set;
9
10import genius.core.AgentID;
11import genius.core.Bid;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.ActionWithBid;
15import genius.core.actions.DefaultAction;
16import genius.core.actions.Offer;
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 Group2 extends AbstractNegotiationParty {
25 Map<Object, G2OpponentModel> opponentModels;
26 G2Bid _previousBid;
27 G2UtilitySpace ourUtilitySpace;
28 G2Logger logger = new G2Logger();
29
30 G2CSVLogger csvLogger_real;
31 G2CSVLogger csvLogger_model;
32 double proposedUtility = 0;
33
34 boolean loggingOn = false;
35
36 int partyNumber = 1;
37 int round = 0;
38 static final int MAX_ROUNDS = 180;
39 double reservationValue;
40
41 @Override
42 public void init(NegotiationInfo info) {
43 super.init(info);
44
45 opponentModels = new HashMap<Object, G2OpponentModel>();
46 ourUtilitySpace = new G2UtilitySpace((AdditiveUtilitySpace) utilitySpace);
47 reservationValue = utilitySpace.getReservationValueUndiscounted();
48 if (loggingOn) {
49 logger.log("Our utilityspace");
50 logger.log(this.ourUtilitySpace.allDataString());
51 csvLogger_real = new G2CSVLogger();
52 csvLogger_model = new G2CSVLogger();
53 }
54
55 }
56
57 /**
58 * Calculates maximum utility bid and returns it as an Action
59 *
60 * @return The maximum utility Action
61 */
62 private Action calcMaxUtilityAction() {
63 // How hard can it be to take the maximum option for each issue??
64 // seems a bit absurd to accept an offer in case of an exception...
65 try {
66 Bid bid = getUtilitySpace().getMaxUtilityBid();
67 _previousBid = new G2Bid(bid);
68 logBid(_previousBid, false);
69 return new Offer(getPartyId(), bid);
70 } catch (Exception e1) {
71 // TODO Auto-generated catch block
72 e1.printStackTrace();
73 System.out.println("Error: Our UtilitySpace is empty.");
74 return new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
75 }
76 }
77
78 private double calcMinUtility() {
79 double time = getTimeLine().getTime();
80 if (round == 0)
81 return 1;
82 double roundTime = time / round;
83 // System.out.println(round + ">"+roundTime);
84 if (roundTime > (1.0 / 20))
85 return 1 - (1 - reservationValue) * time * time;
86 else if (time > (1 - roundTime * 20)) {
87 double newTime = (time - (1 - roundTime * 20)) / (roundTime * 20);
88 // System.out.println("newTime:" + newTime);
89 return 1 - (1 - reservationValue) * newTime * newTime;
90 } else {
91 return 0.95;
92 }
93 }
94
95 /**
96 * Calculates next Action based on the previous bid
97 *
98 * @return the next Action of the agent
99 */
100 private Action calcNextAction() {
101 ArrayList<G2UtilitySpace> opponentUtilitySpaces = new ArrayList<G2UtilitySpace>();
102 for (G2OpponentModel opponent : opponentModels.values()) {
103 opponentUtilitySpaces.add(opponent.getUtilitySpace());
104 }
105 G2ParetoFinder paretoFinder = new G2ParetoFinder(ourUtilitySpace, opponentUtilitySpaces);
106
107 ArrayList<G2Bid> bids = paretoFinder.findParetoOptimalBids();
108
109 double minUtility = calcMinUtility();
110
111 // Random bid picking algorithm
112 int i = 0;
113 while (i < bids.size() - 1 && ourUtilitySpace.calculateUtility(bids.get(i)) < minUtility)
114 i++;
115 Random rand = new Random();
116 int randomNum = rand.nextInt((bids.size() - 1 - i) + 1) + i;
117 G2Bid nextBid = bids.get(randomNum);
118 // System.out.println("size:" + bids.size() + ", i:" + i + ", rand:" +
119 // randomNum);
120
121 // Generate the Bid from the G2Bid
122 Bid returnBid = null;
123 try {
124 returnBid = nextBid.convertToBid(getUtilitySpace().getDomain(),
125 ((AdditiveUtilitySpace) getUtilitySpace()).getEvaluators());
126 } catch (Exception e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130
131 // Accept if the proposedUtility is higher than our minimum or if the
132 // Bid was incorrectly generated
133 if (returnBid == null || calcMinUtility() < proposedUtility) {
134 logBid(nextBid, true);
135
136 return new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
137 } else { // Else make the Bid we just calculated
138 logBid(nextBid, false);
139
140 return new Offer(getPartyId(), returnBid);
141 }
142 }
143
144 public void logBid(G2Bid bid, boolean accept) {
145
146 ArrayList<Double> utilities = new ArrayList<Double>();
147 for (G2OpponentModel opponentModel : opponentModels.values()) {
148 if (accept) {
149 utilities.add(-1.0);
150 } else {
151 utilities.add(opponentModel.getUtility(bid));
152 }
153 }
154 if (opponentModels.size() == 0) {
155 utilities.add(0.0);
156 }
157 if (loggingOn) {
158 csvLogger_model.log(utilities);
159 csvLogger_real.log(ourUtilitySpace.calculateUtility(bid));
160 }
161 }
162
163 /**
164 * Each round this method gets called and ask you to accept or offer. The
165 * first party in the first round is a bit different, it can only propose an
166 * offer.
167 *
168 * @param validActions
169 * Either a list containing both accept and offer or only offer.
170 * @return The chosen action.
171 */
172 @Override
173 public Action chooseAction(List<Class<? extends Action>> validActions) {
174 // Initialize the logger with your partynumber once it is known
175 if (!logger.isInitialized() && loggingOn) {
176 logger.init(partyNumber);
177 csvLogger_real.init(partyNumber, "real");
178 csvLogger_model.init(partyNumber, "model");
179 }
180
181 // Increase round timer
182 round++;
183
184 // In the first round return the bid with maximum utility for ourself
185 if (_previousBid == null) {
186 return calcMaxUtilityAction();
187 // Else make a bid based on out previous bid
188 } else {
189 return calcNextAction();
190 }
191 }
192
193 /**
194 * All offers proposed by the other parties will be received as a message.
195 * You can use this information to your advantage, for example to predict
196 * their utility.
197 *
198 * @param sender
199 * The party that did the action.
200 * @param action
201 * The action that party did.
202 */
203 @Override
204 public void receiveMessage(AgentID sender, Action action) {
205 super.receiveMessage(sender, action);
206 if (round == 0) {
207 partyNumber++;
208 }
209 // don't update opponent when action is accept, and thus contains no bid
210 if (DefaultAction.getBidFromAction(action) != null) {
211 // If the map does not contain a UtilitySpace for this opponent then
212 // make a new one
213 if (!opponentModels.containsKey(sender)) {
214 opponentModels.put(sender, new G2OpponentModel((AdditiveUtilitySpace) getUtilitySpace()));
215 }
216 G2Bid bid = new G2Bid(DefaultAction.getBidFromAction(action));
217 opponentModels.get(sender).updateModel(bid);
218
219 proposedUtility = ourUtilitySpace.calculateUtility(bid);
220 if (loggingOn) {
221 csvLogger_real.log(proposedUtility);
222 logger.log("received bid from: " + sender);
223 logger.log(bid.toString());
224 logger.log("updated opponentModel");
225 logger.log(opponentModels.get(sender).getUtilitySpaceString());
226 }
227 } else {
228 if (loggingOn) {
229 csvLogger_real.log(-1.0);
230 }
231 }
232 }
233
234 static public List<G2Bid> getAlternativeBids(List<G2Bid> bids, Set<Map.Entry<String, G2Issue>> issues) {
235 List<G2Bid> alternativeBids = new ArrayList<G2Bid>(bids);
236 for (G2Bid bid : bids) {
237 alternativeBids.addAll(getAlternativeBids(bid, issues));
238 }
239 return alternativeBids;
240 }
241
242 static public List<G2Bid> getAlternativeBids(G2Bid bid, Set<Map.Entry<String, G2Issue>> issues) {
243
244 List<G2Bid> bids = new ArrayList<G2Bid>();
245 Map<String, String> originalOptions = bid.getChoices();
246 for (Map.Entry<String, G2Issue> entry : issues) {
247 // get all alternative options for current issue
248 Set<String> otherOptions = entry.getValue().getOtherOptions(bid.getChoice(entry.getKey()));
249 for (String option : otherOptions) {
250 // create a new bid, equal to the original one
251 G2Bid newBid = new G2Bid(originalOptions);
252 // set one of the issue to an alternative option
253 newBid.setChoice(entry.getKey(), option);
254 // save it in the array
255 bids.add(newBid);
256 }
257 }
258
259 return bids;
260 }
261
262 protected AgentID partyId = new AgentID("Group 2");
263
264 @Override
265 public String getDescription() {
266 return "ai2014 group2";
267 }
268
269}
Note: See TracBrowser for help on using the repository browser.