package agents.ai2014.group4; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import genius.core.AgentID; import genius.core.Bid; import genius.core.Domain; import genius.core.actions.Accept; import genius.core.actions.Action; import genius.core.actions.DefaultAction; import genius.core.actions.Offer; import genius.core.issue.Issue; import genius.core.issue.IssueDiscrete; import genius.core.issue.ValueDiscrete; public class Party { private String name; private ArrayList issueModels = new ArrayList(); private double rateOfChange = 0.1; private HashMap> accepted = new HashMap>(); private AgentID agentid; /** * * @param name * @param domain * @param agentid * the agent id of the party represented */ public Party(String name, Domain domain, AgentID agentid) { this.name = name; createIssueModels(domain); this.agentid = agentid; } private void createIssueModels(Domain domain) { for (Issue issue : domain.getIssues()) { List values = ((IssueDiscrete) issue).getValues(); IssueModel im = new IssueModel(issue.getName(), values); issueModels.add(im); accepted.put(im, new ArrayList()); for (int i = 0; i < values.size(); i++) { accepted.get(im).add(0); } } } public void updateWithBid(Bid bid, Action action) { // Update utility for (IssueModel issue : issueModels) { for (Issue iss : bid.getIssues()) { if (issue.getName().equals(iss.getName())) { ValueDiscrete value = new ValueDiscrete(""); int i = iss.getNumber() - 1; try { value = (ValueDiscrete) bid.getValue(i + 1); } catch (Exception e) { e.printStackTrace(); } if (action instanceof Accept) { issue.updateUtility(value, rateOfChange); List acc = accepted.get(issue); int j = 0; for (ValueDiscrete v : issue.getValues()) { try { if (v.getValue().equals( bid.getValue(i + 1).toString())) { acc.set(j, acc.get(j) + 1); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } j++; } accepted.put(issue, acc); } else if (action instanceof Offer) { issue.updateUtility(value, -rateOfChange); Bid newBid = DefaultAction.getBidFromAction(action); updateWithBid(newBid, new Accept(agentid, newBid)); List acc = accepted.get(issue); int j = 0; for (ValueDiscrete v : issue.getValues()) { try { if (v.getValue().equals( bid.getValue(i + 1).toString())) { acc.set(j, acc.get(j) - 1); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } j++; } accepted.put(issue, acc); } break; } } } // update weights double sumSpread = 0; double spread = 0; for (IssueModel issue : issueModels) { double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; for (int p : accepted.get(issue)) { max = Math.max(max, p); min = Math.min(min, p); } spread = max - min; sumSpread = sumSpread + spread; } for (IssueModel issue : issueModels) { double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; for (int p : accepted.get(issue)) { max = Math.max(max, p); min = Math.min(min, p); } spread = max - min; issue.setValue(spread / sumSpread); } } public Double estimateUtility(Bid bid) { double utility = 0; for (IssueModel issue : issueModels) { double weight = issue.getValue(); ValueDiscrete value = new ValueDiscrete(""); for (Issue iss : bid.getIssues()) { if (issue.getName() == iss.getName()) { int i = iss.getNumber(); try { value = (ValueDiscrete) bid.getValue(i); } catch (Exception e) { e.printStackTrace(); } break; } } double util = issue.getUtility(value); utility = utility + util * weight; } return utility; } public ArrayList getIssueModels() { return issueModels; } public void setIssueModels(ArrayList issueModels) { this.issueModels = issueModels; } }