source: src/main/java/agents/ai2014/group5/OpponentModel.java@ 126

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

Initial import : Genius 9.0.0

File size: 3.8 KB
Line 
1package agents.ai2014.group5;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6import java.util.Map;
7
8import genius.core.Bid;
9
10/**
11 * An opponent model constructs a model of an opponent's negotiation profile so
12 * that the opponent's utilities of bids can be estimated. These models make use
13 * of a reinforcement learning method to learn the opponents' profiles from the
14 * bids made by the opponents. This method is described in the paper for the
15 * agent HardHeaded from the ANAC 2011 competition.
16 */
17class OpponentModel {
18 // Weight increment during learning
19 private static final double EPSILON = 0.1;
20
21 // The last bid offered by the opponent
22 private Bid lastBid;
23
24 // Issue weights
25 private List<Double> weights;
26
27 // Issue values
28 private List<List<Integer>> values;
29
30 // Map of issue value names to indexes for each issue
31 private List<Map<String, Integer>> valueNames;
32
33 // Issue indexes-1 and their names
34 private List<String> issueNames;
35
36 private Group5 agent;
37
38 @SuppressWarnings("unchecked")
39 public OpponentModel(List<String> issueNames,
40 List<Map<String, Integer>> valueNames, Group5 agent) {
41 lastBid = null;
42 this.valueNames = valueNames;
43 this.issueNames = issueNames;
44 this.agent = agent;
45
46 // Uniformly distribute the weights
47 weights = new ArrayList<Double>(Collections.nCopies(issueNames.size(),
48 1.0 / issueNames.size()));
49
50 // Initialize issue values
51 values = new ArrayList<List<Integer>>();
52 for (int i = 0; i < issueNames.size(); i++) {
53 ArrayList<Integer> tmp = new ArrayList<Integer>(
54 Collections.nCopies(valueNames.get(i).size(), 1));
55 values.add((List<Integer>) tmp.clone());
56 }
57 }
58
59 /**
60 * Updates the model given the new bid received from the opponent. If an
61 * issue value has changed since the last bid then the weight for the
62 * corresponding issue and the issue value will be changed.
63 */
64 public void updateModel(Bid bid) {
65 if (bid == null) {
66 // The bid does not exists, the action was therefore not an offer
67 return;
68 }
69
70 if (lastBid != null) {
71 // This is not the first bid, so update the model
72 for (int i = 0; i < issueNames.size(); i++) {
73 String prevV = null, newV = null;
74 try {
75 prevV = lastBid.getValue(i + 1).toString();
76 newV = bid.getValue(i + 1).toString();
77 } catch (Exception e) {
78 agent.println("Error in \"updateModel\": getValue(" + i + 1
79 + ") fails for bid " + bid + " or bid " + lastBid);
80 }
81
82 if (prevV != null && newV != null && prevV.equals(newV)) {
83 // Update weight and issue value for this issue
84 int vi = valueNames.get(i).get(newV);
85 weights.set(i, weights.get(i) + EPSILON);
86 values.get(i).set(vi, values.get(i).get(vi) + 1);
87 }
88 }
89
90 // Normalize weights
91 double norm = 0.0;
92 for (double w : weights) {
93 norm += w;
94 }
95 for (int i = 0; i < weights.size(); i++) {
96 weights.set(i, weights.get(i) / norm);
97 }
98 }
99
100 lastBid = bid;
101 }
102
103 /**
104 * Calculates the expected utility of a bid.
105 *
106 * @param Bid
107 * to calculate utility of
108 * @return Utility of bid for the opponent of this model
109 */
110 public double expectedUtilityOf(Bid bid) {
111 double u = 0.0;
112 for (int i = 0; i < issueNames.size(); i++) {
113 String tmp = null;
114 try {
115 // Get the name of the issue value used in the bid
116 tmp = bid.getValue(i + 1).toString();
117 } catch (Exception e) {
118 agent.println("Error in \"expectedUtiliyOf\": getValue(" + i
119 + 1 + ") fails for bid " + bid);
120 }
121 if (tmp != null) {
122 // Calculate and normalize estimated chosen issue value
123 int eIndex = valueNames.get(i).get(tmp);
124 int eNorm = 0;
125 for (int v = 0; v < values.get(i).size(); v++) {
126 eNorm += values.get(i).get(v);
127 }
128 double e = ((double) values.get(i).get(eIndex)) / eNorm;
129
130 // Increment utility for this issue
131 u += weights.get(i) * e;
132 }
133 }
134 return u;
135 }
136}
Note: See TracBrowser for help on using the repository browser.