source: src/main/java/agents/anac/y2014/Aster/SearchSA.java

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

Initial import : Genius 9.0.0

File size: 7.8 KB
Line 
1package agents.anac.y2014.Aster;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Bid;
8import genius.core.issue.Issue;
9import genius.core.issue.IssueInteger;
10import genius.core.issue.ValueInteger;
11import genius.core.utility.AbstractUtilitySpace;
12
13public class SearchSA {
14 private AbstractUtilitySpace utilitySpace;
15 private Random randomnr = new Random();
16 private ArrayList<Bid> bidList = new ArrayList<Bid>();
17 private double temperature;
18 private static final double MINIMUM_UTILITY = 0.70; // �期解�最低効用値(高��る��期解�生������)
19 private static final int NUMBER_ITERATIONS = 10000; // 繰り返�回数
20 private static final int NUMBER_SOLUTIONS = 50;
21
22 public SearchSA(AbstractUtilitySpace utilitySpace, int sessionNr) {
23 this.utilitySpace = utilitySpace;
24 this.temperature = 1000000;
25 // this.fileWriter = createFileWriter(sessionNr);
26 }
27
28 public Bid getBidbySA(double bidTarget) throws Exception {
29 Bid currentBid, nextBid, maxBid;
30 double currentUtility, nextUtility, maxUtility, probability;
31 int iteration = 0;
32
33 // �期設定
34 temperature = 1000000;
35 do {
36 currentBid = utilitySpace.getDomain().getRandomBid(null);
37 currentUtility = utilitySpace.getUtility(currentBid);
38 } while (currentUtility < MINIMUM_UTILITY);
39
40 maxBid = currentBid;
41 maxUtility = currentUtility;
42
43 while ((iteration++ < NUMBER_ITERATIONS) && (maxUtility < bidTarget)) {
44 nextBid = nextBid(currentBid);
45 nextUtility = utilitySpace.getUtility(nextBid);
46 temperature = calculateTemperature();
47 probability = calculateProbability(currentUtility, nextUtility,
48 temperature);
49
50 if (probability > randomnr.nextDouble()) {
51 currentBid = nextBid;
52 currentUtility = utilitySpace.getUtility(nextBid);
53 if (nextUtility > maxUtility) {
54 maxBid = nextBid;
55 maxUtility = nextUtility;
56 }
57 }
58 }
59
60 if (maxUtility < bidTarget) {
61 return null;
62 } else {
63 return maxBid;
64 }
65 }
66
67 public Bid getFirstBidbySA(double bidTarget) throws Exception {
68 Bid currentBid, maxBid; // �在�Bid�最�Bid
69 double currentUtility, maxUtility; // �在�効用値�最大効用値
70 int iteration = 0; // 繰り返�回数
71
72 // �期解�生�
73 for (int i = 0; i < NUMBER_SOLUTIONS; i++) {
74 do {
75 currentBid = utilitySpace.getDomain().getRandomBid(null);
76 currentUtility = utilitySpace.getUtility(currentBid);
77 } while (currentUtility < MINIMUM_UTILITY);
78 bidList.add(currentBid);
79 }
80
81 // 最�解�更新
82 maxBid = getMaxBid(bidList);
83 maxUtility = utilitySpace.getUtility(maxBid);
84
85 // fileWrite(fileWriter, maxUtility);
86
87 // 指定回数�最�解�見��る��繰り返�
88 while ((iteration++ < NUMBER_ITERATIONS) && (maxUtility < bidTarget)) {
89 for (int i = 0; i < bidList.size(); i++) {
90 Bid curBid = bidList.get(i);
91 currentUtility = utilitySpace.getUtility(curBid);
92 Bid nextBid = nextBid(curBid); // 近�Bidを探索
93 double nextUtility = utilitySpace.getUtility(nextBid); // 近�Bid�効用値
94 double probability = calculateProbability(currentUtility,
95 nextUtility, temperature); // �移確率�算出
96
97 if (probability > randomnr.nextDouble()) {
98 bidList.set(i, nextBid); // Bid�更新
99 }
100 }
101
102 // 温度�算出
103 temperature = calculateTemperature();
104
105 // 最�解�更新
106 maxBid = getMaxBid(bidList);
107 maxUtility = utilitySpace.getUtility(maxBid);
108
109 // fileWrite(fileWriter, maxUtility);
110
111 }
112
113 // fileWriter.close();
114 return maxBid;
115 }
116
117 /**
118 * �移確率�算出
119 *
120 * @param currentUtil
121 * �在�効用値
122 * @param nextUtil
123 * 近��効用値
124 * @param temperature
125 * 温度
126 * @return �移確率
127 */
128 private double calculateProbability(double currentUtil, double nextUtil,
129 double temperature) {
130 double diff = currentUtil - nextUtil;
131 if (diff > 0.0) {
132 return Math.exp(-diff / temperature); // �在�効用値�方�高�場�
133 } else {
134 return 1.0; // 近��効用値�方�高�場�
135 }
136 }
137
138 /**
139 * 温度�算出
140 *
141 * @param iteration
142 * �在�試行回数
143 * @return 温度
144 */
145 private double calculateTemperature() {
146 return temperature *= 0.97;
147 }
148
149 /**
150 * 指定��Issue���る近��Bidを探索
151 *
152 * @param bid
153 * �在�Bid
154 * @return 近�Bid
155 * @throws Exception
156 */
157 private Bid nextBid(Bid bid) throws Exception {
158 List<Issue> issues = utilitySpace.getDomain().getIssues(); // 全issue��得
159 Bid nextBid = new Bid(bid); // �在�Bidをコピー
160
161 int issueIndex = randomnr.nextInt(issues.size()); // Issueをランダム�指定
162 IssueInteger lIssueInteger = (IssueInteger) issues.get(issueIndex); // 指定��index�issueを�得
163 int issueNumber = lIssueInteger.getNumber(); // issue番�
164 int issueIndexMin = lIssueInteger.getLowerBound(); // issue�下�値
165 int issueIndexMax = lIssueInteger.getUpperBound(); // issue�上�値
166 int optionIndex = 0; // 変更�るValue値
167
168 ValueInteger lIssueValue = (ValueInteger) bid.getValue(issueNumber); // 指定��issue�Value
169 int issueValue = Integer.valueOf(lIssueValue.toString()).intValue();
170 optionIndex = nextOptionIndex(issueIndexMin, issueIndexMax, issueValue); // 値を1増減��る
171
172 nextBid = nextBid.putValue(issueNumber, new ValueInteger(optionIndex)); // �在�Bid�らIssue�値を入れ替�る
173
174 return nextBid;
175 }
176
177 /**
178 * 近�探索
179 *
180 * @param issueIndexMin
181 * 下�値
182 * @param issueIndexMax
183 * 上�値
184 * @param issueValue
185 * �在�値
186 * @return 値を1��増減��る
187 */
188 private int nextOptionIndex(int issueIndexMin, int issueIndexMax,
189 int issueValue) {
190 int step = 1; // 値を増減��る幅
191 int direction = randomnr.nextBoolean() ? 1 : -1; // ランダム�増減を決定
192
193 if (issueIndexMin < issueIndexMax) {
194 if ((issueValue + step) > issueIndexMax) { // +1�る�上�値を超�る場�
195 direction = -1;
196 } else if ((issueValue - step) < issueIndexMin) { // -1�る�下�値を下回る場�
197 direction = 1;
198 }
199 } else {
200 return issueValue; // issueIndexMin == issueIndexMax�場�
201 }
202
203 return issueValue + step * direction;
204 }
205
206 private Bid getMaxBid(ArrayList<Bid> bidList) throws Exception {
207 Bid maxBid = null;
208 double maxUtil = 0.0;
209 for (Bid curBid : bidList) {
210 double curUtil = utilitySpace.getUtility(curBid);
211 if (curUtil > maxUtil) {
212 maxBid = curBid;
213 maxUtil = curUtil;
214 }
215 }
216 return maxBid;
217 }
218}
Note: See TracBrowser for help on using the repository browser.