1 | package agents.anac.y2014.Aster;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import genius.core.Bid;
|
---|
8 | import genius.core.issue.Issue;
|
---|
9 | import genius.core.issue.IssueInteger;
|
---|
10 | import genius.core.issue.ValueInteger;
|
---|
11 | import genius.core.utility.AbstractUtilitySpace;
|
---|
12 |
|
---|
13 | public 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 | }
|
---|