1 | package negotiator.boaframework.opponentmodel.agentx;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import genius.core.issue.Value;
|
---|
7 | import genius.core.issue.ValueDiscrete;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Class for processing discrete values, ranking them based on past bids.
|
---|
11 | *
|
---|
12 | * @author E. Jacobs
|
---|
13 | *
|
---|
14 | */
|
---|
15 | public class DiscreteValueProcessor {
|
---|
16 |
|
---|
17 | private ArrayList<ValueDiscrete> valueList = new ArrayList<ValueDiscrete> ();
|
---|
18 | private ArrayList<Integer> bidList = new ArrayList<Integer> ();
|
---|
19 | private ArrayList<Integer> rankList = new ArrayList<Integer> ();
|
---|
20 |
|
---|
21 | private int nVals;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Creates a valueProcessor containing ranks of and number of bids on a list of values. Use getValues from the Issue class to get such a list.
|
---|
25 | * @param valList
|
---|
26 | */
|
---|
27 | public DiscreteValueProcessor(List<?> valList){
|
---|
28 | for (Object o : valList){
|
---|
29 | if (o instanceof ValueDiscrete){
|
---|
30 | valueList.add((ValueDiscrete) o);
|
---|
31 | }
|
---|
32 | else if (o instanceof Value){
|
---|
33 | Value v = (Value) o;
|
---|
34 | valueList.add((ValueDiscrete) v);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | fillLists();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Fills both the bid- and the ranklists
|
---|
43 | */
|
---|
44 | private void fillLists(){
|
---|
45 |
|
---|
46 | nVals = valueList.size();
|
---|
47 |
|
---|
48 | for (int i = 0;i<nVals;i++){
|
---|
49 | bidList.add(0);
|
---|
50 | rankList.add(i+1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Returns the index of a certain value in the valuelist.
|
---|
57 | * @param v The value
|
---|
58 | * @return The index of the value in the list, or -1 if it is not in the list.
|
---|
59 | */
|
---|
60 | public int getValueIndex(ValueDiscrete v){
|
---|
61 | return valueList.indexOf(v);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Gets the rank of a certain value. Higher is more important. Lowest is rank 1, highest is nValues.
|
---|
66 | * @param v The value
|
---|
67 | * @return The rank of the value, or -1 if the value is not in the list
|
---|
68 | */
|
---|
69 | public int getValueRank(ValueDiscrete v){
|
---|
70 |
|
---|
71 | int index = getValueIndex(v);
|
---|
72 |
|
---|
73 | if (index != -1)
|
---|
74 | return rankList.get(index);
|
---|
75 | else
|
---|
76 | return index;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets the normalized rank of a certain value. Higher is more important. Lowest is rank 1/ nValues, highest is rank 1.
|
---|
81 | * @param v The value
|
---|
82 | * @return The rank of the value, or -1 if the value is not in the list
|
---|
83 | */
|
---|
84 | public double getNormalizedValueRank(ValueDiscrete v){
|
---|
85 |
|
---|
86 | double valueRank = (double) getValueRank(v);
|
---|
87 |
|
---|
88 | if (valueRank != -1)
|
---|
89 | return valueRank / ((double) nVals);
|
---|
90 | else
|
---|
91 | return valueRank;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Returns the list index of a certain rank
|
---|
96 | * @param rank The required rank
|
---|
97 | * @return index of the rank throughout the different lists, or -1 if the rank does not exist.
|
---|
98 | */
|
---|
99 | private int getRankIndex(int rank){
|
---|
100 |
|
---|
101 | if (rank > nVals) return -1;
|
---|
102 |
|
---|
103 | return rankList.indexOf(rank);
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns the highest ranked value within an issue
|
---|
109 | * @return The highest ranked discrete value
|
---|
110 | */
|
---|
111 | public ValueDiscrete getHighestRankedValue(){
|
---|
112 | return valueList.get(getRankIndex(nVals));
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Gives the number of bids for a certain value
|
---|
117 | * @param v The value for which the bids are required
|
---|
118 | * @return The number of bids, or -1 if the value is not in the list.
|
---|
119 | */
|
---|
120 | private int getValueBids(ValueDiscrete v){
|
---|
121 |
|
---|
122 | int index = getValueIndex(v);
|
---|
123 |
|
---|
124 | if (index != -1)
|
---|
125 | return bidList.get(index);
|
---|
126 | else
|
---|
127 | return index;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Adds 1 to the bidList for a certain value. Use this if a bid has been made containing that value, allowing for a change in rank
|
---|
132 | * @param v The value v on which the bid was done
|
---|
133 | */
|
---|
134 | public void addBidForValue(ValueDiscrete v){
|
---|
135 |
|
---|
136 | int index = getValueIndex(v);
|
---|
137 |
|
---|
138 | bidList.set(index, bidList.get(index) + 1);
|
---|
139 | changeRankByBid(v);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Reranks a value based on its number of bids. Use immediately after changing the bids of a value.
|
---|
144 | * @param v The value for which the number of bids has changed
|
---|
145 | */
|
---|
146 | private void changeRankByBid(ValueDiscrete v){
|
---|
147 |
|
---|
148 | //System.out.println("Changing ranks by bid...");
|
---|
149 | //System.out.println("Maximum rank is:" + nVals);
|
---|
150 |
|
---|
151 | int newRank = getValueRank(v)+1;
|
---|
152 | int bids = getValueBids(v);
|
---|
153 | int rankChange = 0;
|
---|
154 | int rankindex = 0;
|
---|
155 |
|
---|
156 | while (newRank <= nVals){
|
---|
157 |
|
---|
158 | rankindex = getRankIndex(newRank);
|
---|
159 | if (bids > bidList.get(rankindex)){
|
---|
160 | newRank ++;
|
---|
161 | rankChange ++;
|
---|
162 | }
|
---|
163 | else
|
---|
164 | break;
|
---|
165 | }
|
---|
166 |
|
---|
167 | while (rankChange > 0){
|
---|
168 | increaseRank(v);
|
---|
169 | rankChange --;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Directly increases the rank of a value. Note that higher ranks are higher numbers, denoting more important values
|
---|
175 | * @param v The value for which the rank has to be changed
|
---|
176 | */
|
---|
177 | public void increaseRank(ValueDiscrete v){
|
---|
178 |
|
---|
179 | int oldRank = getValueRank(v);
|
---|
180 | int newRank = oldRank + 1;
|
---|
181 |
|
---|
182 | if (oldRank == nVals) return;
|
---|
183 |
|
---|
184 | int oldIndex = getRankIndex(oldRank);
|
---|
185 | int newIndex = getRankIndex(newRank);
|
---|
186 |
|
---|
187 | rankList.set(oldIndex, newRank);
|
---|
188 | rankList.set(newIndex, oldRank);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Directly decreases the rank of a value. Note that lower ranks are lower numbers, denoting less important values
|
---|
193 | * @param v The value for which the rank has to be changed
|
---|
194 | */
|
---|
195 | public void decreaseRank(ValueDiscrete v){
|
---|
196 |
|
---|
197 | int oldRank = getValueRank(v);
|
---|
198 | int newRank = oldRank - 1;
|
---|
199 |
|
---|
200 | if (oldRank == 0) return;
|
---|
201 |
|
---|
202 | int oldIndex = getRankIndex(oldRank);
|
---|
203 | int newIndex = getRankIndex(newRank);
|
---|
204 |
|
---|
205 | rankList.set(oldIndex, newRank);
|
---|
206 | rankList.set(newIndex, oldRank);
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public String toString(){
|
---|
211 |
|
---|
212 | String str = "";
|
---|
213 |
|
---|
214 | for (int i = 0;i < nVals;i++){
|
---|
215 | str += "Value: " + valueList.get(i).getValue() + ", bids: " + bidList.get(i) + ", rank: " + rankList.get(i) +"\n";
|
---|
216 | }
|
---|
217 |
|
---|
218 | return str;
|
---|
219 | }
|
---|
220 | }
|
---|