source: src/main/java/agents/anac/y2017/simpleagent/Predictor.java@ 346

Last change on this file since 346 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 6.5 KB
Line 
1package agents.anac.y2017.simpleagent;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import agents.BidComparator;
9import genius.core.AgentID;
10import genius.core.Bid;
11import genius.core.actions.Accept;
12import genius.core.actions.Action;
13import genius.core.actions.Offer;
14import genius.core.issue.Issue;
15import genius.core.issue.Value;
16import genius.core.list.Tuple;
17import genius.core.persistent.StandardInfo;
18import genius.core.persistent.StandardInfoList;
19import genius.core.timeline.TimeLineInfo;
20import genius.core.utility.AbstractUtilitySpace;
21
22/*
23* *TODO* We need to choose our constraints specific to domain, must update some values during the negotiation for
24* better results
25 */
26class Predictor {
27
28 private AbstractUtilitySpace utilitySpace;
29 private TimeLineInfo timeline;
30 private HashMap<String, ArrayList<Offer>> allOffers = new HashMap<>();
31 private HashMap<Integer, Value> mostWantedValues = new HashMap<>();
32 private List<Bid> compatibleBids = new ArrayList<>();
33 private double maxReservationValue = 1;
34 private double minReservationValue = 0.83;
35 private double currentReservationValue = 0;
36 private boolean isBiddedBefore = false;
37
38 public Predictor(AbstractUtilitySpace utilitySpace, TimeLineInfo timeline) {
39 this.utilitySpace = utilitySpace;
40 this.timeline = timeline;
41 }
42
43 public void storeAgentOffer(Offer offer) {
44 if (allOffers.containsKey(offer.getAgent().getName()))
45 allOffers.get(offer.getAgent().getName()).add(offer);
46 else {
47 ArrayList<Offer> newOfferList = new ArrayList<>();
48 newOfferList.add(offer);
49 allOffers.put(offer.getAgent().getName(), newOfferList);
50 }
51 }
52
53 private boolean isBidAcceptable(Bid bid) {
54 return utilitySpace.getUtility(bid) > currentReservationValue;
55 }
56
57 private void updateReservationValue() {
58 double totalRound = timeline.getTotalTime();
59 double currentRound = timeline.getCurrentTime();
60 double resDifference = maxReservationValue - minReservationValue;
61 double optimizer = resDifference / totalRound;
62 currentReservationValue = maxReservationValue - (currentRound * optimizer);
63 if (currentRound > totalRound * 0.8)
64 System.out.println("Current Rez: " + currentReservationValue);
65 }
66
67 private ArrayList<Bid> getAllBids() {
68 ArrayList<Bid> allBids = new ArrayList<>();
69 for (ArrayList<Offer> offers : allOffers.values()) {
70 for (Offer offer : offers) {
71 allBids.add(offer.getBid());
72 }
73 }
74
75 if (allBids.isEmpty()) {
76 return null;
77 }
78 return allBids;
79 }
80
81 private List<List<Value>> getAllValues(ArrayList<Bid> allBids) {
82 List<List<Value>> allValues = new ArrayList<>();
83
84 List<Issue> allIssues = getAllBids().get(0).getIssues();
85 for (int i = 1; i <= allIssues.size(); i++) {
86 List<Value> values = new ArrayList<>();
87
88 for (Bid bid : allBids) {
89 if (!values.contains(bid.getValue(i)))
90 values.add(bid.getValue(i));
91 }
92 allValues.add(values);
93 }
94 return allValues;
95 }
96
97 private void updateMostWantedValues() {
98 mostWantedValues = new HashMap<>();
99 ArrayList<Bid> allBids = getAllBids();
100
101 List<Issue> allIssues = allBids.get(0).getIssues();
102
103 for (int i = 1; i <= allIssues.size(); i++) {
104 List<Value> values = new ArrayList<>();
105
106 for (Bid bid : allBids) {
107 values.add(bid.getValue(i));
108 }
109
110 Value mostCommon = getMostCommonValue(values);
111 mostWantedValues.put(i, mostCommon);
112 }
113
114 }
115
116 private Value getMostCommonValue(List<Value> values) {
117 Map<Value, Integer> map = new HashMap<>();
118
119 for (Value value : values) {
120 Integer val = map.get(value);
121 map.put(value, val == null ? 1 : val + 1);
122 }
123
124 Map.Entry<Value, Integer> max = null;
125
126 for (Map.Entry<Value, Integer> e : map.entrySet()) {
127 if (max == null || e.getValue() > max.getValue())
128 max = e;
129 }
130
131 return max.getKey();
132 }
133
134 private Bid generateBid() throws Exception {
135 updateMostWantedValues();
136
137 if (mostWantedValues.isEmpty())
138 return utilitySpace.getMaxUtilityBid();
139
140 calculateBidList();
141 double totalRound = timeline.getTotalTime();
142 double currentRound = timeline.getCurrentTime();
143
144 double optimizer = currentRound / totalRound;
145 double index = compatibleBids.size() * optimizer;
146 if (index == compatibleBids.size())
147 index = compatibleBids.size() - 1.0;
148 return compatibleBids.get((int) index);
149 }
150
151 private void calculateBidList() throws Exception {
152 compatibleBids = new ArrayList<>();
153 ArrayList<Bid> allBids = getAllBids();
154 List<List<Value>> allValues = getAllValues(allBids);
155 List<List<Value>> cartesianOfAllValues = new CartesianCalculator().calculate(allValues);
156 for (int i = 0; i < cartesianOfAllValues.size(); i++) {
157 HashMap<Integer, Value> map = new HashMap<>();
158 for (int j = 0; j < cartesianOfAllValues.get(i).size(); j++) {
159 map.put(j + 1, cartesianOfAllValues.get(i).get(j));
160 }
161 Bid bid = new Bid(utilitySpace.getDomain(), map);
162 if (utilitySpace.getUtility(bid) > currentReservationValue)
163 compatibleBids.add(bid);
164 }
165 if (compatibleBids.isEmpty())
166 compatibleBids.add(utilitySpace.getMaxUtilityBid());
167 BidComparator comparator = new BidComparator(utilitySpace);
168 compatibleBids.sort(comparator);
169
170 }
171
172 public void setHistoryAndUpdateThreshold(StandardInfoList history) {
173 double maxUtilsSum = 0;
174 double countedOffers = 0;
175 for (StandardInfo prevHistory : history) {
176 int numberOfAgents = prevHistory.getAgentProfiles().size();
177 List<Tuple<String, Double>> agentUtilities = prevHistory.getUtilities();
178 int agentUtilitySize = agentUtilities.size();
179 List<Tuple<String, Double>> finalUtilities = agentUtilities.subList(agentUtilitySize - numberOfAgents,
180 agentUtilitySize);
181 double maxUtility = 0;
182 for (Tuple<String, Double> agentUtility : finalUtilities) {
183 if (agentUtility.get2() > maxUtility)
184 maxUtility = agentUtility.get2();
185 }
186 if (maxUtility != 0) {
187 maxUtilsSum += maxUtility;
188 countedOffers++;
189 }
190 }
191 if (maxUtilsSum != 0)
192 minReservationValue = maxUtilsSum * 0.93 / countedOffers;
193 }
194
195 public Action generateAction(List<Class<? extends Action>> validActions, Bid lastReceivedBid, AgentID agentId) {
196 try {
197 if (!validActions.contains(Accept.class)) {
198 return new Offer(agentId, utilitySpace.getMaxUtilityBid());
199 } else {
200 updateReservationValue();
201 if (isBidAcceptable(lastReceivedBid)) {
202 return new Accept(agentId, lastReceivedBid);
203 } else {
204 if (!isBiddedBefore) {
205 isBiddedBefore = true;
206 return new Offer(agentId, utilitySpace.getMaxUtilityBid());
207 }
208 Bid bid = generateBid();
209 return new Offer(agentId, generateBid());
210 }
211 }
212 } catch (Exception e) {
213 e.printStackTrace();
214 return new Accept(agentId, lastReceivedBid);
215 }
216 }
217}
Note: See TracBrowser for help on using the repository browser.