source: src/main/java/agents/anac/y2015/group2/Group2.java@ 346

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

Initial import : Genius 9.0.0

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