source: src/main/java/agents/ai2014/group5/Group5.java@ 127

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

Initial import : Genius 9.0.0

File size: 6.6 KB
Line 
1package agents.ai2014.group5;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.DeadlineType;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.DefaultAction;
15import genius.core.actions.Offer;
16import genius.core.issue.Issue;
17import genius.core.parties.AbstractNegotiationParty;
18import genius.core.parties.NegotiationInfo;
19import genius.core.xml.SimpleElement;
20
21/**
22 * This is our negotiation agent. It uses the BOA framework.
23 */
24public class Group5 extends AbstractNegotiationParty {
25 // The bidding strategy
26 private BiddingStrategy bidding;
27
28 // Approximate models of each opponent's utility functions
29 private Map<String, OpponentModel> opponentModels;
30
31 // Issues (issue index-1 and issue name)
32 private List<String> issues;
33
34 // Values (issue index-1, value names, and value index-1)
35 private List<Map<String, Integer>> values;
36
37 // Values (issue index-1, value index-1, value names)
38 private List<Map<Integer, String>> valuesRev;
39
40 /**
41 * This constructor is called by genius.
42 *
43 * @param utilitySpace
44 * Your utility space.
45 * @param deadlines
46 * The deadlines set for this negotiation.
47 * @param timeline
48 * Value counting from 0 (start) to 1 (end).
49 * @param randomSeed
50 * If you use any randomization, use this seed for it.
51 */
52 @Override
53 public void init(NegotiationInfo info) {
54 super.init(info);
55
56 List<Issue> domainIssues = utilitySpace.getDomain().getIssues();
57
58 opponentModels = new HashMap<String, OpponentModel>();
59 issues = new ArrayList<String>(Collections.nCopies(domainIssues.size(), (String) null));
60 values = new ArrayList<Map<String, Integer>>(
61 Collections.nCopies(domainIssues.size(), (Map<String, Integer>) null));
62 valuesRev = new ArrayList<Map<Integer, String>>(
63 Collections.nCopies(domainIssues.size(), (Map<Integer, String>) null));
64
65 for (Issue issue : domainIssues) {
66 // Store issue
67 String issueName = issue.toString();
68 issues.set(issue.getNumber() - 1, issueName);
69
70 // Store issue values
71 List<SimpleElement> xmlValues = issue.toXML().getChildElementsAsList();
72 Map<String, Integer> tmpValueNames = new HashMap<String, Integer>();
73 Map<Integer, String> tmpValueNamesRev = new HashMap<Integer, String>();
74 for (SimpleElement value : xmlValues) {
75 String valueName = value.getAttribute("value");
76 Integer valueIndex = Integer.parseInt(value.getAttribute("index"));
77 tmpValueNames.put(valueName, valueIndex - 1);
78 tmpValueNamesRev.put(valueIndex - 1, valueName);
79 }
80 values.set(issue.getNumber() - 1, tmpValueNames);
81 valuesRev.set(issue.getNumber() - 1, tmpValueNamesRev);
82 }
83
84 bidding = new BiddingStrategy(utilitySpace.getDomain(), opponentModels, values, valuesRev, this);
85 }
86
87 /**
88 * Each round this method gets called and ask you to accept or offer. The
89 * first party in the first round is a bit different, it can only propose an
90 * offer.
91 *
92 * @param validActions
93 * Either a list containing both accept and offer or only offer.
94 * @return The chosen action.
95 */
96 @Override
97 public Action chooseAction(List<Class<? extends Action>> validActions) {
98 if (bidding.deadline == null) {
99 bidding.deadline = getDeadlines().getType() == DeadlineType.ROUND ? getDeadlines().getValue() : 0;
100 }
101 // Consult the bidding strategy!
102 Bid ourBid = bidding.generateBid();
103 boolean randomBidCreated = false;
104 if (ourBid == null) {
105 // The bidding strategy could not find a good offer so just do
106 // something
107 ourBid = generateRandomBid();
108 randomBidCreated = true;
109 }
110 if (validActions.contains(Accept.class) && shouldAccept(bidding.currentOpponentBid, ourBid, bidding.lastBid)) {
111 // The most recent bid is acceptable
112 return new Accept(getPartyId(), bidding.currentOpponentBid);
113 }
114
115 // The most recent bid is not acceptable so offer our own
116 if (!randomBidCreated) {
117 bidding.lastBid = ourBid;
118 }
119 return new Offer(getPartyId(), ourBid);
120 }
121
122 /**
123 * All offers proposed by the other parties will be received as a message.
124 * You can use this information to your advantage, for example to predict
125 * their utility.
126 *
127 * @param sender
128 * The party that did the action.
129 * @param action
130 * The action that party did.
131 */
132 @Override
133 public void receiveMessage(AgentID sender, Action action) {
134 Bid bid = DefaultAction.getBidFromAction(action);
135 if (bid != null) {
136 // The action is an offer
137 bidding.updateOffer(bid);
138 }
139
140 // Update our model of the opponent
141 String opponent = sender.toString();
142 if (!opponentModels.containsKey(opponent)) {
143 opponentModels.put(opponent, new OpponentModel(issues, values, this));
144 }
145 opponentModels.get(opponent).updateModel(bid);
146 }
147
148 /**
149 * Acceptance strategy. This method determines whether or not to accept an
150 * offer. An offer is accepted if the bidding strategy has not found a bid
151 * that's better for us than the offer. At the later stages of the
152 * negotiation the agent may concede a lot
153 *
154 * @param theirBid
155 * Newest bid from an opponent
156 * @param ourBid
157 * Our best bid for this round
158 */
159 public boolean shouldAccept(Bid theirBid, Bid ourBid, Bid ourPreviousBid) {
160 if (theirBid == null || ourBid == null) {
161 // Cannot accept non-existing bids
162 return false;
163 }
164 double u1, u2;
165 try {
166 u1 = utilitySpace.getUtility(theirBid);
167 u2 = utilitySpace.getUtility(ourBid);
168 if (ourPreviousBid != null) {
169 // Average utilities of this bid and the last bid
170 u2 = (u2 + utilitySpace.getUtility(ourPreviousBid)) / 2;
171 }
172 } catch (Exception e) {
173 println("Exception when calculating utilities in \"shouldAccept\"");
174 return false;
175 }
176
177 Integer deadline = bidding.deadline;
178 if (deadline == null) {
179 bidding.setDeadline(getDeadlines().getType() == DeadlineType.ROUND ? getDeadlines().getValue() : 0);
180 deadline = bidding.deadline;
181 }
182 // int roundsLeft = Integer.MAX_VALUE;
183 // double discount = 0.0;
184 // if (bidding.deadline != null) {
185 // roundsLeft = bidding.deadline-(bidding.round+1);
186 // discount = (10.0-(roundsLeft))/10.0;
187 // }
188 // if (roundsLeft <= 10) {
189 // println("Discounting by " + discount);
190 // return u1 >= u2-discount;
191 // } else {
192 // return u1 >= u2;
193 // }
194 int roundsLeft = bidding.deadline - (bidding.round + 1);
195 return roundsLeft < 3 || u1 > u2;
196 }
197
198 public void println(String str) {
199 // System.out.println("[" + this + "] " + str);
200 }
201
202 protected AgentID partyId = new AgentID("Group 5");
203
204 @Override
205 public String getDescription() {
206 return "ai2014 group5";
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.