source: src/main/java/agents/ai2014/group12/BidGenerator.java@ 126

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

Initial import : Genius 9.0.0

File size: 5.3 KB
Line 
1package agents.ai2014.group12;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import genius.core.Bid;
9import genius.core.issue.Value;
10import genius.core.issue.ValueDiscrete;
11import genius.core.utility.AbstractUtilitySpace;
12
13public class BidGenerator {
14 static ArrayList<Bid> bidCombinations;
15 static AbstractUtilitySpace utilitySpace;
16
17 /**
18 * This function returns the best bid that an agent can make with regards to
19 * other agents.
20 *
21 * @param ut
22 * @param preference
23 * @param acceptingValue
24 * @param otherAgentsPreference
25 * @return
26 * @throws Exception
27 */
28
29 public static Bid generateBid(AbstractUtilitySpace ut,
30 Preference preference, double acceptingValue,
31 HashMap<String, Preference> otherAgentsPreference) throws Exception {
32 System.out.println("Begin combinations");
33 utilitySpace = ut;
34 bidCombinations = new ArrayList<Bid>();
35
36 if (acceptingValue == 0.9) {
37 return ut.getMaxUtilityBid();
38 }
39
40 cartesianProductW(preference);
41
42 ArrayList<Bid> filteredBidCombinations = filterCombos(acceptingValue,
43 0.1);
44
45 Bid returnBid = new Bid(ut.getDomain());
46 returnBid = getBestBid(filteredBidCombinations, ut, preference,
47 otherAgentsPreference);
48
49 return returnBid;
50 }
51
52 /**
53 * A wrapper function for the cartesianProduct of a agents preferences. Will
54 * make a List<List<Node>> data structured that will be used as input for
55 * cartesianProduct
56 *
57 * @param preference
58 * @throws Exception
59 */
60
61 private static void cartesianProductW(Preference preference)
62 throws Exception {
63 List<List<Node>> blocks = new ArrayList<List<Node>>();
64
65 for (int i = 0; i < preference.preferenceList.size(); i++) {
66 PreferenceBlock block = preference.preferenceList.get(i);
67 List<Node> nodes = new ArrayList<Node>();
68 for (int j = 0; j < block.nodeList.size(); j++) {
69 nodes.add(block.nodeList.get(j));
70 }
71 blocks.add(nodes);
72 }
73
74 List<List<Node>> resultTest = cartesianProduct(blocks);
75 for (int i = 0; i < resultTest.size(); i++) {
76 List<Node> list = resultTest.get(i);
77 HashMap<Integer, Value> hsmp = new HashMap<Integer, Value>();
78 Bid bid = new Bid(utilitySpace.getDomain());
79 for (int j = 0; j < list.size(); j++) {
80 Node node = list.get(j);
81 ValueDiscrete val = new ValueDiscrete(node.getName());
82 Value value = (Value) val;
83 hsmp.put(j + 1, value);
84 bid = new Bid(utilitySpace.getDomain(), hsmp);
85 }
86 bidCombinations.add(bid);
87 }
88 }
89
90 /**
91 * Will return a cartesianProduct list of the input list
92 *
93 * @param lists
94 * @return
95 */
96 protected static <T> List<List<T>> cartesianProduct(List<List<T>> lists) {
97 List<List<T>> resultLists = new ArrayList<List<T>>();
98 if (lists.size() == 0) {
99 resultLists.add(new ArrayList<T>());
100 return resultLists;
101 } else {
102 List<T> firstList = lists.get(0);
103 List<List<T>> remainingLists = cartesianProduct(lists.subList(1,
104 lists.size()));
105 for (T condition : firstList) {
106 for (List<T> remainingList : remainingLists) {
107 ArrayList<T> resultList = new ArrayList<T>();
108 resultList.add(condition);
109 resultList.addAll(remainingList);
110 resultLists.add(resultList);
111 }
112 }
113 }
114 return resultLists;
115 }
116
117 /**
118 * Returns the best bid of the bid list, will calculate all the values of
119 * the bids and takes the best average bid for everybody and returns that
120 * Bid.
121 *
122 * @param bids
123 * @param ut
124 * @param preference
125 * @param otherAgentsPreference
126 * @return
127 * @throws Exception
128 */
129 private static Bid getBestBid(ArrayList<Bid> bids, AbstractUtilitySpace ut,
130 Preference preference,
131 HashMap<String, Preference> otherAgentsPreference) throws Exception {
132 ArrayList<ArrayList<Double>> list = new ArrayList<ArrayList<Double>>();
133 for (Bid bid : bids) {
134 ArrayList<Double> bidValues = new ArrayList<Double>();
135 for (Map.Entry<String, Preference> entry : otherAgentsPreference
136 .entrySet()) {
137 Preference p = (Preference) entry.getValue();
138 double bidValueP = p.getUtilityValue(bid);
139 bidValues.add(bidValueP);
140 }
141 }
142
143 ArrayList<Double> averageValueOfBid = new ArrayList<Double>();
144 for (ArrayList<Double> listOfABid : list) {
145 double averageWeight = 0;
146 for (double bidListValues : listOfABid) {
147 averageWeight += bidListValues;
148 }
149 averageValueOfBid.add(averageWeight);
150 }
151
152 int returnIndex = 0;
153 double biggest = 0;
154 for (int i = 0; i < averageValueOfBid.size(); i++) {
155 if (averageValueOfBid.get(i) > biggest) {
156 biggest = averageValueOfBid.get(i);
157 returnIndex = i;
158 }
159 }
160 return bids.get(returnIndex);
161 }
162
163 /**
164 * Returns an filtered list of the bidcombinations. The filter is an range
165 * from the accepting value to a range percentage above the accepting value.
166 *
167 * @param acceptingValue
168 * @param range
169 * @return
170 * @throws Exception
171 */
172 public static ArrayList<Bid> filterCombos(double acceptingValue,
173 double range) throws Exception {
174 ArrayList<Bid> goodBidCombos = new ArrayList<Bid>();
175
176 double minValue = acceptingValue;
177 double maxValue = acceptingValue + (acceptingValue * range);
178
179 for (int i = 0; i < bidCombinations.size(); i++) {
180 Bid bid = bidCombinations.get(i);
181 double value = utilitySpace.getUtility(bid);
182 if (minValue < value && maxValue > value) {
183 goodBidCombos.add(bid);
184
185 }
186 }
187
188 if (goodBidCombos.size() != 0) {
189 return goodBidCombos;
190 } else
191 return filterCombos(acceptingValue, range * 1.5);
192
193 }
194}
Note: See TracBrowser for help on using the repository browser.