[126] | 1 | package agents.ai2014.group3;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Arrays;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
| 8 | import genius.core.AgentID;
|
---|
| 9 | import genius.core.Bid;
|
---|
| 10 | import genius.core.BidHistory;
|
---|
| 11 | import genius.core.issue.Issue;
|
---|
| 12 | import genius.core.issue.Value;
|
---|
| 13 | import genius.core.issue.ValueDiscrete;
|
---|
| 14 | import genius.core.utility.EvaluatorDiscrete;
|
---|
| 15 |
|
---|
| 16 | public class AgentUtils {
|
---|
| 17 |
|
---|
| 18 | AgentID agent;
|
---|
| 19 | BidHistory bidHistory;
|
---|
| 20 | ArrayList<EvaluatorDiscrete> issueList;
|
---|
| 21 | int nIssues;
|
---|
| 22 | int[] issueIds;
|
---|
| 23 |
|
---|
| 24 | public AgentUtils(AgentID agent, BidHistory bidHistory, int nIssues) {
|
---|
| 25 | super();
|
---|
| 26 | this.agent = agent;
|
---|
| 27 | this.bidHistory = bidHistory;
|
---|
| 28 | this.nIssues = nIssues;
|
---|
| 29 | issueIds = new int[nIssues];
|
---|
| 30 | issueList = new ArrayList<EvaluatorDiscrete>();
|
---|
| 31 | for (int i = 0; i < nIssues; i++) {
|
---|
| 32 | issueList.add(new EvaluatorDiscrete());
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | // Calculates the utility of this bid for the Agent
|
---|
| 37 | public double getAgentUtil(Bid bid) {
|
---|
| 38 | double utility = 0;
|
---|
| 39 | // This HashMap connects the issues IDs to its values
|
---|
| 40 | HashMap<Integer, Value> bidValuesHashMap = bid.getValues();
|
---|
| 41 | for (int i = 0; i < nIssues; i++) {
|
---|
| 42 | double weight = issueList.get(i).getWeight();
|
---|
| 43 | ValueDiscrete VD = (ValueDiscrete) bidValuesHashMap
|
---|
| 44 | .get(issueIds[i]);
|
---|
| 45 | if (issueList.get(i).getValues().contains(VD))
|
---|
| 46 | utility = utility + issueList.get(i).getDoubleValue(VD)
|
---|
| 47 | * weight;
|
---|
| 48 | }
|
---|
| 49 | return utility;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | // based on the most recent bids received recalculates the issue and value
|
---|
| 53 | // weights
|
---|
| 54 | public void recalculateUtilFunction() {
|
---|
| 55 | List<Issue> bidIssueList = bidHistory.getHistory().get(0).getBid()
|
---|
| 56 | .getIssues();
|
---|
| 57 | for (int i = 0; i < bidIssueList.size(); i++) {
|
---|
| 58 | issueIds[i] = bidIssueList.get(i).getNumber();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | double[] bidWeights = calculateBidWeights();
|
---|
| 62 | int[] nChanges = calculateNChanges();
|
---|
| 63 |
|
---|
| 64 | for (int i = 0; i < nIssues; i++) {
|
---|
| 65 | issueList.get(i).clear();
|
---|
| 66 | // First the weight of each issue
|
---|
| 67 | issueList.get(i).setWeight(calculateIssueWeight(i, nChanges));
|
---|
| 68 | ArrayList<ValueDiscrete> valueList = new ArrayList<ValueDiscrete>();
|
---|
| 69 | double[] valueEvaluation = new double[100]; // random large number,
|
---|
| 70 | // just so we don't have
|
---|
| 71 | // to use a List
|
---|
| 72 | for (int j = 0; j < bidHistory.getHistory().size(); j++) {
|
---|
| 73 | ValueDiscrete currentValue = (ValueDiscrete) bidHistory
|
---|
| 74 | .getHistory().get(j).getBid().getValues()
|
---|
| 75 | .get(bidIssueList.get(i).getNumber());
|
---|
| 76 | if (!valueList.contains(currentValue)) {
|
---|
| 77 | valueList.add(currentValue);
|
---|
| 78 | }
|
---|
| 79 | valueEvaluation[(valueList.indexOf(currentValue))] = valueEvaluation[valueList
|
---|
| 80 | .indexOf(currentValue)] + bidWeights[j];
|
---|
| 81 | }
|
---|
| 82 | int k = 0;
|
---|
| 83 | do {
|
---|
| 84 | try {
|
---|
| 85 | issueList.get(i).setEvaluationDouble(valueList.get(k),
|
---|
| 86 | valueEvaluation[k]);
|
---|
| 87 | } catch (Exception e) {
|
---|
| 88 | e.printStackTrace();
|
---|
| 89 | }
|
---|
| 90 | k++;
|
---|
| 91 | } while (valueEvaluation[k] != 0);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | // Returns the number of changes in values for each issue through the entire
|
---|
| 96 | // bid history
|
---|
| 97 | private int[] calculateNChanges() {
|
---|
| 98 | int[] nChanges = new int[nIssues];
|
---|
| 99 | Arrays.fill(nChanges, 1);
|
---|
| 100 | Value oldValue = null;
|
---|
| 101 | for (int i = 0; i < nIssues; i++) {
|
---|
| 102 | for (int j = 0; j < bidHistory.getHistory().size(); j++) {
|
---|
| 103 | try {
|
---|
| 104 | if (oldValue != null
|
---|
| 105 | && !oldValue.equals(bidHistory.getHistory().get(j)
|
---|
| 106 | .getBid().getValue(issueIds[i]))) {
|
---|
| 107 | nChanges[i]++;
|
---|
| 108 | }
|
---|
| 109 | oldValue = bidHistory.getHistory().get(j).getBid()
|
---|
| 110 | .getValue(issueIds[i]);
|
---|
| 111 | } catch (Exception e) {
|
---|
| 112 | e.printStackTrace();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | }
|
---|
| 116 | oldValue = null;
|
---|
| 117 | }
|
---|
| 118 | return nChanges;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // Calculates Issue i weight, based on the number of changes
|
---|
| 122 | private double calculateIssueWeight(int i, int[] nChanges) {
|
---|
| 123 | double weight = 0;
|
---|
| 124 | for (int j = 0; j < nIssues; j++) {
|
---|
| 125 | weight = weight + 1 / nChanges[j];
|
---|
| 126 | }
|
---|
| 127 | return 1 / nChanges[i] * 1 / weight;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // Gives a weight to each bid we received, the first bids are worth much
|
---|
| 131 | // more than the last ones
|
---|
| 132 | private double[] calculateBidWeights() {
|
---|
| 133 | int N = bidHistory.getHistory().size();
|
---|
| 134 | double factor = 0;
|
---|
| 135 | for (int i = 1; i <= N; i++) {
|
---|
| 136 | factor = factor + 1 / i;
|
---|
| 137 | }
|
---|
| 138 | double[] bidWeights = new double[N];
|
---|
| 139 | for (int i = 1; i <= N; i++) {
|
---|
| 140 | bidWeights[i - 1] = 1 / i * 1 / factor;
|
---|
| 141 | }
|
---|
| 142 | return bidWeights;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | }
|
---|