source: src/main/java/agents/anac/y2015/pnegotiator/BayesLearner.java@ 1

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

Initial import : Genius 9.0.0

File size: 6.4 KB
Line 
1package agents.anac.y2015.pnegotiator;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import genius.core.AgentID;
7import genius.core.Bid;
8import genius.core.actions.Accept;
9import genius.core.actions.Action;
10import genius.core.actions.ActionWithBid;
11import genius.core.actions.DefaultAction;
12import genius.core.actions.Offer;
13import genius.core.issue.Objective;
14import genius.core.parties.AbstractNegotiationParty;
15import genius.core.parties.NegotiationInfo;
16import genius.core.utility.AdditiveUtilitySpace;
17
18/**
19 * This is your negotiation party.
20 */
21public class BayesLearner extends AbstractNegotiationParty {
22
23 // State of the agent (am I being a hardliner or am I conceding?)
24 public enum AgentState {
25 HARDLINER, CONCEDER
26 };
27
28 public AgentState agentState;
29
30 // Parameters of the negotiation
31 // private UtilitySpace utilitySpace;
32 // private Timeline timeline;
33 private List<Objective> objectives;
34
35 // Other private fields
36 private Bid currentBid = null;
37 private Bid nextBid = null;
38 // private double concessionFactor = 1;
39 // private int totalBids = 1;
40
41 private BestBids bestBids;
42 private BayesLogic bayesLogic;
43
44 private double lRng = 0.9, rRng = 1.0;
45
46 @Override
47 public void init(NegotiationInfo info) {
48 super.init(info);
49 // Set agent's game parameters
50 // this.utilitySpace = utilitySpace;
51 // this.timeline = timeline;
52 this.objectives = utilitySpace.getDomain().getObjectives();
53
54 try {
55 this.bestBids = new BestBids((AdditiveUtilitySpace) this.utilitySpace);
56 } catch (Exception e) {
57 e.printStackTrace();
58 }
59
60 agentState = AgentState.HARDLINER;
61
62 // Print stuff for the benefit of our understanding
63 // System.out.println("Discount factor: " +
64 // utilitySpace.getDiscountFactor());
65 // for(int i = 1; i < objectives.size(); ++i)
66 // {
67 // System.out.println("(Name, Type, Weight): (" +
68 // objectives.get(i).getName() + ", " + objectives.get(i).getType() +
69 // ", "+ utilitySpace.getWeight(i) + ")");
70 // }
71 //
72 // System.out.println("Disagreement point (Discounted): " +
73 // utilitySpace.getReservationValueWithDiscount(timeline));
74 // System.out.println("Disagreement point (Undiscounted): " +
75 // utilitySpace.getReservationValueUndiscounted());
76 // System.out.println("Discount factor: " +
77 // utilitySpace.getDiscountFactor());
78 // System.out.println("Number of possible bids: " +
79 // domain.getNumberOfPossibleBids());
80 }
81
82 /**
83 * Each round this method gets called and ask you to accept or offer. The
84 * first party in the first round is a bit different, it can only propose an
85 * offer.
86 *
87 * @param validActions
88 * Either a list containing both accept and offer or only offer.
89 * @return The chosen action.
90 */
91 public Action chooseAction(List<Class<? extends Action>> validActions) {
92 // ++totalBids;
93 try {
94 setState();
95 updateBayes();
96 System.out.println(currentBid);
97 if (currentBid == null) {
98 nextBid = utilitySpace.getMaxUtilityBid();
99 } else {
100 // setState();
101 lRng = getConcessionUtility();
102 Bid bestBid = bestBids.getRandomBid(generateRandomBid(), rand, lRng, rRng);
103 double CU = utilitySpace.getUtility(currentBid);
104 System.out.format("%6.4f < %6.4f < %6.4f\n", lRng, utilitySpace.getUtility(bestBid), rRng);
105 switch (agentState) {
106 case HARDLINER:
107 // System.out.println("HARDLINER");
108 nextBid = bestBid;
109 break;
110 default:
111 // System.out.println("CONCEDER");
112 Bid bayes;
113 double U1;
114 do {
115 bayes = bayesLogic.bayesBid(utilitySpace.getMaxUtilityBid());
116 U1 = utilitySpace.getUtility(bayes);
117 // System.out.format("V: %3d U: %6.4f\n", bayesLogic.V,
118 // U1);
119 bayesLogic.V++;
120 } while (U1 < lRng);
121 bayesLogic.V = 1;
122 // bayes = bayesLogic.bayesBid2(rand);
123 // U1 = utilitySpace.getUtility(bayes);
124 // System.out.format("Bayes: %6.4f | Proposed: %6.4f\n",
125 // U1,CU);
126 nextBid = bayes;
127 }
128 // bayesLogic.updateOpponentFrequency(bestBid, 0);
129 if (utilitySpace.getUtility(currentBid) >= lRng)
130 return new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
131 }
132 } catch (Exception e) {
133 e.printStackTrace();
134 }
135 // Print the total utility of the bid we are making and submit it
136 System.out.println("Proposing bid with utility: " + utilitySpace.getUtilityWithDiscount(nextBid, timeline));
137 return new Offer(getPartyId(), nextBid);
138 }
139
140 private void updateBayes() throws Exception {
141 if (bayesLogic == null) {
142 this.bayesLogic = new BayesLogic((AdditiveUtilitySpace) this.utilitySpace, this.getNumberOfParties());
143 }
144 bayesLogic.T = (int) (getTimeLine().getTime() * 100);
145 System.out.format("T: %d\n", bayesLogic.T);
146 }
147
148 /**
149 * All offers proposed by the other parties will be received as a message.
150 * You can use this information to your advantage, for example to predict
151 * their utility.
152 *
153 * @param sender
154 * The party that did the action.
155 * @param action
156 * The action that party did.
157 */
158 @Override
159 public void receiveMessage(AgentID sender, Action action) {
160 super.receiveMessage(sender, action);
161 if (!players.contains(sender)) {
162 players.add(sender);
163 }
164 int P = players.indexOf(sender);
165 if (DefaultAction.getBidFromAction(action) != null)
166 currentBid = DefaultAction.getBidFromAction(action);
167 try {
168 if (currentBid != null) {
169 updateBayes();
170 bayesLogic.updateOpponentFrequency(currentBid, P);
171 }
172 } catch (Exception e) {
173 e.printStackTrace();
174 }
175 }
176
177 ArrayList<Object> players = new ArrayList<Object>();
178
179 // A dumb function that just sets a scalar concession factor based on the
180 // current time
181 private double getConcessionUtility() throws Exception {
182 double Bj = utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
183 double Cj = Bj * timeline.getTime() * timeline.getTime();
184 double Uj = Bj - (Cj / objectives.size());
185 // System.out.println("Max : "+Bj+"Cj "+Cj+" time:
186 // "+timeline.getTime()+"Bound:"+Uj);
187 return Uj * Uj;
188 }
189
190 // Logic to decide the next bid
191 // private void decideNextBid() throws Exception {
192 // switch(agentState) {
193 // case HARDLINER:
194 // System.out.println("MaxRand");
195 // nextBid = bestBids.getRandomBid(generateRandomBid(), rand, 0.8, 1.);
196 // break;
197 // case CONCEDER:
198 // System.out.println("Bayes");
199 // nextBid = bayesLogic.bayesBid(utilitySpace.getMaxUtilityBid());
200 // }
201 // }
202
203 private void setState() {
204 if (timeline.getTime() >= .2) {
205 agentState = AgentState.CONCEDER;
206 }
207 }
208
209 @Override
210 public String getDescription() {
211 return "ANAC2015-22-PNegotiator";
212 }
213}
Note: See TracBrowser for help on using the repository browser.