source: ai2020/group5/src/main/java/DefaultMopacBoa.java@ 3

Last change on this file since 3 was 2, checked in by wouter, 4 years ago

#1925 added aitechniques group5

File size: 6.1 KB
RevLine 
[2]1import java.io.IOException;
2import java.util.Collections;
3import java.util.List;
4import java.util.Set;
5import java.util.logging.Level;
6import java.util.stream.Collectors;
7
8import geniusweb.actions.*;
9import geniusweb.boa.InstantiationFailedException;
10import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
11import geniusweb.boa.acceptancestrategy.TimeDependentAcceptanceStrategy;
12import geniusweb.boa.biddingstrategy.BiddingStrategy;
13import geniusweb.boa.biddingstrategy.TimeDependentBiddingStrategy;
14import geniusweb.inform.*;
15import geniusweb.issuevalue.Bid;
16import geniusweb.opponentmodel.FrequencyOpponentModel;
17import geniusweb.opponentmodel.OpponentModel;
18import geniusweb.party.Capabilities;
19import geniusweb.party.DefaultParty;
20import geniusweb.profile.Profile;
21import geniusweb.profileconnection.ProfileConnectionFactory;
22import geniusweb.profileconnection.ProfileInterface;
23import tudelft.utilities.logging.Reporter;
24
25
26public class DefaultMopacBoa extends DefaultParty {
27 private Group5BoaState negoState;
28 private ProfileInterface profileint = null;
29 private Votes lastVotes;
30
31 public DefaultMopacBoa() {
32 super();
33 negoState = initialState();
34 }
35
36 public DefaultMopacBoa(Reporter reporter) {
37 super(reporter); // for debugging
38 negoState = initialState();
39 }
40
41 @Override
42 public Capabilities getCapabilities() {
43 return new Capabilities(Collections.singleton("MOPAC"), Collections.singleton(Profile.class));
44 }
45
46 @Override
47 public void notifyChange(Inform info) {
48 try {
49 if (info instanceof Settings) {
50 /*
51 * this should be first call. We can not yet create the state
52 * because we need to wait for the profile fetch.
53 */
54 Settings settings = (Settings) info;
55 Class<? extends OpponentModel> omClass = getOpponentModel(
56 settings);
57 BiddingStrategy bidStrat = getBiddingStrategy(settings);
58 AcceptanceStrategy acceptStrat = getAccceptanceStrategy(
59 settings);
60 negoState = negoState.with(settings, bidStrat, acceptStrat,
61 omClass);
62 this.profileint = ProfileConnectionFactory
63 .create(settings.getProfile().getURI(), getReporter());
64 return;
65 }
66
67 // Beyond this point we should have a profile
68 if (negoState.getProfile() == null) {
69 negoState = negoState.with(this.profileint.getProfile());
70 }
71
72 if (info instanceof ActionDone) {
73 // All actions already have been updated in the voting phase
74// negoState = negoState.with(((ActionDone) info).getAction());
75 } else if (info instanceof YourTurn) {
76 getConnection().send(getAction());
77 // we will receive ActionDone and update state then
78 } else if (info instanceof Voting) {
79 lastVotes = vote((Voting) info);
80 getConnection().send(lastVotes);
81 } else if (info instanceof OptIn) {
82 getConnection().send(lastVotes);
83 } else if (info instanceof Finished) {
84 getReporter().log(Level.INFO, "Final outcome:" + info);
85 }
86 } catch (Exception e) {
87 throw new RuntimeException("Failed to handle info " + info, e);
88 }
89
90 }
91
92 /**
93 * Factory method to create initial state. Override to insert mock
94 *
95 * @return initial BoaState
96 */
97 protected Group5BoaState initialState() {
98 return new Group5BoaState(getReporter());
99 }
100
101 protected Class<? extends OpponentModel> getOpponentModel(Settings settings)
102 throws InstantiationFailedException {
103 return FrequencyOpponentModel.class;
104 }
105
106 protected BiddingStrategy getBiddingStrategy(Settings settings)
107 throws InstantiationFailedException {
108 return new TimeDependentBiddingStrategy();
109 }
110
111 protected AcceptanceStrategy getAccceptanceStrategy(Settings settings)
112 throws InstantiationFailedException {
113 return new TimeDependentAcceptanceStrategy();
114 }
115
116 @Override
117 public String getDescription() {
118 return "BOA implementation of Group 5.";
119 }
120
121 /**
122 * @return the most recent bid that was offered, or null if no offer has
123 * been done yet.
124 */
125 protected Bid getLastBid() {
126 List<Action> actions = negoState.getActionHistory();
127 for (int n = actions.size() - 1; n >= 0; n--) {
128 Action action = actions.get(n);
129 if (action instanceof Offer) {
130 return ((Offer) action).getBid();
131 }
132 }
133 return null;
134 }
135
136 private Action getAction() {
137 Bid lastBid = getLastBid();
138 if (lastBid != null && negoState.isAcceptable(lastBid)) {
139 return new Accept(negoState.getSettings().getID(), lastBid);
140 }
141 return negoState.getAction();
142 }
143
144 /**
145 * @param voting the {@link Voting} object containing the options
146 *
147 * @return our next Votes.
148 */
149 private Votes vote(Voting voting) throws IOException, InstantiationFailedException {
150 for (Offer offer : voting.getBids()) {
151 negoState = negoState.with(offer);
152 }
153 Object val = negoState.getSettings().getParameters().get("minPower");
154 Integer minpower = (val instanceof Integer) ? (Integer) val : 2;
155 val = negoState.getSettings().getParameters().get("maxPower");
156 Integer maxpower = (val instanceof Integer) ? (Integer) val
157 : Integer.MAX_VALUE;
158
159 Set<Vote> votes = voting.getBids().stream().distinct()
160 .filter(offer -> negoState.isAcceptable(offer.getBid()))
161 .map(offer -> new Vote(negoState.getSettings().getID(), offer.getBid(), minpower, maxpower))
162 .collect(Collectors.toSet());
163 return new Votes(negoState.getSettings().getID(), votes);
164 }
165}
Note: See TracBrowser for help on using the repository browser.