[1] | 1 | package geniusweb.blingbling;
|
---|
| 2 |
|
---|
| 3 | // old one, not used.
|
---|
| 4 | import java.math.BigInteger;
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.Collections;
|
---|
| 7 | import java.util.Comparator;
|
---|
| 8 | import java.util.HashMap;
|
---|
| 9 | import java.util.HashSet;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
| 12 | import java.util.Set;
|
---|
| 13 | import java.util.logging.Level;
|
---|
| 14 |
|
---|
| 15 | import org.nd4j.linalg.api.ndarray.INDArray;
|
---|
| 16 | import org.nd4j.linalg.factory.Nd4j;
|
---|
| 17 |
|
---|
| 18 | import geniusweb.blingbling.Ranknet.NeuralRankNet;
|
---|
| 19 | import geniusweb.blingbling.Ranknet.*;
|
---|
| 20 |
|
---|
| 21 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
| 22 | import geniusweb.profile.Profile;
|
---|
| 23 | import geniusweb.profile.utilityspace.UtilitySpace;
|
---|
| 24 | import geniusweb.issuevalue.Bid;
|
---|
| 25 | import geniusweb.issuevalue.Domain;
|
---|
| 26 | import geniusweb.issuevalue.Value;
|
---|
| 27 |
|
---|
| 28 | public 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 | }
|
---|