1 | package parties.in4010.q12015.group16;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import genius.core.AgentID;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.bidding.BidDetails;
|
---|
11 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
12 | import genius.core.issue.Issue;
|
---|
13 | import genius.core.issue.ValueDiscrete;
|
---|
14 | import genius.core.misc.Range;
|
---|
15 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
16 | import genius.core.utility.EvaluatorDiscrete;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Model representing the 'average' of all negotiators during a session. This is
|
---|
20 | * done by combining all opponent utility spaces and our own utility space
|
---|
21 | * halfway through the negotiation into one general utility space. This
|
---|
22 | * combining is done by averaging all issue weights and values in all spaces,
|
---|
23 | * and putting the average values in the general utility space.
|
---|
24 | */
|
---|
25 | public class GeneralModel {
|
---|
26 | private HashMap<AgentID, Opponent> opponentList;
|
---|
27 |
|
---|
28 | private int i = -1;
|
---|
29 |
|
---|
30 | private AdditiveUtilitySpace utilSpaceAgent16;
|
---|
31 | private AdditiveUtilitySpace utilSpaceGeneral;
|
---|
32 | private SortedOutcomeSpace outcomeSpace;
|
---|
33 |
|
---|
34 | private ArrayList<AgentID> opponentIDs;
|
---|
35 | private ArrayList<Bid> proposals;
|
---|
36 |
|
---|
37 | public boolean generated = false;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @param utilSpace
|
---|
41 | * utility space of Group16
|
---|
42 | * @param sortedOutcomeSpace
|
---|
43 | * all possible bids sorted according to utilSpace
|
---|
44 | */
|
---|
45 | public GeneralModel(AdditiveUtilitySpace utilSpace,
|
---|
46 | SortedOutcomeSpace sortedOutcomeSpace) {
|
---|
47 | utilSpaceAgent16 = utilSpace;
|
---|
48 | // Create general utility space by copying ours; otherwise we can't do
|
---|
49 | // 'new SortedOutcomeSpace(utilSpaceGeneral)' in 'generate()'
|
---|
50 | utilSpaceGeneral = new AdditiveUtilitySpace(utilSpaceAgent16);
|
---|
51 | outcomeSpace = sortedOutcomeSpace;
|
---|
52 |
|
---|
53 | opponentList = new HashMap<AgentID, Opponent>();
|
---|
54 | opponentIDs = new ArrayList<AgentID>();
|
---|
55 | proposals = new ArrayList<Bid>();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Retrieve the indicated agent when a bid is received, so that this bid can
|
---|
60 | * be added to the correct opponent's bidList.
|
---|
61 | *
|
---|
62 | * @param opponentID
|
---|
63 | * ID of opponent to retrieve
|
---|
64 | * @return opponent indicated with opponentID
|
---|
65 | */
|
---|
66 | public Opponent getOpponent(AgentID opponentID) {
|
---|
67 | if (opponentList.get(opponentID) != null) {
|
---|
68 | return opponentList.get(opponentID);
|
---|
69 | } else if (opponentList.get(opponentID) == null) {
|
---|
70 | opponentList.put(opponentID, new Opponent(utilSpaceGeneral,
|
---|
71 | utilSpaceAgent16));
|
---|
72 | opponentIDs.add(opponentID);
|
---|
73 | return opponentList.get(opponentID);
|
---|
74 | } else {
|
---|
75 | return null;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Generates a table of proposals when the normalized time = 0.5.
|
---|
81 | */
|
---|
82 | public void generate() {
|
---|
83 | calculateOpponentsValues();
|
---|
84 | combineUtilitySpaces();
|
---|
85 | getBidsForOwnOutcomeSpace();
|
---|
86 | setGenerated(true);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Calls the value calculation method for all opponents.
|
---|
91 | */
|
---|
92 | private void calculateOpponentsValues() {
|
---|
93 | for (int i = 0; i < opponentIDs.size(); i++) {
|
---|
94 | try {
|
---|
95 | opponentList.get(opponentIDs.get(i)).calculateValues();
|
---|
96 | } catch (Exception e) {
|
---|
97 | e.printStackTrace();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Creates a single utility space out of all opponent utility spaces by
|
---|
104 | * averaging issue weights and values.
|
---|
105 | */
|
---|
106 | private void combineUtilitySpaces() {
|
---|
107 | AdditiveUtilitySpace opponentUtilitySpace = null;
|
---|
108 | double sumOfWeights = 0;
|
---|
109 | double weight = 0;
|
---|
110 | for (Issue issue : utilSpaceGeneral.getDomain().getIssues()) {
|
---|
111 | for (int i = 0; i < opponentIDs.size(); i++) {
|
---|
112 | opponentUtilitySpace = opponentList.get(opponentIDs.get(i))
|
---|
113 | .getUtilSpace();
|
---|
114 |
|
---|
115 | // Average issue weights
|
---|
116 | weight = utilSpaceGeneral.getWeight(issue.getNumber())
|
---|
117 | + opponentUtilitySpace.getWeight(issue.getNumber());
|
---|
118 | utilSpaceGeneral.setWeight(issue, weight);
|
---|
119 | sumOfWeights += weight;
|
---|
120 |
|
---|
121 | // Average values
|
---|
122 | EvaluatorDiscrete evOpp = (EvaluatorDiscrete) opponentUtilitySpace
|
---|
123 | .getEvaluator(issue.getNumber());
|
---|
124 | Set<ValueDiscrete> valueList = evOpp.getValues();
|
---|
125 | for (ValueDiscrete val : valueList) {
|
---|
126 | // Get evaluation for general model
|
---|
127 | EvaluatorDiscrete evGen = (EvaluatorDiscrete) utilSpaceGeneral
|
---|
128 | .getEvaluator(issue.getNumber());
|
---|
129 | // Sum evaluations and update evaluation for general model
|
---|
130 | evGen.addEvaluation(val,
|
---|
131 | (evGen.getValue(val) + evOpp.getValue(val)));
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Divide all issue weights by (#opponents * sum of all issue weights)
|
---|
137 | double divisionFactor = opponentIDs.size() * sumOfWeights;
|
---|
138 | for (Issue issue : utilSpaceGeneral.getDomain().getIssues()) {
|
---|
139 | utilSpaceGeneral.setWeight(issue,
|
---|
140 | utilSpaceGeneral.getWeight(issue.getNumber())
|
---|
141 | / divisionFactor);
|
---|
142 | }
|
---|
143 |
|
---|
144 | // TODO: include utilSpaceGeneral.isComplete()
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Fills the proposals field by sorting halfOfAllBids according to our
|
---|
149 | * utility space.
|
---|
150 | */
|
---|
151 | private void getBidsForOwnOutcomeSpace() {
|
---|
152 | // Order the table to our utility profile, select all bids above our
|
---|
153 | // reservation value
|
---|
154 | // and add them to proposals
|
---|
155 | List<BidDetails> bids = outcomeSpace.getBidsinRange(new Range(
|
---|
156 | utilSpaceAgent16.getReservationValue(), 1));
|
---|
157 | Bid bid;
|
---|
158 | try {
|
---|
159 | for (BidDetails bidDetails : bids) {
|
---|
160 | bid = bidDetails.getBid();
|
---|
161 | if (utilSpaceGeneral.getUtility(bid) >= 0.5) {
|
---|
162 | proposals.add(bid);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | } catch (Exception e) {
|
---|
166 | e.printStackTrace();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Increments the index counter used to access the proposals field, and
|
---|
172 | * returns the bid at that index if it exists or null otherwise.
|
---|
173 | *
|
---|
174 | * @return bid from proposals
|
---|
175 | */
|
---|
176 | public Bid getProposal() {
|
---|
177 | Bid proposal = null;
|
---|
178 | i++;
|
---|
179 | if (i < proposals.size()) {
|
---|
180 | proposal = proposals.get(i);
|
---|
181 | }
|
---|
182 | return proposal;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public AdditiveUtilitySpace getUtilSpaceGeneral() {
|
---|
186 | return utilSpaceGeneral;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public boolean isGenerated() {
|
---|
190 | return generated;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void setGenerated(boolean generated) {
|
---|
194 | this.generated = generated;
|
---|
195 | }
|
---|
196 | }
|
---|