source: src/main/java/agents/anac/y2017/caduceusdc16/CaduceusDC16.java@ 319

Last change on this file since 319 was 46, checked in by Tim Baarslag, 6 years ago

agents get their getUtilitySpace() from the API, not the info object anymore

File size: 7.3 KB
Line 
1package agents.anac.y2017.caduceusdc16;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.Map;
8import java.util.Random;
9
10import agents.anac.y2016.atlas3.Atlas32016;
11import agents.anac.y2016.caduceus.agents.Caduceus.UtilFunctions;
12import agents.anac.y2016.farma.Farma;
13import agents.anac.y2016.myagent.MyAgent;
14import agents.anac.y2016.parscat.ParsCat;
15import agents.anac.y2016.yxagent.YXAgent;
16import genius.core.AgentID;
17import genius.core.Bid;
18import genius.core.actions.Accept;
19import genius.core.actions.Action;
20import genius.core.actions.Offer;
21import genius.core.issue.Issue;
22import genius.core.issue.Value;
23import genius.core.parties.AbstractNegotiationParty;
24import genius.core.parties.NegotiationInfo;
25import genius.core.parties.NegotiationParty;
26import genius.core.persistent.StandardInfo;
27import genius.core.persistent.StandardInfoList;
28import genius.core.utility.AbstractUtilitySpace;
29
30/**
31 * This is your negotiation party.
32 */
33public class CaduceusDC16 extends AbstractNegotiationParty {
34
35 /**
36 * Each round this method gets called and ask you to accept or offer. The
37 * first party in the first round is a bit different, it can only propose an
38 * offer.
39 *
40 * @param validActions
41 * Either a list containing both accept and offer or only offer.
42 * @return The chosen action.
43 */
44
45 public double discountFactor = 0; // if you want to keep the discount factor
46 private double selfReservationValue = 0.75;
47 private double percentageOfOfferingBestBid = 0.83;
48 private Random random;
49 private Bid lastReceivedBid = null;
50 private AbstractUtilitySpace uspace = null;
51
52 public NegotiationParty[] agents = new NegotiationParty[5];
53 public double[] scores = UtilFunctions
54 .normalize(new double[] { 5, 4, 3, 2, 1 });
55
56 public double getScore(int agentIndex) {
57 return scores[agentIndex];
58 }
59
60 @Override
61 public void init(NegotiationInfo info) {
62
63 super.init(info);
64
65 random = new Random(info.getRandomSeed());
66
67 agents[0] = new YXAgent();
68 agents[1] = new ParsCat();
69 agents[2] = new Farma();
70 agents[3] = new MyAgent();
71 agents[4] = new Atlas32016();
72
73 uspace = getUtilitySpace();
74 discountFactor = getUtilitySpace().getDiscountFactor(); // read
75 // discount
76 // factor
77 double reservationValue = getUtilitySpace()
78 .getReservationValueUndiscounted();
79
80 System.out.println("Discount Factor is " + discountFactor);
81 System.out.println("Reservation Value is " + reservationValue);
82
83 percentageOfOfferingBestBid = percentageOfOfferingBestBid
84 * discountFactor;
85 StandardInfoList history = (StandardInfoList) getData().get();
86 if (!history.isEmpty()) {
87 double total = 0;
88 for (StandardInfo prevHist : history) {
89 int numberOfAgents = prevHist.getAgentProfiles().size();
90 List<genius.core.list.Tuple<String, Double>> agentUtilities = prevHist
91 .getUtilities();
92 int agentUtilitySize = agentUtilities.size();
93 List<genius.core.list.Tuple<String, Double>> finalUtilities = agentUtilities
94 .subList(agentUtilitySize - numberOfAgents,
95 agentUtilitySize);
96 for (genius.core.list.Tuple<String, Double> agentUtility : finalUtilities) {
97 if (agentUtility.get1().toLowerCase()
98 .contains("CaduceusDC16".toLowerCase())) {
99 total += agentUtility.get2();
100 }
101 }
102 }
103 selfReservationValue = total / history.size();
104 System.out.println(selfReservationValue);
105 }
106
107 for (NegotiationParty agent : agents) {
108 agent.init(info);
109 }
110
111 }
112
113 @Override
114 public Action chooseAction(List<Class<? extends Action>> validActions) {
115 if (isBestOfferTime()) {
116 Bid bestBid = this.getBestBid();
117 if (bestBid != null)
118 return new Offer(getPartyId(), bestBid);
119 else
120 System.err.println("Best Bid is null?");
121 }
122
123 ArrayList<Bid> bidsFromAgents = new ArrayList<Bid>();
124 ArrayList<Action> possibleActions = new ArrayList<Action>();
125
126 for (NegotiationParty agent : agents) {
127 Action action = agent.chooseAction(validActions);
128 possibleActions.add(action);
129 }
130
131 double scoreOfAccepts = 0;
132 double scoreOfBids = 0;
133 ArrayList<Integer> agentsWithBids = new ArrayList<>();
134
135 int i = 0;
136 for (Action action : possibleActions) {
137 if (action instanceof Accept) {
138 scoreOfAccepts += getScore(i);
139 } else if (action instanceof Offer) {
140 scoreOfBids += getScore(i);
141 bidsFromAgents.add(((Offer) action).getBid());
142 agentsWithBids.add(i);
143 }
144 i++;
145 }
146 if (scoreOfAccepts > scoreOfBids
147 && uspace.getUtility(lastReceivedBid) >= selfReservationValue) {
148 return new Accept(getPartyId(), lastReceivedBid);
149
150 } else if (scoreOfBids > scoreOfAccepts) {
151 return new Offer(getPartyId(), getMostProposedBidWithWeight(
152 agentsWithBids, bidsFromAgents));
153 }
154
155 return new Offer(getPartyId(), getBestBid());
156 }
157
158 private Bid getRandomizedAction(ArrayList<Integer> agentsWithBids,
159 ArrayList<Bid> bidsFromAgents) {
160 double[] possibilities = new double[agentsWithBids.size()];
161
162 int i = 0;
163 for (Integer agentWithBid : agentsWithBids) {
164 possibilities[i] = getScore(agentWithBid);
165 i++;
166 }
167 possibilities = UtilFunctions.normalize(possibilities);
168 UtilFunctions.print(possibilities);
169 double randomPick = random.nextDouble();
170
171 double acc = 0;
172 i = 0;
173 for (double possibility : possibilities) {
174 acc += possibility;
175
176 if (randomPick < acc) {
177 return bidsFromAgents.get(i);
178 }
179
180 i++;
181 }
182
183 return null;
184 }
185
186 /**
187 * All offers proposed by the other parties will be received as a message.
188 * You can use this information to your advantage, for example to predict
189 * their utility.
190 *
191 * @param sender
192 * The party that did the action. Can be null.
193 * @param action
194 * The action that party did.
195 */
196 @Override
197 public void receiveMessage(AgentID sender, Action action) {
198 super.receiveMessage(sender, action);
199 if (action instanceof Offer)
200 lastReceivedBid = ((Offer) action).getBid();
201
202 for (NegotiationParty agent : agents) {
203 agent.receiveMessage(sender, action);
204 }
205
206 }
207
208 @Override
209 public String getDescription() {
210 return "ANAC2017";
211 }
212
213 private Bid getBestBid() {
214 try {
215 return this.utilitySpace.getMaxUtilityBid();
216 } catch (Exception e) {
217 e.printStackTrace();
218 }
219 return null;
220 }
221
222 private boolean isBestOfferTime() {
223 return this.getTimeLine()
224 .getCurrentTime() < (this.getTimeLine().getTotalTime()
225 * percentageOfOfferingBestBid);
226 }
227
228 private Bid getMostProposedBidWithWeight(ArrayList<Integer> agentsWithBids,
229 ArrayList<Bid> bidsFromAgents) {
230 try {
231 List<Issue> allIssues = bidsFromAgents.get(0).getIssues();
232 HashMap<Integer, Value> bidMap = new HashMap<>();
233 for (int i = 1; i <= allIssues.size(); i++) {
234 Map<Value, Double> proposedValues = new HashMap<>();
235 for (int k = 0; k < agentsWithBids.size(); k++) {
236 Value agentBidValue = bidsFromAgents.get(k).getValue(i);
237 int agentNumber = agentsWithBids.get(k);
238 Double val = proposedValues.get(agentBidValue);
239 proposedValues.put(agentBidValue, val == null ? 1
240 : val + scores[agentsWithBids.get(k)]);
241 }
242 Map.Entry<Value, Double> max = null;
243
244 for (Map.Entry<Value, Double> e : proposedValues.entrySet()) {
245 if (max == null || e.getValue() > max.getValue())
246 max = e;
247 }
248 bidMap.put(i, max.getKey());
249 }
250 return new Bid(utilitySpace.getDomain(), bidMap);
251
252 } catch (Exception e) {
253 e.printStackTrace();
254 return null;
255 }
256 }
257}
Note: See TracBrowser for help on using the repository browser.