source: src/main/java/genius/core/AgentAdapter.java@ 71

Last change on this file since 71 was 71, checked in by Wouter Pasman, 6 years ago

javadoc

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