1 | package geniusweb.blingbling;
|
---|
2 |
|
---|
3 | import java.math.BigInteger;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import geniusweb.bidspace.AllBidsList;
|
---|
14 | import geniusweb.blingbling.Ranknet.Layer;
|
---|
15 | import geniusweb.blingbling.Ranknet.NeuralRankNet;
|
---|
16 | import geniusweb.blingbling.Ranknet.SigmoidActivationFunction;
|
---|
17 | import geniusweb.issuevalue.Bid;
|
---|
18 | import geniusweb.issuevalue.Domain;
|
---|
19 | import geniusweb.issuevalue.Value;
|
---|
20 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
21 | import geniusweb.profile.Profile;
|
---|
22 |
|
---|
23 | import org.neuroph.core.NeuralNetwork;
|
---|
24 | import org.neuroph.core.data.DataSet;
|
---|
25 | import org.neuroph.core.data.DataSetRow;
|
---|
26 | import org.neuroph.core.learning.LearningRule;
|
---|
27 | import org.neuroph.util.TransferFunctionType;
|
---|
28 |
|
---|
29 | import geniusweb.blingbling.Ranknet4j.Ranknet;
|
---|
30 | import geniusweb.blingbling.Ranknet4j.BackPropagation;
|
---|
31 |
|
---|
32 | public class MyProfile {
|
---|
33 | //model param
|
---|
34 | private Ranknet ann;
|
---|
35 | private DataSet dataset;
|
---|
36 | //these params can be set via the Strategy entry
|
---|
37 | private double LearningRate = 0.0025;
|
---|
38 | private int Epoch = 3000;
|
---|
39 | private int inputcount = 0;
|
---|
40 | private int hiddencount = 30;
|
---|
41 |
|
---|
42 | //negotiation param
|
---|
43 | private Domain domain;
|
---|
44 | private List<Bid> bidlist = new ArrayList<>();
|
---|
45 | private AllBidsList allbid;
|
---|
46 | private Bid reservationbid;
|
---|
47 | private Bid maxBid;
|
---|
48 | private Bid minBid;
|
---|
49 | private HashMap<Bid, Double> AllUtilityMap = new HashMap<Bid, Double>();
|
---|
50 | private HashMap<String, HashMap<Value, Integer>> valuePosition = new HashMap<String, HashMap<Value, Integer>>();
|
---|
51 |
|
---|
52 | //for elicit compare
|
---|
53 | private HashMap<String, HashMap<Value, Integer>> valuefrequency = new HashMap<String, HashMap<Value, Integer>>();//new?or null?
|
---|
54 |
|
---|
55 | public MyProfile(Profile profile, int epoch, double learningrate) {
|
---|
56 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
57 |
|
---|
58 | this.LearningRate = learningrate;
|
---|
59 | this.Epoch = epoch;
|
---|
60 | this.domain = prof.getDomain();
|
---|
61 | this.bidlist = prof.getBids();//the partial info bid
|
---|
62 | this.reservationbid = prof.getReservationBid();
|
---|
63 | this.allbid = new AllBidsList(domain);
|
---|
64 |
|
---|
65 | //get input size.
|
---|
66 | for (String issue: domain.getIssues()) {
|
---|
67 | int num = domain.getValues(issue).size().intValue();
|
---|
68 | inputcount = inputcount+num;
|
---|
69 | }
|
---|
70 | hiddencount = inputcount*2;
|
---|
71 | this.ann = new Ranknet(TransferFunctionType.SIGMOID, inputcount, hiddencount, 1);
|
---|
72 | this.dataset = new DataSet(inputcount*2, 1);
|
---|
73 |
|
---|
74 | // if (true) {
|
---|
75 | // throw new RuntimeException("ttt done"+ inputcount);
|
---|
76 | // }
|
---|
77 | setvaluefrequency(bidlist);
|
---|
78 | getValueind();
|
---|
79 | constructdata(profile);
|
---|
80 | train(dataset, Epoch, LearningRate);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | public void constructdata(Profile profile) {
|
---|
85 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
86 | double[] output = new double[1];
|
---|
87 | output[0] = 1.0;
|
---|
88 | for(int i = 0; i < bidlist.size(); i++) {
|
---|
89 | for (int j = i+1; j < bidlist.size(); j++) {
|
---|
90 | Bid bid1 = bidlist.get(i);
|
---|
91 | Bid bid2 = bidlist.get(j);
|
---|
92 |
|
---|
93 | if(prof.isPreferredOrEqual(bid1, bid2)) {
|
---|
94 | double[] data1 = new double[inputcount*2];
|
---|
95 | for (int ind=0; ind<inputcount*2; ind++) {
|
---|
96 | if (ind<inputcount) {
|
---|
97 | data1[ind] = bidtoVector(bid1)[ind];
|
---|
98 | }else {
|
---|
99 | data1[ind] = bidtoVector(bid2)[ind-inputcount];
|
---|
100 | }
|
---|
101 | }
|
---|
102 | dataset.add(data1, output);
|
---|
103 | }
|
---|
104 | if (prof.isPreferredOrEqual(bid2, bid1)) {
|
---|
105 | double[] data2 = new double[inputcount*2];
|
---|
106 | for (int ind=0; ind<inputcount*2; ind++) {
|
---|
107 | if (ind<inputcount) {
|
---|
108 | data2[ind] = bidtoVector(bid2)[ind];
|
---|
109 | }else {
|
---|
110 | data2[ind] = bidtoVector(bid1)[ind-inputcount];
|
---|
111 | }
|
---|
112 | }
|
---|
113 | dataset.add(data2, output);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | // return null;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void getValueind() {//get the input position of a value
|
---|
121 |
|
---|
122 | int valueind = 0;
|
---|
123 | for(String issue: domain.getIssues()) {
|
---|
124 | HashMap<Value, Integer> temp = new HashMap<Value, Integer>();
|
---|
125 | for (Value value: domain.getValues(issue)) {
|
---|
126 | temp.put(value, valueind);
|
---|
127 | valuePosition.put(issue, temp);
|
---|
128 | valueind ++;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public double[] bidtoVector(Bid bid) {//input the bid, return a double[] vector.
|
---|
134 |
|
---|
135 | double[] features = new double[inputcount];
|
---|
136 | for (int i =0; i<inputcount; i++) {
|
---|
137 | features[i]=0.0;
|
---|
138 | }
|
---|
139 | for (String issue: domain.getIssues()) {
|
---|
140 | Value v = bid.getValue(issue);
|
---|
141 | int valuepos = valuePosition.get(issue).get(v);
|
---|
142 | features[valuepos] = 1.0;
|
---|
143 | }
|
---|
144 | // features.putScalar(row, col, value)
|
---|
145 | return features;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | public void train(DataSet ds, int epoch, double lr) {
|
---|
150 |
|
---|
151 | BackPropagation bp = new BackPropagation();
|
---|
152 | bp.setMaxIterations(epoch);
|
---|
153 | bp.setLearningRate(lr);
|
---|
154 | ann.setLearningRule(bp);
|
---|
155 | ds.shuffle();
|
---|
156 | ann.learn(ds);
|
---|
157 | getSorted(allbid);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void subtrain(int epoch, double lr) {
|
---|
161 | train(dataset, epoch, lr);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | public double getUtility(Bid bid) {
|
---|
166 | //get the utility from the model.
|
---|
167 | double[] bidvec = bidtoVector(bid);
|
---|
168 | ann.setInput(bidvec);
|
---|
169 | ann.calculate();
|
---|
170 | return ann.getOutput()[0];
|
---|
171 | }
|
---|
172 |
|
---|
173 | public double getRankUtility(Bid bid) {
|
---|
174 | return AllUtilityMap.get(bid);
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void getSorted(AllBidsList allbids) {
|
---|
178 | long spacesize = allbids.size().intValue();
|
---|
179 | List<Bid> allbidlist = new ArrayList<Bid>();
|
---|
180 | for (int n = 0; n< spacesize; n++) {
|
---|
181 | Bid bid = allbids.get(BigInteger.valueOf(n));
|
---|
182 | allbidlist.add(bid);
|
---|
183 | }
|
---|
184 | Collections.sort(allbidlist, new Comparator<Bid>() {
|
---|
185 | public int compare(Bid b1, Bid b2) {
|
---|
186 | return getUtility(b1)>=getUtility(b2) ? -1 : 1; //descending order
|
---|
187 | }
|
---|
188 | });
|
---|
189 |
|
---|
190 | for (double n=0.0; n<allbidlist.size(); n++) {
|
---|
191 | Bid bid = allbidlist.get((int) n);
|
---|
192 | double utility = (double)(allbidlist.size()-n)/allbidlist.size();
|
---|
193 | AllUtilityMap.put(bid, utility);
|
---|
194 | }
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void setvaluefrequency(List<Bid> inbidlist) {//init and update the valuefrequency.
|
---|
199 | // Set<Bid> inbidset = new HashSet<Bid>(inbidlist);
|
---|
200 | //init valuefrequency map
|
---|
201 | if (valuefrequency.isEmpty()) {
|
---|
202 | for (String issue: domain.getIssues()) {
|
---|
203 | HashMap<Value, Integer> temp = new HashMap<Value, Integer>();
|
---|
204 | for(Value value: domain.getValues(issue)) {
|
---|
205 | temp.put(value, 0);
|
---|
206 | }
|
---|
207 | valuefrequency.put(issue, temp);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | for (Bid bid: inbidlist) {
|
---|
212 | for (String issue: bid.getIssues()) {
|
---|
213 | Value v = bid.getValue(issue);
|
---|
214 | HashMap<Value, Integer> temp = valuefrequency.get(issue);
|
---|
215 | int cnt = temp.get(v);
|
---|
216 | temp.put(v, cnt+1);
|
---|
217 | valuefrequency.put(issue, temp);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public HashMap<String, List<Value>> getmostinformative(){//return a map contains the
|
---|
223 |
|
---|
224 | HashMap<String, List<Value>> infovalue = new HashMap<String, List<Value>>();
|
---|
225 | for (String issue : domain.getIssues()) {
|
---|
226 | List<Value> elicitvalueset = new ArrayList<Value>();
|
---|
227 | int minfreq = 0;
|
---|
228 | for (Value value: domain.getValues(issue)) {
|
---|
229 | int freq = valuefrequency.get(issue).get(value);
|
---|
230 | if (elicitvalueset.isEmpty()) {
|
---|
231 | elicitvalueset.add(value);
|
---|
232 | minfreq = freq;
|
---|
233 | }else {
|
---|
234 | if (freq<minfreq) {
|
---|
235 | elicitvalueset.clear();
|
---|
236 | elicitvalueset.add(value);
|
---|
237 | }else if(freq == minfreq) {
|
---|
238 | elicitvalueset.add(value);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | infovalue.put(issue, elicitvalueset);
|
---|
243 |
|
---|
244 | }
|
---|
245 | return infovalue;
|
---|
246 | }
|
---|
247 |
|
---|
248 | public List<Bid> getElicitBid() {
|
---|
249 | //find the most informative value of every issue.
|
---|
250 | HashMap<String, List<Value>> infomap = getmostinformative();
|
---|
251 | HashMap<String, Value> bidmap = new HashMap<String, Value>();
|
---|
252 | List<Bid> bidresult = new ArrayList<Bid>();
|
---|
253 | bidDFS(new ArrayList<String>(infomap.keySet()), bidmap, infomap, bidresult);
|
---|
254 | return bidresult;
|
---|
255 | }
|
---|
256 |
|
---|
257 | public void bidDFS(List<String> issues, HashMap<String, Value> bidmap, HashMap<String, List<Value>> infomap, List<Bid> bidresultlist) {
|
---|
258 | if (bidmap.keySet().size() == issues.size()) {
|
---|
259 | bidresultlist.add(new Bid(bidmap));
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | for (Value value: infomap.get(issues.get(bidmap.size()))) {
|
---|
264 | bidmap.put(issues.get(bidmap.size()), value);
|
---|
265 | bidDFS(issues, bidmap, infomap, bidresultlist);
|
---|
266 | bidmap.remove(issues.get(bidmap.size()-1));
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | public void update(Bid bid, List<Bid> betterBids, List<Bid> worseBids) {
|
---|
271 | updateDataset(bid, betterBids, worseBids);
|
---|
272 | updateBidAndValueFrequency(bid);
|
---|
273 | train(dataset, Epoch, LearningRate);
|
---|
274 | }
|
---|
275 |
|
---|
276 | public void updateDataset(Bid bid, List<Bid> betterBids, List<Bid> worseBids) {
|
---|
277 | double[] output = new double[1];
|
---|
278 | output[0] = 1.0;
|
---|
279 | for (int i=0; i< betterBids.size(); i++) {
|
---|
280 | Bid betterbid = betterBids.get(i);
|
---|
281 | double[] data = new double[inputcount*2];
|
---|
282 | for (int ind=0; ind<inputcount*2; ind++) {
|
---|
283 | if (ind< inputcount) {
|
---|
284 | data[ind] = bidtoVector(betterbid)[ind];
|
---|
285 | }else {
|
---|
286 | data[ind] = bidtoVector(bid)[ind-inputcount];
|
---|
287 | }
|
---|
288 | }
|
---|
289 | dataset.add(data, output);
|
---|
290 | }
|
---|
291 | for (int i=0; i< worseBids.size(); i++) {
|
---|
292 | Bid worsebid = worseBids.get(i);
|
---|
293 | double[] data = new double[inputcount*2];
|
---|
294 | for (int ind=0; ind<inputcount*2; ind++) {
|
---|
295 | if (ind< inputcount) {
|
---|
296 | data[ind] = bidtoVector(bid)[ind];
|
---|
297 | }else {
|
---|
298 | data[ind] = bidtoVector(worsebid)[ind-inputcount];
|
---|
299 | }
|
---|
300 | }
|
---|
301 | dataset.add(data, output);
|
---|
302 | }
|
---|
303 |
|
---|
304 | }
|
---|
305 |
|
---|
306 | public void updateBidAndValueFrequency(Bid bid) {
|
---|
307 | bidlist.add(bid);
|
---|
308 | setvaluefrequency(new ArrayList<Bid>(Arrays.asList(bid)));//add upon the original.
|
---|
309 | }
|
---|
310 |
|
---|
311 | public Domain getDomain() {
|
---|
312 | return this.domain;
|
---|
313 | }
|
---|
314 |
|
---|
315 | public Bid getBestBid() {
|
---|
316 | return this.maxBid;
|
---|
317 | }
|
---|
318 |
|
---|
319 | public Bid getWorstBid() {
|
---|
320 | return this.minBid;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public Bid getReservationBid() {
|
---|
324 | return this.reservationbid;
|
---|
325 | }
|
---|
326 | public List<Bid> getBidlist(){
|
---|
327 | return this.bidlist;
|
---|
328 | }
|
---|
329 | public List<Bid> getAllBidlist(){
|
---|
330 | long spacesize = allbid.size().intValue();
|
---|
331 | List<Bid> allbidlist = new ArrayList<Bid>();
|
---|
332 | for (int n = 0; n< spacesize; n++) {
|
---|
333 | Bid bid = allbid.get(BigInteger.valueOf(n));
|
---|
334 | allbidlist.add(bid);
|
---|
335 | }
|
---|
336 | return allbidlist;
|
---|
337 | }
|
---|
338 | public Ranknet getann() {
|
---|
339 | return this.ann;
|
---|
340 | }
|
---|
341 |
|
---|
342 | } |
---|