[1] | 1 | package agents.anac.y2015.pnegotiator;
|
---|
| 2 |
|
---|
| 3 | import java.util.Arrays;
|
---|
| 4 | import java.util.Random;
|
---|
| 5 |
|
---|
| 6 | import genius.core.Bid;
|
---|
| 7 | import genius.core.issue.IssueDiscrete;
|
---|
| 8 | import genius.core.issue.ValueDiscrete;
|
---|
| 9 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
| 10 | import genius.core.utility.EvaluatorDiscrete;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Created by chad on 2/27/15.
|
---|
| 14 | */
|
---|
| 15 | public class BestBids {
|
---|
| 16 | // private Bid minBid;
|
---|
| 17 | // private ArrayList<Tuple<Bid,Double>> preferredBids;
|
---|
| 18 |
|
---|
| 19 | int[] iprefs;
|
---|
| 20 | ValueDiscrete[][] vs;
|
---|
| 21 | int[][] vprefs;
|
---|
| 22 |
|
---|
| 23 | AdditiveUtilitySpace utilitySpace;
|
---|
| 24 |
|
---|
| 25 | public BestBids(AdditiveUtilitySpace utilitySpace) {
|
---|
| 26 | this.utilitySpace = utilitySpace;
|
---|
| 27 | try {
|
---|
| 28 | this.genIssuePreferenceOrder();
|
---|
| 29 | } catch (Exception e) {
|
---|
| 30 | e.printStackTrace();
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | private void genIssuePreferenceOrder() throws Exception {
|
---|
| 35 | int N = utilitySpace.getDomain().getIssues().size();
|
---|
| 36 | Tuple<Integer, Double> issues[] = new Tuple[N];
|
---|
| 37 | iprefs = new int[N];
|
---|
| 38 | vprefs = new int[N][];
|
---|
| 39 | vs = new ValueDiscrete[N][];
|
---|
| 40 | for (int i = 0; i < N; i++) {
|
---|
| 41 | IssueDiscrete di = (IssueDiscrete) utilitySpace.getDomain()
|
---|
| 42 | .getIssues().get(i);
|
---|
| 43 | EvaluatorDiscrete de = (EvaluatorDiscrete) utilitySpace
|
---|
| 44 | .getEvaluator(i + 1);
|
---|
| 45 | issues[i] = new Tuple<Integer, Double>(i, de.getWeight());
|
---|
| 46 | int M = di.getNumberOfValues();
|
---|
| 47 | Tuple<Integer, Double> values[] = new Tuple[M];
|
---|
| 48 | vs[i] = new ValueDiscrete[M];
|
---|
| 49 | for (int j = 0; j < M; j++) {
|
---|
| 50 | double w = de.getEvaluation(di.getValue(j));
|
---|
| 51 | values[j] = new Tuple<Integer, Double>(j, w);
|
---|
| 52 | vs[i][j] = di.getValue(j);
|
---|
| 53 | }
|
---|
| 54 | Arrays.sort(values);
|
---|
| 55 | vprefs[i] = new int[M];
|
---|
| 56 | for (int k = 0; k < M; k++)
|
---|
| 57 | vprefs[i][k] = values[k].key;
|
---|
| 58 | }
|
---|
| 59 | Arrays.sort(issues);
|
---|
| 60 | for (int i = 0; i < N; i++)
|
---|
| 61 | iprefs[i] = issues[i].key;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public int getValue(int issue, ValueDiscrete v) {
|
---|
| 65 | for (int j = 0; j < vs[issue].length; j++) {
|
---|
| 66 | if (vs[issue][j] == v)
|
---|
| 67 | return j;
|
---|
| 68 | }
|
---|
| 69 | return -1;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private static <T> int indexOf(int[] arr, int item) {
|
---|
| 73 | for (int i = 0; i < arr.length; i++) {
|
---|
| 74 | if (arr[i] == item)
|
---|
| 75 | return i;
|
---|
| 76 | }
|
---|
| 77 | return -1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public Bid getRandomBid(Bid r, Random rand, double minValue, double maxValue)
|
---|
| 81 | throws Exception {
|
---|
| 82 | Bid b = new Bid(r);
|
---|
| 83 | int N = iprefs.length;
|
---|
| 84 | // int[] vs = new int[N];
|
---|
| 85 | // for(int i = 0; i < N; i++)
|
---|
| 86 | // vs[iprefs[i]] = getValue(i, (ValueDiscrete)b.getValue(i));
|
---|
| 87 | double U = utilitySpace.getUtility(b);
|
---|
| 88 | System.out.println(Arrays.toString(iprefs));
|
---|
| 89 | int t = 0;
|
---|
| 90 | while (U < minValue || U >= maxValue) {
|
---|
| 91 | t++;
|
---|
| 92 | if (t > 100)
|
---|
| 93 | break;
|
---|
| 94 | // System.out.println(U);
|
---|
| 95 | int ri = rand.nextInt(N);// .get((int)((1.-rand.nextDouble()*rand.nextDouble())*(available.size()-1)));
|
---|
| 96 | int rI = iprefs[ri];
|
---|
| 97 | int v = getValue(rI, (ValueDiscrete) b.getValue(rI + 1));
|
---|
| 98 | int V = indexOf(vprefs[rI], v);
|
---|
| 99 | int M = vprefs[rI].length;
|
---|
| 100 | if (U < minValue) { // increase the preference of an issue to the
|
---|
| 101 | // next highest level
|
---|
| 102 | if (V >= M - 1) {
|
---|
| 103 | continue;
|
---|
| 104 | }
|
---|
| 105 | // System.out.format("%d %d %d", vs[ri].length,
|
---|
| 106 | // vprefs[ri].length, V+1);
|
---|
| 107 | // System.out.println(vs[ri][vprefs[ri][V+1]]);
|
---|
| 108 | b = b.putValue(rI + 1, vs[rI][vprefs[rI][V + 1]]);
|
---|
| 109 | } else if (U >= maxValue) { // decrease the preference of an issue
|
---|
| 110 | // to the next lowest level
|
---|
| 111 | if (V <= 0) {
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 | b = b.putValue(rI + 1, vs[rI][vprefs[rI][V - 1]]);
|
---|
| 115 | }
|
---|
| 116 | U = utilitySpace.getUtility(b);
|
---|
| 117 | }
|
---|
| 118 | return b;
|
---|
| 119 | // return preferredBids.get(rand.nextInt(preferredBids.size())).key;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | // public Bid getMinBid() {
|
---|
| 123 | // if(minBid == null) {
|
---|
| 124 | // minBid = Collections.min(preferredBids).key;
|
---|
| 125 | // }
|
---|
| 126 | // return minBid;
|
---|
| 127 | // }
|
---|
| 128 |
|
---|
| 129 | // public void genPreferredBids(int count) throws Exception {
|
---|
| 130 | // Bid best = utilitySpace.getMaxUtilityBid();
|
---|
| 131 | // genIssuePreferenceOrder();
|
---|
| 132 | // System.out.println(Arrays.toString(iprefs));
|
---|
| 133 | // System.out.println(Arrays.toString(vprefs));
|
---|
| 134 | // maxCount = count;
|
---|
| 135 | // preferredBids = genBids(best, 0);
|
---|
| 136 | // }
|
---|
| 137 |
|
---|
| 138 | // int maxCount;
|
---|
| 139 | // int count = 0;
|
---|
| 140 | // private ArrayList<Tuple<Bid,Double>> genBids(Bid b, int i) throws
|
---|
| 141 | // Exception {
|
---|
| 142 | // ArrayList<Tuple<Bid,Double>> bids = new ArrayList<Tuple<Bid,Double>>();
|
---|
| 143 | // int I = iprefs[i];
|
---|
| 144 | // int M = vprefs[I].length;
|
---|
| 145 | // if(i < iprefs.length-1) {
|
---|
| 146 | // for(int j = M-1; j >= 0 && count < maxCount; j--) {
|
---|
| 147 | // Bid b1 = new Bid(b);
|
---|
| 148 | // Value v =
|
---|
| 149 | // ((IssueDiscrete)utilitySpace.getDomain().getIssue(I)).getValue(vprefs[I][j]);
|
---|
| 150 | // b1.setValue(I+1,v);
|
---|
| 151 | // bids.addAll(genBids(b1, i + 1));
|
---|
| 152 | // }
|
---|
| 153 | // }
|
---|
| 154 | // else {
|
---|
| 155 | // for(int j = M-1; j >= 0 && count < maxCount; j--) {
|
---|
| 156 | // Bid b1 = new Bid(b);
|
---|
| 157 | // Value v =
|
---|
| 158 | // ((IssueDiscrete)utilitySpace.getDomain().getIssue(I)).getValue(vprefs[I][j]);
|
---|
| 159 | // b1.setValue(I+1,v);
|
---|
| 160 | // bids.add(new Tuple<Bid,Double>(b1,utilitySpace.getUtility(b1)));
|
---|
| 161 | // count++;
|
---|
| 162 | // }
|
---|
| 163 | // }
|
---|
| 164 | // return bids;
|
---|
| 165 | // }
|
---|
| 166 | }
|
---|