source: anac2020/BlingBling/src/main/java/geniusweb/blingbling/MyUtilitySpace.java@ 77

Last change on this file since 77 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 10.2 KB
Line 
1package geniusweb.blingbling;
2
3//old one not used.
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Set;
12import java.util.logging.Level;
13
14import org.neuroph.core.Layer;
15import org.neuroph.core.NeuralNetwork;
16import org.neuroph.core.Neuron;
17import org.neuroph.core.data.DataSet;
18import org.neuroph.core.data.DataSetRow;
19import org.neuroph.core.learning.LearningRule;
20import org.neuroph.nnet.learning.BackPropagation;
21import org.neuroph.util.ConnectionFactory;
22
23import geniusweb.profile.DefaultPartialOrdering;
24import geniusweb.profile.Profile;
25import geniusweb.profile.utilityspace.UtilitySpace;
26import geniusweb.issuevalue.Bid;
27import geniusweb.issuevalue.Domain;
28import geniusweb.issuevalue.Value;
29
30//ranknet
31import geniusweb.blingbling.Ranknet.NeuralRankNet;
32import geniusweb.blingbling.Ranknet.*;
33
34
35import tudelft.utilities.logging.Reporter;
36
37
38public class MyUtilitySpace {
39 //my estimate utility space class.
40 private Domain domain;
41 private List<Bid> bidlist = new ArrayList<>();
42 private List<Bid> sortedbidlist = new LinkedList<>();
43 private Bid reservationbid;
44 private Bid maxBid;
45 private Bid minBid;
46 private HashMap<String, HashMap<Value, Integer>> valuefrequency = new HashMap<String, HashMap<Value, Integer>>();//new?or null?
47 private NeuralNetwork<LearningRule> ann = new NeuralNetwork<LearningRule>();// the network
48 private HashMap<String, Integer> issueToNeuron = new HashMap<String, Integer>(); //track the neuron position of issue
49 private HashMap<String, HashMap<Value, Integer>> valueToNeuron = new HashMap<String, HashMap<Value, Integer>>();
50 int maxIter = 500; //Integer.parseInt(args[0]);
51 double maxError = 0.01; // Double.parseDouble(args[1]);
52 double learningRate = 0.0005 ; // Double.parseDouble(args[2]);
53
54 DefaultPartialOrdering prof;
55 List<List<Integer>> betterlist;
56
57
58 public MyUtilitySpace(Profile profile, Reporter reporter) {
59
60 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
61
62 this.domain = prof.getDomain();
63 this.bidlist = prof.getBids();
64 this.reservationbid = prof.getReservationBid();
65 this.sortedbidlist = setSortedBids(prof);
66 this.maxBid = sortedbidlist.get(sortedbidlist.size()-1);
67 this.minBid = sortedbidlist.get(0);
68 this.betterlist = prof.getBetter();//for learning to rank method.
69
70
71 initValueFrequency();
72 setvaluefrequency(prof.getBids());//for elicit compare.
73 buildNetwork(prof.getDomain());
74 initweight();
75 DataSet ds = setDataset(this.sortedbidlist);
76 trainNetwork(ds);
77 }
78
79
80 public void buildNetwork(Domain domain) {
81 //neural network model.
82 Set<String> issueset = domain.getIssues();//get all the issues.
83 Layer inputlayer = new Layer(); // set the input layer.
84 Layer weightlayer = new Layer(); // set the weight layer.
85 int weightind = 0; // the ind tracks the issue position in weight layer
86 int valueind = 0; // the ind tracks the value position in input layer
87 for (String issue: issueset) {//iterate the issues
88 weightlayer.addNeuron(weightind, new Neuron()); // add one neuron to the weight layer as
89 issueToNeuron.put(issue, weightind);//put the index to the issue
90 weightind ++;//update the weight index
91
92 HashMap<Value, Integer> temp = new HashMap<Value, Integer>();
93 for (Value value: domain.getValues(issue)) {
94 inputlayer.addNeuron(valueind, new Neuron());// add neuron for every value.
95 temp.put(value, valueind);
96 valueToNeuron.put(issue, temp);
97 valueind ++; //update the value index(position)
98 }
99 }
100 ann.addLayer(0, inputlayer);//set layer position
101 ann.addLayer(1, weightlayer);
102 Layer outputlayer = new Layer();
103 outputlayer.addNeuron(0, new Neuron());//add one neuron to the output layer.
104 ann.addLayer(2, outputlayer);
105 ann.setInputNeurons(inputlayer.getNeurons());
106 ann.setOutputNeurons(outputlayer.getNeurons());
107 }
108
109 public void initweight() {
110 //init the NN weight via a specific method. And create connections.
111 Set<String> issueset = domain.getIssues();// get all the issues
112 for (String issue: issueset) {
113 for (Value value: domain.getValues(issue)) {
114 //create connections from value neurons to issue neurons
115 if(valuefrequency.get(issue).get(value) == 0) {
116 ann.createConnection(ann.getLayerAt(0).getNeuronAt(valueToNeuron.get(issue).get(value)),
117 ann.getLayerAt(1).getNeuronAt(issueToNeuron.get(issue)),
118 0.0);// if one value never shows in the partial data, then we assign 0 to the weight of this value.
119 continue;
120 }
121 ann.createConnection(ann.getLayerAt(0).getNeuronAt(valueToNeuron.get(issue).get(value)),
122 ann.getLayerAt(1).getNeuronAt(issueToNeuron.get(issue)),
123 0.5);// otherwise we assign 0.5 to the weight. If there is another proper way to init this weight?
124 }
125 ann.createConnection(ann.getLayerAt(1).getNeuronAt(issueToNeuron.get(issue)),
126 ann.getLayerAt(2).getNeuronAt(0),
127 1.0/issueset.size());//equal issueweight according to number of issues. Is there a way to control the sum of the weight to be 1.0?
128 }
129 }
130
131 public void initValueFrequency() {
132 // create an all-0 hashmap
133 for (String issue: domain.getIssues()) {
134 HashMap<Value, Integer> vmap = new HashMap<Value, Integer>();
135 for (Value value: domain.getValues(issue)) {
136 vmap.put(value, 0);
137 }
138 this.valuefrequency.put(issue, vmap);
139 }
140 }
141
142 public void setvaluefrequency(List<Bid> inbidlist) {
143 //init and update the valuefrequency.
144 for (Bid bid: inbidlist) {
145 for (String issue: bid.getIssues()) {
146 Value v = bid.getValue(issue);
147 HashMap<Value, Integer> temp = valuefrequency.get(issue);
148 int cnt = temp.get(v);
149 temp.put(v, cnt+1);
150 valuefrequency.put(issue, temp);
151 }
152 }
153 }
154
155 public HashMap<String, List<Value>> getmostinformative(){
156 HashMap<String, List<Value>> infovalue = new HashMap<String, List<Value>>();
157 for (String issue : domain.getIssues()) {
158 List<Value> elicitvalueset = new ArrayList<Value>();
159 int minfreq = 0;
160 for (Value value: domain.getValues(issue)) {
161 int freq = valuefrequency.get(issue).get(value);
162 if (elicitvalueset.isEmpty()) {
163 elicitvalueset.add(value);
164 minfreq = freq;
165 }else {
166 if (freq<minfreq) {
167 elicitvalueset.clear();
168 elicitvalueset.add(value);
169 }else if(freq == minfreq) {
170 elicitvalueset.add(value);
171 }
172 }
173 }
174 infovalue.put(issue, elicitvalueset);
175
176 }
177 return infovalue;
178 }
179
180 public DataSet setDataset(List<Bid> inbidlist) {
181 //Construct the training dataset.
182 // input sorted bidlist. Utility from low to high. Trained using assigned utility.
183 int inputsize = ann.getInputsCount();
184 int outputsize = ann.getOutputsCount();
185 DataSet ds = new DataSet(inputsize, outputsize);
186 int datasize = inbidlist.size();// the size
187 double cnt = 0;//
188 for (Bid bid: inbidlist) {
189 double[] input = new double[inputsize];
190 double[] output = new double[outputsize];
191 Set<Integer> indset = new HashSet<Integer>();
192 for (String issue: domain.getIssues()) {
193 Value v = bid.getValue(issue);//get bid's value of issue.
194 indset.add(valueToNeuron.get(issue).get(v));// keep a set of the position of the bid values.
195 //ValuetoNeural is a map maps every possible value into the position of input value.
196 }
197
198 for (int ind =0; ind<inputsize; ind++) { //construct input vector.
199 if (indset.contains(ind)) {
200 input[ind] = 1.0; // assign 1.0 to the bid's value position
201 }else {
202 input[ind] =0.0;// assign 0 to others
203 }
204 }
205 output[0] = 0.9*(cnt/datasize)+0.1;//the uniform distribution between [0.1, 1], the minimum set to 0.1?
206 DataSetRow dsr = new DataSetRow(input, output);// combine the input vector and the output values.
207 ds.add(dsr);//add this row to the dataset
208 cnt = cnt+1.0; //update the cnt by +1.
209 }
210 return ds;
211 }
212
213 public void trainNetwork(DataSet ds) {
214 //update the NN via the given compared bids. retrain the
215 // is there space to improve? maybe a better learning rule?
216 BackPropagation bp = new BackPropagation();
217 bp.setMaxIterations(maxIter);
218 //bp.setMaxError(maxError);
219 bp.setLearningRate(learningRate);
220 ann.setLearningRule(bp);
221 ann.learn(ds);
222 }
223
224 private List<Bid> setSortedBids(Profile profile) {
225 //returns a sortedlist here. from low utility to high utility.
226 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
227 List<Bid> bidslist = prof.getBids(); //get all the bid in the partial information.
228 // NOTE sort defaults to ascending order.
229 Collections.sort(bidslist, new Comparator<Bid>() {
230
231 @Override
232 public int compare(Bid b1, Bid b2) {
233 return prof.isPreferredOrEqual(b1, b2) ? 1 : -1; //
234 }
235
236 });
237
238 return bidslist;
239 }
240
241 //get and update method
242 public void update(Bid bid, List<Bid> worseBids) {//note the frequency also need to be updated
243 //relist the bids
244 int n = 0;
245 while (n < sortedbidlist.size() && worseBids.contains(sortedbidlist.get(n)))
246 n++;
247 LinkedList<Bid> newbids = new LinkedList<Bid>(sortedbidlist);
248 newbids.add(n, bid);
249 sortedbidlist = newbids;
250
251 DataSet ds = setDataset(sortedbidlist);
252 trainNetwork(ds);
253
254 bidlist.add(bid);//this is unsorted bid list.
255 }
256
257 public double getUtility(Bid bid) {
258 int inputsize = ann.getInputsCount();
259 double[] input = new double[inputsize];
260
261 Set<Integer> indset = new HashSet<Integer>();
262 for (String issue: domain.getIssues()) {
263 Value v = bid.getValue(issue);
264 indset.add(valueToNeuron.get(issue).get(v));
265 }
266
267 for (int ind =0; ind<inputsize; ind++) { //construct input
268 if (indset.contains(ind)) {
269 input[ind] = 1.0;
270 }else {
271 input[ind] = 0.0;
272 }
273 }
274 ann.setInput(input);
275 ann.calculate();
276 return ann.getOutput()[0];//transfer double[] to double.
277 }
278
279 public Domain getDomain() {
280 return this.domain;
281 }
282
283 public Bid getReservationBid() {
284 return this.reservationbid;
285 }
286
287 public List<Bid> getSortedBids(){
288 return this.sortedbidlist;
289 }
290
291 public Bid getBestBid() {
292 return this.maxBid;
293 }
294
295 public Bid getWorstBid() {
296 return this.minBid;
297 }
298 public NeuralNetwork getann() {
299 return this.ann;
300 }
301
302 // The following is for the learning to rank method. use deeplearning4j.
303 public DataSet setPairwiseDataset() {
304
305 betterlist= prof.getBetter();
306
307 return null;
308 }
309
310 public void trainPairwise(DataSet ds) {
311 //using pairwise information
312 }
313
314}
Note: See TracBrowser for help on using the repository browser.