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

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

Initial import : Genius 9.0.0

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