source: anac2020/HammingAgent/src/main/java/HammingAgent.java@ 77

Last change on this file since 77 was 39, checked in by wouter, 3 years ago

#3

File size: 5.9 KB
Line 
1import java.io.IOException;
2import java.math.BigInteger;
3import java.util.Arrays;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Map;
9import java.util.Random;
10import java.util.Set;
11import java.util.logging.Level;
12
13import geniusweb.actions.Accept;
14import geniusweb.actions.Action;
15import geniusweb.actions.Offer;
16import geniusweb.actions.PartyId;
17import geniusweb.bidspace.AllBidsList;
18import geniusweb.inform.ActionDone;
19import geniusweb.inform.Finished;
20import geniusweb.inform.Inform;
21import geniusweb.inform.Settings;
22import geniusweb.inform.YourTurn;
23import geniusweb.issuevalue.Bid;
24import geniusweb.issuevalue.Value;
25import geniusweb.issuevalue.ValueSet;
26import geniusweb.party.Capabilities;
27import geniusweb.party.DefaultParty;
28import geniusweb.profile.DefaultPartialOrdering;
29import geniusweb.profile.Profile;
30import geniusweb.profileconnection.ProfileConnectionFactory;
31import geniusweb.profileconnection.ProfileInterface;
32import geniusweb.progress.Progress;
33import geniusweb.progress.ProgressRounds;
34import tudelft.utilities.logging.Reporter;
35
36public class HammingAgent extends DefaultParty {
37 private Bid lastReceivedBid = null;
38 private Set<String> issues;
39 private int n_issues;
40 private final Map<String, Value> maxBid = new HashMap<>();
41 private final Map<String, Value> minBid = new HashMap<>();
42 private List<Bid> SortedBid = null;
43 private final Map<String, ValueSet> values = new HashMap<>();
44 private PartyId me;
45 private final Random random = new Random();
46 protected ProfileInterface profileint;
47 private Progress progress;
48
49 public HammingAgent() {
50 }
51
52 public HammingAgent(Reporter reporter) {
53 super(reporter);
54 }
55
56 @Override
57 public void notifyChange(Inform info) {
58 try {
59 if (info instanceof Settings) {
60 Settings settings = (Settings) info;
61 this.profileint = ProfileConnectionFactory.create(settings.getProfile().getURI(), getReporter());
62 this.me = settings.getID();
63 this.progress = settings.getProgress();
64 if (this.profileint.getProfile() instanceof DefaultPartialOrdering) {
65 // Partial時だけ呼び出す
66 this.initProfile((DefaultPartialOrdering) this.profileint.getProfile());
67 }
68 } else if (info instanceof ActionDone) {
69 Action otheract = ((ActionDone) info).getAction();
70 if (otheract instanceof Offer && otheract.getActor() != me) {
71 lastReceivedBid = ((Offer) otheract).getBid();
72 }
73 } else if (info instanceof YourTurn) {
74 myTurn();
75 } else if (info instanceof Finished) {
76 getReporter().log(Level.INFO, "Final outcome:" + info);
77 }
78 } catch (Exception e) {
79 throw new RuntimeException("Failed to handle info", e);
80 }
81 }
82
83 @Override
84 public Capabilities getCapabilities() {
85 return new Capabilities(new HashSet<>(Arrays.asList("SHAOP")), Collections.singleton(Profile.class));
86 }
87
88 @Override
89 public String getDescription() {
90 return "Without elicitation agent";
91 }
92
93 private void initProfile(DefaultPartialOrdering profile) {
94 SortedBid = getSortedBids(profile);
95 Bid maxBid = SortedBid.get(0);
96 Bid minBid = SortedBid.get(SortedBid.size() - 1);
97 issues = profile.getDomain().getIssues();
98 n_issues = issues.size();
99 for (String i : issues) {
100 this.maxBid.put(i, maxBid.getValue(i));
101 this.minBid.put(i, minBid.getValue(i));
102 this.values.put(i, profile.getDomain().getValues(i));
103 }
104 }
105
106 private static List<Bid> getSortedBids(DefaultPartialOrdering prof) {
107 List<Bid> bidslist = prof.getBids();
108 // 効用値降順にソート
109 bidslist.sort((b1, b2) -> prof.isPreferredOrEqual(b1, b2) ? -1 : 1);
110 return bidslist;
111 }
112
113 private void myTurn() throws IOException {
114 Action action = SortedBid == null ? randomBid()
115 : (isAcceptable() ? new Accept(me, lastReceivedBid) : makeBid());
116 if (progress instanceof ProgressRounds) {
117 progress = ((ProgressRounds) progress).advance();
118 }
119 getConnection().send(action);
120 }
121
122 private boolean isAcceptable() {
123 if (lastReceivedBid == null)
124 return false;
125 if (progress instanceof ProgressRounds) {
126 ProgressRounds rounds = (ProgressRounds) progress;
127 if (rounds.getCurrentRound() + 1 == rounds.getTotalRounds()) {
128 return true;
129 }
130 }
131 int max_count = 0;
132 int min_count = 0;
133 for (String i : issues) {
134 Value value = lastReceivedBid.getValue(i);
135 if (value.equals(maxBid.get(i))) {
136 max_count++;
137 } else if (value.equals(minBid.get(i))) {
138 min_count++;
139 if (min_count >= threshold()) {
140 max_count--;
141 min_count = 0;
142 }
143 }
144 }
145 return max_count > n_issues - threshold();
146 }
147
148 private int threshold() {
149 int n;
150 Double time = progress.get(System.currentTimeMillis());
151 if (time <= 0.3) {
152 n = 1;
153 } else if (time <= 0.5) {
154 n = Math.min(2, (int) Math.ceil((double) n_issues / 3));
155 } else if (time <= 0.7) {
156 n = Math.min(3, (int) Math.ceil((double) n_issues / 3));
157 } else if (time <= 0.9) {
158 n = Math.min(4, (int) Math.ceil((double) n_issues / 3));
159 } else {
160 n = Math.min(4, (int) Math.ceil((double) n_issues / 2));
161 }
162 return n;
163 }
164
165 private Offer makeBid() {
166 int count = 0;
167 Set<Integer> integerSet = new HashSet<>();
168
169 for (int i = 0; i < threshold(); i++) {
170 integerSet.add(random.nextInt(n_issues));
171 }
172
173 Map<String, Value> bid = new HashMap<>();
174 for (String i : issues) {
175 if (integerSet.contains(count)) {
176 ValueSet issueValues = values.get(i);
177 Value minValue = minBid.get(i);
178 int nextValueNum;
179 Value nextValue;
180 do {
181 nextValueNum = random.nextInt(issueValues.size().intValue());
182 nextValue = issueValues.get(nextValueNum);
183 } while (nextValue.equals(minValue));
184 bid.put(i, nextValue);
185 } else {
186 bid.put(i, maxBid.get(i));
187 }
188 count++;
189 }
190 return new Offer(me, new Bid(bid));
191 }
192
193 private Offer randomBid() throws IOException {
194 AllBidsList bidspace = new AllBidsList(profileint.getProfile().getDomain());
195 long i = random.nextInt(bidspace.size().intValue());
196 Bid bid = bidspace.get(BigInteger.valueOf(i));
197 return new Offer(me, bid);
198 }
199}
Note: See TracBrowser for help on using the repository browser.