1 | package agents.ai2014.group4;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import genius.core.AgentID;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.DeadlineType;
|
---|
11 | import genius.core.actions.Accept;
|
---|
12 | import genius.core.actions.Action;
|
---|
13 | import genius.core.actions.DefaultAction;
|
---|
14 | import genius.core.actions.Offer;
|
---|
15 | import genius.core.issue.Issue;
|
---|
16 | import genius.core.issue.IssueDiscrete;
|
---|
17 | import genius.core.issue.Value;
|
---|
18 | import genius.core.issue.ValueDiscrete;
|
---|
19 | import genius.core.parties.AbstractNegotiationParty;
|
---|
20 | import genius.core.parties.NegotiationInfo;
|
---|
21 | import genius.core.utility.AbstractUtilitySpace;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This is your negotiation party.
|
---|
25 | */
|
---|
26 | public class Group4 extends AbstractNegotiationParty {
|
---|
27 |
|
---|
28 | private Double currentUtility = 0.0;
|
---|
29 | private Double threshold;
|
---|
30 | private Double RESERVATION_VALUE;
|
---|
31 | private final Double STARTING_THRESHOLD = 0.9;
|
---|
32 | private final Double COMPROMISE_RATE = 4.0; // 1 is linear, higher is slower
|
---|
33 | // to compromise
|
---|
34 | private int turns;
|
---|
35 | private int round = 0;
|
---|
36 |
|
---|
37 | private Bid highestBid;
|
---|
38 | private Bid lastGivenBid;
|
---|
39 | private Bid lastReceivedBid;
|
---|
40 | private HashMap<String, Party> parties = new HashMap<String, Party>();
|
---|
41 | private ArrayList<List<ValueDiscrete>> values = new ArrayList<List<ValueDiscrete>>();
|
---|
42 | private BidGenerator bidGenerator;
|
---|
43 |
|
---|
44 | private HashMap<Bid, Double> possibleBids = new HashMap<Bid, Double>();
|
---|
45 | private AbstractUtilitySpace utilitySpace;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Please keep this constructor. This is called by genius.
|
---|
49 | *
|
---|
50 | * @param utilitySpace
|
---|
51 | * Your utility space.
|
---|
52 | * @param deadlines
|
---|
53 | * The deadlines set for this negotiation.
|
---|
54 | * @param timeline
|
---|
55 | * Value counting from 0 (start) to 1 (end).
|
---|
56 | * @param randomSeed
|
---|
57 | * If you use any randomisation, use this seed for it.
|
---|
58 | */
|
---|
59 | @Override
|
---|
60 | public void init(NegotiationInfo info) {
|
---|
61 | // Make sure that this constructor calls it's parent.
|
---|
62 | super.init(info);
|
---|
63 |
|
---|
64 | for (Issue issue : utilitySpace.getDomain().getIssues()) {
|
---|
65 | values.add(((IssueDiscrete) issue).getValues());
|
---|
66 | }
|
---|
67 |
|
---|
68 | // creates the generator
|
---|
69 | generatePossibleBids(0, null);
|
---|
70 | turns = getDeadlines().getType() == DeadlineType.ROUND ? getDeadlines().getValue() : 0; // -1
|
---|
71 | // helps
|
---|
72 | // with
|
---|
73 | // very
|
---|
74 | // low
|
---|
75 | // deadline,
|
---|
76 | // doesn't
|
---|
77 | // hurt
|
---|
78 | // large.
|
---|
79 | bidGenerator = new BidGenerator(this, possibleBids, turns);
|
---|
80 |
|
---|
81 | RESERVATION_VALUE = utilitySpace.getReservationValue();
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void generatePossibleBids(int n, HashMap<Integer, Value> bidValues) {
|
---|
85 | if (n >= values.size()) {
|
---|
86 | Bid b = null;
|
---|
87 |
|
---|
88 | try {
|
---|
89 | b = new Bid(utilitySpace.getDomain(), bidValues);
|
---|
90 | possibleBids.put(b, getUtility(b));
|
---|
91 | } catch (Exception e) {
|
---|
92 | e.printStackTrace();
|
---|
93 | }
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | for (Value v : values.get(n)) {
|
---|
97 | HashMap<Integer, Value> currentBid;
|
---|
98 |
|
---|
99 | if (n == 0) {
|
---|
100 | currentBid = new HashMap<Integer, Value>();
|
---|
101 | } else {
|
---|
102 | currentBid = (HashMap<Integer, Value>) bidValues.clone();
|
---|
103 | }
|
---|
104 |
|
---|
105 | currentBid.put(n + 1, v);
|
---|
106 | generatePossibleBids(n + 1, currentBid);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Each round this method gets called and ask you to accept or offer. The
|
---|
112 | * first party in the first round is a bit different, it can only propose an
|
---|
113 | * offer.
|
---|
114 | *
|
---|
115 | * @param validActions
|
---|
116 | * Either a list containing both accept and offer or only offer.
|
---|
117 | * @return The chosen action.
|
---|
118 | */
|
---|
119 | @Override
|
---|
120 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
121 | round++;
|
---|
122 | threshold = STARTING_THRESHOLD
|
---|
123 | - (STARTING_THRESHOLD - RESERVATION_VALUE) * Math.pow((double) round / (double) turns, COMPROMISE_RATE);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * if(round >= turns){ return new Accept(); }
|
---|
127 | */
|
---|
128 |
|
---|
129 | if (!validActions.contains(Accept.class) || currentUtility < threshold) {
|
---|
130 | Bid b = null;
|
---|
131 | // if it's first turn, get out with best possible bid
|
---|
132 | if (lastGivenBid == null) {
|
---|
133 | try {
|
---|
134 | b = utilitySpace.getMaxUtilityBid();
|
---|
135 | } catch (Exception e) {
|
---|
136 | // TODO Auto-generated catch block
|
---|
137 | e.printStackTrace();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | // do something to get the bid as answer
|
---|
142 | else {
|
---|
143 | // it generates the best not used bid
|
---|
144 | int index = 0;
|
---|
145 | do {
|
---|
146 | b = bidGenerator.generateBestBid();
|
---|
147 |
|
---|
148 | index++;
|
---|
149 | if (index > 100) {
|
---|
150 | index = 0;
|
---|
151 | threshold = threshold - 0.01; // safety measure
|
---|
152 | }
|
---|
153 | } while (getUtility(b) < threshold);
|
---|
154 |
|
---|
155 | }
|
---|
156 | setLastGivenBid(b);
|
---|
157 | return new Offer(getPartyId(), b);
|
---|
158 |
|
---|
159 | } else {
|
---|
160 | return new Accept(getPartyId(), lastReceivedBid);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * All offers proposed by the other parties will be received as a message.
|
---|
166 | * You can use this information to your advantage, for example to predict
|
---|
167 | * their utility.
|
---|
168 | *
|
---|
169 | * @param sender
|
---|
170 | * The party that did the action.
|
---|
171 | * @param action
|
---|
172 | * The action that party did.
|
---|
173 | */
|
---|
174 | @Override
|
---|
175 | public void receiveMessage(AgentID sender, Action action) {
|
---|
176 | super.receiveMessage(sender, action);
|
---|
177 |
|
---|
178 | if (!parties.containsKey(sender.toString())) {
|
---|
179 | Party party = new Party(sender.toString(), utilitySpace.getDomain(), sender);
|
---|
180 | parties.put(sender.toString(), party);
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (lastReceivedBid != null) {
|
---|
184 | parties.get(sender.toString()).updateWithBid(lastReceivedBid, action);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (action instanceof Offer) {
|
---|
188 | lastReceivedBid = DefaultAction.getBidFromAction(action);
|
---|
189 | currentUtility = getUtility(lastReceivedBid);
|
---|
190 | updateHighestBid(lastReceivedBid);
|
---|
191 | } else if (action instanceof Accept) {
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void updateHighestBid(Bid b) {
|
---|
197 | // TODO it check if it is the highest and in case update it
|
---|
198 | }
|
---|
199 |
|
---|
200 | // *******GETTER AND SETTER********
|
---|
201 |
|
---|
202 | public Double getCurrentUtility() {
|
---|
203 | return currentUtility;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void setCurrentUtility(Double currentUtility) {
|
---|
207 | this.currentUtility = currentUtility;
|
---|
208 | }
|
---|
209 |
|
---|
210 | public Double getThreshold() {
|
---|
211 | return threshold;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void setThreshold(Double threshold) {
|
---|
215 | this.threshold = threshold;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public Bid getHighestBid() {
|
---|
219 | return highestBid;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void setHighestBid(Bid highestBid) {
|
---|
223 | this.highestBid = highestBid;
|
---|
224 | }
|
---|
225 |
|
---|
226 | public Bid getLastGivenBid() {
|
---|
227 | return lastGivenBid;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public void setLastGivenBid(Bid lastGivenBid) {
|
---|
231 | this.lastGivenBid = lastGivenBid;
|
---|
232 | }
|
---|
233 |
|
---|
234 | public HashMap<String, Party> getParties() {
|
---|
235 | return parties;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public void setParties(HashMap<String, Party> parties) {
|
---|
239 | this.parties = parties;
|
---|
240 | }
|
---|
241 |
|
---|
242 | public int getTurns() {
|
---|
243 | return turns;
|
---|
244 | }
|
---|
245 |
|
---|
246 | protected AgentID partyId = new AgentID("Group 4");
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public String getDescription() {
|
---|
250 | return "ai2014 group4";
|
---|
251 | }
|
---|
252 |
|
---|
253 | }
|
---|