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