source: src/main/java/agents/anac/y2018/agentherb/AgentHerb.java@ 343

Last change on this file since 343 was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 7.6 KB
Line 
1package agents.anac.y2018.agentherb;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.Map;
8
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.BidIterator;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.EndNegotiation;
15import genius.core.actions.Offer;
16import genius.core.list.Tuple;
17import genius.core.parties.AbstractNegotiationParty;
18import genius.core.parties.NegotiationInfo;
19import genius.core.persistent.PersistentDataType;
20import genius.core.persistent.StandardInfo;
21import genius.core.persistent.StandardInfoList;
22
23public class AgentHerb extends AbstractNegotiationParty{
24 private final Map<AgentID, BidHistory> agentsBidHistories = new HashMap<>();
25 private Bid lastOfferedBid = null;
26 private BidHistory initialHistory = new BidHistory();
27 private final VectorConverter vectorConverter = new VectorConverter();
28
29 private class BidHistory extends ArrayList<Tuple<Bid, Boolean>> {}
30
31 @Override
32 public void init(NegotiationInfo info) {
33 super.init(info);
34 if (this.getData().getPersistentDataType() == PersistentDataType.STANDARD) {
35 StandardInfoList infoList = (StandardInfoList) info.getPersistentData().get();
36 for (StandardInfo sessionInfo : infoList) {
37 Bid initialBid = sessionInfo.getAgreement().get1();
38 if (initialBid != null) {
39 System.out.println(String.format("initial bid: %s", initialBid.toString()));
40 initialHistory.add(new Tuple<>(initialBid, true));
41 }
42 }
43 }
44 }
45
46 /**
47 * Receive message and save it in the bid history
48 * If it is accept save the bid the agent accept with true
49 * if it is offer save the last bid with false since the agent did not accept and
50 * save the bid he made in his offer with true assuming because he offered it he would accept it
51 *
52 * @param sender The id of the agent who sent the message
53 * @param act the action that was sent
54 */
55 @Override
56 public void receiveMessage(AgentID sender, Action act) {
57 super.receiveMessage(sender, act);
58 if (act instanceof Offer || act instanceof Accept) {
59 if (!agentsBidHistories.containsKey(sender)) {
60 agentsBidHistories.put(sender, (BidHistory) initialHistory.clone());
61 }
62 }
63
64 if (act instanceof Offer) {
65 Bid bid = ((Offer) act).getBid();
66 this.agentsBidHistories.get(sender).add(new Tuple<>(bid, true));
67 if (this.lastOfferedBid != null) {
68 this.agentsBidHistories.get(sender).add(new Tuple<>(this.lastOfferedBid, false));
69 }
70 this.lastOfferedBid = bid;
71 } else if (act instanceof Accept) {
72 Bid bid = ((Accept) act).getBid();
73 this.agentsBidHistories.get(sender).add(new Tuple<>(bid, true));
74 }
75 }
76
77 /**
78 * Choose if to accept the last offer or make a new offer
79 *
80 * First we initialize a logistic regression model for each agent (except us) with his bid history
81 * and whether he accepted each bid or rejected it
82 * We train a new logistic regression each time and not use the same one for the whole session
83 * because retrain it each time gives a better results
84 * (probably because of the random weight before you start train it)
85 *
86 * Then for each bid we value the chances each agent will accept it using the logistic regression models
87 * and evaluate the bid by multiple them all together with the utility of the bid and choose the bid
88 * with the highest evaluation
89 *
90 * Then if the last offered bid has an higher utility we accept it, otherwise we offer the bid we chose
91 *
92 * @param list The available actions to do
93 * @return The chosen action
94 */
95 @Override
96 public Action chooseAction(List<Class<? extends Action>> list) {
97 try {
98 System.out.println(getPartyId());
99 System.out.println("choosing action");
100
101 System.out.println("initializing model");
102 List<LogisticRegression> logisticRegressionsModels = this.initializeModels();
103
104 System.out.println("searching for best bid");
105 Bid nextBid = this.findNextBid(logisticRegressionsModels);
106
107 System.out.println("choosing of accepting or offering");
108
109 if (list.contains(Accept.class) && shouldAccept(nextBid)) {
110 System.out.println("Accepting");
111 return new Accept(getPartyId(), lastOfferedBid);
112 } else {
113 System.out.println("offering");
114 this.lastOfferedBid = nextBid;
115 return new Offer(this.getPartyId(), nextBid);
116 }
117
118 } catch (Exception e) {
119 e.printStackTrace();
120 return new EndNegotiation(getPartyId());
121 }
122 }
123
124 /**
125 * @param nextBid The next bid to offer
126 * @return Whether to accept the last bid or offer the nextBid
127 */
128 private boolean shouldAccept(Bid nextBid) {
129 if (lastOfferedBid != null) {
130 System.out.println(String.format("last bid utility %f", this.utilitySpace.getUtility(this.lastOfferedBid)));
131 if (this.getUtility(lastOfferedBid) >= this.getUtility(nextBid) * this.utilitySpace.getDiscountFactor()) {
132 return true;
133 }
134 }
135 return false;
136 }
137
138 /**
139 * @param logisticRegressionsModels The models of the agents
140 * @return The next bid to offer
141 */
142 private Bid findNextBid(List<LogisticRegression> logisticRegressionsModels) {
143 double bestBidEvaluation = 0;
144 Bid nextBid = null;
145
146 BidIterator bidIterator = new BidIterator(this.utilitySpace.getDomain());
147
148 while (bidIterator.hasNext()) {
149 Bid bid = bidIterator.next();
150 Vector vector = this.vectorConverter.convert(bid);
151 double chancesForAcceptance = 1;
152 for (LogisticRegression model : logisticRegressionsModels) {
153 chancesForAcceptance *= model.classify(vector);
154 }
155 double bidUtility = this.utilitySpace.getUtility(bid);
156 double bidEvaluation = bidUtility + chancesForAcceptance;
157
158 if (bidEvaluation >= bestBidEvaluation) {
159 nextBid = bid;
160 bestBidEvaluation = bidEvaluation;
161 }
162 }
163 System.out.println(String.format("next bid evaluation %f", bestBidEvaluation));
164 System.out.println(String.format("next bid utility %f", this.utilitySpace.getUtility(nextBid)));
165 return nextBid;
166 }
167
168 /**
169 * Initialize the models of the agents
170 * each model gets a bid and returns the chances the agent will accept it
171 *
172 * @return The logistic models of the agents
173 */
174 private List<LogisticRegression> initializeModels() {
175 List<LogisticRegression> logisticRegressionsModels = new ArrayList<>();
176 for (BidHistory bidHistory : this.agentsBidHistories.values()) {
177 LogisticRegression logisticRegression = new LogisticRegression(
178 this.vectorConverter.getVectorSize(this.utilitySpace.getDomain()));
179 for (Tuple<Bid, Boolean> bidToDidAccept : bidHistory) {
180 Vector vector = this.vectorConverter.convert(bidToDidAccept.get1());
181 int label = bidToDidAccept.get2() ? 1 : 0;
182 logisticRegression.train(vector, label);
183 }
184 logisticRegressionsModels.add(logisticRegression);
185 }
186 return logisticRegressionsModels;
187 }
188
189 @Override
190 public String getDescription() {
191 return "ANAC2018 AgentHerb";
192 }
193}
Note: See TracBrowser for help on using the repository browser.