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

Last change on this file since 256 was 160, checked in by Tim Baarslag, 6 years ago

Cleanup of agent names to improve the readability of the party repository

File size: 3.0 KB
Line 
1package genius.core;
2
3import java.util.List;
4
5import java.util.Date;
6import java.util.HashMap;
7import java.util.Map;
8
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.EndNegotiation;
12import genius.core.actions.Offer;
13import genius.core.actions.OfferForVoting;
14import genius.core.parties.NegotiationInfo;
15import genius.core.parties.NegotiationParty;
16import genius.core.protocol.MultilateralProtocol;
17import genius.core.protocol.StackedAlternatingOffersProtocol;
18import genius.core.tournament.VariablesAndValues.AgentParamValue;
19import genius.core.tournament.VariablesAndValues.AgentParameterVariable;
20import 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")
33public 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}
Note: See TracBrowser for help on using the repository browser.