source: src/main/java/parties/in4010/q12015/group17/OpMod.java@ 1

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

Initial import : Genius 9.0.0

File size: 5.7 KB
Line 
1package parties.in4010.q12015.group17;
2
3import java.util.HashMap;
4import java.util.Iterator;
5import java.util.Map;
6import java.util.Set;
7
8import genius.core.Bid;
9import genius.core.BidHistory;
10import genius.core.Domain;
11import genius.core.bidding.BidDetails;
12import genius.core.boaframework.NegotiationSession;
13import genius.core.boaframework.OpponentModel;
14import genius.core.issue.Value;
15import genius.core.issue.ValueDiscrete;
16import genius.core.utility.AdditiveUtilitySpace;
17import genius.core.utility.Evaluator;
18import genius.core.utility.EvaluatorDiscrete;
19
20public class OpMod extends OpponentModel {
21
22 /**
23 * This is the opponent model class. Class variables.
24 */
25 HashMap<String, AdditiveUtilitySpace> opponentSpace;
26 HashMap<String, BidHistory> bidHistories;
27 AdditiveUtilitySpace startSpace;
28 double growRate;
29 Domain domain;
30
31 /**
32 * Initialisation method for {@link NegotiationSession}, opponentSpace,
33 * {@link BidHistory} and {@link Domain}.
34 *
35 * @param negotiationSession
36 */
37 @Override
38 public void init(NegotiationSession negotiationSession, Map<String, Double> params) {
39 super.init(negotiationSession, params);
40 opponentSpace = new HashMap<String, AdditiveUtilitySpace>();
41 bidHistories = new HashMap<String, BidHistory>();
42 domain = negotiationSession.getUtilitySpace().getDomain();
43 }
44
45 /**
46 * Creates a new {@link AdditiveUtilitySpace} for the current domain, with
47 * equal weights and evaluation values. Does this by cloning a "clean"
48 * {@link AdditiveUtilitySpace} of the domain. This clean
49 * {@link AdditiveUtilitySpace} is created the first time this method is
50 * called.
51 *
52 * @returns The new {@link AdditiveUtilitySpace}
53 * @throws Exception
54 */
55 private AdditiveUtilitySpace cloneUtil() throws Exception {
56 if (startSpace == null) {
57 startSpace = new AdditiveUtilitySpace(domain);
58
59 int issueAmount = domain.getObjectives().size();
60 for (int i = 1; i < issueAmount; i++) {
61
62 EvaluatorDiscrete eval = ((EvaluatorDiscrete) ((AdditiveUtilitySpace) negotiationSession
63 .getUtilitySpace()).getEvaluator(i)).clone();
64 Set<ValueDiscrete> valset = eval.getValues();
65 Iterator<ValueDiscrete> vals = valset.iterator();
66
67 while (vals.hasNext()) {
68 ValueDiscrete v = vals.next();
69 eval.setEvaluationDouble(v, (double) 0.0);
70 }
71 eval.setWeight((double) 1 / (issueAmount - 1));
72 startSpace.addEvaluator(domain.getObjectivesRoot().getObjective(i), eval);
73 }
74 }
75 return new AdditiveUtilitySpace(startSpace);
76 }
77
78 /**
79 * Updates the opponent {@link AdditiveUtilitySpace} (predicted utility
80 * space).
81 *
82 * @param bid
83 * The {@link Bid} offered by the opponent
84 * @param sender
85 * The name of the opponent
86 * @throws Exception
87 */
88 public void updateModel(Bid bid, String sender) throws Exception {
89 // If this is the first time we hear of the opponent, we create a new
90 // opponentSpace and bidHistory.
91 if (!opponentSpace.containsKey(sender)) {
92 AdditiveUtilitySpace us = cloneUtil();
93 opponentSpace.put(sender, us);
94 bidHistories.put(sender, new BidHistory());
95 }
96
97 // Retrieves the opponents UtilitySpace for updating
98 AdditiveUtilitySpace opUtil = opponentSpace.get(sender);
99 BidHistory opHist = bidHistories.get(sender);
100
101 for (int i = 0; i < bid.getIssues().size(); i++) {
102
103 Evaluator eval = opUtil.getEvaluator(i + 1);
104 Value c = bid.getValue(i + 1);
105
106 if (opHist.size() > 0) {
107 Bid prevbid = opHist.getLastBid();
108 Value p = prevbid.getValue(i + 1);
109
110 if (p.equals(c)) {
111 eval.setWeight(opUtil.getEvaluator(i + 1).getWeight() + 0.1);
112 }
113 }
114
115 if (eval instanceof EvaluatorDiscrete) {
116 ValueDiscrete dVal = (ValueDiscrete) c;
117 EvaluatorDiscrete dEval = (EvaluatorDiscrete) eval;
118 dEval.setEvaluation(dVal, dEval.getValue(dVal) + 1);
119 }
120 }
121 // Normalise values after updating weights
122 normalize(opUtil);
123
124 // Adds bid to opponent history
125 opHist.add(new BidDetails(bid, opUtil.getUtility(bid)));
126 }
127
128 /**
129 * Normalises the weights of every evaluator within the given
130 * {@link AdditiveUtilitySpace}.
131 *
132 * @param util
133 * The provided {@link AdditiveUtilitySpace}
134 */
135 private void normalize(AdditiveUtilitySpace util) {
136 int size = util.getNrOfEvaluators();
137 double sum = 0;
138
139 // Get the sum of all weights
140 for (int i = 1; i < size + 1; i++) {
141 sum += util.getEvaluator(i).getWeight();
142 }
143 // Normalise the weight relative to the sum of the weights
144 for (int i = 1; i < size + 1; i++) {
145 util.getEvaluator(i).setWeight(util.getEvaluator(i).getWeight() / sum);
146 }
147 }
148
149 /**
150 * Computes the product of all utilities for the input {@link Bid}.
151 *
152 * @param bid
153 * which is the bid to compute the groupUtility for
154 * @return The groupUtility
155 * @throws Exception
156 */
157 public double getGroupUtility(Bid bid, double selfWeight) throws Exception {
158 double util = Math.pow(negotiationSession.getUtilitySpace().getUtility(bid), selfWeight);
159 Iterator<AdditiveUtilitySpace> opSpaces = opponentSpace.values().iterator();
160
161 while (opSpaces.hasNext()) {
162 util = util * opSpaces.next().getUtility(bid);
163 }
164 return util;
165 }
166
167 /**
168 * Returns the {@link AdditiveUtilitySpace} belonging to the provided party.
169 *
170 * @param name
171 * which is the name of the provided party
172 * @return Opponent {@link AdditiveUtilitySpace}
173 */
174 public AdditiveUtilitySpace getOpSpace(String name) {
175 return opponentSpace.get(name);
176 }
177
178 public String toString() {
179 return "OpModel tracking " + opponentSpace.size() + "opponents.";
180 }
181
182 /**
183 * Unused silly method which is not needed.
184 */
185 @Override
186 public void updateModel(Bid arg0, double arg1) {
187
188 }
189}
Note: See TracBrowser for help on using the repository browser.