[1] | 1 | package agents.ai2014.group4;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.HashMap;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | import genius.core.AgentID;
|
---|
| 8 | import genius.core.Bid;
|
---|
| 9 | import genius.core.Domain;
|
---|
| 10 | import genius.core.actions.Accept;
|
---|
| 11 | import genius.core.actions.Action;
|
---|
| 12 | import genius.core.actions.DefaultAction;
|
---|
| 13 | import genius.core.actions.Offer;
|
---|
| 14 | import genius.core.issue.Issue;
|
---|
| 15 | import genius.core.issue.IssueDiscrete;
|
---|
| 16 | import genius.core.issue.ValueDiscrete;
|
---|
| 17 |
|
---|
| 18 | public class Party {
|
---|
| 19 |
|
---|
| 20 | private String name;
|
---|
| 21 | private ArrayList<IssueModel> issueModels = new ArrayList<IssueModel>();
|
---|
| 22 | private double rateOfChange = 0.1;
|
---|
| 23 | private HashMap<IssueModel, List<Integer>> accepted = new HashMap<IssueModel, List<Integer>>();
|
---|
| 24 | private AgentID agentid;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | *
|
---|
| 28 | * @param name
|
---|
| 29 | * @param domain
|
---|
| 30 | * @param agentid
|
---|
| 31 | * the agent id of the party represented
|
---|
| 32 | */
|
---|
| 33 | public Party(String name, Domain domain, AgentID agentid) {
|
---|
| 34 | this.name = name;
|
---|
| 35 | createIssueModels(domain);
|
---|
| 36 | this.agentid = agentid;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | private void createIssueModels(Domain domain) {
|
---|
| 40 | for (Issue issue : domain.getIssues()) {
|
---|
| 41 | List<ValueDiscrete> values = ((IssueDiscrete) issue).getValues();
|
---|
| 42 | IssueModel im = new IssueModel(issue.getName(), values);
|
---|
| 43 | issueModels.add(im);
|
---|
| 44 | accepted.put(im, new ArrayList<Integer>());
|
---|
| 45 | for (int i = 0; i < values.size(); i++) {
|
---|
| 46 | accepted.get(im).add(0);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void updateWithBid(Bid bid, Action action) {
|
---|
| 53 | // Update utility
|
---|
| 54 | for (IssueModel issue : issueModels) {
|
---|
| 55 | for (Issue iss : bid.getIssues()) {
|
---|
| 56 | if (issue.getName().equals(iss.getName())) {
|
---|
| 57 | ValueDiscrete value = new ValueDiscrete("");
|
---|
| 58 | int i = iss.getNumber() - 1;
|
---|
| 59 | try {
|
---|
| 60 | value = (ValueDiscrete) bid.getValue(i + 1);
|
---|
| 61 | } catch (Exception e) {
|
---|
| 62 | e.printStackTrace();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | if (action instanceof Accept) {
|
---|
| 66 | issue.updateUtility(value, rateOfChange);
|
---|
| 67 |
|
---|
| 68 | List<Integer> acc = accepted.get(issue);
|
---|
| 69 | int j = 0;
|
---|
| 70 | for (ValueDiscrete v : issue.getValues()) {
|
---|
| 71 | try {
|
---|
| 72 | if (v.getValue().equals(
|
---|
| 73 | bid.getValue(i + 1).toString())) {
|
---|
| 74 | acc.set(j, acc.get(j) + 1);
|
---|
| 75 | }
|
---|
| 76 | } catch (Exception e) {
|
---|
| 77 | // TODO Auto-generated catch block
|
---|
| 78 | e.printStackTrace();
|
---|
| 79 | }
|
---|
| 80 | j++;
|
---|
| 81 | }
|
---|
| 82 | accepted.put(issue, acc);
|
---|
| 83 | } else if (action instanceof Offer) {
|
---|
| 84 | issue.updateUtility(value, -rateOfChange);
|
---|
| 85 | Bid newBid = DefaultAction.getBidFromAction(action);
|
---|
| 86 | updateWithBid(newBid, new Accept(agentid, newBid));
|
---|
| 87 |
|
---|
| 88 | List<Integer> acc = accepted.get(issue);
|
---|
| 89 | int j = 0;
|
---|
| 90 | for (ValueDiscrete v : issue.getValues()) {
|
---|
| 91 | try {
|
---|
| 92 | if (v.getValue().equals(
|
---|
| 93 | bid.getValue(i + 1).toString())) {
|
---|
| 94 | acc.set(j, acc.get(j) - 1);
|
---|
| 95 | }
|
---|
| 96 | } catch (Exception e) {
|
---|
| 97 | // TODO Auto-generated catch block
|
---|
| 98 | e.printStackTrace();
|
---|
| 99 | }
|
---|
| 100 | j++;
|
---|
| 101 | }
|
---|
| 102 | accepted.put(issue, acc);
|
---|
| 103 | }
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // update weights
|
---|
| 110 | double sumSpread = 0;
|
---|
| 111 | double spread = 0;
|
---|
| 112 | for (IssueModel issue : issueModels) {
|
---|
| 113 | double max = Double.NEGATIVE_INFINITY;
|
---|
| 114 | double min = Double.POSITIVE_INFINITY;
|
---|
| 115 | for (int p : accepted.get(issue)) {
|
---|
| 116 | max = Math.max(max, p);
|
---|
| 117 | min = Math.min(min, p);
|
---|
| 118 | }
|
---|
| 119 | spread = max - min;
|
---|
| 120 | sumSpread = sumSpread + spread;
|
---|
| 121 | }
|
---|
| 122 | for (IssueModel issue : issueModels) {
|
---|
| 123 | double max = Double.NEGATIVE_INFINITY;
|
---|
| 124 | double min = Double.POSITIVE_INFINITY;
|
---|
| 125 | for (int p : accepted.get(issue)) {
|
---|
| 126 | max = Math.max(max, p);
|
---|
| 127 | min = Math.min(min, p);
|
---|
| 128 | }
|
---|
| 129 | spread = max - min;
|
---|
| 130 | issue.setValue(spread / sumSpread);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public Double estimateUtility(Bid bid) {
|
---|
| 135 | double utility = 0;
|
---|
| 136 |
|
---|
| 137 | for (IssueModel issue : issueModels) {
|
---|
| 138 | double weight = issue.getValue();
|
---|
| 139 | ValueDiscrete value = new ValueDiscrete("");
|
---|
| 140 | for (Issue iss : bid.getIssues()) {
|
---|
| 141 | if (issue.getName() == iss.getName()) {
|
---|
| 142 | int i = iss.getNumber();
|
---|
| 143 | try {
|
---|
| 144 | value = (ValueDiscrete) bid.getValue(i);
|
---|
| 145 | } catch (Exception e) {
|
---|
| 146 | e.printStackTrace();
|
---|
| 147 | }
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | double util = issue.getUtility(value);
|
---|
| 152 | utility = utility + util * weight;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return utility;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public ArrayList<IssueModel> getIssueModels() {
|
---|
| 159 | return issueModels;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | public void setIssueModels(ArrayList<IssueModel> issueModels) {
|
---|
| 163 | this.issueModels = issueModels;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | }
|
---|