source: anac2020/BlingBling/src/main/java/geniusweb/blingbling/MyUtilityModel.java@ 14

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

#1910 added anac2020 parties

File size: 5.8 KB
Line 
1package geniusweb.blingbling;
2
3// old one, not used.
4import java.math.BigInteger;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Set;
13import java.util.logging.Level;
14
15import org.nd4j.linalg.api.ndarray.INDArray;
16import org.nd4j.linalg.factory.Nd4j;
17
18import geniusweb.blingbling.Ranknet.NeuralRankNet;
19import geniusweb.blingbling.Ranknet.*;
20
21import geniusweb.profile.DefaultPartialOrdering;
22import geniusweb.profile.Profile;
23import geniusweb.profile.utilityspace.UtilitySpace;
24import geniusweb.issuevalue.Bid;
25import geniusweb.issuevalue.Domain;
26import geniusweb.issuevalue.Value;
27
28public class MyUtilityModel {
29 //model param
30 private NeuralRankNet ann;
31 private double LearningRate = 0.001;
32 private int Epoch = 500;
33 private int inputcount = 0;
34 private int hiddencount = 30;
35
36 //train data
37 private List<INDArray> Input1 = new ArrayList<>();
38 private List<INDArray> Input2 = new ArrayList<>();
39
40 //negotiation param
41 private Domain domain;
42 private List<Bid> bidlist = new ArrayList<>();
43 private Bid reservationbid;
44 private Bid maxBid;
45 private Bid minBid;
46 private HashMap<String, HashMap<Value, Integer>> valuePosition = new HashMap<String, HashMap<Value, Integer>>();
47
48 //for elicit compare
49 private HashMap<String, HashMap<Value, Integer>> valuefrequency = new HashMap<String, HashMap<Value, Integer>>();//new?or null?
50
51 public MyUtilityModel(Profile profile) {
52 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
53
54 this.domain = prof.getDomain();
55 this.bidlist = prof.getBids();
56 this.reservationbid = prof.getReservationBid();
57
58 //get input size.
59 for (String issue: domain.getIssues()) {
60 int num = domain.getValues(issue).size().intValue();
61 inputcount = inputcount+num;
62 }
63
64 buildRankModel(prof.getDomain(), hiddencount);
65
66// if (true) {
67// throw new RuntimeException("ttt done"+ inputcount);
68// }
69 getValueind();
70 constructdata(profile);
71 train();
72 }
73
74 public void buildRankModel(Domain domain, int Hiddencount) {
75// Set<String> issueset = domain.getIssues();
76 ann = NeuralRankNet.Builder().setLearningRate(LearningRate)
77 .addLayer(Layer.Builder().setInCount(inputcount).setOutCount(Hiddencount).setActivationFunction(SigmoidActivationFunction.INSTANCE).build())
78 .addLayer(Layer.Builder().setInCount(Hiddencount).setOutCount(Hiddencount).setActivationFunction(SigmoidActivationFunction.INSTANCE).build())
79 .addLayer(Layer.Builder().setInCount(Hiddencount).setOutCount(1).setActivationFunction(SigmoidActivationFunction.INSTANCE).build())
80 .build();
81 }
82
83 public void constructdata(Profile profile) {
84 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
85 int ind = 0;
86 for(int i = 0; i < bidlist.size(); i++) {
87 for (int j = i+1; j < bidlist.size(); j++) {
88 Bid bid1 = bidlist.get(i);
89 Bid bid2 = bidlist.get(j);
90
91 if(prof.isPreferredOrEqual(bid1, bid2)) {
92 Input1.add(ind, bidtoArray(bid1));
93 Input2.add(ind, bidtoArray(bid2));
94 ind ++;
95 }
96 if (prof.isPreferredOrEqual(bid2, bid1)) {
97 Input1.add(ind, bidtoArray(bid2));
98 Input2.add(ind, bidtoArray(bid1));
99 ind ++;
100 }
101 }
102 }
103// return null;
104 }
105
106 public void getValueind() {//get the input position of a value
107
108 int valueind = 0;
109 for(String issue: domain.getIssues()) {
110 HashMap<Value, Integer> temp = new HashMap<Value, Integer>();
111 for (Value value: domain.getValues(issue)) {
112 temp.put(value, valueind);
113 valuePosition.put(issue, temp);
114 valueind ++;
115 }
116 }
117 }
118
119 public INDArray bidtoArray(Bid bid) {//input the bid, return a indarray vector.
120
121 INDArray features = Nd4j.zeros(1, inputcount);
122 for (String issue: domain.getIssues()) {
123 Value v = bid.getValue(issue);
124 int valuepos = valuePosition.get(issue).get(v);
125 features.putScalar(0, valuepos, 1.0);
126 }
127// features.putScalar(row, col, value)
128 return features;
129 }
130
131
132 public void train() {
133
134 for (int epoch = 0; epoch< Epoch; epoch++) {
135 for (int ind= 0; ind < Input1.size(); ind++) {
136 ann.train(Input1.get(ind), Input2.get(ind), Nd4j.scalar(1));
137 }
138 }
139 }
140
141 public void update(Bid bid, List<Bid> betterBids, List<Bid> worseBids) {
142
143 }
144
145 public double getUtility(Bid bid) {
146 //get the utility from the model.
147 List<INDArray> feedForward = ann.feedForward(bidtoArray(bid));
148 return feedForward.get(feedForward.size() - 1).getDouble(0);
149 }
150
151 public void setvaluefrequency(List<Bid> inbidlist) {//init and update the valuefrequency.
152
153 for (Bid bid: inbidlist) {
154 for (String issue: bid.getIssues()) {
155 Value v = bid.getValue(issue);
156 HashMap<Value, Integer> temp = valuefrequency.get(issue);
157 int cnt = temp.get(v);
158 temp.put(v, cnt+1);
159 valuefrequency.put(issue, temp);
160 }
161 }
162 }
163
164 public HashMap<String, List<Value>> getmostinformative(){//return a map contains the
165
166 HashMap<String, List<Value>> infovalue = new HashMap<String, List<Value>>();
167 for (String issue : domain.getIssues()) {
168 List<Value> elicitvalueset = new ArrayList<Value>();
169 int minfreq = 0;
170 for (Value value: domain.getValues(issue)) {
171 int freq = valuefrequency.get(issue).get(value);
172 if (elicitvalueset.isEmpty()) {
173 elicitvalueset.add(value);
174 minfreq = freq;
175 }else {
176 if (freq<minfreq) {
177 elicitvalueset.clear();
178 elicitvalueset.add(value);
179 }else if(freq == minfreq) {
180 elicitvalueset.add(value);
181 }
182 }
183 }
184 infovalue.put(issue, elicitvalueset);
185
186 }
187 return infovalue;
188 }
189
190 public Domain getDomain() {
191 return this.domain;
192 }
193
194 public Bid getBestBid() {
195 return this.maxBid;
196 }
197
198 public Bid getWorstBid() {
199 return this.minBid;
200 }
201
202 public Bid getReservationBid() {
203 return this.reservationbid;
204 }
205 public List<Bid> getBidlist(){
206 return this.bidlist;
207 }
208 public NeuralRankNet getann() {
209 return this.ann;
210 }
211
212}
Note: See TracBrowser for help on using the repository browser.