1 | /*
|
---|
2 | Team members:
|
---|
3 | Eden Hartman
|
---|
4 | Shalom Hassid
|
---|
5 | Gal Lev
|
---|
6 | Tidhar Suchard
|
---|
7 | Yoav Wizhendler
|
---|
8 | */
|
---|
9 |
|
---|
10 | package agents.anac.y2019.eagent;
|
---|
11 |
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.actions.Accept;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.Offer;
|
---|
16 | import genius.core.issue.Issue;
|
---|
17 | import genius.core.issue.IssueDiscrete;
|
---|
18 | import genius.core.issue.Value;
|
---|
19 | import genius.core.issue.ValueDiscrete;
|
---|
20 | import genius.core.parties.AbstractNegotiationParty;
|
---|
21 | import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
|
---|
22 | import genius.core.utility.AbstractUtilitySpace;
|
---|
23 |
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.Iterator;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Map;
|
---|
29 | import java.lang.Math;
|
---|
30 |
|
---|
31 | public class EAgent extends AbstractNegotiationParty {
|
---|
32 | public EAgent() {
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Action chooseAction(List<Class<? extends Action>> var1) {
|
---|
36 | if (this.getLastReceivedAction() instanceof Offer && this.hasPreferenceUncertainty()) {
|
---|
37 | Bid offerBid = ((Offer) this.getLastReceivedAction()).getBid();
|
---|
38 | if (this.utilitySpace.getUtility(offerBid) > 0.8) {
|
---|
39 | return new Accept(this.getPartyId(), offerBid);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | return new Offer(this.getPartyId(), this.generateSmartBid());
|
---|
44 | }
|
---|
45 |
|
---|
46 | private Bid generateSmartBid() {
|
---|
47 | try {
|
---|
48 | Bid maxBid = this.utilitySpace.getMaxUtilityBid();
|
---|
49 | return maxBid;
|
---|
50 | } catch (Exception e) {
|
---|
51 | e.printStackTrace();
|
---|
52 | return new Bid(utilitySpace.getDomain());
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private float calculateStdev(List<Integer> intList) {
|
---|
57 | // calculate average
|
---|
58 | int sum = 0;
|
---|
59 | Iterator intListIterator = intList.iterator();
|
---|
60 | while (intListIterator.hasNext()) {
|
---|
61 | sum += (int) intListIterator.next();
|
---|
62 | }
|
---|
63 | float avg = sum / intList.size();
|
---|
64 | // calculate and return standard deviation
|
---|
65 | float variance = 0;
|
---|
66 | intListIterator = intList.iterator();
|
---|
67 | while (intListIterator.hasNext()) {
|
---|
68 | variance += Math.pow(((int) intListIterator.next()) - avg, 2);
|
---|
69 | }
|
---|
70 | variance /= intList.size();
|
---|
71 | return (float)Math.sqrt(variance);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public AbstractUtilitySpace estimateUtilitySpace() {
|
---|
76 | AdditiveUtilitySpaceFactory utilitySpaceFactory = new AdditiveUtilitySpaceFactory(this.getDomain());
|
---|
77 | List issuesList = utilitySpaceFactory.getIssues();
|
---|
78 | Iterator issuesIterator = issuesList.iterator();
|
---|
79 | // number of bids in the bid ranking
|
---|
80 | int bidRankingSize = 0;
|
---|
81 | // utility map: the utility for each value of each issue is stored in this map
|
---|
82 | Map<String,Map<String, Float>> issuesValueToUtility = new HashMap<>();
|
---|
83 | // bid count map: the number of bids each value of each issue appears in the bid ranking is stored in this map
|
---|
84 | Map<String,Map<String, Integer>> issuesValueToBidCount = new HashMap<>();
|
---|
85 | // rank map: the ranks (scores) for each value of each issue is stored in this map
|
---|
86 | Map<String,Map<String, List<Integer>>> issuesValueToRanks = new HashMap<>();
|
---|
87 |
|
---|
88 | // initialize utility and bid count maps - iterate issues
|
---|
89 | while (issuesIterator.hasNext()) {
|
---|
90 | IssueDiscrete issueDiscrete = (IssueDiscrete) issuesIterator.next();
|
---|
91 | Iterator issueDiscreteValuesIterator = issueDiscrete.getValues().iterator();
|
---|
92 | Map<String, Float> valueToUtility = new HashMap<>();
|
---|
93 | Map<String, Integer> valueToBidCount = new HashMap<>();
|
---|
94 | Map<String, List<Integer>> valueToRanks = new HashMap<>();
|
---|
95 | // iterate values of issue
|
---|
96 | while (issueDiscreteValuesIterator.hasNext()) {
|
---|
97 | ValueDiscrete valueDiscrete = (ValueDiscrete) issueDiscreteValuesIterator.next();
|
---|
98 | valueToUtility.put(valueDiscrete.getValue(), (float) 0);
|
---|
99 | valueToBidCount.put(valueDiscrete.getValue(), 0);
|
---|
100 | List<Integer> rankList = new ArrayList<Integer>();
|
---|
101 | valueToRanks.put(valueDiscrete.getValue(), rankList);
|
---|
102 | }
|
---|
103 | issuesValueToUtility.put(issueDiscrete.getName(), valueToUtility);
|
---|
104 | issuesValueToBidCount.put(issueDiscrete.getName(), valueToBidCount);
|
---|
105 | issuesValueToRanks.put(issueDiscrete.getName(), valueToRanks);
|
---|
106 | }
|
---|
107 |
|
---|
108 | // calculate scores for the values (basic sum, without average or normalize), count bids for each value and fill rank (score) list for each value
|
---|
109 | Iterator bidOrderIterator = this.userModel.getBidRanking().getBidOrder().iterator();
|
---|
110 | int bidScore = 1;
|
---|
111 | int totalBidScores = 0;
|
---|
112 | // iterate bid order (preferences in ascending order - best is last)
|
---|
113 | while (bidOrderIterator.hasNext()) {
|
---|
114 | Bid bid = (Bid) bidOrderIterator.next();
|
---|
115 | // iterate issues
|
---|
116 | issuesIterator = issuesList.iterator();
|
---|
117 | while (issuesIterator.hasNext()) {
|
---|
118 | IssueDiscrete issueDiscrete = (IssueDiscrete) issuesIterator.next();
|
---|
119 | String valueStr = bid.getValue(issueDiscrete).toString();
|
---|
120 | // update utility of value
|
---|
121 | Map<String, Float> valueToUtility = issuesValueToUtility.get(issueDiscrete.getName());
|
---|
122 | float currScore = valueToUtility.get(valueStr);
|
---|
123 | valueToUtility.put(valueStr, currScore + bidScore);
|
---|
124 | // update bid count of value
|
---|
125 | Map<String, Integer> valueToBidCount = issuesValueToBidCount.get(issueDiscrete.getName());
|
---|
126 | int currBidCount = valueToBidCount.get(valueStr);
|
---|
127 | valueToBidCount.put(valueStr, currBidCount + 1);
|
---|
128 | // update rank list of value
|
---|
129 | Map<String, List<Integer>> valueToRanks = issuesValueToRanks.get(issueDiscrete.getName());
|
---|
130 | List<Integer> rankList = valueToRanks.get(valueStr);
|
---|
131 | rankList.add(bidScore);
|
---|
132 | }
|
---|
133 | totalBidScores += bidScore;
|
---|
134 | bidScore++;
|
---|
135 | }
|
---|
136 | bidRankingSize = bidScore - 1;
|
---|
137 |
|
---|
138 | // calculate average scores for the values, normalize them and set them in utility space
|
---|
139 | issuesIterator = issuesList.iterator();
|
---|
140 | while (issuesIterator.hasNext()) {
|
---|
141 | IssueDiscrete issueDiscrete = (IssueDiscrete) issuesIterator.next();
|
---|
142 | Map<String, Float> valueToUtility = issuesValueToUtility.get(issueDiscrete.getName());
|
---|
143 | Map<String, Integer> valueToBidCount = issuesValueToBidCount.get(issueDiscrete.getName());
|
---|
144 | float averageSumScore = totalBidScores / valueToUtility.size();
|
---|
145 | float bidRankingSizeDividedValuesSize = bidRankingSize / valueToUtility.size();
|
---|
146 | float maxNewSumScore = 0;
|
---|
147 | // iterate values of issue - calculate average scores for the values
|
---|
148 | Iterator issueDiscreteValuesIterator = issueDiscrete.getValues().iterator();
|
---|
149 | while (issueDiscreteValuesIterator.hasNext()) {
|
---|
150 | ValueDiscrete valueDiscrete = (ValueDiscrete) issueDiscreteValuesIterator.next();
|
---|
151 | int bidCount = valueToBidCount.get(valueDiscrete.getValue());
|
---|
152 | float newSumScore = 0;
|
---|
153 | if (bidCount > 0) {
|
---|
154 | float sumScore = valueToUtility.get(valueDiscrete.getValue());
|
---|
155 | if (bidCount > bidRankingSizeDividedValuesSize) {
|
---|
156 | newSumScore = sumScore / bidCount;
|
---|
157 | } else {
|
---|
158 | newSumScore = sumScore / bidRankingSizeDividedValuesSize;
|
---|
159 | }
|
---|
160 | } else {
|
---|
161 | newSumScore = averageSumScore / bidRankingSizeDividedValuesSize;
|
---|
162 | }
|
---|
163 | // update (average) utility of value
|
---|
164 | valueToUtility.put(valueDiscrete.getValue(), newSumScore);
|
---|
165 | if (newSumScore > maxNewSumScore) {
|
---|
166 | maxNewSumScore = newSumScore;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | // iterate values of issue - normalize scores and set them in utility space
|
---|
170 | Iterator issueDiscreteValuesIterator1 = issueDiscrete.getValues().iterator();
|
---|
171 | while (issueDiscreteValuesIterator1.hasNext()) {
|
---|
172 | ValueDiscrete valueDiscrete = (ValueDiscrete) issueDiscreteValuesIterator1.next();
|
---|
173 | float scoreNotNormalized = valueToUtility.get(valueDiscrete.getValue());
|
---|
174 | float scoreNormalized = scoreNotNormalized / maxNewSumScore;
|
---|
175 | // update (normalize) utility of value
|
---|
176 | valueToUtility.put(valueDiscrete.getValue(), scoreNormalized);
|
---|
177 | // set final score in utility space
|
---|
178 | utilitySpaceFactory.setUtility(issueDiscrete, valueDiscrete, scoreNormalized);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | // calculate the weights of the issues and set them in utility space
|
---|
183 | Map<String, Float> issueToStdevAvgInverse = new HashMap<>();
|
---|
184 | float sumStdevAvgInverse = 0;
|
---|
185 | issuesIterator = issuesList.iterator();
|
---|
186 | while (issuesIterator.hasNext()) {
|
---|
187 | IssueDiscrete issueDiscrete = (IssueDiscrete) issuesIterator.next();
|
---|
188 | Map<String, List<Integer>> valueToRanks = issuesValueToRanks.get(issueDiscrete.getName());
|
---|
189 | float stdevSum = 0;
|
---|
190 | // iterate values of issue - calculate stdev sum for the values
|
---|
191 | Iterator issueDiscreteValuesIterator = issueDiscrete.getValues().iterator();
|
---|
192 | while (issueDiscreteValuesIterator.hasNext()) {
|
---|
193 | ValueDiscrete valueDiscrete = (ValueDiscrete) issueDiscreteValuesIterator.next();
|
---|
194 | List<Integer> rankList = valueToRanks.get(valueDiscrete.getValue());
|
---|
195 | if (rankList.size() > 0) {
|
---|
196 | stdevSum += calculateStdev(rankList);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | float stdevAvg = stdevSum / valueToRanks.size();
|
---|
200 | double stdevAvgInverse = Math.pow(stdevAvg, -1);
|
---|
201 | issueToStdevAvgInverse.put(issueDiscrete.getName(), (float) stdevAvgInverse);
|
---|
202 | sumStdevAvgInverse += stdevAvgInverse;
|
---|
203 | }
|
---|
204 | issuesIterator = issuesList.iterator();
|
---|
205 | while (issuesIterator.hasNext()) {
|
---|
206 | IssueDiscrete issueDiscrete = (IssueDiscrete) issuesIterator.next();
|
---|
207 | float stdevAvgInverse = issueToStdevAvgInverse.get(issueDiscrete.getName());
|
---|
208 | // set final weight of issue in utility space
|
---|
209 | utilitySpaceFactory.setWeight(issueDiscrete, stdevAvgInverse / sumStdevAvgInverse);
|
---|
210 | }
|
---|
211 |
|
---|
212 | utilitySpaceFactory.scaleAllValuesFrom0To1();
|
---|
213 | return utilitySpaceFactory.getUtilitySpace();
|
---|
214 | }
|
---|
215 |
|
---|
216 | public String getDescription() {
|
---|
217 | return "Our negotiation agent for uncertain preferences";
|
---|
218 | }
|
---|
219 | }
|
---|