1 | package parties.in4010.q12015.group12;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | import genius.core.AgentID;
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.BidHistory;
|
---|
10 | import genius.core.actions.Accept;
|
---|
11 | import genius.core.actions.Action;
|
---|
12 | import genius.core.actions.DefaultAction;
|
---|
13 | import genius.core.actions.Offer;
|
---|
14 | import genius.core.bidding.BidDetails;
|
---|
15 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
16 | import genius.core.issue.Issue;
|
---|
17 | import genius.core.issue.Value;
|
---|
18 | import genius.core.parties.AbstractNegotiationParty;
|
---|
19 | import genius.core.parties.NegotiationInfo;
|
---|
20 |
|
---|
21 | public class Group12 extends AbstractNegotiationParty {
|
---|
22 |
|
---|
23 | boolean reset = false;
|
---|
24 | double totalTime;
|
---|
25 | double threshold;
|
---|
26 | int amountIssues;
|
---|
27 | BidHistory history;
|
---|
28 | List<BidDetails> listBids;
|
---|
29 | HashMap<Object, OtherAgent> otherAgents;
|
---|
30 | SortedOutcomeSpace possibleBids;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * init is called when a next session starts with the same opponent.
|
---|
34 | */
|
---|
35 | @Override
|
---|
36 | public void init(NegotiationInfo info) {
|
---|
37 | super.init(info);
|
---|
38 | resetValues();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Reset all values the agent used
|
---|
43 | */
|
---|
44 | private void resetValues() {
|
---|
45 | reset = true;
|
---|
46 | totalTime = this.getTimeLine().getTotalTime();
|
---|
47 | threshold = 1;
|
---|
48 | history = new BidHistory();
|
---|
49 | otherAgents = new HashMap<Object, OtherAgent>();
|
---|
50 | possibleBids = new SortedOutcomeSpace(utilitySpace);
|
---|
51 | makeList();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * All offers proposed by the other parties will be received as a message.
|
---|
56 | * You can use this information to your advantage, for example to predict
|
---|
57 | * their utility.
|
---|
58 | *
|
---|
59 | * @param sender
|
---|
60 | * The party that did the action.
|
---|
61 | * @param action
|
---|
62 | * The action that party did.
|
---|
63 | */
|
---|
64 | @Override
|
---|
65 | public void receiveMessage(AgentID sender, Action action) {
|
---|
66 | super.receiveMessage(sender, action);
|
---|
67 |
|
---|
68 | if (!reset) {
|
---|
69 | resetValues();
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (!otherAgents.containsKey(sender)) {
|
---|
73 | OtherAgent newAgent = new OtherAgent(String.valueOf(sender), amountIssues);
|
---|
74 | otherAgents.put(sender, newAgent);
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (DefaultAction.getBidFromAction(action) != null) {
|
---|
78 | BidDetails bidDetail = new BidDetails(DefaultAction.getBidFromAction(action),
|
---|
79 | getUtility(DefaultAction.getBidFromAction(action)));
|
---|
80 | freqAnalysis(DefaultAction.getBidFromAction(action), sender);
|
---|
81 | otherAgents.get(sender).addBidDetails(bidDetail);
|
---|
82 | history.add(bidDetail);
|
---|
83 | } else if (action.getClass().getName() == "negotiator.actions.Accept") { // Hier
|
---|
84 | // dingen
|
---|
85 | // aangepast
|
---|
86 | freqAnalysis(history.getLastBidDetails().getBid(), sender);
|
---|
87 | otherAgents.get(sender).addBidDetails(history.getLastBidDetails());
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Each round this method gets called and ask you to accept or offer. The
|
---|
93 | * first party in the first round is a bit different, it can only propose an
|
---|
94 | * offer.
|
---|
95 | *
|
---|
96 | * @param validActions
|
---|
97 | * Either a list containing both accept and offer or only offer.
|
---|
98 | * @return The chosen action.
|
---|
99 | */
|
---|
100 | @Override
|
---|
101 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
102 |
|
---|
103 | calculateThreshold();
|
---|
104 | if (history.size() != 0 && history.getLastBidDetails().getMyUndiscountedUtil() > threshold) {
|
---|
105 | return new Accept(getPartyId(), history.getLastBid());
|
---|
106 | }
|
---|
107 |
|
---|
108 | return new Offer(getPartyId(), generateBid(threshold));
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Generates bid
|
---|
113 | *
|
---|
114 | * @param utility
|
---|
115 | * @return
|
---|
116 | */
|
---|
117 | private Bid generateBid(double utility) {
|
---|
118 |
|
---|
119 | double currentTime = this.getTimeLine().getCurrentTime();
|
---|
120 | Bid newBid = generateRandomBid();
|
---|
121 |
|
---|
122 | if (!listBids.isEmpty() && (currentTime / totalTime) < 0.5)
|
---|
123 | newBid = monotonicStrategy();
|
---|
124 | if (getUtility(newBid) > 0.7) {
|
---|
125 | return newBid;
|
---|
126 | }
|
---|
127 |
|
---|
128 | else if (!history.isEmpty() && (currentTime / totalTime) < 0.10) {
|
---|
129 | newBid = useFreqAnalysis(utility);
|
---|
130 | } else if (!history.isEmpty()) {
|
---|
131 | newBid = history.getBestBidDetails().getBid();
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (getUtility(newBid) < threshold)
|
---|
135 | try {
|
---|
136 | newBid = this.utilitySpace.getMaxUtilityBid();
|
---|
137 | } catch (Exception e) {
|
---|
138 | e.printStackTrace();
|
---|
139 | }
|
---|
140 |
|
---|
141 | return newBid;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // monotonic strategy
|
---|
145 | // listBids contains bids that have not been done so far by this agent
|
---|
146 | private Bid monotonicStrategy() {
|
---|
147 | Bid bid = listBids.get(0).getBid();
|
---|
148 | listBids.remove(0);
|
---|
149 | return bid;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // apply believes of other agents
|
---|
153 | // get their most important issue
|
---|
154 | // and get from that issue their most import value
|
---|
155 | // update bid by using this knowledge
|
---|
156 | private Bid useFreqAnalysis(double utility) {
|
---|
157 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
158 | Bid returnBid = possibleBids.getBidNearUtility(utility).getBid();
|
---|
159 | HashMap<Integer, Value> changes = new HashMap<Integer, Value>();
|
---|
160 | Object[] keys = otherAgents.keySet().toArray();
|
---|
161 |
|
---|
162 | for (int i = 0; i < keys.length - 1; i++) {
|
---|
163 | OtherAgent agent = otherAgents.get(keys[i]);
|
---|
164 | if (agent.isAgent()) {
|
---|
165 | int issueNmr = agent.heighWeight();
|
---|
166 | Value value = agent.getBestValue(issueNmr, issues);
|
---|
167 |
|
---|
168 | if (changes.containsKey(issueNmr)) {
|
---|
169 | Value valueOld = changes.get(issueNmr);
|
---|
170 |
|
---|
171 | Bid possibleBid = returnBid;
|
---|
172 | possibleBid = possibleBid.putValue(issueNmr, value);
|
---|
173 |
|
---|
174 | Bid possibleBid2 = returnBid;
|
---|
175 | possibleBid2 = possibleBid2.putValue(issueNmr, valueOld);
|
---|
176 |
|
---|
177 | if (getUtility(possibleBid) > getUtility(possibleBid2)) {
|
---|
178 | changes.put(issueNmr, value);
|
---|
179 | }
|
---|
180 |
|
---|
181 | } else {
|
---|
182 | changes.put(issueNmr, value);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | Set<Integer> allChanges = changes.keySet();
|
---|
188 | for (int j : allChanges) {
|
---|
189 | returnBid = returnBid.putValue(j + 1, changes.get(j));
|
---|
190 | }
|
---|
191 |
|
---|
192 | return returnBid;
|
---|
193 | }
|
---|
194 |
|
---|
195 | // used for making a list concerning all bits
|
---|
196 | private void makeList() {
|
---|
197 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
198 | amountIssues = issues.size();
|
---|
199 | listBids = possibleBids.getAllOutcomes();
|
---|
200 | }
|
---|
201 |
|
---|
202 | // calculate time-dependent threshold
|
---|
203 | private void calculateThreshold() {
|
---|
204 | double currentTime = this.getTimeLine().getCurrentTime();
|
---|
205 | threshold = 1 - Math.pow((currentTime / totalTime), 2);
|
---|
206 |
|
---|
207 | // we do not want to get a utility lower then 0.65
|
---|
208 | if (threshold < 0.7)
|
---|
209 | threshold = 0.7;
|
---|
210 | }
|
---|
211 |
|
---|
212 | // apply frequency analysis
|
---|
213 | private void freqAnalysis(Bid newBid, Object sender) {
|
---|
214 | OtherAgent senderAgent = otherAgents.get(sender);
|
---|
215 | if (!senderAgent.getHistory().isEmpty()) {
|
---|
216 | double[] newWeights = determineWeights(newBid, senderAgent.getHistory().getLastBid(), 0.1,
|
---|
217 | senderAgent.getWeights());
|
---|
218 | senderAgent.setWeights(newWeights, true);
|
---|
219 | senderAgent.setValues(newBid);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | // update weights by using previous and new bid
|
---|
224 | private double[] determineWeights(Bid newBid, Bid prevBid, double n, double[] weight) {
|
---|
225 | double totalWeight = 0;
|
---|
226 |
|
---|
227 | for (int i = 1; i < amountIssues; i++) {
|
---|
228 | try {
|
---|
229 | if (newBid.getValue(i) == prevBid.getValue(i))
|
---|
230 | weight[i] = weight[i] + n;
|
---|
231 | } catch (Exception e) {
|
---|
232 | e.printStackTrace();
|
---|
233 | }
|
---|
234 |
|
---|
235 | totalWeight = totalWeight + weight[i];
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (totalWeight > 0) {
|
---|
239 | weight = normalizeWeight(totalWeight, weight);
|
---|
240 | }
|
---|
241 |
|
---|
242 | return weight;
|
---|
243 | }
|
---|
244 |
|
---|
245 | // normalize the given weights
|
---|
246 | private double[] normalizeWeight(double total, double[] weights) {
|
---|
247 | for (int i = 0; i < amountIssues - 1; i++) {
|
---|
248 | weights[i] = weights[i] / total;
|
---|
249 | }
|
---|
250 | return weights;
|
---|
251 | }
|
---|
252 |
|
---|
253 | @Override
|
---|
254 | public String getDescription() {
|
---|
255 | return "example party group 12b";
|
---|
256 | }
|
---|
257 | }
|
---|