source: src/main/java/agents/anac/y2016/caduceus/Caduceus.java@ 3

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

Initial import : Genius 9.0.0

File size: 5.5 KB
Line 
1package agents.anac.y2016.caduceus;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import agents.anac.y2016.caduceus.agents.Atlas3.Atlas3;
8import agents.anac.y2016.caduceus.agents.Caduceus.UtilFunctions;
9import agents.anac.y2016.caduceus.agents.ParsAgent.ParsAgent;
10import agents.anac.y2016.caduceus.agents.RandomDance.RandomDance;
11import agents.anac.y2016.caduceus.agents.kawaii.kawaii;
12import genius.core.AgentID;
13import genius.core.Bid;
14import genius.core.actions.Accept;
15import genius.core.actions.Action;
16import genius.core.actions.ActionWithBid;
17import genius.core.actions.Offer;
18import genius.core.parties.AbstractNegotiationParty;
19import genius.core.parties.NegotiationInfo;
20import genius.core.parties.NegotiationParty;
21import genius.core.persistent.DefaultPersistentDataContainer;
22import genius.core.persistent.PersistentDataType;
23
24/**
25 * This is your negotiation party.
26 */
27public class Caduceus extends AbstractNegotiationParty {
28
29 /**
30 * Each round this method gets called and ask you to accept or offer. The
31 * first party in the first round is a bit different, it can only propose an
32 * offer.
33 *
34 * @param validActions
35 * Either a list containing both accept and offer or only offer.
36 * @return The chosen action.
37 */
38
39 public double discountFactor = 0; // if you want to keep the discount factor
40 private double selfReservationValue = 0.75;
41 private double percentageOfOfferingBestBid = 0.83;
42 private Random random;
43
44 public NegotiationParty[] agents = new NegotiationParty[5];
45 public double[] scores = UtilFunctions
46 .normalize(new double[] { 500, 10, 5, 3, 1 });
47
48 public double getScore(int agentIndex) {
49 return scores[agentIndex];
50 }
51
52 @Override
53 public void init(NegotiationInfo info) {
54
55 super.init(info);
56
57 random = new Random(info.getRandomSeed());
58
59 agents[0] = new ParsAgent();
60 agents[1] = new RandomDance();
61 agents[2] = new kawaii();
62 agents[3] = new Atlas3();
63 agents[4] = new agents.anac.y2016.caduceus.agents.Caduceus.Caduceus();
64
65 discountFactor = info.getUtilitySpace().getDiscountFactor(); // read
66 // discount
67 // factor
68 double reservationValue = info.getUtilitySpace()
69 .getReservationValueUndiscounted();
70
71 System.out.println("Discount Factor is " + discountFactor);
72 System.out.println("Reservation Value is " + reservationValue);
73
74 selfReservationValue = Math.max(selfReservationValue, reservationValue);
75 percentageOfOfferingBestBid = percentageOfOfferingBestBid
76 * discountFactor;
77
78 for (NegotiationParty agent : agents) {
79
80 agent.init(new NegotiationInfo(info.getUtilitySpace(),
81 info.getDeadline(), info.getTimeline(),
82 info.getRandomSeed(), info.getAgentID(),
83 new DefaultPersistentDataContainer(null,
84 PersistentDataType.DISABLED)));
85 }
86
87 }
88
89 @Override
90 public Action chooseAction(List<Class<? extends Action>> validActions) {
91 if (isBestOfferTime()) {
92 Bid bestBid = this.getBestBid();
93 if (bestBid != null)
94 return new Offer(getPartyId(), bestBid);
95 else
96 System.err.println("Best Bid is null?");
97 }
98
99 ArrayList<Bid> bidsFromAgents = new ArrayList<Bid>();
100 ArrayList<Action> possibleActions = new ArrayList<Action>();
101
102 for (NegotiationParty agent : agents) {
103 Action action = agent.chooseAction(validActions);
104 possibleActions.add(action);
105 }
106
107 double scoreOfAccepts = 0;
108 double scoreOfBids = 0;
109 ArrayList<Integer> agentsWithBids = new ArrayList<>();
110
111 int i = 0;
112 for (Action action : possibleActions) {
113 if (action instanceof Accept) {
114 scoreOfAccepts += getScore(i);
115 } else if (action instanceof Offer) {
116 scoreOfBids += getScore(i);
117 bidsFromAgents.add(((Offer) action).getBid());
118 agentsWithBids.add(i);
119 }
120 i++;
121 }
122 if (scoreOfAccepts > scoreOfBids) {
123 return new Accept(getPartyId(),
124 ((ActionWithBid) getLastReceivedAction()).getBid());
125
126 } else if (scoreOfBids > scoreOfAccepts) {
127 return new Offer(getPartyId(),
128 getRandomizedAction(agentsWithBids, bidsFromAgents));
129 }
130
131 return new Offer(getPartyId(), getBestBid());
132 }
133
134 private Bid getRandomizedAction(ArrayList<Integer> agentsWithBids,
135 ArrayList<Bid> bidsFromAgents) {
136 double[] possibilities = new double[agentsWithBids.size()];
137
138 int i = 0;
139 for (Integer agentWithBid : agentsWithBids) {
140 possibilities[i] = getScore(agentWithBid);
141 i++;
142 }
143 possibilities = UtilFunctions.normalize(possibilities);
144 UtilFunctions.print(possibilities);
145 double randomPick = random.nextDouble();
146
147 double acc = 0;
148 i = 0;
149 for (double possibility : possibilities) {
150 acc += possibility;
151
152 if (randomPick < acc) {
153 return bidsFromAgents.get(i);
154 }
155
156 i++;
157 }
158
159 return null;
160 }
161
162 /**
163 * All offers proposed by the other parties will be received as a message.
164 * You can use this information to your advantage, for example to predict
165 * their utility.
166 *
167 * @param sender
168 * The party that did the action. Can be null.
169 * @param action
170 * The action that party did.
171 */
172 @Override
173 public void receiveMessage(AgentID sender, Action action) {
174 super.receiveMessage(sender, action);
175
176 for (NegotiationParty agent : agents) {
177 agent.receiveMessage(sender, action);
178 }
179
180 }
181
182 @Override
183 public String getDescription() {
184 return "ANAC2016";
185 }
186
187 private Bid getBestBid() {
188 try {
189 return this.utilitySpace.getMaxUtilityBid();
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 return null;
194 }
195
196 private boolean isBestOfferTime() {
197 return this.getTimeLine()
198 .getCurrentTime() < (this.getTimeLine().getTotalTime()
199 * percentageOfOfferingBestBid);
200 }
201}
Note: See TracBrowser for help on using the repository browser.