source: src/main/java/parties/in4010/q12015/group16/Opponent.java@ 84

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

Initial import : Genius 9.0.0

File size: 4.3 KB
Line 
1package parties.in4010.q12015.group16;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5
6import genius.core.Bid;
7import genius.core.issue.Issue;
8import genius.core.issue.Value;
9import genius.core.utility.AdditiveUtilitySpace;
10import genius.core.utility.EvaluatorDiscrete;
11
12public class Opponent {
13 private AdditiveUtilitySpace utilSpace, U16;
14
15 // The learning rate of the weights
16 private double n = 0.05;
17
18 private ArrayList<Bid> bidList;
19
20 public Opponent(AdditiveUtilitySpace utilSpaceGeneral,
21 AdditiveUtilitySpace utilSpaceAgent16) {
22 U16 = new AdditiveUtilitySpace(utilSpaceAgent16);
23 utilSpace = new AdditiveUtilitySpace(utilSpaceGeneral.getDomain());
24 // Initializing all weights as 1/NrOfIssues:
25 double initWeight = (double) 1
26 / (utilSpace.getDomain().getObjectives().size() - 1);
27
28 for (int i = 1; i <= utilSpace.getDomain().getIssues().size(); i++) {
29 utilSpace.addEvaluator(utilSpace.getDomain().getObjectivesRoot()
30 .getObjective(i), U16.getEvaluator(i));
31 utilSpace.getEvaluator(i).setWeight(initWeight);
32 }
33
34 bidList = new ArrayList<Bid>();
35 }
36
37 public void update(Bid bid) {
38 bidList.add(bid);
39 double error = updateIssueWeights();
40 validateIssueWeights(error);
41 }
42
43 /**
44 * Applies frequency analysis to update issue weights.
45 *
46 * @return average deviation of the sum of the new issue weights from
47 * (1/#issues)
48 */
49 private double updateIssueWeights() {
50 Value val = null;
51 double sumOfWeights = 0;
52
53 // Adding 'n' to the weights of the issues that have changed
54 for (int i = 1; i <= utilSpace.getDomain().getIssues().size(); i++) {
55 try {
56 val = bidList.get(bidList.size() - 2).getValue(i);
57 } catch (Exception e) {
58 e.printStackTrace();
59 }
60 if (bidList.get(bidList.size() - 1).getValues().containsValue(val)) {
61 utilSpace.getEvaluator(i).setWeight(
62 utilSpace.getEvaluator(i).getWeight() + n);
63 }
64 sumOfWeights += utilSpace.getWeight(i);
65 }
66 double error = (sumOfWeights - 1)
67 / utilSpace.getDomain().getIssues().size();
68 return error;
69 }
70
71 /**
72 * Validates the issue weights acquired from frequency analysis and keeps
73 * them inside the [0, 1] range.
74 *
75 * @param error
76 * average deviation of the sum of the new issue weights from
77 * (1/#issues)
78 */
79 private void validateIssueWeights(double error) {
80 double checkSum;
81 do {
82 checkSum = 0;
83 // Adjusting all weights (and checking impossible values <0 or >1)
84 for (int i = 1; i <= utilSpace.getDomain().getIssues().size(); i++) {
85 utilSpace.getEvaluator(i).setWeight(
86 utilSpace.getEvaluator(i).getWeight() - error);
87 // To prevent weights from getting below 0 or above 1:
88 if (utilSpace.getEvaluator(i).getWeight() <= 0) {
89 utilSpace.getEvaluator(i).setWeight(0);
90 }
91 if (utilSpace.getEvaluator(i).getWeight() > 1) {
92 utilSpace.getEvaluator(i).setWeight(1);
93 }
94 checkSum += utilSpace.getWeight(i);
95 }
96 error = (checkSum - 1) / utilSpace.getDomain().getIssues().size();
97 } while (0.9999 > checkSum || checkSum > 1.0001);
98 }
99
100 /**
101 * Calculates the values for all issues at the halfway time of the
102 * negotiation.
103 *
104 * @throws Exception
105 */
106 public void calculateValues() throws Exception {
107 for (Issue issue : utilSpace.getDomain().getIssues()) {
108 HashMap<Value, Integer> counter = new HashMap<Value, Integer>();
109 ArrayList<Value> values = new ArrayList<Value>();
110 Value val = null;
111 // Count how many times each value occurs per issue
112 for (int i = 0; i < bidList.size(); i++) {
113 val = bidList.get(i).getValue(issue.getNumber());
114 if (!counter.containsKey(val)) {
115 counter.put(val, 1);
116 values.add(val);
117 } else if (counter.containsKey(val)) {
118 counter.put(val, counter.get(val) + 1);
119 }
120 }
121
122 // Add the evaluators with their values to the utility space
123 EvaluatorDiscrete ev = (EvaluatorDiscrete) U16.getEvaluator(issue
124 .getNumber());
125 for (Value value : values) {
126 ev.setEvaluation(value, counter.get(value));
127 }
128 // Add the updated evaluator to the utility space
129 utilSpace.addEvaluator(issue, ev);
130 }
131 }
132
133 public AdditiveUtilitySpace getUtilSpace() {
134 return utilSpace;
135 }
136
137 public ArrayList<Bid> getBidHistory() {
138 return bidList;
139 }
140}
Note: See TracBrowser for help on using the repository browser.