source: src/main/java/agents/anac/y2017/agentkn/AgentKN.java

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

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

File size: 4.7 KB
RevLine 
[1]1package agents.anac.y2017.agentkn;
2
[46]3import java.util.List;
4
[1]5import java.util.HashMap;
6import java.util.Map;
7
8import agents.anac.y2017.agentkn.etc.bidSearch;
9import agents.anac.y2017.agentkn.etc.negotiationInfo;
10import agents.anac.y2017.agentkn.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.list.Tuple;
19import genius.core.parties.AbstractNegotiationParty;
20import genius.core.parties.NegotiationInfo;
21import genius.core.persistent.PersistentDataType;
22import genius.core.persistent.StandardInfo;
23import genius.core.persistent.StandardInfoList;
24import genius.core.timeline.TimeLineInfo;
25import genius.core.utility.AbstractUtilitySpace;
26import genius.core.utility.AdditiveUtilitySpace;
27
28public class AgentKN extends AbstractNegotiationParty {
29 private TimeLineInfo timelineInfo; // タイムライン
30 private AbstractUtilitySpace utilitySpace;
31
32 private Bid mLastReceivedBid = null;
33 private int nrChosenActions = 0; // number of times chosenAction was called.
34 private StandardInfoList history;
35 private negotiationStrategy mStrategy;
36 private negotiationInfo mNegotiationInfo;
37 private bidSearch mBidSerch;
38
39 private boolean isPrinting = false;
40
41 private Bid mOfferedBid = null;
42
43 @Override
44 public void init(NegotiationInfo aInfo) {
45 super.init(aInfo);
46
47 if (isPrinting) {
48 System.out.println("*** AgentKN ***");
49 }
[46]50 System.out.println("Discount Factor is " + getUtilitySpace().getDiscountFactor());
51 System.out.println("Reservation Value is " + getUtilitySpace().getReservationValueUndiscounted());
[1]52
[46]53 mNegotiationInfo = new negotiationInfo((AdditiveUtilitySpace) getUtilitySpace(), isPrinting);
[1]54 try {
[46]55 mBidSerch = new bidSearch((AdditiveUtilitySpace) getUtilitySpace(), mNegotiationInfo, isPrinting);
[1]56 } catch (Exception e) {
57 throw new RuntimeException("init failed: " + e);
58 }
59
60 // need standard data 過去情報取得
61 if (getData().getPersistentDataType() != PersistentDataType.STANDARD) {
62 throw new IllegalStateException("need standard persistent data");
63 }
64 history = (StandardInfoList) getData().get();
65
66 if (!history.isEmpty()) {
67 // example of using the history. Compute for each party the maximum
68 // utility of the bids in last session.
69 Map<String, Double> maxutils = new HashMap<String, Double>();
70 StandardInfo lastinfo = history.get(history.size() - 1);
71 for (Tuple<String, Double> offered : lastinfo.getUtilities()) {
72 String party = offered.get1();
73 Double util = offered.get2();
74 maxutils.put(party, maxutils.containsKey(party) ? Math.max(maxutils.get(party), util) : util);
75 }
76 }
77
[46]78 mStrategy = new negotiationStrategy((AdditiveUtilitySpace) getUtilitySpace(), mNegotiationInfo,
[1]79 isPrinting);
80 }
81
82 @Override
83 public Action chooseAction(List<Class<? extends Action>> validActions) {
84 double time = getTimeLine().getTime();
85 return (Action) (validActions.contains(Accept.class) && mStrategy.selectAccept(this.mOfferedBid, time)
86 ? new Accept(this.getPartyId(), this.mOfferedBid)
87 : (mStrategy.selectEndNegotiation(time) ? new EndNegotiation(this.getPartyId()) : this.OfferAction()));
88 }
89
90 private Action OfferAction() {
91 Bid offerBid = mBidSerch.getBid(this.generateRandomBid(), mStrategy.getThreshold(getTimeLine().getTime()));
92 mNegotiationInfo.updateMyBidHistory(offerBid);
93 return new Offer(this.getPartyId(), offerBid);
94 }
95
96 @Override
97 public void receiveMessage(AgentID sender, Action action) {
98 super.receiveMessage(sender, action);
99
100 if (action != null) {
101 if (action instanceof Inform && ((Inform) action).getName() == "NumberOfAgents"
102 && ((Inform) action).getValue() instanceof Integer) {
103 Integer e = (Integer) ((Inform) action).getValue();
104 mNegotiationInfo.updateOpponentsNum(e.intValue());
105 if (isPrinting) {
106 System.out.println("NumberofNegotiator:" + mNegotiationInfo.getNegotiatorNum());
107 }
108 } else if (action instanceof Accept) {
109 if (!mNegotiationInfo.getOpponents().contains(sender)) {
110 mNegotiationInfo.initOpponent(sender);
111 }
112 } else if (action instanceof Offer) {
113 if (!mNegotiationInfo.getOpponents().contains(sender)) {
114 mNegotiationInfo.initOpponent(sender);
115 }
116
117 mOfferedBid = ((Offer) action).getBid();
118
119 try {
120 mNegotiationInfo.updateInfo(sender, this.mOfferedBid);
121 mNegotiationInfo.updateOfferedValueNum(sender, this.mOfferedBid, getTimeLine().getTime());
122 } catch (Exception e) {
123 System.out.println("交渉情報の更新に失敗しました");
124 e.printStackTrace();
125 }
126 } else {
127 boolean var10000 = action instanceof EndNegotiation;
128 }
129 }
130 }
131
132 @Override
133 public String getDescription() {
134 return "ANAC2017 accept Nth offer";
135 }
136
137}
Note: See TracBrowser for help on using the repository browser.