1 | package agents.anac.y2018.meng_wan;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import genius.core.AgentID;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.actions.Accept;
|
---|
11 | import genius.core.actions.Action;
|
---|
12 | import genius.core.actions.Offer;
|
---|
13 | import genius.core.issue.Issue;
|
---|
14 | import genius.core.issue.IssueDiscrete;
|
---|
15 | import genius.core.issue.IssueInteger;
|
---|
16 | import genius.core.issue.Value;
|
---|
17 | import genius.core.issue.ValueInteger;
|
---|
18 | import genius.core.parties.AbstractNegotiationParty;
|
---|
19 | import genius.core.parties.NegotiationInfo;
|
---|
20 | import genius.core.timeline.TimeLineInfo;
|
---|
21 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
22 |
|
---|
23 | public class Agent36 extends AbstractNegotiationParty {
|
---|
24 | //可变的target utility
|
---|
25 | Double changeTargetUtility=0.8D;
|
---|
26 | private TimeLineInfo TimeLineInfo = null;
|
---|
27 | //我的方法给offer调用的次数
|
---|
28 | int DamonOfferTime=0;
|
---|
29 | //时间格式化
|
---|
30 | private double t1 = 0.0D;
|
---|
31 | private double u2 = 1.0D;
|
---|
32 | private final String description = "Example Agent";
|
---|
33 |
|
---|
34 | Bid lastBid;
|
---|
35 | Action lastAction;
|
---|
36 | String oppAName;
|
---|
37 | String oppBName;
|
---|
38 | int round;
|
---|
39 | boolean Imfirst = false;
|
---|
40 | Boolean withDiscount = null;
|
---|
41 | boolean fornullAgent = false;
|
---|
42 | ArrayList<BidUtility> opponentAB = new ArrayList();
|
---|
43 | //保存大于0。7的offer
|
---|
44 | ArrayList<BidUtility> opponentABC = new ArrayList();
|
---|
45 | OpponentPreferences oppAPreferences = new OpponentPreferences();
|
---|
46 | OpponentPreferences oppBPreferences = new OpponentPreferences();
|
---|
47 |
|
---|
48 |
|
---|
49 | //初始化
|
---|
50 | public void init(NegotiationInfo info) {
|
---|
51 | super.init(info);
|
---|
52 | this.TimeLineInfo = info.getTimeline();
|
---|
53 | }
|
---|
54 |
|
---|
55 | //选择策略,当进入自己turn的时候,进入此方法;
|
---|
56 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
57 | try {
|
---|
58 | if (this.lastBid == null) {
|
---|
59 | this.Imfirst = true;
|
---|
60 | Bid b = getMybestBid(this.utilitySpace.getMaxUtilityBid()); //algorithm 2
|
---|
61 | return new Offer(getPartyId(), b);
|
---|
62 | }
|
---|
63 | if(this.TimeLineInfo.getTime()>=0.95D){
|
---|
64 | this.changeTargetUtility=0.75D;
|
---|
65 | }
|
---|
66 | if(this.TimeLineInfo.getTime()>0.98D){
|
---|
67 | this.changeTargetUtility=0.7D;
|
---|
68 | }
|
---|
69 | System.out.println("MyUtiityValueMax:-"+getMyutility());
|
---|
70 | //如果来了一个utility大于MyUtility的offer
|
---|
71 | if (this.utilitySpace.getUtility(this.lastBid) >= getMyutility()) {
|
---|
72 | //在前150秒不接受任何offer,这种缺点就是万一有一个sbagent在之前都接受这个offer,但是后来又不接受了,但一般不会
|
---|
73 | if(this.TimeLineInfo.getTime()<0.9D){
|
---|
74 | //只有这个协议是其他俩都接受的时候,才来改变targetAction
|
---|
75 | if(this.lastAction instanceof Accept) {
|
---|
76 | this.changeTargetUtility = this.utilitySpace.getUtility(this.lastBid);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | //最后30秒开始接受offer
|
---|
80 | else{
|
---|
81 | return new Accept(getPartyId(), this.lastBid);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | //如果时间0.8以上了,就增加一个策略:
|
---|
86 | //看commonAccept是否空,不空就
|
---|
87 | //从common里面挑选,有没有大于等于myutility的offer,有的话,抛出去
|
---|
88 | if(this.TimeLineInfo.getTime()>= 0.9D) {
|
---|
89 | if (opponentABC != null && opponentABC.size() != 0) {
|
---|
90 | Double maxU = 0D;
|
---|
91 | int indexI = 0;
|
---|
92 | for (int i = 0; i < opponentABC.size(); i++) {
|
---|
93 | if (opponentABC.get(i).utility > maxU) {
|
---|
94 | maxU = opponentABC.get(i).utility;
|
---|
95 | indexI = i;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | Bid b;
|
---|
99 | //如果这个列表中的最高utility 大于 当前的MyUtility
|
---|
100 | if (opponentABC.get(indexI).utility >=getMyutility()) {
|
---|
101 | //一旦把此offer发出去,就从列表中删除
|
---|
102 | b = opponentABC.get(indexI).bid;
|
---|
103 | opponentABC.remove(opponentABC.get(indexI));
|
---|
104 | DamonOfferTime += 1;
|
---|
105 | System.out.println("MyMethodGiveOfferTime:-" + DamonOfferTime);
|
---|
106 | } else {
|
---|
107 | b = offerMyNewBid(false);
|
---|
108 | }
|
---|
109 |
|
---|
110 | return new Offer(getPartyId(), b);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | //前150s 或者 来的offer的utility 小于 MyUtility,都执行下面代码
|
---|
114 | Bid b = offerMyNewBid(false);
|
---|
115 | // if (this.utilitySpace.getUtility(b) < getMyutility()) {
|
---|
116 | // return new Offer(getPartyId(), getMybestBid(this.utilitySpace.getMaxUtilityBid()));
|
---|
117 | // }
|
---|
118 | return new Offer(getPartyId(), b);
|
---|
119 | } catch (Exception e) {
|
---|
120 | System.out.println("Error Occured " + e.getMessage());
|
---|
121 |
|
---|
122 | Bid mb = null;
|
---|
123 | try {
|
---|
124 | mb = this.utilitySpace.getMaxUtilityBid();
|
---|
125 | return new Offer(getPartyId(), getMybestBid(mb));
|
---|
126 | } catch (Exception e1) {
|
---|
127 | try {
|
---|
128 | return new Offer(getPartyId(), mb);
|
---|
129 | } catch (Exception localException1) {
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return new Accept(getPartyId(), this.lastBid);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void receiveMessage(AgentID sender, Action action) {
|
---|
137 | super.receiveMessage(sender, action); //this.lastBid = ((Offer) arguments).getBid();
|
---|
138 | String agentName = sender == null ? "null" : sender.toString();
|
---|
139 |
|
---|
140 | this.fornullAgent = (!this.fornullAgent); //好像没用?
|
---|
141 | if ((action != null) && ((action instanceof Offer))) { //如果读到一个offer
|
---|
142 | Bid newBid = ((Offer) action).getBid();
|
---|
143 | try {
|
---|
144 | //根据时间变化的utility 没用
|
---|
145 | BidUtility opBid = new BidUtility(newBid, this.utilitySpace.getUtility(newBid),
|
---|
146 | System.currentTimeMillis()); //BidUtility为私有类,用来储存bid、该bid对Damon的utility、时间, 这个时间是干嘛的?
|
---|
147 |
|
---|
148 | //这下面将bid加入arraylist的操作是否有用? 暂时没发现作用
|
---|
149 | if ((this.oppAName != null) && (this.oppAName.equals(agentName))) {
|
---|
150 | addBidToList(this.oppAPreferences.getOpponentBids(), opBid); //如果是A对手给的offer,则添加到A对手的OpponentPreference类中的arraylist里
|
---|
151 | } else if ((this.oppBName != null) && (this.oppBName.equals(agentName))) {
|
---|
152 | addBidToList(this.oppBPreferences.getOpponentBids(), opBid); //如果是B对手给的offer,则添加到B对手的OpponentPreference类中的arraylist里
|
---|
153 | } else if (this.oppAName == null) { //如果没有A名称,则添加A名称,并添加bid
|
---|
154 | this.oppAName = agentName;
|
---|
155 | this.oppAPreferences.getOpponentBids().add(opBid);
|
---|
156 | } else if (this.oppBName == null) {//如果没有B名称,则添加B名称,并添加bid
|
---|
157 | this.oppBName = agentName;
|
---|
158 | this.oppBPreferences.getOpponentBids().add(opBid);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | calculateParamForOpponent(this.oppAName.equals(agentName) ? this.oppAPreferences : this.oppBPreferences,
|
---|
163 | newBid); //针对A或B执行calculateParamForOpponent().这个function是干嘛的?
|
---|
164 | System.out.println("opp placed bid:" + newBid);
|
---|
165 | this.lastBid = newBid;
|
---|
166 | } catch (Exception e) {
|
---|
167 | e.printStackTrace();
|
---|
168 | }
|
---|
169 | //如果读到一个accept
|
---|
170 | } else if ((action != null) && ((action instanceof Accept))) {
|
---|
171 | BidUtility opBid = null;
|
---|
172 | try {
|
---|
173 | opBid = new BidUtility(this.lastBid, this.utilitySpace.getUtility(this.lastBid),
|
---|
174 | System.currentTimeMillis());
|
---|
175 | } catch (Exception e) {
|
---|
176 | System.out.println("Exception 44" + e.getMessage());
|
---|
177 | }
|
---|
178 | addBidToList(this.opponentAB,opBid); //如果读到一个accept,则添加这个bid到AB共同bidList。
|
---|
179 | //对于我大于0。7且 他们都接受的offer
|
---|
180 | if(opBid.utility > 0.7D){
|
---|
181 | opponentABC.add(opBid);
|
---|
182 | System.out.println("OpponentABC-SIZE:-"+opponentABC.size());
|
---|
183 | }
|
---|
184 | //但是收到accept有可能是某个对手accept Damon的offer。需考虑怎么辨别是否是AB共同的。需辨别这个bid是否是Damon发出去的,如果不是,则AB同时同意这个offer,需加入list。
|
---|
185 | //可以用Bid.equal(pBid)来比较
|
---|
186 | }
|
---|
187 | this.lastAction = action;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public int MyBestValue(int issueindex) {
|
---|
191 | List<Issue> dissues = this.utilitySpace.getDomain().getIssues();
|
---|
192 | Issue isu = (Issue) dissues.get(issueindex);
|
---|
193 | HashMap<Integer, Value> map = new HashMap();
|
---|
194 | double maxutil = 0.0D;
|
---|
195 | int maxvalIndex = 0;
|
---|
196 | try {
|
---|
197 | map = this.utilitySpace.getMaxUtilityBid().getValues();
|
---|
198 | } catch (Exception e) {
|
---|
199 | e.printStackTrace();
|
---|
200 | }
|
---|
201 | if ((isu instanceof IssueDiscrete)) {
|
---|
202 | IssueDiscrete is = (IssueDiscrete) isu;
|
---|
203 |
|
---|
204 | int num = 0;
|
---|
205 | if (num < is.getNumberOfValues()) {
|
---|
206 | map.put(new Integer(issueindex + 1), is.getValue(num));
|
---|
207 |
|
---|
208 | double u = 0.0D;
|
---|
209 | try {
|
---|
210 | Bid temp = new Bid(this.utilitySpace.getDomain(), map);
|
---|
211 | u = this.utilitySpace.getUtility(temp);
|
---|
212 | } catch (Exception e) {
|
---|
213 | e.printStackTrace();
|
---|
214 | }
|
---|
215 | if (u > maxutil) {
|
---|
216 | maxutil = u;
|
---|
217 | maxvalIndex = num;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | } else if (((isu instanceof IssueInteger)) && (map != null)) {
|
---|
221 | return ((ValueInteger) map.get(Integer.valueOf(issueindex + 1))).getValue();
|
---|
222 | }
|
---|
223 | return maxvalIndex;
|
---|
224 | }
|
---|
225 |
|
---|
226 | //true开启50%策略, false不开启
|
---|
227 | public Bid offerMyNewBid(boolean strategy) {
|
---|
228 | Bid bidNN = null;
|
---|
229 | if ((this.opponentAB != null) && (this.opponentAB.size() != 0)) {
|
---|
230 | bidNN = getNNBid(this.opponentAB);
|
---|
231 | }
|
---|
232 | try {
|
---|
233 | if ((bidNN == null) || (this.utilitySpace.getUtility(bidNN) < getMyutility())) {
|
---|
234 | List<List<Object>> mutualIssues = getMutualIssues(strategy);
|
---|
235 | //第一次map
|
---|
236 | HashMap<Integer, Value> map1 = new HashMap();
|
---|
237 | //第二次可能出现 X Y Y X形式的map
|
---|
238 | HashMap<Integer, Value> map2 = new HashMap();
|
---|
239 |
|
---|
240 | List<Issue> issues = this.utilitySpace.getDomain().getIssues();
|
---|
241 | for (int i = 0; i < mutualIssues.size(); i++) {
|
---|
242 | //issueFrequency
|
---|
243 | List<Object> issueFrequency = (List) mutualIssues.get(i);
|
---|
244 |
|
---|
245 | Issue issue = (Issue) issues.get(i);
|
---|
246 | if ((issue instanceof IssueDiscrete)) {
|
---|
247 | if (issueFrequency != null) {
|
---|
248 | IssueDiscrete discrete = (IssueDiscrete) issues.get(i);
|
---|
249 | //目的在于map里面只能放 issue.valuel类型,而不能直接放string , 放入map1
|
---|
250 | for (int num = 0; num < discrete.getNumberOfValues(); num++) {
|
---|
251 | if(issueFrequency.size()>4) {
|
---|
252 | if (discrete.getValue(num).toString().equals(issueFrequency.get(4).toString())) {
|
---|
253 | map2.put(new Integer(i + 1), discrete.getValue(num));
|
---|
254 | }
|
---|
255 | }
|
---|
256 | if (discrete.getValue(num).toString().equals(issueFrequency.get(0).toString())) {
|
---|
257 | map1.put(new Integer(i + 1), discrete.getValue(num));
|
---|
258 | if(map2.get(new Integer(i+1))!=null) {
|
---|
259 | map2.put(new Integer(i + 1), discrete.getValue(num));
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | //X Y Y X形式 ,放入map 2
|
---|
264 | if(issueFrequency.size()>4){
|
---|
265 | for (int num = 0; num < discrete.getNumberOfValues(); num++) {
|
---|
266 | if (discrete.getValue(num).toString().equals(issueFrequency.get(4).toString())) {
|
---|
267 | map2.put(new Integer(i + 1), discrete.getValue(num));
|
---|
268 | break;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | }
|
---|
273 | } else {
|
---|
274 | IssueDiscrete is = (IssueDiscrete) issues.get(i);
|
---|
275 | map1.put(new Integer(i + 1), is.getValue(MyBestValue(i)));
|
---|
276 | map2.put(new Integer(i + 1), is.getValue(MyBestValue(i)));
|
---|
277 | }
|
---|
278 | } else {
|
---|
279 | throw new IllegalStateException("not supported issue " + issue);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | try {
|
---|
283 | Bid bid1 = new Bid(this.utilitySpace.getDomain(), (HashMap) map1.clone());
|
---|
284 | Bid bid2 = new Bid(this.utilitySpace.getDomain(), (HashMap) map2.clone());
|
---|
285 | if (this.utilitySpace.getUtility(bid1) >= getMyutility()) {
|
---|
286 | return bid1;
|
---|
287 | }else if(this.utilitySpace.getUtility(bid2) >= getMyutility()){
|
---|
288 | return bid2;
|
---|
289 | }
|
---|
290 | if(!strategy){
|
---|
291 | offerMyNewBid(true);
|
---|
292 | }
|
---|
293 |
|
---|
294 | return getMybestBid(this.utilitySpace.getMaxUtilityBid());
|
---|
295 | } catch (Exception e) {
|
---|
296 | System.out.println("Exception 55 " + e.getMessage());
|
---|
297 | }
|
---|
298 | }
|
---|
299 | return bidNN;
|
---|
300 | } catch (Exception e) {
|
---|
301 | e.printStackTrace();
|
---|
302 | }
|
---|
303 | return null;
|
---|
304 | }
|
---|
305 |
|
---|
306 | //获取其他agent共同偏爱的issue ,strategy为true就开启50%停止策略
|
---|
307 | public List<List<Object>> getMutualIssues(boolean strategy) {
|
---|
308 | //对手agent共同偏爱的list
|
---|
309 | List<List<Object>> mutualList = new ArrayList();
|
---|
310 | //该party对应的issue列表
|
---|
311 | List<Issue> dissues = this.utilitySpace.getDomain().getIssues();
|
---|
312 | //
|
---|
313 | int onlyFirstFrequency=2;
|
---|
314 | while(onlyFirstFrequency>0) {
|
---|
315 | mutualList = new ArrayList();
|
---|
316 | System.out.println("ISSUES SIZE:"+dissues.size());
|
---|
317 | int count=0;
|
---|
318 | for (int i = 0; i < dissues.size(); i++) {
|
---|
319 | //防止第一次注入对手频率大的就超过50% 导致我们utility不够
|
---|
320 | if(strategy) {
|
---|
321 | if (updateMutualList(mutualList, dissues, i, onlyFirstFrequency)) {
|
---|
322 | count += 1;
|
---|
323 | }
|
---|
324 | //超过50%就停止
|
---|
325 | if (count * 2 >= dissues.size()) {
|
---|
326 | break;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | //一样?
|
---|
330 | //如果对手agent的issue 的频率map为空,则进行下一次循环
|
---|
331 | //也就是说这个是判断是不是对手还没给过offer??
|
---|
332 | if ((this.oppAPreferences.getRepeatedissue().get(dissues.get(i).getName()).size() == 0)
|
---|
333 | || (this.oppBPreferences.getRepeatedissue().get(dissues.get(i).getName())).size() == 0) {
|
---|
334 | return null;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | System.out.println("MutualList SIZE:"+mutualList.size());
|
---|
338 | if (this.opponentAB.size() == 0) {
|
---|
339 | float nullval = 0.0F;
|
---|
340 | for (int i = 0; i < mutualList.size(); i++) {
|
---|
341 | if (mutualList.get(i) != null) {
|
---|
342 | nullval += 1.0F;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | nullval /= mutualList.size();
|
---|
346 | if (nullval >= 0.5D) {
|
---|
347 | break;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | onlyFirstFrequency--;
|
---|
351 | }
|
---|
352 |
|
---|
353 | return mutualList;
|
---|
354 | }
|
---|
355 |
|
---|
356 | //更新其他agent共同偏爱的List
|
---|
357 | private boolean updateMutualList(List<List<Object>> mutualList, List<Issue> dissues,int i ,int onlyFirstFrequency) {
|
---|
358 | //如果这个issue A之前抛出过
|
---|
359 | if (this.oppAPreferences.getRepeatedissue().get(dissues.get(i).getName()) != null) {
|
---|
360 | //此issue A抛出过的值的集合
|
---|
361 | HashMap<Value, Integer> valsA = this.oppAPreferences.getRepeatedissue()
|
---|
362 | .get(dissues.get(i).getName());
|
---|
363 | //此issue B抛出过的值的集合
|
---|
364 | HashMap<Value, Integer> valsB =this.oppBPreferences.getRepeatedissue()
|
---|
365 | .get(dissues.get(i).getName());
|
---|
366 |
|
---|
367 | //A集合的key转换成数组
|
---|
368 | Object[] keys = valsA.keySet().toArray();
|
---|
369 | //最大值数组
|
---|
370 | int[] maxA = new int[2];
|
---|
371 | //最大值对应的key的数组
|
---|
372 | Object[] maxkeyA = new Object[2];
|
---|
373 | //对于此isuue 遍历A抛出过的值的数组
|
---|
374 | for (int j = 0; j < keys.length; j++) {
|
---|
375 | //temp临时储存 A的这个值抛出的次数
|
---|
376 | Integer temp = (Integer) valsA.get(keys[j]);
|
---|
377 | //即找最大的次数以及所对应的key
|
---|
378 | if (temp.intValue() > maxA[0]) {
|
---|
379 | maxA[0] = temp.intValue();
|
---|
380 | maxkeyA[0] = keys[j];
|
---|
381 | //找第二大的次数以及所对应的key
|
---|
382 | } else if (temp.intValue() > maxA[1]) {
|
---|
383 | maxA[1] = temp.intValue();
|
---|
384 | maxkeyA[1] = keys[j];
|
---|
385 | }
|
---|
386 | }
|
---|
387 | //如果B 在此issue上 之前抛出过offer
|
---|
388 | if (valsB != null) {
|
---|
389 | //B集合的key转换成数组
|
---|
390 | Object[] keysB = valsB.keySet().toArray();
|
---|
391 | //同理
|
---|
392 | int[] maxB = new int[2];
|
---|
393 | Object[] maxkeyB = new Object[2];
|
---|
394 | for (int j = 0; j < keysB.length; j++) {
|
---|
395 | Integer temp = (Integer) valsB.get(keysB[j]);
|
---|
396 | if (temp.intValue() > maxB[0]) {
|
---|
397 | maxB[0] = temp.intValue();
|
---|
398 | maxkeyB[0] = keysB[j];
|
---|
399 | } else if (temp.intValue() > maxB[1]) {
|
---|
400 | maxB[1] = temp.intValue();
|
---|
401 | maxkeyB[1] = keysB[j];
|
---|
402 | }
|
---|
403 | }
|
---|
404 | //如果是第一次循环
|
---|
405 | if (onlyFirstFrequency == 2) {
|
---|
406 | //如果A,B俩的最高频率的值是一样的
|
---|
407 | if ((maxkeyA[0] != null) && (maxkeyB[0] != null) && (maxkeyA[0].equals(maxkeyB[0]))) {
|
---|
408 | ArrayList<Object> l = new ArrayList();
|
---|
409 | l.add(maxkeyA[0]);
|
---|
410 | l.add(Integer.valueOf(maxB[0]));
|
---|
411 | l.add(Integer.valueOf(maxA[0]));
|
---|
412 | //把第i个issue的共同列表中添加这个l结果
|
---|
413 | mutualList.add(i, l);
|
---|
414 | return true;
|
---|
415 | } else {
|
---|
416 | mutualList.add(i, null);
|
---|
417 | return false;
|
---|
418 | }
|
---|
419 | //如果是第二次循环
|
---|
420 | } else {
|
---|
421 | boolean insideloop = true;
|
---|
422 | for (int m = 0; (insideloop) && (m < 2); m++) {
|
---|
423 | for (int n = 0; (insideloop) && (n < 2); n++) {
|
---|
424 | if ((maxkeyA[m] != null) && (maxkeyB[n] != null) && (maxkeyA[m].equals(maxkeyB[n]))) {
|
---|
425 | ArrayList<Object> l = new ArrayList();
|
---|
426 | l.add(maxkeyA[m]);
|
---|
427 | l.add(Integer.valueOf(maxB[n]));
|
---|
428 | l.add(Integer.valueOf(maxA[m]));
|
---|
429 | //感觉可以改进一下从B开始, 防止A:X Y B: Y X 情况发生,而取不到Y
|
---|
430 | if ((maxkeyA[0] != null) && (maxkeyA[1] != null) && (maxkeyB[0] != null) && (maxkeyB[1] != null) && (maxkeyA[0].equals(maxkeyB[1])) && (maxkeyA[1].equals(maxkeyB[0]))) {
|
---|
431 | l.add(maxkeyB[0]);
|
---|
432 | l.add(Integer.valueOf(maxB[0]));
|
---|
433 | l.add(Integer.valueOf(maxA[1]));
|
---|
434 | }
|
---|
435 | mutualList.add(i, l);
|
---|
436 | insideloop = false;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 | if (insideloop) {
|
---|
441 | mutualList.add(i, null);
|
---|
442 | return false;
|
---|
443 | }
|
---|
444 | return true;
|
---|
445 | }
|
---|
446 |
|
---|
447 | } else {
|
---|
448 | mutualList.add(i, null);
|
---|
449 | this.oppBPreferences.getRepeatedissue().put(((Issue) dissues.get(i)).getName(), new HashMap());
|
---|
450 | return false;
|
---|
451 | }
|
---|
452 | } else {
|
---|
453 |
|
---|
454 | this.oppAPreferences.getRepeatedissue().put(((Issue) dissues.get(i)).getName(), new HashMap());
|
---|
455 | mutualList.add(i, null);
|
---|
456 | return false;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | public Bid getNNBid(ArrayList<BidUtility> oppAB) {
|
---|
461 | List<Issue> dissues = this.utilitySpace.getDomain().getIssues();
|
---|
462 | Bid maxBid = null;
|
---|
463 | double maxutility = 0.0D;
|
---|
464 | int size = 0;
|
---|
465 | int exloop = 0;
|
---|
466 | while (exloop < dissues.size()) {
|
---|
467 | int bi = chooseBestIssue();
|
---|
468 | size = 0;
|
---|
469 | while ((oppAB != null) && (oppAB.size() > size)) {
|
---|
470 | Bid b = ((BidUtility) oppAB.get(size)).getBid();
|
---|
471 | Bid newBid = new Bid(b);
|
---|
472 | try {
|
---|
473 | HashMap vals = b.getValues();
|
---|
474 | vals.put(Integer.valueOf(bi), getRandomValue((Issue) dissues.get(bi - 1)));
|
---|
475 | newBid = new Bid(this.utilitySpace.getDomain(), vals);
|
---|
476 | if ((this.utilitySpace.getUtility(newBid) > getMyutility())
|
---|
477 | && (this.utilitySpace.getUtility(newBid) > maxutility)) {
|
---|
478 | maxBid = new Bid(newBid);
|
---|
479 | maxutility = this.utilitySpace.getUtility(maxBid);
|
---|
480 | }
|
---|
481 | } catch (Exception e) {
|
---|
482 | System.out.println("Exception 66 " + e.getMessage());
|
---|
483 | }
|
---|
484 | size++;
|
---|
485 | }
|
---|
486 | exloop++;
|
---|
487 | }
|
---|
488 | return maxBid;
|
---|
489 | }
|
---|
490 |
|
---|
491 | public int chooseBestIssue() {
|
---|
492 | double random = Math.random();
|
---|
493 | double sumWeight = 0.0D;
|
---|
494 | AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) this.utilitySpace;
|
---|
495 | for (int i = utilitySpace1.getDomain().getIssues().size(); i > 0; i--) {
|
---|
496 | sumWeight += utilitySpace1.getWeight(i);
|
---|
497 | if (sumWeight > random) {
|
---|
498 | return i;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | return 0;
|
---|
502 | }
|
---|
503 |
|
---|
504 | public int chooseWorstIssue() {
|
---|
505 | double random = Math.random() * 100.0D;
|
---|
506 | double sumWeight = 0.0D;
|
---|
507 | int minin = 1;
|
---|
508 | double min = 1.0D;
|
---|
509 | AdditiveUtilitySpace utilitySpace1 = (AdditiveUtilitySpace) this.utilitySpace;
|
---|
510 | for (int i = utilitySpace1.getDomain().getIssues().size(); i > 0; i--) {
|
---|
511 | sumWeight += 1.0D / utilitySpace1.getWeight(i);
|
---|
512 | if (utilitySpace1.getWeight(i) < min) {
|
---|
513 | min = utilitySpace1.getWeight(i);
|
---|
514 | minin = i;
|
---|
515 | }
|
---|
516 | if (sumWeight > random) {
|
---|
517 | return i;
|
---|
518 | }
|
---|
519 | }
|
---|
520 | return minin;
|
---|
521 | }
|
---|
522 |
|
---|
523 | public Bid getMybestBid(Bid sugestBid) { //time没用到啊?
|
---|
524 | List<Issue> dissues = this.utilitySpace.getDomain().getIssues();
|
---|
525 | Bid newBid = new Bid(sugestBid);
|
---|
526 | int index = chooseWorstIssue();
|
---|
527 | boolean loop = true;
|
---|
528 | long bidTime = System.currentTimeMillis();
|
---|
529 | while (loop) {
|
---|
530 | if ((System.currentTimeMillis() - bidTime) * 1000L > 3L) {
|
---|
531 | break;
|
---|
532 | }
|
---|
533 | newBid = new Bid(sugestBid);
|
---|
534 | try {
|
---|
535 | HashMap map = newBid.getValues();
|
---|
536 | map.put(Integer.valueOf(index), getRandomValue((Issue) dissues.get(index - 1)));
|
---|
537 | newBid = new Bid(this.utilitySpace.getDomain(), map);
|
---|
538 | if (this.utilitySpace.getUtility(newBid) > getMyutility()) {
|
---|
539 | return newBid;
|
---|
540 | }
|
---|
541 | } catch (Exception e) {
|
---|
542 | loop = false;
|
---|
543 | }
|
---|
544 | }
|
---|
545 | return newBid;
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | /*
|
---|
550 | * 在原来的list中不存在相同的bid,则把新的bid加入list中。
|
---|
551 | * ?没有弄懂为什么要做下面的第一个if? 如果list中有utility list: 0:0.7; 1:0.5, 一个新的bid,u=0.8, 则这个list会变成 0:0.7; 1:0.8; 2:0.5
|
---|
552 | * 是不是弄错了? 大概是希望得到一个utility降序的list:0:0.8; 1:0.7; 2:0.5 大概吧?
|
---|
553 | *
|
---|
554 | */
|
---|
555 | public void addBidToList(ArrayList<BidUtility> mybids, BidUtility newbid) {
|
---|
556 | int index = mybids.size();
|
---|
557 | for (int i = 0; i < mybids.size(); i++) {
|
---|
558 | if (((BidUtility) mybids.get(i)).getUtility() <= newbid.getUtility()) { //新的offer让parsagnet更满意
|
---|
559 | if (!((BidUtility) mybids.get(i)).getBid().equals(newbid.getBid())) { //且新的offer和之前的不一样
|
---|
560 | index = i;
|
---|
561 | } else {
|
---|
562 | return;
|
---|
563 | }
|
---|
564 | } //所以else就是新的offer比原来的offer更差
|
---|
565 | }
|
---|
566 | mybids.add(index, newbid);
|
---|
567 | }
|
---|
568 |
|
---|
569 | /*
|
---|
570 | * 这个function在拿到一个对手的bid后,根据这个bid所有issue的value值,更新OpoponentPreference的repeeatedIssue中value的frequency值。
|
---|
571 | */
|
---|
572 | public void calculateParamForOpponent(OpponentPreferences op, Bid bid) {
|
---|
573 | List<Issue> dissues = this.utilitySpace.getDomain().getIssues(); //向系统读取所有issues
|
---|
574 | HashMap<Integer, Value> bidVal = bid.getValues(); //获取这个bid的所有issues的value值。 hashmap的key应该是issue的index值
|
---|
575 | Integer[] keys = new Integer[0];
|
---|
576 | keys = (Integer[]) bidVal.keySet().toArray(keys); //将issue的index值放入Integer[]。 需测试放入的顺序是否和原来一致?
|
---|
577 | for (int i = 0; i < dissues.size(); i++) {
|
---|
578 | if (op.getRepeatedissue().get(((Issue) dissues.get(i)).getName()) != null) { //如果op的repeatedIssue已经存在这个issue了
|
---|
579 | HashMap<Value, Integer> vals = (HashMap) op.getRepeatedissue().get(((Issue) dissues.get(i)).getName()); //读取这个issue的value
|
---|
580 | try {
|
---|
581 | if (vals.get(bidVal.get(keys[i])) != null) { //如果这个value原来有freq值,则+1
|
---|
582 | Integer repet = (Integer) vals.get(bidVal.get(keys[i]));
|
---|
583 | repet = Integer.valueOf(repet.intValue() + 1);
|
---|
584 | vals.put((Value) bidVal.get(keys[i]), repet);
|
---|
585 | } else { //以前没有则创建1
|
---|
586 | vals.put((Value) bidVal.get(keys[i]), new Integer(1));
|
---|
587 | }
|
---|
588 | } catch (Exception localException) {
|
---|
589 | }
|
---|
590 | } else { //如果op的repeatedIssue还不存在这个issue
|
---|
591 | HashMap<Value, Integer> h = new HashMap();
|
---|
592 | try {
|
---|
593 | h.put((Value) bidVal.get(keys[i]), new Integer(1)); //对应的value freq值为1
|
---|
594 | } catch (Exception localException1) {
|
---|
595 | }
|
---|
596 | op.getRepeatedissue().put(((Issue) dissues.get(i)).getName(), h);
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 |
|
---|
603 | //获取我当前的Utility
|
---|
604 | public double getMyutility() {
|
---|
605 | double changeU=this.changeTargetUtility;
|
---|
606 | double systemU= 1-Math.pow(this.TimeLineInfo.getTime(),5);
|
---|
607 | if(changeU>systemU){
|
---|
608 | return changeU;
|
---|
609 | }else {
|
---|
610 | return systemU;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | public double getE() {
|
---|
616 | if (this.withDiscount.booleanValue()) {
|
---|
617 | return 0.2D;
|
---|
618 | }
|
---|
619 | return 0.15D;
|
---|
620 | }
|
---|
621 |
|
---|
622 | //对手偏好类
|
---|
623 | private class OpponentPreferences {
|
---|
624 | //String是 issue 的name ,HashMap<Value,Integer>是 issue的各种选项所对应的次数
|
---|
625 | private HashMap<String, HashMap<Value, Integer>> repeatedissue = new HashMap();
|
---|
626 | private ArrayList selectedValues;
|
---|
627 | ArrayList<Agent36.BidUtility> opponentBids = new ArrayList();
|
---|
628 |
|
---|
629 | private OpponentPreferences() {
|
---|
630 | }
|
---|
631 |
|
---|
632 | public void setRepeatedissue(HashMap<String, HashMap<Value, Integer>> repeatedissue) {
|
---|
633 | this.repeatedissue = repeatedissue;
|
---|
634 | }
|
---|
635 |
|
---|
636 | public HashMap<String, HashMap<Value, Integer>> getRepeatedissue() {
|
---|
637 | return this.repeatedissue;
|
---|
638 | }
|
---|
639 |
|
---|
640 | public void setSelectedValues(ArrayList selectedValues) {
|
---|
641 | this.selectedValues = selectedValues;
|
---|
642 | }
|
---|
643 |
|
---|
644 | public ArrayList getSelectedValues() {
|
---|
645 | return this.selectedValues;
|
---|
646 | }
|
---|
647 |
|
---|
648 | public void setOpponentBids(ArrayList<Agent36.BidUtility> opponentBids) {
|
---|
649 | this.opponentBids = opponentBids;
|
---|
650 | }
|
---|
651 |
|
---|
652 | public ArrayList<Agent36.BidUtility> getOpponentBids() {
|
---|
653 | return this.opponentBids;
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | private class BidUtility {
|
---|
658 | private Bid bid;
|
---|
659 | private double utility;
|
---|
660 | private long time;
|
---|
661 |
|
---|
662 | BidUtility(Bid b, double u, long t) {
|
---|
663 | this.bid = b;
|
---|
664 | this.utility = u;
|
---|
665 | this.time = t;
|
---|
666 | }
|
---|
667 |
|
---|
668 | BidUtility(BidUtility newbid) {
|
---|
669 | this.bid = newbid.getBid();
|
---|
670 | this.utility = newbid.getUtility();
|
---|
671 | this.time = newbid.getTime();
|
---|
672 | }
|
---|
673 |
|
---|
674 | public void setBid(Bid bid) {
|
---|
675 | this.bid = bid;
|
---|
676 | }
|
---|
677 |
|
---|
678 | public Bid getBid() {
|
---|
679 | return this.bid;
|
---|
680 | }
|
---|
681 |
|
---|
682 | public void setUtility(double utility) {
|
---|
683 | this.utility = utility;
|
---|
684 | }
|
---|
685 |
|
---|
686 | public double getUtility() {
|
---|
687 | return this.utility;
|
---|
688 | }
|
---|
689 |
|
---|
690 | public void setTime(long time) {
|
---|
691 | this.time = time;
|
---|
692 | }
|
---|
693 |
|
---|
694 | public long getTime() {
|
---|
695 | return this.time;
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | @Override
|
---|
700 | public String getDescription() {
|
---|
701 | return "ANAC2018";
|
---|
702 | }
|
---|
703 | }
|
---|