[1] | 1 | package genius.core;
|
---|
| 2 |
|
---|
[160] | 3 | import java.util.List;
|
---|
| 4 |
|
---|
[1] | 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 | /**
|
---|
[160] | 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
|
---|
[1] | 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 | */
|
---|
[71] | 32 | @SuppressWarnings("serial")
|
---|
[1] | 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 |
|
---|
[71] | 44 | @SuppressWarnings("deprecation")
|
---|
[1] | 45 | @Override
|
---|
| 46 | public final void init(NegotiationInfo info) {
|
---|
| 47 | this.utilSpace = info.getUtilitySpace();
|
---|
[71] | 48 | getAgent().internalInit(0, 1, new Date(),
|
---|
| 49 | info.getDeadline().getTimeOrDefaultTimeout(),
|
---|
| 50 | info.getTimeline(), utilSpace,
|
---|
| 51 | new HashMap<AgentParameterVariable, AgentParamValue>(),
|
---|
| 52 | info.getAgentID());
|
---|
[1] | 53 | getAgent().setName(info.getAgentID().toString());
|
---|
| 54 | getAgent().init();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[71] | 57 | @SuppressWarnings("deprecation")
|
---|
[1] | 58 | @Override
|
---|
[71] | 59 | public final Action chooseAction(
|
---|
| 60 | List<Class<? extends Action>> possibleActions) {
|
---|
[1] | 61 | lastAction = getAgent().chooseAction();
|
---|
| 62 | return lastAction;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[71] | 65 | @SuppressWarnings("deprecation")
|
---|
[1] | 66 | @Override
|
---|
| 67 | public final void receiveMessage(AgentID sender, Action action) {
|
---|
[71] | 68 | if (action instanceof Offer || action instanceof Accept
|
---|
| 69 | || action instanceof OfferForVoting
|
---|
[1] | 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 |
|
---|
[71] | 89 | @SuppressWarnings("deprecation")
|
---|
[1] | 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 | }
|
---|
[71] | 99 | getAgent().endSession(
|
---|
| 100 | new NegotiationResult(util, lastAction, acceptedBid));
|
---|
[1] | 101 | return null;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | }
|
---|