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

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

Initial import : Genius 9.0.0

File size: 8.5 KB
Line 
1package agents.anac.y2017.agentf;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import agents.anac.y2017.agentf.etc.bidSearch;
8import agents.anac.y2017.agentf.etc.negotiationInfo;
9import agents.anac.y2017.agentf.etc.negotiationStrategy;
10import genius.core.AgentID;
11import genius.core.Bid;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.EndNegotiation;
15import genius.core.actions.Inform;
16import genius.core.actions.Offer;
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;
22import genius.core.timeline.TimeLineInfo;
23import genius.core.utility.AbstractUtilitySpace;;
24
25/**
26 * This is your negotiation party.
27 */
28public class AgentF extends AbstractNegotiationParty {
29
30 private TimeLineInfo timelineInfo; // タイムライン
31 private AbstractUtilitySpace utilitySpace;
32
33 private Bid lastReceivedBid = null;
34 private StandardInfoList history; // 過去の交渉情報
35
36 private negotiationInfo negotiationInfo; // 交渉情報
37 private bidSearch bidSearch; // 合意案候補の探索
38 private negotiationStrategy negotiationStrategy; // 交渉戦略
39
40 private Bid offeredBid = null; // 最近提案された合意案候補
41 private int supporter_num = 0; // 支持者数
42 private int CList_index = 0; // CListのインデックス:最終提案フェーズにおける遡行を行うために利用(ConcessionList)
43 private boolean isPrinting = false; // デバッグ用
44
45 @Override
46 // public void init(AbstractUtilitySpace utilSpace, Deadline dl,
47 // negotiator.timeline.TimeLineInfo tl, long randomSeed,
48 // AgentID agentId, PersistentDataContainer storage) {
49 // super.init(utilSpace, dl, tl, randomSeed, agentId, storage);
50 public void init(NegotiationInfo info) {
51 super.init(info);
52
53 this.timelineInfo = info.getTimeline();
54 this.utilitySpace = info.getUtilitySpace();
55
56 negotiationInfo = new negotiationInfo(utilitySpace, isPrinting);
57 negotiationStrategy = new negotiationStrategy(utilitySpace, negotiationInfo, isPrinting);
58 try {
59 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 {
71 negotiationInfo.updatePBList(standardInfo.getAgreement().get1());
72 } catch (Exception e) {
73 e.printStackTrace();
74 }
75 }
76 }
77 }
78 // throw new IllegalStateException("need standard persistent data");
79 }
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 @Override
92 public Action chooseAction(List<Class<? extends Action>> validActions) {
93 // negotiationInfo.saveFrequency();
94 // negotiationInfo.saveSort();
95
96 double time = timelineInfo.getTime(); // 現在の時刻
97 negotiationInfo.updateTimeScale(time); // 自身の手番が回ってくる時間間隔を記録
98
99 // 最終提案フェーズにおけるアクション
100 ArrayList<Bid> CList = negotiationInfo.getPBList();
101 if (time > 1.0 - negotiationInfo.getTimeScale() * (CList.size() + 1)) {
102 // System.out.println("FinalAction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
103 try {
104 return chooseFinalAction(offeredBid, CList);
105 } catch (Exception e) {
106 System.out.println("最終提案フェーズにおけるActionの選択に失敗しました");
107 e.printStackTrace();
108 }
109 }
110
111 // Acceptの判定
112 if (validActions.contains(Accept.class) && negotiationStrategy.selectAccept(offeredBid, time)) {
113 return new Accept(getPartyId(), lastReceivedBid);
114 }
115 // EndNegotiationの判定
116 if (negotiationStrategy.selectEndNegotiation(time)) {
117 return new EndNegotiation(getPartyId());
118 }
119 // 他のプレイヤーに新たなBidをOffer
120 return OfferAction();
121
122 }
123
124 public Action OfferAction() {
125 Bid offerBid = bidSearch.getBid(utilitySpace.getDomain().getRandomBid(new Random()),
126 negotiationStrategy.getThreshold(timelineInfo.getTime()), timelineInfo.getTime());
127 return OfferBidAction(offerBid);
128 }
129
130 public Action OfferBidAction(Bid offerBid) {
131 // System.out.println("call CA:"+utilitySpace.getUtility(offerBid));
132 negotiationInfo.updateMyBidHistory(offerBid);
133 return new Offer(getPartyId(), offerBid);
134 }
135
136 public Action chooseFinalAction(Bid offeredBid, ArrayList<Bid> CList) throws Exception {
137 double offeredBid_util = 0;
138 double rv = utilitySpace.getReservationValue();
139
140 if (offeredBid != null) {
141 offeredBid_util = utilitySpace.getUtility(offeredBid);
142 }
143 if (CList_index >= CList.size()) {
144 if (offeredBid_util >= rv)
145 return new Accept(getPartyId(), lastReceivedBid); // 遡行を行っても合意が失敗する場合,Acceptする
146 else
147 OfferAction();
148 }
149
150 // CListの遡行
151 Bid CBid = CList.get(CList_index);
152 double CBid_util = utilitySpace.getUtility(CBid);
153 if (CBid_util > offeredBid_util && CBid_util > rv) {
154 CList_index++;
155 OfferBidAction(CBid);
156 } else if (offeredBid_util > rv)
157 return new Accept(getPartyId(), lastReceivedBid);
158
159 return OfferAction();
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 // private int callRM = 1;
173 @Override
174 public void receiveMessage(AgentID sender, Action action) {
175
176 // if(action instanceof Inform)
177 // System.out.println("Inf RM:"+callRM+++","+sender);
178 // if(action instanceof Accept)
179 // System.out.println("Acc RM:"+callRM+++","+sender);
180 // if(action instanceof Offer)
181 // //System.out.println("Off
182 // RM:"+callRM+++","+sender+":"+utilitySpace.getUtility(((Offer)
183 // action).getBid()));
184 // System.out.println("Off RM:"+callRM+++","+sender+":"+((Offer)
185 // action).getBid());
186 // if(action instanceof EndNegotiation){
187 // System.out.println("End RM:"+callRM+++","+sender);
188 // }
189 super.receiveMessage(sender, action);
190 if (action instanceof Offer) {
191 lastReceivedBid = ((Offer) action).getBid();
192 }
193 // if(isPrinting){ System.out.println("Sender:"+sender+",
194 // Action:"+action); }
195
196 if (action != null) {
197 if (action instanceof Inform && ((Inform) action).getName() == "NumberOfAgents"
198 && ((Inform) action).getValue() instanceof Integer) {
199 Integer opponentsNum = (Integer) ((Inform) action).getValue();
200 negotiationInfo.updateOpponentsNum(opponentsNum);
201 // if(isPrinting){ System.out.println("NumberofNegotiator:" +
202 // negotiationInfo.getNegotiatorNum());}
203 } else if (action instanceof Accept) {
204 if (!negotiationInfo.getOpponents().contains(sender)) {
205 negotiationInfo.initOpponent(sender);
206 } // 初出の交渉者は初期化
207 supporter_num++;
208 negotiationInfo.updateAcceptNum(sender);
209 } else if (action instanceof Offer) {
210 if (!negotiationInfo.getOpponents().contains(sender)) {
211 negotiationInfo.initOpponent(sender);
212 } // 初出の交渉者は初期化
213 supporter_num = 1; // supporterをリセット
214 offeredBid = ((Offer) action).getBid(); // 提案された合意案候補
215 try {
216 negotiationInfo.updateInfo(sender, offeredBid);
217 } // 交渉情報を更新
218 catch (Exception e) {
219 System.out.println("交渉情報の更新に失敗しました");
220 e.printStackTrace();
221 }
222 } else if (action instanceof EndNegotiation) {
223 }
224
225 // 自身以外が賛成している合意案候補を記録(自身以外のエージェントを1つの交渉者とみなす.そもそも自身以外のエージェントが二人以上非協力であれば,自身の選択に関わらず合意は不可能である)
226 if (supporter_num == negotiationInfo.getNegotiatorNum() - 1) {
227 if (offeredBid != null) {
228 try {
229 negotiationInfo.updatePBList(offeredBid);
230 } catch (Exception e) {
231 System.out.println("PBListの更新に失敗しました"); // PopularBidHistoryを更新
232 e.printStackTrace();
233 }
234 }
235 }
236 }
237 }
238
239 @Override
240 public String getDescription() {
241 return "ANAC2017";
242 }
243
244}
Note: See TracBrowser for help on using the repository browser.