[175] | 1 | package exampleagentstest;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertNotNull;
|
---|
| 4 | import static org.mockito.Mockito.mock;
|
---|
| 5 | import static org.mockito.Mockito.when;
|
---|
| 6 |
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.util.Arrays;
|
---|
[177] | 9 | import java.util.HashMap;
|
---|
[175] | 10 | import java.util.List;
|
---|
[177] | 11 | import java.util.Random;
|
---|
[175] | 12 |
|
---|
| 13 | import org.junit.Before;
|
---|
| 14 | import org.junit.Test;
|
---|
| 15 |
|
---|
| 16 | import genius.core.AgentID;
|
---|
[177] | 17 | import genius.core.Bid;
|
---|
[175] | 18 | import genius.core.DomainImpl;
|
---|
| 19 | import genius.core.Global;
|
---|
| 20 | import genius.core.actions.Accept;
|
---|
| 21 | import genius.core.actions.Action;
|
---|
| 22 | import genius.core.actions.EndNegotiation;
|
---|
| 23 | import genius.core.actions.Offer;
|
---|
| 24 | import genius.core.exceptions.InstantiateException;
|
---|
[177] | 25 | import genius.core.issue.Issue;
|
---|
| 26 | import genius.core.issue.IssueDiscrete;
|
---|
| 27 | import genius.core.issue.IssueInteger;
|
---|
| 28 | import genius.core.issue.IssueReal;
|
---|
| 29 | import genius.core.issue.Value;
|
---|
| 30 | import genius.core.issue.ValueInteger;
|
---|
| 31 | import genius.core.issue.ValueReal;
|
---|
[175] | 32 | import genius.core.parties.NegotiationInfo;
|
---|
| 33 | import genius.core.parties.NegotiationParty;
|
---|
| 34 | import genius.core.persistent.PersistentDataContainer;
|
---|
[177] | 35 | import genius.core.timeline.TimeLineInfo;
|
---|
[175] | 36 | import genius.core.utility.AbstractUtilitySpace;
|
---|
| 37 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Test an example agent: try to load it, init it etc.
|
---|
| 41 | */
|
---|
| 42 | public abstract class AgentTest {
|
---|
| 43 |
|
---|
| 44 | private NegotiationParty party;
|
---|
| 45 | private Class<? extends NegotiationParty> partyclass;
|
---|
| 46 | private final String PARTY = "src/test/resources/partydomain/";
|
---|
| 47 | List<Class<? extends Action>> actions = Arrays.asList(Accept.class,
|
---|
| 48 | Offer.class, EndNegotiation.class);
|
---|
| 49 | private NegotiationInfo info;
|
---|
[176] | 50 | protected PersistentDataContainer persistentData;
|
---|
[177] | 51 | private AbstractUtilitySpace utilspace;
|
---|
| 52 | private Random rand = new Random();
|
---|
[175] | 53 |
|
---|
| 54 | public AgentTest(Class<? extends NegotiationParty> partyclass) {
|
---|
| 55 | this.partyclass = partyclass;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Before
|
---|
| 59 | public void before() throws InstantiateException, IOException {
|
---|
| 60 | party = (NegotiationParty) Global
|
---|
| 61 | .loadObject(partyclass.getCanonicalName());
|
---|
| 62 |
|
---|
| 63 | info = mock(NegotiationInfo.class);
|
---|
| 64 | when(info.getAgentID()).thenReturn(new AgentID("test"));
|
---|
[176] | 65 | persistentData = mock(PersistentDataContainer.class);
|
---|
[175] | 66 | when(info.getPersistentData()).thenReturn(persistentData);
|
---|
[177] | 67 | utilspace = createUtilSpace();
|
---|
| 68 | when(info.getUtilitySpace()).thenReturn(utilspace);
|
---|
| 69 | TimeLineInfo timeline = mock(TimeLineInfo.class);
|
---|
| 70 | when(timeline.getTime()).thenReturn(0.2);
|
---|
| 71 | when(info.getTimeline()).thenReturn(timeline);
|
---|
[175] | 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | @Test
|
---|
| 76 | public void loadClassTest() throws InstantiateException {
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | @Test
|
---|
| 80 | public void getDescriptionTest() {
|
---|
| 81 | assertNotNull(party.getDescription());
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | @Test
|
---|
| 85 | public void getProtocolTest() {
|
---|
| 86 | assertNotNull(party.getProtocol());
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | @Test
|
---|
| 90 | public void initTest() throws IOException {
|
---|
| 91 | party.init(info);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Test
|
---|
| 95 | public void getActionTest() {
|
---|
| 96 | party.init(info);
|
---|
| 97 | party.chooseAction(actions);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Test
|
---|
| 101 | public void receiveMessageTest() {
|
---|
| 102 | party.init(info);
|
---|
| 103 | AgentID other = new AgentID("other");
|
---|
[177] | 104 | party.receiveMessage(other, new Offer(other, generateRandomBid()));
|
---|
[175] | 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
[177] | 108 | * this is tricky. We need a full-fledged utilspace as the agent gets
|
---|
[175] | 109 | * cracking on it. mocking that would be difficult.
|
---|
| 110 | *
|
---|
| 111 | * @throws IOException
|
---|
| 112 | */
|
---|
| 113 | private AbstractUtilitySpace createUtilSpace() throws IOException {
|
---|
| 114 | DomainImpl domain = new DomainImpl(PARTY + "party_domain.xml");
|
---|
| 115 | return new AdditiveUtilitySpace(domain, PARTY + "party1_utility.xml");
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[177] | 118 | protected Bid generateRandomBid() {
|
---|
| 119 | try {
|
---|
| 120 | // Pairs <issue number, chosen value string>
|
---|
| 121 | HashMap<Integer, Value> values = new HashMap<Integer, Value>();
|
---|
| 122 |
|
---|
| 123 | // For each issue, put a random value
|
---|
| 124 | for (Issue currentIssue : utilspace.getDomain().getIssues()) {
|
---|
| 125 | values.put(currentIssue.getNumber(),
|
---|
| 126 | getRandomValue(currentIssue));
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // return the generated bid
|
---|
| 130 | return new Bid(utilspace.getDomain(), values);
|
---|
| 131 |
|
---|
| 132 | } catch (Exception e) {
|
---|
| 133 |
|
---|
| 134 | // return empty bid if an error occurred
|
---|
| 135 | return new Bid(utilspace.getDomain());
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Gets a random value for the given issue.
|
---|
| 141 | *
|
---|
| 142 | * @param currentIssue
|
---|
| 143 | * The issue to generate a random value for
|
---|
| 144 | * @return The random value generated for the issue
|
---|
| 145 | * @throws Exception
|
---|
| 146 | * if the issues type is not Discrete, Real or Integer.
|
---|
| 147 | */
|
---|
| 148 | protected Value getRandomValue(Issue currentIssue) throws Exception {
|
---|
| 149 |
|
---|
| 150 | Value currentValue;
|
---|
| 151 | int index;
|
---|
| 152 |
|
---|
| 153 | switch (currentIssue.getType()) {
|
---|
| 154 | case DISCRETE:
|
---|
| 155 | IssueDiscrete discreteIssue = (IssueDiscrete) currentIssue;
|
---|
| 156 | index = (rand.nextInt(discreteIssue.getNumberOfValues()));
|
---|
| 157 | currentValue = discreteIssue.getValue(index);
|
---|
| 158 | break;
|
---|
| 159 | case REAL:
|
---|
| 160 | IssueReal realIss = (IssueReal) currentIssue;
|
---|
| 161 | index = rand.nextInt(realIss.getNumberOfDiscretizationSteps()); // check
|
---|
| 162 | // this!
|
---|
| 163 | currentValue = new ValueReal(realIss.getLowerBound()
|
---|
| 164 | + (((realIss.getUpperBound() - realIss.getLowerBound()))
|
---|
| 165 | / (realIss.getNumberOfDiscretizationSteps()))
|
---|
| 166 | * index);
|
---|
| 167 | break;
|
---|
| 168 | case INTEGER:
|
---|
| 169 | IssueInteger integerIssue = (IssueInteger) currentIssue;
|
---|
| 170 | index = rand.nextInt(integerIssue.getUpperBound()
|
---|
| 171 | - integerIssue.getLowerBound() + 1);
|
---|
| 172 | currentValue = new ValueInteger(
|
---|
| 173 | integerIssue.getLowerBound() + index);
|
---|
| 174 | break;
|
---|
| 175 | default:
|
---|
| 176 | throw new Exception(
|
---|
| 177 | "issue type " + currentIssue.getType() + " not supported");
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return currentValue;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[175] | 183 | }
|
---|