source: src/main/java/agents/anac/y2017/agentf/AgentF.java@ 337

Last change on this file since 337 was 46, checked in by Tim Baarslag, 6 years ago

agents get their getUtilitySpace() from the API, not the info object anymore

File size: 8.5 KB
Line 
1package agents.anac.y2017.agentf;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Random;
7
8import agents.anac.y2017.agentf.etc.bidSearch;
9import agents.anac.y2017.agentf.etc.negotiationInfo;
10import agents.anac.y2017.agentf.etc.negotiationStrategy;
11import genius.core.AgentID;
12import genius.core.Bid;
13import genius.core.actions.Accept;
14import genius.core.actions.Action;
15import genius.core.actions.EndNegotiation;
16import genius.core.actions.Inform;
17import genius.core.actions.Offer;
18import genius.core.parties.AbstractNegotiationParty;
19import genius.core.parties.NegotiationInfo;
20import genius.core.persistent.PersistentDataType;
21import genius.core.persistent.StandardInfo;
22import genius.core.persistent.StandardInfoList;
23import genius.core.timeline.TimeLineInfo;
24import genius.core.utility.AbstractUtilitySpace;;
25
26/**
27 * This is your negotiation party.
28 */
29public class AgentF extends AbstractNegotiationParty {
30
31 private TimeLineInfo timelineInfo; // タイムライン
32 private AbstractUtilitySpace utilitySpace;
33
34 private Bid lastReceivedBid = null;
35 private StandardInfoList history; // 過去の交渉情報
36
37 private negotiationInfo negotiationInfo; // 交渉情報
38 private bidSearch bidSearch; // 合意案候補の探索
39 private negotiationStrategy negotiationStrategy; // 交渉戦略
40
41 private Bid offeredBid = null; // 最近提案された合意案候補
42 private int supporter_num = 0; // 支持者数
43 private int CList_index = 0; // CListのインデックス:最終提案フェーズにおける遡行を行うために利用(ConcessionList)
44 private boolean isPrinting = false; // デバッグ用
45
46 @Override
47 // public void init(AbstractUtilitySpace utilSpace, Deadline dl,
48 // negotiator.timeline.TimeLineInfo tl, long randomSeed,
49 // AgentID agentId, PersistentDataContainer storage) {
50 // super.init(utilSpace, dl, tl, randomSeed, agentId, storage);
51 public void init(NegotiationInfo info) {
52 super.init(info);
53
54 this.timelineInfo = timeline;
55 this.utilitySpace = getUtilitySpace();
56
57 negotiationInfo = new negotiationInfo(utilitySpace, isPrinting);
58 negotiationStrategy = new negotiationStrategy(utilitySpace, negotiationInfo, isPrinting);
59 try {
60 bidSearch = new bidSearch(utilitySpace, negotiationInfo, isPrinting);
61 } catch (Exception e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 }
65
66 if (getData().getPersistentDataType() == PersistentDataType.STANDARD) {
67 history = (StandardInfoList) getData().get();
68 if (history.size() > 0) {
69 for (StandardInfo standardInfo : history) {
70 if (standardInfo.getAgreement().get2() > 0.1) {
71 try {
72 negotiationInfo.updatePBList(standardInfo.getAgreement().get1());
73 } catch (Exception e) {
74 e.printStackTrace();
75 }
76 }
77 }
78 }
79 // throw new IllegalStateException("need standard persistent data");
80 }
81 }
82
83 /**
84 * Each round this method gets called and ask you to accept or offer. The
85 * first party in the first round is a bit different, it can only propose an
86 * offer.
87 *
88 * @param validActions
89 * Either a list containing both accept and offer or only offer.
90 * @return The chosen action.
91 */
92 @Override
93 public Action chooseAction(List<Class<? extends Action>> validActions) {
94 // negotiationInfo.saveFrequency();
95 // negotiationInfo.saveSort();
96
97 double time = timelineInfo.getTime(); // 現在の時刻
98 negotiationInfo.updateTimeScale(time); // 自身の手番が回ってくる時間間隔を記録
99
100 // 最終提案フェーズにおけるアクション
101 ArrayList<Bid> CList = negotiationInfo.getPBList();
102 if (time > 1.0 - negotiationInfo.getTimeScale() * (CList.size() + 1)) {
103 // System.out.println("FinalAction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
104 try {
105 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
125 public Action OfferAction() {
126 Bid offerBid = bidSearch.getBid(utilitySpace.getDomain().getRandomBid(new Random()),
127 negotiationStrategy.getThreshold(timelineInfo.getTime()), timelineInfo.getTime());
128 return OfferBidAction(offerBid);
129 }
130
131 public Action OfferBidAction(Bid offerBid) {
132 // System.out.println("call CA:"+utilitySpace.getUtility(offerBid));
133 negotiationInfo.updateMyBidHistory(offerBid);
134 return new Offer(getPartyId(), offerBid);
135 }
136
137 public Action chooseFinalAction(Bid offeredBid, ArrayList<Bid> CList) throws Exception {
138 double offeredBid_util = 0;
139 double rv = utilitySpace.getReservationValue();
140
141 if (offeredBid != null) {
142 offeredBid_util = utilitySpace.getUtility(offeredBid);
143 }
144 if (CList_index >= CList.size()) {
145 if (offeredBid_util >= rv)
146 return new Accept(getPartyId(), lastReceivedBid); // 遡行を行っても合意が失敗する場合,Acceptする
147 else
148 OfferAction();
149 }
150
151 // CListの遡行
152 Bid CBid = CList.get(CList_index);
153 double CBid_util = utilitySpace.getUtility(CBid);
154 if (CBid_util > offeredBid_util && CBid_util > rv) {
155 CList_index++;
156 OfferBidAction(CBid);
157 } else if (offeredBid_util > rv)
158 return new Accept(getPartyId(), lastReceivedBid);
159
160 return OfferAction();
161 }
162
163 /**
164 * All offers proposed by the other parties will be received as a message.
165 * You can use this information to your advantage, for example to predict
166 * their utility.
167 *
168 * @param sender
169 * The party that did the action. Can be null.
170 * @param action
171 * The action that party did.
172 */
173 // private int callRM = 1;
174 @Override
175 public void receiveMessage(AgentID sender, Action action) {
176
177 // if(action instanceof Inform)
178 // System.out.println("Inf RM:"+callRM+++","+sender);
179 // if(action instanceof Accept)
180 // System.out.println("Acc RM:"+callRM+++","+sender);
181 // if(action instanceof Offer)
182 // //System.out.println("Off
183 // RM:"+callRM+++","+sender+":"+utilitySpace.getUtility(((Offer)
184 // action).getBid()));
185 // System.out.println("Off RM:"+callRM+++","+sender+":"+((Offer)
186 // action).getBid());
187 // if(action instanceof EndNegotiation){
188 // System.out.println("End RM:"+callRM+++","+sender);
189 // }
190 super.receiveMessage(sender, action);
191 if (action instanceof Offer) {
192 lastReceivedBid = ((Offer) action).getBid();
193 }
194 // if(isPrinting){ System.out.println("Sender:"+sender+",
195 // Action:"+action); }
196
197 if (action != null) {
198 if (action instanceof Inform && ((Inform) action).getName() == "NumberOfAgents"
199 && ((Inform) action).getValue() instanceof Integer) {
200 Integer opponentsNum = (Integer) ((Inform) action).getValue();
201 negotiationInfo.updateOpponentsNum(opponentsNum);
202 // if(isPrinting){ System.out.println("NumberofNegotiator:" +
203 // negotiationInfo.getNegotiatorNum());}
204 } else if (action instanceof Accept) {
205 if (!negotiationInfo.getOpponents().contains(sender)) {
206 negotiationInfo.initOpponent(sender);
207 } // 初出の交渉者は初期化
208 supporter_num++;
209 negotiationInfo.updateAcceptNum(sender);
210 } else if (action instanceof Offer) {
211 if (!negotiationInfo.getOpponents().contains(sender)) {
212 negotiationInfo.initOpponent(sender);
213 } // 初出の交渉者は初期化
214 supporter_num = 1; // supporterをリセット
215 offeredBid = ((Offer) action).getBid(); // 提案された合意案候補
216 try {
217 negotiationInfo.updateInfo(sender, offeredBid);
218 } // 交渉情報を更新
219 catch (Exception e) {
220 System.out.println("交渉情報の更新に失敗しました");
221 e.printStackTrace();
222 }
223 } else if (action instanceof EndNegotiation) {
224 }
225
226 // 自身以外が賛成している合意案候補を記録(自身以外のエージェントを1つの交渉者とみなす.そもそも自身以外のエージェントが二人以上非協力であれば,自身の選択に関わらず合意は不可能である)
227 if (supporter_num == negotiationInfo.getNegotiatorNum() - 1) {
228 if (offeredBid != null) {
229 try {
230 negotiationInfo.updatePBList(offeredBid);
231 } catch (Exception e) {
232 System.out.println("PBListの更新に失敗しました"); // PopularBidHistoryを更新
233 e.printStackTrace();
234 }
235 }
236 }
237 }
238 }
239
240 @Override
241 public String getDescription() {
242 return "ANAC2017";
243 }
244
245}
Note: See TracBrowser for help on using the repository browser.