1 | package parties.in4010.q12015.group14;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import genius.core.AgentID;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.actions.Offer;
|
---|
11 | import genius.core.issue.Issue;
|
---|
12 | import genius.core.issue.IssueDiscrete;
|
---|
13 |
|
---|
14 | public class OpponentModelling {
|
---|
15 |
|
---|
16 | // private String agent; //the agent this opponent modelling will be applied
|
---|
17 | // to
|
---|
18 | private Bid lastBid = null; // save the last Bid made by the agent
|
---|
19 | private List<Issue> domainIssues = new ArrayList<Issue>();
|
---|
20 | private ArrayList<IssueDiscrete> domainIssuesDiscrete = new ArrayList<IssueDiscrete>();
|
---|
21 | // private ArrayList<Integer> noOfvaluesPerIssue=new ArrayList<Integer>();
|
---|
22 | private ArrayList<Double> estWeights = new ArrayList<Double>(); // the
|
---|
23 | // weights
|
---|
24 | // estimation
|
---|
25 | // for the
|
---|
26 | // preference
|
---|
27 | // profile
|
---|
28 | // of this
|
---|
29 | // agent
|
---|
30 | private double n = 0.1; // n double to update the weight values at each
|
---|
31 | // offer
|
---|
32 | private double p = 5;
|
---|
33 | private ArrayList<HashMap<String, ArrayList<Double>>> estValues = new ArrayList<HashMap<String, ArrayList<Double>>>();
|
---|
34 | private AgentID opponentID;
|
---|
35 |
|
---|
36 | // arraylist of issues each of one having a hashmap to contain all possible
|
---|
37 | // values and an arraylist to contain the estimated value
|
---|
38 | // and the frequency this value appeared so far
|
---|
39 |
|
---|
40 | // initialization
|
---|
41 | public OpponentModelling(String senderAgent, List<Issue> domIss, Bid b,
|
---|
42 | AgentID opponentID) {
|
---|
43 | this.opponentID = opponentID;
|
---|
44 | domainIssues = domIss;
|
---|
45 | for (Issue iss : domainIssues) {
|
---|
46 | IssueDiscrete temp = (IssueDiscrete) iss;
|
---|
47 | domainIssuesDiscrete.add(temp);
|
---|
48 | // noOfvaluesPerIssue.add(temp.getNumberOfValues()); //the number of
|
---|
49 | // values per issue stored in this arraylist
|
---|
50 | }
|
---|
51 | // lastBid=b; //save the first ever bid of the opponent agent
|
---|
52 | initWeights();
|
---|
53 | // create domainIssueDiscrete arraylist
|
---|
54 | initValues();
|
---|
55 | updateModel(b);
|
---|
56 | // agent=senderAgent;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // you get the value of every issue given the bid of the opponent and update
|
---|
60 | // its model
|
---|
61 | public void updateModel(Bid oppBid) {
|
---|
62 | double sum = 0; // sum of the weights
|
---|
63 | for (int j = 0; j < domainIssuesDiscrete.size(); j++) {
|
---|
64 | try {
|
---|
65 | String val = oppBid.getValue(j + 1).toString(); // YOU NEED J+1
|
---|
66 | // ISSUE INDECES
|
---|
67 | // START FROM 1
|
---|
68 | if (lastBid == null) { // we must update the model with the
|
---|
69 | // first bid given
|
---|
70 | estValues.get(j).get(val).set(0, 1.0);
|
---|
71 | estValues.get(j).get(val).set(1, 1.0);
|
---|
72 | } else { // if this is not the first bid
|
---|
73 | double prevFreq = estValues.get(j).get(val).get(1);
|
---|
74 | estValues.get(j).get(val).set(1, prevFreq + 1); // update
|
---|
75 | // the total
|
---|
76 | // count/appearance
|
---|
77 | // of this
|
---|
78 | // specific
|
---|
79 | // value
|
---|
80 | updateValuesInIssue(j);
|
---|
81 | // update weights here
|
---|
82 | if (oppBid.getValue(j + 1).equals(lastBid.getValue(j + 1))) {
|
---|
83 | estWeights.set(j, estWeights.get(j) + n);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | } catch (Exception e) {
|
---|
87 | System.out
|
---|
88 | .println("Exception thrown in updateModel, while trying to get the value of an issue from a Bid");
|
---|
89 | }
|
---|
90 | sum = sum + estWeights.get(j);
|
---|
91 | }
|
---|
92 | for (int index = 0; index < estWeights.size(); index++) {
|
---|
93 | double temp = estWeights.get(index);
|
---|
94 | estWeights.set(index, temp / sum);
|
---|
95 | }
|
---|
96 | lastBid = oppBid;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // for a mentioned issue, update all the values ( =freq/maxFreq )
|
---|
100 | private void updateValuesInIssue(int issueIndex) {
|
---|
101 | // first get the max
|
---|
102 | double max = 0;
|
---|
103 | for (Map.Entry<String, ArrayList<Double>> entry : estValues.get(
|
---|
104 | issueIndex).entrySet()) {
|
---|
105 | if (entry.getValue().get(1) > max) {
|
---|
106 | max = entry.getValue().get(1);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | // now update values
|
---|
111 | for (Map.Entry<String, ArrayList<Double>> entry : estValues.get(
|
---|
112 | issueIndex).entrySet()) {
|
---|
113 | double temp = Math.pow((entry.getValue().get(1) / max), (1 / p));
|
---|
114 | entry.getValue().set(0, temp);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // initialize the weights of the opponent modeling ( =1/nummberOfIssues )
|
---|
119 | private void initWeights() {
|
---|
120 | double noIssues = domainIssues.size();
|
---|
121 | double initWeightValues = 1.0 / (double) noIssues;
|
---|
122 | for (int i = 0; i < noIssues; i++) {
|
---|
123 | estWeights.add(initWeightValues); // the initial weights are added
|
---|
124 | // by the order of the issues
|
---|
125 | estValues.add(new HashMap<String, ArrayList<Double>>()); // initialize
|
---|
126 | // the
|
---|
127 | // arraylist
|
---|
128 | // of
|
---|
129 | // hashmaps
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | // initialize the values of the opponent modeling
|
---|
134 | private void initValues() {
|
---|
135 | int index = 0;
|
---|
136 | for (IssueDiscrete isDis : domainIssuesDiscrete) {
|
---|
137 | for (int i = 0; i < isDis.getNumberOfValues(); i++) {
|
---|
138 | String valueName = isDis.getStringValue(i);
|
---|
139 | ArrayList<Double> temp1 = new ArrayList<Double>();
|
---|
140 | temp1.add(0.0); // estimation of value
|
---|
141 | temp1.add(0.0); // frequency of the value
|
---|
142 | estValues.get(index).put(valueName, temp1);
|
---|
143 | }
|
---|
144 | index++;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | // get the utility of the bid using this opponent modeling
|
---|
149 | public double getOppUtil(Bid b) {
|
---|
150 | double util = 0;
|
---|
151 | for (int j = 0; j < domainIssuesDiscrete.size(); j++) {
|
---|
152 | try {
|
---|
153 | String val = b.getValue(j + 1).toString();
|
---|
154 | util = util + estWeights.get(j)
|
---|
155 | * estValues.get(j).get(val).get(0);
|
---|
156 | } catch (Exception e) {
|
---|
157 | System.out
|
---|
158 | .println("Exception thrown in getOpputil, while trying to get the value of an issue from a Bid");
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return util;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // get the bid with the highest utility from the ArrayList (method similar
|
---|
165 | // to maxOppUtilIndex)
|
---|
166 | public Offer maxOppUtilBid(ArrayList<Bid> bidList) {
|
---|
167 | try {
|
---|
168 | double maxUtil = getOppUtil(bidList.get(0));
|
---|
169 | int maxIndex = 0;
|
---|
170 | for (int i = 1; i < bidList.size(); i++) {
|
---|
171 | if (getOppUtil(bidList.get(i)) > maxUtil) {
|
---|
172 | maxUtil = getOppUtil(bidList.get(i));
|
---|
173 | maxIndex = i;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | return new Offer(opponentID, bidList.get(maxIndex));
|
---|
177 | } catch (Exception e) {
|
---|
178 | System.err
|
---|
179 | .println("Error generating maximum utility using maxOppUtilBid!");
|
---|
180 | e.printStackTrace();
|
---|
181 | return null;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | // get the index with the highest utility from the ArrayList (method similar
|
---|
186 | // to maxOppUtilBid)
|
---|
187 | public int maxOppUtilIndex(ArrayList<Bid> bidList) {
|
---|
188 | try {
|
---|
189 | double maxUtil = getOppUtil(bidList.get(0));
|
---|
190 | int maxIndex = 0;
|
---|
191 | for (int i = 1; i < bidList.size(); i++) {
|
---|
192 | if (getOppUtil(bidList.get(i)) > maxUtil) {
|
---|
193 | maxUtil = getOppUtil(bidList.get(i));
|
---|
194 | maxIndex = i;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | return maxIndex;
|
---|
198 | } catch (Exception e) {
|
---|
199 | System.err
|
---|
200 | .println("Error generating maximum utility using maxOppUtilBid!");
|
---|
201 | e.printStackTrace();
|
---|
202 | return 0;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | // print the estimated weights and values of this opponent model
|
---|
207 | public void printOppModel() {
|
---|
208 | System.out.println("The number of issues are: "
|
---|
209 | + domainIssuesDiscrete.size());
|
---|
210 | printWeights();
|
---|
211 | for (int i = 0; i < domainIssuesDiscrete.size(); i++) {
|
---|
212 | System.out.println("Values for issue #" + i + ":");
|
---|
213 | printMap(estValues.get(i));
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | // print the values (contents of the hashmap)
|
---|
218 | public void printMap(HashMap<String, ArrayList<Double>> hm) {
|
---|
219 | for (Map.Entry<String, ArrayList<Double>> entry : hm.entrySet()) {
|
---|
220 | System.out.println("Preference: " + entry.getKey());
|
---|
221 | System.out.println("Value: " + entry.getValue().get(0)
|
---|
222 | + " - Frequency: " + entry.getValue().get(1));
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | // print the issue weights
|
---|
227 | public void printWeights() {
|
---|
228 | for (int index = 0; index < estWeights.size(); index++) {
|
---|
229 | System.out.println("The estimated weight of issue #" + index
|
---|
230 | + " is :" + estWeights.get(index));
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|