source: src/main/java/agents/anac/y2019/agentgp/AgentGP.java@ 346

Last change on this file since 346 was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 10.3 KB
Line 
1package agents.anac.y2019.agentgp;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import agents.anac.y2019.agentgp.etc.BidSearch;
8import agents.anac.y2019.agentgp.etc.NegotiationInfo;
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.Domain;
12import genius.core.actions.Accept;
13import genius.core.actions.Inform;
14import genius.core.actions.Action;
15import genius.core.actions.EndNegotiation;
16import genius.core.actions.Offer;
17import genius.core.parties.AbstractNegotiationParty;
18import genius.core.persistent.PersistentDataType;
19import genius.core.persistent.StandardInfoList;
20import genius.core.persistent.StandardInfo;
21import genius.core.timeline.TimeLineInfo;
22import genius.core.uncertainty.BidRanking;
23import genius.core.utility.AbstractUtilitySpace;
24import genius.core.utility.CustomUtilitySpace;
25import agents.anac.y2019.agentgp.etc.NegotiationStrategy;
26
27/**
28 * This is your negotiation party.
29 */
30public class AgentGP extends AbstractNegotiationParty {
31
32 private TimeLineInfo timelineInfo; // タイムライン
33 private AbstractUtilitySpace utilitySpace;
34
35 private Bid lastReceivedBid = null;
36 private StandardInfoList history; //過去の交渉情報
37
38 private NegotiationInfo negotiationInfo; // 交渉情報
39 private BidSearch bidSearch; // 合意案候補の探索
40 private NegotiationStrategy negotiationStrategy; // 交渉戦略
41
42 private Bid offeredBid = null; // 最近提案された合意案候補
43 private int supporter_num = 0; // 支持者数
44 private int CList_index = 0; // CListのインデックス:最終提案フェーズにおける遡行を行うために利用(ConcessionList)
45 private boolean isPrinting = false; // デバッグ用
46
47 @Override
48 //public void init(AbstractUtilitySpace utilSpace, Deadline dl, negotiator.timeline.TimeLineInfo tl, long randomSeed,
49 // AgentID agentId, PersistentDataContainer storage) {
50 // super.init(utilSpace, dl, tl, randomSeed, agentId, storage);
51 public void init(genius.core.parties.NegotiationInfo info) {
52 super.init(info);
53
54 this.timelineInfo = info.getTimeline();
55 this.utilitySpace = info.getUtilitySpace();
56
57 negotiationInfo = new NegotiationInfo(utilitySpace, isPrinting);
58 negotiationStrategy = new NegotiationStrategy(utilitySpace, isPrinting);
59 try { bidSearch = new BidSearch(utilitySpace, negotiationInfo, isPrinting);
60 } catch (Exception e) {
61 // TODO Auto-generated catch block
62 e.printStackTrace();
63 }
64
65 if (getData().getPersistentDataType() == PersistentDataType.STANDARD) {
66 history = (StandardInfoList) getData().get();
67 if(history.size() > 0){
68 for(StandardInfo standardInfo: history){
69 if(standardInfo.getAgreement().get2()>0.1){
70 try { negotiationInfo.updatePBList(standardInfo.getAgreement().get1());
71 } catch (Exception e) {
72 e.printStackTrace();
73 }
74 }
75 }
76 }
77 //throw new IllegalStateException("need standard persistent data");
78 }
79 }
80
81 /**
82 * Each round this method gets called and ask you to accept or offer. The
83 * first party in the first round is a bit different, it can only propose an
84 * offer.
85 *
86 * @param validActions
87 * Either a list containing both accept and offer or only offer.
88 * @return The chosen action.
89 */
90 static int turn_num;
91 @Override
92 public Action chooseAction(List<Class<? extends Action>> validActions) {
93 //NegotiationInfo.saveFrequency();
94 //NegotiationInfo.saveSort();
95
96 double time = timelineInfo.getTime(); // 現在の時刻
97 //if(turn_num++ % 3 == 0)
98 //System.out.println(turn_num+":"+NegotiationStrategy.getThreshold(time));
99 negotiationInfo.updateTimeScale(time); // 自身の手番が回ってくる時間間隔を記録
100
101 // 最終提案フェーズにおけるアクション
102 ArrayList<Bid> CList = negotiationInfo.getPBList();
103 if(time > 1.0 - negotiationInfo.getTimeScale() * (CList.size() + 1)){
104 //System.out.println("FinalAction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
105 try { return chooseFinalAction(offeredBid, CList); }
106 catch (Exception e) {
107 System.out.println("最終提案フェーズにおけるActionの選択に失敗しました");
108 e.printStackTrace();
109 }
110 }
111
112 // Acceptの判定
113 if(validActions.contains(Accept.class) && negotiationStrategy.selectAccept(offeredBid, time)){
114 return new Accept(getPartyId(), lastReceivedBid);
115 }
116 // EndNegotiationの判定
117 if(negotiationStrategy.selectEndNegotiation(time)){
118 return new EndNegotiation(getPartyId());
119 }
120 // 他のプレイヤーに新たなBidをOffer
121 return OfferAction();
122
123 }
124 public Action OfferAction() {
125 Bid offerBid = bidSearch.getBid(utilitySpace.getDomain().getRandomBid(new Random()), negotiationStrategy.getThreshold(timelineInfo.getTime()), timelineInfo.getTime());
126 return OfferBidAction(offerBid);
127 }
128
129 public Action OfferBidAction(Bid offerBid) {
130 //System.out.println("call CA:"+utilitySpace.getUtility(offerBid));
131 negotiationInfo.updateMyBidHistory(offerBid);
132 return new Offer(getPartyId(), offerBid);
133 }
134
135 public Action chooseFinalAction(Bid offeredBid, ArrayList<Bid> CList) throws Exception {
136 double offeredBid_util = 0;
137 double rv = utilitySpace.getReservationValue();
138
139 if(offeredBid != null){ offeredBid_util = utilitySpace.getUtility(offeredBid); }
140 if(CList_index >= CList.size()){
141 if(offeredBid_util >= rv) return new Accept(getPartyId(), lastReceivedBid); // 遡行を行っても合意が失敗する場合,Acceptする
142 else OfferAction();
143 }
144
145 // CListの遡行
146 Bid CBid = CList.get(CList_index);
147 double CBid_util = utilitySpace.getUtility(CBid);
148 if(CBid_util > offeredBid_util && CBid_util > rv){
149 CList_index++;
150 OfferBidAction(CBid);
151 } else if(offeredBid_util > rv) return new Accept(getPartyId(), lastReceivedBid);
152
153 return OfferAction();
154 }
155
156
157 /**
158 * All offers proposed by the other parties will be received as a message.
159 * You can use this information to your advantage, for example to predict
160 * their utility.
161 *
162 * @param sender
163 * The party that did the action. Can be null.
164 * @param action
165 * The action that party did.
166 */
167 //private int callRM = 1;
168 @Override
169 public void receiveMessage(AgentID sender, Action action) {
170
171 //if(action instanceof Inform)
172 // System.out.println("Inf RM:"+callRM+++","+sender);
173 //if(action instanceof Accept)
174 // System.out.println("Acc RM:"+callRM+++","+sender);
175 //if(action instanceof Offer)
176 // //System.out.println("Off RM:"+callRM+++","+sender+":"+utilitySpace.getUtility(((Offer) action).getBid()));
177 // System.out.println("Off RM:"+callRM+++","+sender+":"+((Offer) action).getBid());
178 //if(action instanceof EndNegotiation){
179 // System.out.println("End RM:"+callRM+++","+sender);
180 //}
181 super.receiveMessage(sender, action);
182 if (action instanceof Offer) {
183 lastReceivedBid = ((Offer) action).getBid();
184 }
185 //if(isPrinting){ System.out.println("Sender:"+sender+", Action:"+action); }
186
187 if (action != null) {
188 if(action instanceof Inform && ((Inform) action).getName() == "NumberOfAgents" && ((Inform) action).getValue() instanceof Integer) {
189 Integer opponentsNum = (Integer) ((Inform) action).getValue();
190 negotiationInfo.updateOpponentsNum(opponentsNum);
191 //if(isPrinting){ System.out.println("NumberofNegotiator:" + NegotiationInfo.getNegotiatorNum());}
192 } else if(action instanceof Accept){
193 if(!negotiationInfo.getOpponents().contains(sender)){ negotiationInfo.initOpponent(sender); } // 初出の交渉者は初期化
194 supporter_num++;
195 negotiationInfo.updateAcceptNum(sender);
196 } else if(action instanceof Offer) {
197 if(!negotiationInfo.getOpponents().contains(sender)){ negotiationInfo.initOpponent(sender); } // 初出の交渉者は初期化
198 supporter_num = 1; // supporterをリセット
199 offeredBid = ((Offer) action).getBid(); // 提案された合意案候補
200 negotiationStrategy.addBid(timelineInfo.getTime(), utilitySpace.getUtility(offeredBid));
201 try { negotiationInfo.updateInfo(sender, offeredBid); } // 交渉情報を更新
202 catch (Exception e) {
203 System.out.println("交渉情報の更新に失敗しました");
204 e.printStackTrace();
205 }
206 } else if(action instanceof EndNegotiation) { }
207
208 // 自身以外が賛成している合意案候補を記録(自身以外のエージェントを1つの交渉者とみなす.そもそも自身以外のエージェントが二人以上非協力であれば,自身の選択に関わらず合意は不可能である)
209 if(supporter_num == negotiationInfo.getNegotiatorNum()-1){
210 if(offeredBid != null){
211 try { negotiationInfo.updatePBList(offeredBid); }
212 catch (Exception e) {
213 System.out.println("PBListの更新に失敗しました"); // PopularBidHistoryを更新
214 e.printStackTrace();
215 }
216 }
217 }
218 }
219 }
220
221 /**
222 * We override the default estimate of the utility
223 * space by using {@link ClosestKnownBid} defined below.
224 */
225 @Override
226 public AbstractUtilitySpace estimateUtilitySpace()
227 {
228 return new ClosestKnownBid(getDomain());
229 }
230
231 /**
232 * Defines a custom UtilitySpace based on the closest known bid to deal with preference uncertainty.
233 */
234 private class ClosestKnownBid extends CustomUtilitySpace
235 {
236
237 public ClosestKnownBid(Domain dom) {
238 super(dom);
239 }
240
241 @Override
242 public double getUtility(Bid bid)
243 {
244 Bid closestRankedBid = getClosestBidRanked(bid);
245 return estimateUtilityOfRankedBid(closestRankedBid);
246 }
247
248 @Override
249 public Double getReservationValue()
250 {
251 return 0.15;
252 }
253
254 public double estimateUtilityOfRankedBid(Bid b)
255 {
256 BidRanking bidRanking = getUserModel().getBidRanking();
257 Double min = bidRanking.getLowUtility();
258 double max = bidRanking.getHighUtility();
259
260 int i = bidRanking.indexOf(b);
261
262 // index:0 has utility min, index n-1 has utility max
263 return min + i * (max - min) / (double) bidRanking.getSize();
264 }
265
266 /**
267 * Finds the bid in the bid ranking that is most similar to bid given in the argument bid
268 */
269 public Bid getClosestBidRanked(Bid bid)
270 {
271 List<Bid> bidOrder = getUserModel().getBidRanking().getBidOrder();
272 Bid closestBid = null;
273 double closestDistance = Double.MAX_VALUE;
274
275 for (Bid b : bidOrder)
276 {
277 double d = 1 / (double) b.countEqualValues(bid);
278 if (d < closestDistance)
279 {
280 closestDistance = d;
281 closestBid = b;
282 }
283 }
284 return closestBid;
285 }
286
287 }
288
289 @Override
290 public String getDescription() {
291 return "Gaussian Process Agent";
292 }
293
294}
295
Note: See TracBrowser for help on using the repository browser.