[1] | 1 | package agents.ai2014.group12;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.HashMap;
|
---|
| 5 | import java.util.List;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 |
|
---|
| 8 | import genius.core.Bid;
|
---|
| 9 | import genius.core.Domain;
|
---|
| 10 | import genius.core.issue.IssueDiscrete;
|
---|
| 11 | import genius.core.issue.Objective;
|
---|
| 12 | import genius.core.issue.Value;
|
---|
| 13 | import genius.core.issue.ValueDiscrete;
|
---|
| 14 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
| 15 | import genius.core.utility.Evaluator;
|
---|
| 16 | import genius.core.utility.EvaluatorDiscrete;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * The Preference class will hold the issues, their weights and for each issue
|
---|
| 20 | * their values and the order of these values. Only compatible with
|
---|
| 21 | * {@link AdditiveUtilitySpace}.
|
---|
| 22 | *
|
---|
| 23 | */
|
---|
| 24 | public class Preference {
|
---|
| 25 | ArrayList<PreferenceBlock> preferenceList = new ArrayList<PreferenceBlock>();
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * First constructor for the Preference class, this is to create a
|
---|
| 29 | * Preference profile for the agent that is currently in turn. This
|
---|
| 30 | * Preference object will not be updated. The Utilityspace is used to fill
|
---|
| 31 | * the values of each issue and their order.
|
---|
| 32 | *
|
---|
| 33 | * @param utilitySpace
|
---|
| 34 | * of the player in turn
|
---|
| 35 | */
|
---|
| 36 | public Preference(AdditiveUtilitySpace utilitySpace) {
|
---|
| 37 | Domain domain = utilitySpace.getDomain();
|
---|
| 38 | int size = utilitySpace.getEvaluators().size();
|
---|
| 39 | System.out.println("size: " + size);
|
---|
| 40 | List<Objective> objectives = domain.getObjectives();
|
---|
| 41 | for (int i = 1; i <= size; i++) {
|
---|
| 42 | IssueDiscrete issue = (IssueDiscrete) objectives.get(i);
|
---|
| 43 | Double weight = utilitySpace.getWeight(i);
|
---|
| 44 | Evaluator evalor = utilitySpace.getEvaluator(i);
|
---|
| 45 | EvaluatorDiscrete evalorDis = (EvaluatorDiscrete) evalor;
|
---|
| 46 | // Define each issue with every weight attached
|
---|
| 47 | PreferenceBlock prefBlock = new PreferenceBlock(evalorDis,
|
---|
| 48 | issue.getName(), weight);
|
---|
| 49 | prefBlock.normalizeNodeValues();
|
---|
| 50 | Node highest = prefBlock.getHighestPreference();
|
---|
| 51 | highest.setFlag(true);
|
---|
| 52 | preferenceList.add(prefBlock);
|
---|
| 53 | }
|
---|
| 54 | // Possibility to randomize the other values of each issue.
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * This constructor is used to create a Preference object for an Agent of
|
---|
| 59 | * which you have received a bid for the first time. The utilitySpace of the
|
---|
| 60 | * agent receiving the message will be used for the initial issue, value and
|
---|
| 61 | * order setup. This will be later updated using the bids of the agent to
|
---|
| 62 | * which this preference belongs to.
|
---|
| 63 | *
|
---|
| 64 | * @param utilitySpace
|
---|
| 65 | * of the agent receiving the message
|
---|
| 66 | * @param bid
|
---|
| 67 | */
|
---|
| 68 | public Preference(AdditiveUtilitySpace utilitySpace, Bid bid) {
|
---|
| 69 | Domain domain = utilitySpace.getDomain();
|
---|
| 70 | int size = utilitySpace.getEvaluators().size();
|
---|
| 71 | System.out.println("size: " + size);
|
---|
| 72 | List<Objective> objectives = domain.getObjectives();
|
---|
| 73 | for (int i = 1; i <= size; i++) {
|
---|
| 74 | IssueDiscrete issue = (IssueDiscrete) objectives.get(i);
|
---|
| 75 | double weight = 1.0 / size;
|
---|
| 76 | Evaluator evalor = utilitySpace.getEvaluator(i);
|
---|
| 77 | System.out.println(evalor);
|
---|
| 78 | EvaluatorDiscrete evalorDis = (EvaluatorDiscrete) evalor;
|
---|
| 79 | // Define an issue with all weights the same
|
---|
| 80 | PreferenceBlock prefBlock = new PreferenceBlock(evalorDis,
|
---|
| 81 | issue.getName(), weight);
|
---|
| 82 | prefBlock.normalizeNodeValues();
|
---|
| 83 | Node highest = prefBlock.getHighestPreference();
|
---|
| 84 | highest.setFlag(true);
|
---|
| 85 | preferenceList.add(prefBlock);
|
---|
| 86 | }
|
---|
| 87 | setHighestIssuePreference(bid);
|
---|
| 88 | // Possibility to randomize the other values of each issue.
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Updates the preference of a party given the current bid. It considers the
|
---|
| 93 | * first bid to be the agents highest utility bid. It will not take into
|
---|
| 94 | * account the nodes flagged value for this first bid.
|
---|
| 95 | *
|
---|
| 96 | * @param bid
|
---|
| 97 | */
|
---|
| 98 | public void setHighestIssuePreference(Bid bid) {
|
---|
| 99 | HashMap<Integer, Value> values = bid.getValues();
|
---|
| 100 | for (int i = 1; i <= values.size(); i++) {
|
---|
| 101 | ValueDiscrete value = (ValueDiscrete) values.get(i);
|
---|
| 102 | PreferenceBlock preferBlock = preferenceList.get(i - 1);
|
---|
| 103 | Node currentHighest = preferBlock.getHighestPreference();
|
---|
| 104 | if (!currentHighest.getName().equals(value.getValue())) {
|
---|
| 105 | // preferBlock.orderNodesLowToHigh();
|
---|
| 106 | ArrayList<Node> nodes = preferBlock.nodeList;
|
---|
| 107 |
|
---|
| 108 | int index = preferBlock.indexOf(value.getValue());
|
---|
| 109 |
|
---|
| 110 | for (int j = index; j < nodes.size() - 1; j++) {
|
---|
| 111 | nodes.get(j).setName(nodes.get(j + 1).getName());
|
---|
| 112 | }
|
---|
| 113 | nodes.get(nodes.size() - 1).setName(value.getValue());
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * This method updates the preference order of an already initialized
|
---|
| 120 | * preference object This will used the flagged status to determine what
|
---|
| 121 | * nodes have alreay been set. It will determine what values have been
|
---|
| 122 | * changed and were not yet flagged and will set those in the appropriate
|
---|
| 123 | * order and update their flagged status.
|
---|
| 124 | *
|
---|
| 125 | * @param bid
|
---|
| 126 | */
|
---|
| 127 | public void updatePreferenceOrder(Bid bid) {
|
---|
| 128 | // First update the sequence of the values of an issue
|
---|
| 129 | HashMap<Integer, Value> values = bid.getValues();
|
---|
| 130 | for (int i = 1; i <= values.size(); i++) {
|
---|
| 131 | ValueDiscrete value = (ValueDiscrete) values.get(i);
|
---|
| 132 | PreferenceBlock preferBlock = preferenceList.get(i - 1);
|
---|
| 133 | ArrayList<Node> nodesWithoutFlag = preferBlock
|
---|
| 134 | .getValuesWithoutFlag();
|
---|
| 135 | for (int j = 0; i < nodesWithoutFlag.size(); i++) {
|
---|
| 136 | // If not flagged yet, flag it and set it to the next value
|
---|
| 137 | // after the last flagged
|
---|
| 138 | if (nodesWithoutFlag.get(i).getName().equals(value.getValue())) {
|
---|
| 139 | int highestIndexWithoutFlag = preferBlock
|
---|
| 140 | .getHighestIndexWithoutFlag();
|
---|
| 141 |
|
---|
| 142 | ArrayList<Node> nodes = preferBlock.nodeList;
|
---|
| 143 | int index = preferBlock.indexOf(value.getValue());
|
---|
| 144 |
|
---|
| 145 | for (int k = index; k < nodes.size() - 1
|
---|
| 146 | && !nodes.get(k + 1).getFlag(); k++) {
|
---|
| 147 | nodes.get(k).setName(nodes.get(k + 1).getName());
|
---|
| 148 | }
|
---|
| 149 | nodes.get(highestIndexWithoutFlag)
|
---|
| 150 | .setName(value.getValue());
|
---|
| 151 | nodes.get(highestIndexWithoutFlag).setFlag(true);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * This method updates the issue weights given the previous bid and the new
|
---|
| 159 | * bid. Each weight of an issue that has been changed will be decreased and
|
---|
| 160 | * then normalized.
|
---|
| 161 | *
|
---|
| 162 | * @param previousBids
|
---|
| 163 | * @param newBid
|
---|
| 164 | */
|
---|
| 165 | public void updateIssueWeights(ArrayList<Bid> previousBids, Bid newBid) {
|
---|
| 166 | Bid previousBid = previousBids.get(previousBids.size() - 1);
|
---|
| 167 | ArrayList<Integer> changedIssues = getChangedIndices(previousBid,
|
---|
| 168 | newBid);
|
---|
| 169 |
|
---|
| 170 | // Decrease the weights that have been changed
|
---|
| 171 | for (int i = 0; i < changedIssues.size(); i++) {
|
---|
| 172 | preferenceList.get(changedIssues.get(i)).weight -= 0.50 / changedIssues
|
---|
| 173 | .size();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | // Sum the weights
|
---|
| 177 | double sum = 0.0;
|
---|
| 178 | for (int i = 0; i < preferenceList.size(); i++) {
|
---|
| 179 | sum += preferenceList.get(i).weight;
|
---|
| 180 | }
|
---|
| 181 | // Normalize the weights
|
---|
| 182 | for (int i = 0; i < preferenceList.size(); i++) {
|
---|
| 183 | preferenceList.get(i).weight = preferenceList.get(i).weight / sum;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Method that returns the changed indices by comparing the previous bid and
|
---|
| 189 | * the new bid
|
---|
| 190 | *
|
---|
| 191 | * @param previousBid
|
---|
| 192 | * @param newBid
|
---|
| 193 | * @return list of integers with the changed indices
|
---|
| 194 | */
|
---|
| 195 | public ArrayList<Integer> getChangedIndices(Bid previousBid, Bid newBid) {
|
---|
| 196 | ArrayList<Integer> indices = new ArrayList<Integer>();
|
---|
| 197 | HashMap<Integer, Value> valuesNewBid = newBid.getValues();
|
---|
| 198 | HashMap<Integer, Value> valuesPreviousBid = previousBid.getValues();
|
---|
| 199 | for (int i = 1; i <= valuesNewBid.size(); i++) {
|
---|
| 200 | ValueDiscrete valueNewBid = (ValueDiscrete) valuesNewBid.get(i);
|
---|
| 201 | ValueDiscrete valuePreviousBid = (ValueDiscrete) valuesPreviousBid
|
---|
| 202 | .get(i);
|
---|
| 203 | if (!valueNewBid.getValue().equals(valuePreviousBid.getValue())) {
|
---|
| 204 | // i-1 to get the right index in the preferenceList
|
---|
| 205 | indices.add(i - 1);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | return indices;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * Calculates the utility value for a bid given the current preference
|
---|
| 213 | * profile. This gives the agent the possibility to calculate other agents
|
---|
| 214 | * utilities without knowing their exact preference profile.
|
---|
| 215 | *
|
---|
| 216 | * @param bid
|
---|
| 217 | * @return double
|
---|
| 218 | */
|
---|
| 219 | public double getUtilityValue(Bid bid) {
|
---|
| 220 | HashMap<Integer, Value> bidvalues = bid.getValues();
|
---|
| 221 | double returnUtility = 0;
|
---|
| 222 | for (PreferenceBlock block : preferenceList) {
|
---|
| 223 | double weight = block.weight;
|
---|
| 224 | for (Map.Entry<Integer, Value> entry : bidvalues.entrySet()) {
|
---|
| 225 | Value value = entry.getValue();
|
---|
| 226 | for (Node node : block.nodeList) {
|
---|
| 227 | if (node.getName() == value.toString()) {
|
---|
| 228 | returnUtility += node.getValue() * weight;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | return returnUtility;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|