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

Last change on this file since 27 was 1, checked in by Wouter Pasman, 7 years ago

Initial import : Genius 9.0.0

File size: 2.8 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 */
31public abstract class AgentAdapter implements NegotiationParty {
32
33 /**
34 *
35 * @return the actual agent that is being adapted.
36 */
37 abstract protected Agent getAgent();
38
39 private Action lastAction = null;
40 private AbstractUtilitySpace utilSpace;
41
42 @Override
43 public final void init(NegotiationInfo info) {
44 this.utilSpace = info.getUtilitySpace();
45 getAgent().internalInit(0, 1, new Date(), info.getDeadline().getTimeOrDefaultTimeout(), info.getTimeline(),
46 utilSpace, new HashMap<AgentParameterVariable, AgentParamValue>(), info.getAgentID());
47 getAgent().setName(info.getAgentID().toString());
48 getAgent().init();
49 }
50
51 @Override
52 public final Action chooseAction(List<Class<? extends Action>> possibleActions) {
53 lastAction = getAgent().chooseAction();
54 return lastAction;
55 }
56
57 @Override
58 public final void receiveMessage(AgentID sender, Action action) {
59 if (action instanceof Offer || action instanceof Accept || action instanceof OfferForVoting
60 || action instanceof EndNegotiation) {
61 getAgent().ReceiveMessage(action);
62 }
63 }
64
65 /**
66 * This is a convenience wrapper so that we don't have to fix all old agent
67 * descriptions (these used to be in the xml file)
68 */
69 @Override
70 public String getDescription() {
71 return "Agent " + getAgent().getClass().getSimpleName();
72 }
73
74 @Override
75 public final Class<? extends MultilateralProtocol> getProtocol() {
76 return StackedAlternatingOffersProtocol.class;
77 }
78
79 @Override
80 public final Map<String, String> negotiationEnded(Bid acceptedBid) {
81 double util = 0;
82 if (acceptedBid != null) {
83 try {
84 util = getAgent().getUtility(acceptedBid);
85 } catch (Exception e) {
86 }
87 }
88 getAgent().endSession(new NegotiationResult(util, lastAction, acceptedBid));
89 return null;
90 }
91
92}
Note: See TracBrowser for help on using the repository browser.