1 | package agents.anac.y2019.ibasic.boacomponents;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 |
|
---|
12 | import genius.core.bidding.BidDetails;
|
---|
13 | import genius.core.boaframework.*;
|
---|
14 | import genius.core.utility.AbstractUtilitySpace;
|
---|
15 | import genius.core.uncertainty.UserModel;
|
---|
16 | import genius.core.misc.Range;
|
---|
17 |
|
---|
18 |
|
---|
19 | public class IBasicOMStrategy extends OMStrategy {
|
---|
20 |
|
---|
21 | double updateThreshold = 1.1;
|
---|
22 | private Map<BidDetails, Integer> bidlistfreq;
|
---|
23 | AbstractUtilitySpace utilSpace;
|
---|
24 | UserModel usermodel;
|
---|
25 | private SortedOutcomeSpace outcomespace;
|
---|
26 | List<BidDetails> allBids;
|
---|
27 | Map<BidDetails, Integer> finalbids;
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public void init(NegotiationSession negotiationSession, OpponentModel model, Map<String, Double> parameters) {
|
---|
31 | super.init(negotiationSession, model, parameters);
|
---|
32 | if (parameters.get("t") != null) {
|
---|
33 | updateThreshold = parameters.get("t").doubleValue();
|
---|
34 | } else {
|
---|
35 | System.out.println("OMStrategy assumed t = 1.1");
|
---|
36 | }
|
---|
37 | //Initializes the usermodel, utility space and reservation value.
|
---|
38 | this.usermodel = negotiationSession.getUserModel();
|
---|
39 | this.utilSpace = negotiationSession.getUtilitySpace();
|
---|
40 |
|
---|
41 | //If we operate under preference uncertainty, we recalculate the utility space.
|
---|
42 | if (usermodel != null)
|
---|
43 | this.utilSpace = IBasicPU.createUtilitySpace(negotiationSession.getDomain(), usermodel);
|
---|
44 |
|
---|
45 | //Sets the outcomespace with the respective utility space
|
---|
46 | this.outcomespace = new SortedOutcomeSpace(utilSpace);
|
---|
47 | negotiationSession.setOutcomeSpace(outcomespace);
|
---|
48 | this.bidlistfreq = new HashMap<BidDetails, Integer>();
|
---|
49 | initbidlist();
|
---|
50 | finalbids = new HashMap<BidDetails, Integer>();
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 | //Initializes all of the bids
|
---|
55 | private void initbidlist() {
|
---|
56 | allBids = this.outcomespace.getAllOutcomes();
|
---|
57 | for( BidDetails bids : allBids) {
|
---|
58 | this.bidlistfreq.put(bids, 0);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | //Updates the frequency recording list of our bids
|
---|
63 | private void Updatebidslist(BidDetails bid) {
|
---|
64 | int i;
|
---|
65 |
|
---|
66 | for(BidDetails key : bidlistfreq.keySet()) {
|
---|
67 | if(key.equals(bid)){
|
---|
68 | i = this.bidlistfreq.get(key);
|
---|
69 | this.bidlistfreq.replace(key, i += 2);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public BidDetails getBid(List<BidDetails> allBids) {
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | //Returns the best bid from the given list according to the stage we are in
|
---|
80 | public BidDetails BestBid(double utilgoal, List<BidDetails> oppbids) {
|
---|
81 | List<BidDetails> selectedbids = SelectBids(utilgoal);
|
---|
82 | BidDetails Bestbid;
|
---|
83 | HashMap<BidDetails, Integer> tempbidmap = new HashMap<BidDetails, Integer>();
|
---|
84 | for( BidDetails bid : selectedbids) {
|
---|
85 | if(bidlistfreq.containsKey(bid)) {
|
---|
86 | tempbidmap.put(bid, bidlistfreq.get(bid));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | Bestbid = getMin(tempbidmap);
|
---|
91 |
|
---|
92 | int currenttime = (int) negotiationSession.getTimeline().getCurrentTime();
|
---|
93 | int totaltime = (int) negotiationSession.getTimeline().getTotalTime();
|
---|
94 | double phase = (double) currenttime /totaltime;
|
---|
95 | if(phase > .25 && currenttime % 4 == 0) {
|
---|
96 | Bestbid = (SelectNearParetoBid(selectedbids) == null) ? Bestbid : SelectNearParetoBid(selectedbids);
|
---|
97 | }
|
---|
98 | if(currenttime >= totaltime -10) {
|
---|
99 | selectedbids = SelectBids(new Range(utilgoal, 1));
|
---|
100 | BidDetails lastbid = SelectFinalOffer(selectedbids);
|
---|
101 | Bestbid = (lastbid == null) ? Bestbid : lastbid;
|
---|
102 | }
|
---|
103 | if(Bestbid.getMyUndiscountedUtil() <= BestOpponentbid(oppbids).getMyUndiscountedUtil()) {
|
---|
104 | Bestbid = BestOpponentbid(oppbids);
|
---|
105 | }
|
---|
106 | Updatebidslist(Bestbid);
|
---|
107 | return Bestbid;
|
---|
108 | }
|
---|
109 |
|
---|
110 | //Return the best opponent bid
|
---|
111 | public BidDetails BestOpponentbid(List<BidDetails> oppbids) {
|
---|
112 | BidDetails bestbid = oppbids.get(0);
|
---|
113 | double bestutility = bestbid.getMyUndiscountedUtil();
|
---|
114 | for(BidDetails bidd : oppbids) {
|
---|
115 | if(bidd.getMyUndiscountedUtil() > bestutility) {
|
---|
116 | bestbid = bidd;
|
---|
117 | bestutility = bidd.getMyUndiscountedUtil();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return bestbid;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static BidDetails getMin (HashMap<BidDetails, Integer> freqlist) {
|
---|
124 |
|
---|
125 | BidDetails minKey = null;
|
---|
126 | int minValue = Integer.MAX_VALUE;
|
---|
127 | for (BidDetails key : freqlist.keySet()) {
|
---|
128 | int value = freqlist.get(key);
|
---|
129 | if (value < minValue) {
|
---|
130 | minValue = value;
|
---|
131 | minKey = key;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | return minKey;
|
---|
135 | }
|
---|
136 |
|
---|
137 | //Gets an amount of bids within the specified range given to the method.
|
---|
138 | public List<BidDetails> SelectBids(Range range){
|
---|
139 | List<BidDetails> bidlist = new ArrayList<>();
|
---|
140 | for(BidDetails key : bidlistfreq.keySet()) {
|
---|
141 | if (key.getMyUndiscountedUtil() >= range.getLowerbound() && key.getMyUndiscountedUtil() <= range.getUpperbound())
|
---|
142 | bidlist.add(key);
|
---|
143 | }
|
---|
144 | return bidlist;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public List<BidDetails> SelectBids(Double utilgoal){
|
---|
148 | return outcomespace.getBidsinRange(new Range(utilgoal - .05, utilgoal + .05));
|
---|
149 | }
|
---|
150 |
|
---|
151 | //Gets bids near the Pareto Line
|
---|
152 | private BidDetails SelectNearParetoBid(List<BidDetails> selectedBids) {
|
---|
153 | BidDetails paretobid = null;
|
---|
154 | double paretodiff = 0;
|
---|
155 | for(BidDetails bid : selectedBids) {
|
---|
156 | double diff = this.model.getBidEvaluation(bid.getBid()) + bid.getMyUndiscountedUtil() ;
|
---|
157 | if(diff > paretodiff ) {
|
---|
158 | paretobid = bid;
|
---|
159 | paretodiff = diff;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return paretobid;
|
---|
163 | }
|
---|
164 |
|
---|
165 | //This method is used in order to get a bid in the last couple of rounds.
|
---|
166 | private BidDetails SelectFinalOffer(List<BidDetails> selectedbids) {
|
---|
167 | BidDetails finaloffer = null;
|
---|
168 | double totalutil = 0;
|
---|
169 | HashMap<BidDetails, Integer> tempbidmap = new HashMap<BidDetails, Integer>();
|
---|
170 | if(finalbids.size() == 0) {
|
---|
171 | initfinalbids(selectedbids);
|
---|
172 | for(BidDetails bid: finalbids.keySet()) {
|
---|
173 | if(bid.getMyUndiscountedUtil() < this.model.getBidEvaluation(bid.getBid())) {
|
---|
174 | double diff = this.model.getBidEvaluation(bid.getBid()) + bid.getMyUndiscountedUtil();
|
---|
175 | if(diff > totalutil ) {
|
---|
176 | tempbidmap.put(bid, finalbids.get(bid));
|
---|
177 | ;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | finaloffer = getMin(tempbidmap);
|
---|
183 | Updatefinalbids(finaloffer);
|
---|
184 | return finaloffer;
|
---|
185 | }
|
---|
186 |
|
---|
187 | //Initializes the final offers that the agent will make
|
---|
188 | private void initfinalbids(List<BidDetails> bidlist) {
|
---|
189 | for (BidDetails bid : bidlist) {
|
---|
190 | finalbids.put(bid, 0);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void Updatefinalbids(BidDetails bid) {
|
---|
195 | int i;
|
---|
196 |
|
---|
197 | for(BidDetails key : finalbids.keySet()) {
|
---|
198 | if(key.equals(bid)){
|
---|
199 | i = this.finalbids.get(key);
|
---|
200 | this.finalbids.replace(key, i += 2);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public boolean canUpdateOM() {
|
---|
207 | return negotiationSession.getTime() < updateThreshold;
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public Set<BOAparameter> getParameterSpec() {
|
---|
212 | Set<BOAparameter> set = new HashSet<BOAparameter>();
|
---|
213 | set.add(new BOAparameter("t", 1.1, "Time after which the OM should not be updated"));
|
---|
214 | return set;
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public String getName() {
|
---|
219 | return "IBasicOMS";
|
---|
220 | }
|
---|
221 | }
|
---|