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