1 | package agents.ai2014.group12;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import genius.core.AgentID;
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.actions.Accept;
|
---|
10 | import genius.core.actions.Action;
|
---|
11 | import genius.core.actions.ActionWithBid;
|
---|
12 | import genius.core.actions.DefaultAction;
|
---|
13 | import genius.core.actions.Offer;
|
---|
14 | import genius.core.parties.AbstractNegotiationParty;
|
---|
15 | import genius.core.parties.NegotiationInfo;
|
---|
16 | import genius.core.timeline.TimeLineInfo;
|
---|
17 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This is your negotiation party.
|
---|
21 | */
|
---|
22 | public class Group12 extends AbstractNegotiationParty {
|
---|
23 |
|
---|
24 | ArrayList<Bid> previousBids = new ArrayList<Bid>();
|
---|
25 | ArrayList<BidWithSender> previousBidsWithSender = new ArrayList<BidWithSender>();
|
---|
26 | Preference preference;
|
---|
27 | UtilityOracle oracle;
|
---|
28 | int round = 0;
|
---|
29 | HashMap<String, Preference> otherAgentsPreference = new HashMap<String, Preference>();
|
---|
30 | TimeLineInfo timeline;
|
---|
31 | int timeLimit;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Please keep this constructor. This is called by genius.
|
---|
35 | *
|
---|
36 | * @param utilitySpace
|
---|
37 | * Your utility space.
|
---|
38 | * @param deadlines
|
---|
39 | * The deadlines set for this negotiation.
|
---|
40 | * @param timeline
|
---|
41 | * Value counting from 0 (start) to 1 (end).
|
---|
42 | * @param randomSeed
|
---|
43 | * If you use any randomization, use this seed for it.
|
---|
44 | */
|
---|
45 | @Override
|
---|
46 | public void init(NegotiationInfo info) {
|
---|
47 | // Make sure that this constructor calls it's parent.
|
---|
48 | super.init(info);
|
---|
49 | preference = new Preference((AdditiveUtilitySpace) utilitySpace);
|
---|
50 |
|
---|
51 | this.timeline = info.getTimeline();
|
---|
52 | timeLimit = (int) timeline.getTotalTime();
|
---|
53 |
|
---|
54 | double utilityValue = calculateUtilityValue(timeLimit);
|
---|
55 | oracle = new UtilityOracle(utilityValue);
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Each round this method gets called and ask you to accept or offer. The
|
---|
61 | * first party in the first round is a bit different, it can only propose an
|
---|
62 | * offer.
|
---|
63 | *
|
---|
64 | * @param validActions
|
---|
65 | * Either a list containing both accept and offer or only offer.
|
---|
66 | * @return The chosen action.
|
---|
67 | */
|
---|
68 | @Override
|
---|
69 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
70 | double acceptingValue = oracle.getAcceptingValue(round);
|
---|
71 | double bidValue = 0;
|
---|
72 | if (previousBids.size() != 0) {
|
---|
73 | try {
|
---|
74 | bidValue = this.utilitySpace.getUtility(previousBids.get(previousBids.size() - 1));
|
---|
75 | } catch (Exception e) {
|
---|
76 | // TODO Auto-generated catch block
|
---|
77 | e.printStackTrace();
|
---|
78 | }
|
---|
79 | }
|
---|
80 | round++;
|
---|
81 |
|
---|
82 | if ((round == timeLimit - 2 || round == timeLimit - 2) && acceptableLastOffer(bidValue, acceptingValue)) {
|
---|
83 | return new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!validActions.contains(Accept.class) || acceptingValue >= bidValue) {
|
---|
87 | Bid bid = new Bid(utilitySpace.getDomain());
|
---|
88 | try {
|
---|
89 | bid = BidGenerator.generateBid(this.utilitySpace, this.preference, acceptingValue,
|
---|
90 | this.otherAgentsPreference);
|
---|
91 | } catch (Exception e) {
|
---|
92 | // TODO Auto-generated catch block
|
---|
93 | e.printStackTrace();
|
---|
94 | }
|
---|
95 | return new Offer(getPartyId(), bid);
|
---|
96 | } else {
|
---|
97 | System.out.println("Accept");
|
---|
98 | return new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * All offers proposed by the other parties will be received as a message.
|
---|
104 | * You can use this information to your advantage, for example to predict
|
---|
105 | * their utility.
|
---|
106 | *
|
---|
107 | * @param sender
|
---|
108 | * The party that did the action.
|
---|
109 | * @param action
|
---|
110 | * The action that party did.
|
---|
111 | */
|
---|
112 | @Override
|
---|
113 | public void receiveMessage(AgentID sender, Action action) {
|
---|
114 | // Here you can listen to other parties' messages
|
---|
115 | super.receiveMessage(sender, action);
|
---|
116 |
|
---|
117 | System.out.println(sender.toString());
|
---|
118 |
|
---|
119 | if (DefaultAction.getBidFromAction(action) != null) {
|
---|
120 | Bid bid = DefaultAction.getBidFromAction(action);
|
---|
121 | try {
|
---|
122 | if (!otherAgentsPreference.containsKey(sender.toString())) {
|
---|
123 | Preference pref = new Preference((AdditiveUtilitySpace) this.utilitySpace, bid);
|
---|
124 | otherAgentsPreference.put(sender.toString(), pref);
|
---|
125 | } else {
|
---|
126 | ArrayList<Bid> previousbidsOfSender = getBidsOfSender(sender.toString());
|
---|
127 | otherAgentsPreference.get(sender.toString()).updatePreferenceOrder(bid);
|
---|
128 | otherAgentsPreference.get(sender.toString()).updateIssueWeights(previousbidsOfSender, bid);
|
---|
129 | }
|
---|
130 | Double utilityOfBid = this.utilitySpace.getUtility(bid);
|
---|
131 | } catch (Exception e) {
|
---|
132 | // TODO Auto-generated catch block
|
---|
133 | e.printStackTrace();
|
---|
134 | }
|
---|
135 | BidWithSender bidWS = new BidWithSender(bid, sender.toString());
|
---|
136 | previousBidsWithSender.add(bidWS);
|
---|
137 | previousBids.add(bid);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private ArrayList<Bid> getBidsOfSender(String sender) {
|
---|
142 | ArrayList<Bid> previousBids = new ArrayList<Bid>();
|
---|
143 | for (BidWithSender bidWS : previousBidsWithSender) {
|
---|
144 | if (bidWS.sender.equals(sender)) {
|
---|
145 | previousBids.add(bidWS.bid);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | return previousBids;
|
---|
149 | }
|
---|
150 |
|
---|
151 | private boolean acceptableLastOffer(double bidvalue, double acceptingValue) {
|
---|
152 | double bidvalueBottom = bidvalue - 2 * (bidvalue / 10);
|
---|
153 |
|
---|
154 | if (bidvalueBottom > acceptingValue) {
|
---|
155 | return true;
|
---|
156 | } else {
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | private double calculateUtilityValue(int timeline) {
|
---|
162 | double retUtility = 0.5 / timeline;
|
---|
163 | return retUtility;
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected AgentID partyId = new AgentID("Group 12");
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public String getDescription() {
|
---|
170 | return "ai2014 group12";
|
---|
171 | }
|
---|
172 |
|
---|
173 | }
|
---|