1 | package genius.core;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.Date;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import genius.core.actions.Accept;
|
---|
10 | import genius.core.actions.Action;
|
---|
11 | import genius.core.actions.EndNegotiation;
|
---|
12 | import genius.core.actions.Offer;
|
---|
13 | import genius.core.actions.OfferForVoting;
|
---|
14 | import genius.core.parties.NegotiationInfo;
|
---|
15 | import genius.core.parties.NegotiationParty;
|
---|
16 | import genius.core.protocol.MultilateralProtocol;
|
---|
17 | import genius.core.protocol.StackedAlternatingOffersProtocol;
|
---|
18 | import genius.core.tournament.VariablesAndValues.AgentParamValue;
|
---|
19 | import genius.core.tournament.VariablesAndValues.AgentParameterVariable;
|
---|
20 | import genius.core.utility.AbstractUtilitySpace;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Adapts {@link Agent} to the {@link NegotiationParty} so that bilateral agents
|
---|
24 | * can be run as a multiparty system. Notice that these agents can
|
---|
25 | * handle only 1 opponent, and thus may behave weird if presented with more than
|
---|
26 | * one opponent.
|
---|
27 | *
|
---|
28 | * What is unusual (in the Java sense) is that Agent extends this, not the other
|
---|
29 | * way round. This way, all old agents also become a NegotiationParty.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | @SuppressWarnings("serial")
|
---|
33 | public abstract class AgentAdapter implements NegotiationParty {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | *
|
---|
37 | * @return the actual agent that is being adapted.
|
---|
38 | */
|
---|
39 | abstract protected Agent getAgent();
|
---|
40 |
|
---|
41 | private Action lastAction = null;
|
---|
42 | private AbstractUtilitySpace utilSpace;
|
---|
43 |
|
---|
44 | @SuppressWarnings("deprecation")
|
---|
45 | @Override
|
---|
46 | public final void init(NegotiationInfo info) {
|
---|
47 | this.utilSpace = info.getUtilitySpace();
|
---|
48 | getAgent().internalInit(0, 1, new Date(),
|
---|
49 | info.getDeadline().getTimeOrDefaultTimeout(),
|
---|
50 | info.getTimeline(), utilSpace,
|
---|
51 | new HashMap<AgentParameterVariable, AgentParamValue>(),
|
---|
52 | info.getAgentID());
|
---|
53 | getAgent().setName(info.getAgentID().toString());
|
---|
54 | getAgent().init();
|
---|
55 | }
|
---|
56 |
|
---|
57 | @SuppressWarnings("deprecation")
|
---|
58 | @Override
|
---|
59 | public final Action chooseAction(
|
---|
60 | List<Class<? extends Action>> possibleActions) {
|
---|
61 | lastAction = getAgent().chooseAction();
|
---|
62 | return lastAction;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @SuppressWarnings("deprecation")
|
---|
66 | @Override
|
---|
67 | public final void receiveMessage(AgentID sender, Action action) {
|
---|
68 | if (action instanceof Offer || action instanceof Accept
|
---|
69 | || action instanceof OfferForVoting
|
---|
70 | || action instanceof EndNegotiation) {
|
---|
71 | getAgent().ReceiveMessage(action);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * This is a convenience wrapper so that we don't have to fix all old agent
|
---|
77 | * descriptions (these used to be in the xml file)
|
---|
78 | */
|
---|
79 | @Override
|
---|
80 | public String getDescription() {
|
---|
81 | return "Agent " + getAgent().getClass().getSimpleName();
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public final Class<? extends MultilateralProtocol> getProtocol() {
|
---|
86 | return StackedAlternatingOffersProtocol.class;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @SuppressWarnings("deprecation")
|
---|
90 | @Override
|
---|
91 | public final Map<String, String> negotiationEnded(Bid acceptedBid) {
|
---|
92 | double util = 0;
|
---|
93 | if (acceptedBid != null) {
|
---|
94 | try {
|
---|
95 | util = getAgent().getUtility(acceptedBid);
|
---|
96 | } catch (Exception e) {
|
---|
97 | }
|
---|
98 | }
|
---|
99 | getAgent().endSession(
|
---|
100 | new NegotiationResult(util, lastAction, acceptedBid));
|
---|
101 | return null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|