source: src/main/java/agents/anac/y2015/group2/G2UtilitySpace.java

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

Initial import : Genius 9.0.0

File size: 3.5 KB
Line 
1package agents.anac.y2015.group2;
2
3import java.util.HashMap;
4import java.util.Map;
5import java.util.Map.Entry;
6import java.util.Set;
7
8import agents.anac.y2015.group2.G2Issue;
9import genius.core.issue.Objective;
10import genius.core.issue.ValueDiscrete;
11import genius.core.utility.AdditiveUtilitySpace;
12import genius.core.utility.Evaluator;
13import genius.core.utility.EvaluatorDiscrete;
14
15class G2UtilitySpace {
16 Map<String, G2Issue> utilities;
17 G2Bid _lastBid;
18
19 Map<String, G2Issue> extractUtilitiesFromUtilitySpace(AdditiveUtilitySpace utilitySpace) {
20 HashMap<String, G2Issue> util = new HashMap<String, G2Issue>();
21
22 Set<Entry<Objective, Evaluator>> evaluatorSet = utilitySpace.getEvaluators();
23 for(Entry<Objective, Evaluator> entry : evaluatorSet) {
24 String name = ((Objective) entry.getKey()).getName();
25 G2Issue newIssue = new G2Issue();
26 newIssue.setWeight(utilitySpace.getWeight(entry.getKey().getNumber()));
27
28 EvaluatorDiscrete evaluator = ((EvaluatorDiscrete)entry.getValue());
29 Set<ValueDiscrete> valueSet = evaluator.getValues();
30 for(ValueDiscrete value : valueSet) {
31 try {
32 newIssue.putValue(value.getValue(), evaluator.getEvaluation(value));
33 } catch (Exception e) {
34 // TODO Auto-generated catch block
35 e.printStackTrace();
36 }
37 }
38
39 util.put(name, newIssue);
40 }
41 return util;
42 }
43
44 public double calculateUtility(G2Bid bid) {
45 double score = 0.0;
46
47 for(Entry<String, String> entry : bid.getEntrySet()) {
48 score += utilities.get(entry.getKey()).getScoreForOption(entry.getValue());
49 }
50
51 return score;
52 }
53 public double calculateOptionUtility(String issue, String option) {
54 return utilities.get(issue).getScoreForOption(option);
55 }
56
57 public void ResetWeights() {
58 int size = utilities.size();
59 for(G2Issue i: utilities.values()) {
60 i.setWeight(1.0/size);
61 }
62 }
63 public void SetZeroPreferences() {
64 for(G2Issue i: utilities.values()) {
65 i.SetZeroPreference();
66 }
67 }
68 public void SetNeutralPreferences() {
69 for(G2Issue i: utilities.values()) {
70 i.SetNeutralPreference();
71 }
72 }
73
74 G2UtilitySpace(AdditiveUtilitySpace utilitySpace)
75 {
76 utilities = extractUtilitiesFromUtilitySpace(utilitySpace);
77 }
78
79 public void resetAll(){
80 ResetWeights();
81 SetNeutralPreferences();
82 }
83
84 public String allDataString() {
85 Set<Entry<String, G2Issue>> entrySet = utilities.entrySet();
86 String dataString = "";
87 for(Entry<String, G2Issue> entry : entrySet) {
88 dataString += entry.getKey() + " weight:" + entry.getValue().weightString() + "\r\n";
89 }
90 return dataString;
91 }
92
93 public void updateIssues(G2Bid bid, int nBids){
94
95 int hasChanged = 0;
96
97 //@todo: update for initial bid
98 if(_lastBid != null){
99
100 //update the value of the options based on the bid
101 for(Entry<String, String> entry:bid.getEntrySet() ){
102 utilities.get(entry.getKey()).increaseOption(entry.getValue(), nBids);
103 //keep track on how many issues the bid has changed since the last time
104 if(entry.getValue() != _lastBid.getChoice(entry.getKey())){
105 hasChanged++;
106 }
107 }
108
109 if(hasChanged > 0){
110 //update the weight of the issue
111 for(Entry<String, String> entry:bid.getEntrySet() ){
112 if(entry.getValue() != _lastBid.getChoice(entry.getKey())){
113 utilities.get(entry.getKey()).updateWeight(nBids, 1.0/hasChanged);
114 }else{
115 utilities.get(entry.getKey()).updateWeight(nBids, 0);
116 }
117 }
118 }
119 }
120 _lastBid = bid;
121
122 }
123
124 public G2Issue getIssue(String issue){
125 return utilities.get(issue);
126 }
127
128 public Set<Entry<String, G2Issue>> getIssues(){
129 return utilities.entrySet();
130 }
131}
Note: See TracBrowser for help on using the repository browser.