source: src/main/java/genius/core/utility/UtilityFunction.java

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 3.6 KB
Line 
1package genius.core.utility;
2
3import java.util.ArrayList;
4
5import genius.core.Bid;
6
7/**
8 * Implements part of {@link NonlinearUtilitySpace}. Needs more documentation.
9 *
10 */
11public class UtilityFunction {
12
13 private AGGREGATIONTYPE aggregationType;
14 private ArrayList<UtilityFunction> utilityFunctions;
15 private ArrayList<Constraint> constraints;
16 private double weight;
17
18 public UtilityFunction() {
19 this.utilityFunctions = new ArrayList<UtilityFunction>();
20 this.constraints = new ArrayList<Constraint>();
21 this.weight = 1;
22 this.aggregationType = aggregationType.SUM;
23 }
24
25 public UtilityFunction(AGGREGATIONTYPE aggregationtype) {
26 this.utilityFunctions = new ArrayList<UtilityFunction>();
27 this.constraints = new ArrayList<Constraint>();
28 this.weight = 1;
29 this.aggregationType = aggregationtype;
30 }
31
32 public UtilityFunction(AGGREGATIONTYPE aggregationtype, double weight) {
33 this.utilityFunctions = new ArrayList<UtilityFunction>();
34 this.constraints = new ArrayList<Constraint>();
35 this.weight = weight;
36 this.aggregationType = aggregationtype;
37 }
38
39 public double getWeight() {
40 return weight;
41 }
42
43 public void setWeight(double weight) {
44 this.weight = weight;
45 }
46
47 public ArrayList<Constraint> getConstraints() {
48 return constraints;
49 }
50
51 public void setConstraints(ArrayList<Constraint> constraints) {
52 this.constraints = constraints;
53 }
54
55 public void addConstraint(Constraint newConstraint) {
56 if (!this.constraints.contains(newConstraint))
57 this.constraints.add(newConstraint);
58 }
59
60 public void addConstraint(ArrayList<Constraint> newContraints) {
61
62 for (Constraint newConstraint : newContraints) {
63 if (!this.constraints.contains(newConstraint))
64 this.constraints.add(newConstraint);
65 }
66 }
67
68 public ArrayList<UtilityFunction> getUtilityFunctions() {
69 return utilityFunctions;
70 }
71
72 public void setUtilityFunctions(ArrayList<UtilityFunction> utilityFunctions) {
73 this.utilityFunctions = utilityFunctions;
74 }
75
76 public void addUtilityFunction(UtilityFunction newUtilityFunction) {
77 if (!utilityFunctions.contains(newUtilityFunction))
78 this.utilityFunctions.add(newUtilityFunction);
79 }
80
81 public AGGREGATIONTYPE getAggregationType() {
82 return aggregationType;
83 }
84
85 public void setAggregationType(AGGREGATIONTYPE aggreationType) {
86 this.aggregationType = aggreationType;
87 }
88
89 public double getUtility(Bid bid) {
90
91 double finalUtility = 0.0;
92 double currentUtility = 0.0;
93
94 if (aggregationType == AGGREGATIONTYPE.MIN)
95 finalUtility = Integer.MAX_VALUE;
96 else if (aggregationType == AGGREGATIONTYPE.MAX)
97 finalUtility = -Integer.MAX_VALUE;
98
99 // Utility from Constraints
100 for (Constraint constraint : constraints) {
101
102 currentUtility = constraint.getUtility(bid);
103
104 switch (aggregationType) {
105
106 case SUM:
107 finalUtility += currentUtility;
108 break;
109 case MAX:
110 if (finalUtility < currentUtility)
111 finalUtility = currentUtility;
112 break;
113 case MIN:
114 if (finalUtility > currentUtility)
115 finalUtility = currentUtility;
116 break;
117 }
118 }
119
120 // Utility from Utility Functions
121 for (UtilityFunction function : utilityFunctions) {
122
123 currentUtility = function.getUtility(bid) * function.getWeight();
124
125 switch (aggregationType) {
126
127 case SUM:
128 finalUtility += currentUtility;
129 break;
130 case MAX:
131 if (finalUtility < currentUtility)
132 finalUtility = currentUtility;
133 break;
134 case MIN:
135 if (finalUtility > currentUtility)
136 finalUtility = currentUtility;
137 break;
138
139 }
140 } // for
141
142 return finalUtility;
143 }
144
145}
Note: See TracBrowser for help on using the repository browser.