1 | package geniusweb.boa;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.logging.Level;
|
---|
7 |
|
---|
8 | import geniusweb.actions.Action;
|
---|
9 | import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
|
---|
10 | import geniusweb.boa.biddingstrategy.BiddingStrategy;
|
---|
11 | import geniusweb.inform.ActionDone;
|
---|
12 | import geniusweb.inform.Finished;
|
---|
13 | import geniusweb.inform.Inform;
|
---|
14 | import geniusweb.inform.Settings;
|
---|
15 | import geniusweb.inform.YourTurn;
|
---|
16 | import geniusweb.opponentmodel.OpponentModel;
|
---|
17 | import geniusweb.party.Capabilities;
|
---|
18 | import geniusweb.party.DefaultParty;
|
---|
19 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
20 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
21 | import geniusweb.profileconnection.ProfileInterface;
|
---|
22 | import tudelft.utilities.logging.Reporter;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Default behaviour for Boa-style parties
|
---|
26 | */
|
---|
27 | public abstract class DefaultBoa extends DefaultParty {
|
---|
28 |
|
---|
29 | private BoaState boaState;
|
---|
30 | private ProfileInterface profileint = null;
|
---|
31 | private BiddingStrategy biddingStrat = null;
|
---|
32 | private AcceptanceStrategy acceptanceStrat = null;
|
---|
33 |
|
---|
34 | public DefaultBoa() {
|
---|
35 | super();
|
---|
36 | boaState = initialState();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public DefaultBoa(Reporter reporter) {
|
---|
40 | super(reporter); // for debugging
|
---|
41 | boaState = initialState();
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This only supports SAOP protocol because it is not clear how the
|
---|
46 | * BiddingStrategy could be made more general without basically becoming a
|
---|
47 | * full Party.
|
---|
48 | *
|
---|
49 | */
|
---|
50 | @Override
|
---|
51 | public Capabilities getCapabilities() {
|
---|
52 | /*
|
---|
53 | * At this moment only SAOP is supported by BOA. To support others, the
|
---|
54 | * components also need to get capabilities. FIXME limiting this to
|
---|
55 | * linearadditive may be overly restrictive #1871
|
---|
56 | */
|
---|
57 | return new Capabilities(new HashSet<>(Arrays.asList("SAOP")),
|
---|
58 | Collections.singleton(LinearAdditive.class));
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Impementation of generic BOA behaviour.
|
---|
63 | */
|
---|
64 | @Override
|
---|
65 | public void notifyChange(Inform info) {
|
---|
66 | try {
|
---|
67 | if (info instanceof Settings) {
|
---|
68 | /*
|
---|
69 | * this should be first call. We can not yet create the state
|
---|
70 | * because we need to wait for the profile fetch.
|
---|
71 | */
|
---|
72 | Settings settings = (Settings) info;
|
---|
73 | Class<? extends OpponentModel> omClass = getOpponentModel(
|
---|
74 | settings);
|
---|
75 | biddingStrat = getBiddingStrategy(settings);
|
---|
76 | acceptanceStrat = getAccceptanceStrategy(settings);
|
---|
77 | boaState = boaState.with(settings, omClass);
|
---|
78 | this.profileint = ProfileConnectionFactory
|
---|
79 | .create(settings.getProfile().getURI(), getReporter());
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Beyond this point we should have a profile
|
---|
84 | if (boaState.getProfile() == null) {
|
---|
85 | boaState = boaState.with(this.profileint.getProfile());
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (info instanceof ActionDone) {
|
---|
89 | boaState = boaState.with(((ActionDone) info).getAction());
|
---|
90 | } else if (info instanceof YourTurn) {
|
---|
91 | getConnection().send(getAction());
|
---|
92 | // we will receive ActionDone and update state then
|
---|
93 | } else if (info instanceof Finished) {
|
---|
94 | getReporter().log(Level.INFO, "Final ourcome:" + info);
|
---|
95 | terminate();
|
---|
96 | }
|
---|
97 | } catch (Exception e) {
|
---|
98 | getReporter().log(Level.SEVERE, "Failed to handle info " + info, e);
|
---|
99 | // keep running, still hoping to get Finished callback at the end
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | *
|
---|
106 | * @return current {@link BoaState}
|
---|
107 | */
|
---|
108 | public BoaState getState() {
|
---|
109 | return boaState;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Overridable getter for the acceptance strategy. Override if you want to
|
---|
114 | * hard code a {@link BoaParty} behaviour
|
---|
115 | *
|
---|
116 | * @param settings the {@link Settings}
|
---|
117 | * @return the {@link OpponentModel} class to use for modeling opponents
|
---|
118 | * @throws InstantiationFailedException if the requested opponent model can
|
---|
119 | * not be loaded
|
---|
120 | */
|
---|
121 | abstract protected Class<? extends OpponentModel> getOpponentModel(
|
---|
122 | Settings settings) throws InstantiationFailedException;
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Overridable getter for the acceptance strategy. Override if you want to
|
---|
126 | * hard code a {@link BoaParty} behaviour
|
---|
127 | *
|
---|
128 | * @param settings the {@link Settings}
|
---|
129 | * @return the {@link BiddingStrategy} to use for determining the next
|
---|
130 | * {@link Action}
|
---|
131 | * @throws InstantiationFailedException if the requested opponent model can
|
---|
132 | * not be loaded
|
---|
133 | */
|
---|
134 | abstract protected BiddingStrategy getBiddingStrategy(Settings settings)
|
---|
135 | throws InstantiationFailedException;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Overridable getter for the acceptance strategy. Override if you want to
|
---|
139 | * hard code a {@link BoaParty} behaviour
|
---|
140 | *
|
---|
141 | * @param settings the {@link Settings}
|
---|
142 | * @return the {@link AcceptanceStrategy} to use for determining if a bid is
|
---|
143 | * acceptable
|
---|
144 | * @throws InstantiationFailedException if the requested opponent model can
|
---|
145 | * not be loaded
|
---|
146 | */
|
---|
147 | abstract protected AcceptanceStrategy getAccceptanceStrategy(
|
---|
148 | Settings settings) throws InstantiationFailedException;
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Factory method to create initial state. Override to insert mock
|
---|
152 | *
|
---|
153 | * @return initial BoaState
|
---|
154 | */
|
---|
155 | protected BoaState initialState() {
|
---|
156 | return new BoaState(getReporter());
|
---|
157 | }
|
---|
158 |
|
---|
159 | private Action getAction() {
|
---|
160 | Action proposedAction = biddingStrat.getAction(boaState);
|
---|
161 | return acceptanceStrat.filter(proposedAction, boaState);
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|