source: src/main/java/agents/anac/y2019/thenewdeal/TheNewDeal.java

Last change on this file was 202, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents (3)

  • Property svn:executable set to *
File size: 12.0 KB
Line 
1package agents.anac.y2019.thenewdeal;
2
3import genius.core.AgentID;
4import genius.core.Bid;
5import genius.core.BidIterator;
6import genius.core.actions.Accept;
7import genius.core.actions.Action;
8import genius.core.actions.Offer;
9import genius.core.issue.IssueDiscrete;
10import genius.core.issue.Value;
11import genius.core.issue.ValueDiscrete;
12import genius.core.parties.AbstractNegotiationParty;
13import genius.core.parties.NegotiationInfo;
14import genius.core.utility.AbstractUtilitySpace;
15
16import java.util.*;
17
18public class TheNewDeal extends AbstractNegotiationParty {
19 private Bid lastReceivedBid = null;
20 private int counter = 1; // Temp counter, when it reaches ratio that will be the variable which is set to be 1
21 private int roundCounter = 0;
22 private int ratio = 0; //Estimated utility interval between two bids
23 private int bidIndex = 0; //Index of current offering bid
24 private Bid[] allPossibleBids;
25 private Double[] sortedUtilityArray; //Final version of estimated util array
26 private int issueCount;
27 private HashMap<Bid, Double> estimatedUtils = new HashMap<>(); // Bid ranking with equally distributed utilities as assumption
28 private HashMap<Value, Double> valuesAverage = new HashMap<>(); // Values with average weight
29 private HashMap<Bid, Double> allUtils = new HashMap<>(); //All possible bids in the domain with their estimated utilities
30 private int[] issueWeights; // Weights as multiplier
31 private int[] issueUtils; // Normalized weights
32 private ArrayList<Double> issueFinalWeights = new ArrayList<>();
33 private boolean firstAgent = false; //Variable to check if we go first
34 @Override
35 public void init(NegotiationInfo info) {
36 super.init(info);
37 Bid sampleBid = generateRandomBid();
38 IssueDiscrete[] issueDiscretes = new IssueDiscrete[sampleBid.getIssues().size()];
39 ValueDiscrete[][] valueDiscretes = new ValueDiscrete[issueDiscretes.length][];
40 issueCount = issueDiscretes.length;
41 issueWeights = new int[issueCount];
42 issueUtils = new int[issueCount];
43 BidIterator iterator = new BidIterator(utilitySpace.getDomain());
44 ArrayList<Bid> bids = new ArrayList<>();
45 List<Bid> bidOrder = userModel.getBidRanking().getBidOrder();
46 IssueValueItems(sampleBid, issueDiscretes, valueDiscretes);
47 EstimateIssueWeight(bidOrder, issueWeights, issueUtils);
48 initArrayAndRatio(iterator, bids);
49 double step = EstimateEqualDifferenceUtilities(userModel.getBidRanking().getHighUtility(), userModel.getBidRanking().getLowUtility(), bidOrder.size());
50 EstimateEqualDistribution(bidOrder, step);
51 HashMap<Value, double[]> mp = new HashMap<>();
52 ValuesWithTotalCounter(mp, bidOrder);
53 GenerateValuesAverage(mp);
54 NormalizeUtils();
55 sortedUtilityArray = createUtilArray();
56 SortUtilArray(sortedUtilityArray);
57 NormalizeMap();
58 NormalizeSortedArray();
59
60 //TestRanking(bids); -- Print elements for debugging purpose
61 }
62
63 //Returns bid with specified utility
64 public Bid findBid(Double util){
65 for (Map.Entry<Bid, Double> entry : allUtils.entrySet()) {
66 if(entry.getValue().equals(util)){
67 return entry.getKey();
68 }
69 }
70 return null;
71 }
72
73 public void NormalizeMap(){
74 for (Map.Entry<Bid, Double> entry : allUtils.entrySet()) {
75 allUtils.put(entry.getKey(), entry.getValue()/sortedUtilityArray[sortedUtilityArray.length - 1]);
76 }
77 }
78
79 // Store estimated utilities in an array
80 public Double[] createUtilArray(){
81 Double[] arr = new Double[allUtils.size()];
82 int i = 0;
83 for (Map.Entry<Bid, Double> entry : allUtils.entrySet()) {
84 arr[i] = entry.getValue();
85 i++;
86 }
87 return arr;
88 }
89
90 public double CalculateTotalUtility() {
91 double totalUtilities = 0;
92 for (Bid b : allPossibleBids) {
93 double bidUtil = 0;
94 for (int c = 1; c <= issueCount; c++) {
95 Value v = b.getValue(c);
96 if(valuesAverage.containsKey(v) && issueFinalWeights.get(c - 1) != null)
97 bidUtil += valuesAverage.get(v) * issueFinalWeights.get(c - 1);
98 }
99 totalUtilities += bidUtil;
100 }
101 return totalUtilities;
102 }
103
104 //For debugging
105 public void TestRanking(ArrayList<Bid> bids){
106 for(Bid b : bids){
107 log("Bid: " + b.getValues() + " ---> Utility: " + getUtility(b) + "Estimated Utility: " + allUtils.get(b));
108 }
109 }
110
111 public void NormalizeUtils() {
112 double total = CalculateTotalUtility();
113 for (Bid b : allPossibleBids) {
114 double bidUtil = 0;
115 for (int c = 1; c <= issueCount; c++) {
116 Value v = b.getValue(c);
117 if(valuesAverage.containsKey(v) && issueFinalWeights.get(c - 1) != null)
118 bidUtil += valuesAverage.get(v) * issueFinalWeights.get(c - 1);
119 }
120 allUtils.put(b, bidUtil / total);
121 }
122 }
123
124 //Method that solves linear equation with one variable and puts into a map
125 public void ValuesWithTotalCounter(HashMap<Value, double[]> map, List<Bid> bidOrder) {
126 for (int i = 0; i < bidOrder.size(); i++) {
127 double util = estimatedUtils.get(bidOrder.get(i));
128 Bid b = bidOrder.get(i);
129 for (int c = 1; c <= issueCount; c++) {
130 Value v = b.getValue(c);
131 if (!map.containsKey(v)) {
132 double[] arr = {util / issueFinalWeights.get(c - 1), 1.0};
133 map.put(v, arr);
134 } else {
135 double[] arr = map.get(v);
136 arr[1] += 1.0;
137 arr[0] = arr[0] + (util / issueFinalWeights.get(c - 1));
138 map.put(v, arr);
139 }
140 }
141 }
142 }
143 //Put average values into a map
144 public void GenerateValuesAverage(HashMap<Value, double[]> map) {
145 for (Map.Entry<Value, double[]> entry : map.entrySet()) {
146 double average = entry.getValue()[0] / entry.getValue()[1];
147 valuesAverage.put(entry.getKey(), average);
148 }
149 }
150
151 //Calculates the step increment
152 public double EstimateEqualDifferenceUtilities(double maxUtil, double minUtil, int itemCount) {
153 return (maxUtil - minUtil) / (itemCount - 1);
154 }
155
156 //Equally distribute the estimations
157 public void EstimateEqualDistribution(List<Bid> bidOrder, double step) {
158 double startUtil = userModel.getBidRanking().getLowUtility();
159 estimatedUtils.put(bidOrder.get(0), startUtil);
160 for (int i = 1; i < bidOrder.size() - 1; i++) {
161 estimatedUtils.put(bidOrder.get(i), startUtil + step);
162 startUtil += step;
163 }
164 estimatedUtils.put(bidOrder.get(bidOrder.size() - 1), userModel.getBidRanking().getHighUtility());
165 }
166
167 public void EstimateIssueWeight(List<Bid> bidOrder, int[] issueWeights, int[] issueUtils) {
168 Bid tmp = generateRandomBid();
169 for (int i = 1; i < issueCount + 1; i++) {
170 for (int j = 0; j < bidOrder.size() - 1; j++) {
171 if (j < bidOrder.size() - 1)
172 tmp = bidOrder.get(j + 1);
173 if (bidOrder.get(j).getValue(i) == tmp.getValue(i)) {
174 issueWeights[i - 1]++;
175 if (issueUtils[i - 1] < issueWeights[i - 1])
176 issueUtils[i - 1] = issueWeights[i - 1];
177 }
178 if (bidOrder.get(j).getValue(i) != tmp.getValue(i))
179 issueWeights[i - 1] = 0;
180 }
181 }
182 double sum = 0;
183 for (int i : issueUtils) {
184 sum = sum + i;
185 }
186 for (int i : issueUtils) {
187 issueFinalWeights.add(i / sum);
188 }
189 }
190
191 //Hold values in valueDiscretes array
192 public void IssueValueItems(Bid sampleBid, IssueDiscrete[] issueDiscretes, ValueDiscrete[][] valueDiscretes) {
193 for (int i = 0; i < sampleBid.getIssues().size(); i++) {
194 IssueDiscrete issueDiscrete = (IssueDiscrete) sampleBid.getIssues().get(i);
195 issueDiscretes[i] = issueDiscrete;
196 valueDiscretes[i] = new ValueDiscrete[issueDiscretes[i].getValues().size()];
197 for (int j = 0; j < issueDiscretes[i].getValues().size(); j++) {
198 valueDiscretes[i][j] = issueDiscretes[i].getValues().get(j);
199 }
200 }
201 }
202
203 //Sorts array according to bid utilities
204 public void SortUtilArray(Double[] utilArr) {
205 quicksort(utilArr, 0, allPossibleBids.length - 1);
206 }
207
208 public void quicksort(Double arr[], int low, int high) {
209 if (low < high) {
210 int pi = partition(arr, low, high);
211 quicksort(arr, low, pi - 1);
212 quicksort(arr, pi + 1, high);
213 }
214 }
215
216 public int partition(Double arr[], int low, int high) {
217 double pivot = arr[high];
218 int i = (low - 1);
219 for (int j = low; j < high; j++) {
220 if (arr[j] <= pivot) {
221 i++;
222 Double temp = arr[i];
223 arr[i] = arr[j];
224 arr[j] = temp;
225 }
226 }
227 Double temp = arr[i + 1];
228 arr[i + 1] = arr[high];
229 arr[high] = temp;
230 return i + 1;
231 }
232
233 //Finding ratio and initializing bids array
234 public void initArrayAndRatio(BidIterator iterator, ArrayList<Bid> bids) {
235 int j = 0;
236 while (iterator.hasNext()) {
237 Bid bid = iterator.next();
238 bids.add(bid);
239 j++;
240 }
241 allPossibleBids = new Bid[j];
242 for (int i = 0; i < j; i++) {
243 allPossibleBids[i] = bids.get(i);
244 }
245 ratio = (int) ((timeline.getTotalTime() - 1) / allPossibleBids.length);
246 }
247
248 public void NormalizeSortedArray(){
249 for(int i = 0; i < sortedUtilityArray.length ; i++){
250 sortedUtilityArray[i] = sortedUtilityArray[i]/sortedUtilityArray[sortedUtilityArray.length - 1];
251 }
252 }
253
254 @Override
255 public Action chooseAction(List<Class<? extends Action>> validActions) {
256 if (getLastReceivedAction() instanceof Offer)
257 lastReceivedBid = ((Offer) getLastReceivedAction()).getBid();
258 double ourBidUtil = sortedUtilityArray[sortedUtilityArray.length - 1 - bidIndex];
259 double ourBidUtil2 = sortedUtilityArray[sortedUtilityArray.length - 2 - bidIndex];
260 double opponentBidUtil = 0;
261 if(lastReceivedBid != null){
262 opponentBidUtil = allUtils.get(lastReceivedBid);
263 }else{
264 firstAgent = true;
265 }
266 if (roundCounter == timeline.getTotalTime() - 2) {
267 if(firstAgent)
268 return new Offer(getPartyId(), findBid(sortedUtilityArray[sortedUtilityArray.length - 1]));
269 return new Accept(getPartyId(), lastReceivedBid);
270 } else if ((ourBidUtil <= opponentBidUtil || ourBidUtil2 <= opponentBidUtil) && lastReceivedBid != null ) {
271 return new Accept(getPartyId(), lastReceivedBid);
272 } else {
273 if (counter == ratio) {
274 counter = 1;
275 bidIndex++;
276 if(bidIndex == sortedUtilityArray.length / 2){
277 bidIndex = 0;
278 }
279 }
280 counter++;
281 roundCounter++;
282 return new Offer(getPartyId(), findBid(ourBidUtil));
283 }
284 }
285
286 @Override
287 public void receiveMessage(AgentID sender, Action action) {
288 super.receiveMessage(sender, action);
289 }
290
291 @Override
292 public AbstractUtilitySpace estimateUtilitySpace() {
293 return super.estimateUtilitySpace();
294 }
295
296 @Override
297 public String getDescription() {
298 return "ANAC2019";
299 }
300
301 private static void log(String s) {
302 System.out.println(s);
303 }
304}
Note: See TracBrowser for help on using the repository browser.