1 | package negotiator.parties;
|
---|
2 |
|
---|
3 | import java.io.FileNotFoundException;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import genius.core.AgentID;
|
---|
11 | import genius.core.Bid;
|
---|
12 | import genius.core.Domain;
|
---|
13 | import genius.core.actions.Action;
|
---|
14 | import genius.core.actions.Offer;
|
---|
15 | import genius.core.issue.Issue;
|
---|
16 | import genius.core.issue.IssueDiscrete;
|
---|
17 | import genius.core.issue.Value;
|
---|
18 | import genius.core.logging.CsvLogger;
|
---|
19 | import genius.core.parties.AbstractNegotiationParty;
|
---|
20 | import genius.core.parties.NegotiationInfo;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Debug class for enumerating all offers to a file
|
---|
24 | */
|
---|
25 | public class EnumeratorParty extends AbstractNegotiationParty {
|
---|
26 |
|
---|
27 | public static int id = 0;
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public void init(NegotiationInfo info) {
|
---|
31 | super.init(info);
|
---|
32 |
|
---|
33 | // vars
|
---|
34 | Domain dom = getUtilitySpace().getDomain();
|
---|
35 | List<Integer> issueSizes = new ArrayList<Integer>();
|
---|
36 | for (Issue issue : dom.getIssues())
|
---|
37 | issueSizes.add(((IssueDiscrete) issue).getNumberOfValues());
|
---|
38 |
|
---|
39 | System.out.printf("Enumerating all %d possible offers\n", dom.getNumberOfPossibleBids());
|
---|
40 | System.out.printf("There are %d issues with the following number of values: %s\n", dom.getIssues().size(),
|
---|
41 | issueSizes.toString());
|
---|
42 | try {
|
---|
43 | String party = "unknown";
|
---|
44 | switch (id) {
|
---|
45 | case 0:
|
---|
46 | party = "Minister";
|
---|
47 | break;
|
---|
48 | case 1:
|
---|
49 | party = "NS";
|
---|
50 | break;
|
---|
51 | case 2:
|
---|
52 | party = "ProRail";
|
---|
53 | break;
|
---|
54 | }
|
---|
55 |
|
---|
56 | id++;
|
---|
57 | CsvLogger logger = new CsvLogger(String.format("Railway-%s-utils.csv", party));
|
---|
58 |
|
---|
59 | for (int issueIndex = 0; issueIndex < issueSizes.size(); issueIndex++) {
|
---|
60 | logger.log(String.format("\"Issue %d\"", issueIndex + 1));
|
---|
61 | }
|
---|
62 | logger.logLine("utility");
|
---|
63 |
|
---|
64 | for (int bidIndex = 0; bidIndex < dom.getNumberOfPossibleBids(); bidIndex++) {
|
---|
65 | Integer[] issueIndices = new Integer[issueSizes.size()];
|
---|
66 | Arrays.fill(issueIndices, 0);
|
---|
67 | int remainder = bidIndex;
|
---|
68 | int currentIndex = 0;
|
---|
69 |
|
---|
70 | while (remainder > 0) {
|
---|
71 | int size = issueSizes.get(currentIndex);
|
---|
72 | int div = remainder / size;
|
---|
73 | int mod = remainder % size;
|
---|
74 |
|
---|
75 | issueIndices[currentIndex] = mod;
|
---|
76 | remainder = div;
|
---|
77 | currentIndex++;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Bid bid = generateBid(issueIndices);
|
---|
81 | double util = getUtility(bid);
|
---|
82 |
|
---|
83 | System.out.printf("%s -> %.3f\n", new ArrayList<Integer>(Arrays.asList(issueIndices)), util);
|
---|
84 | for (Integer issueIndex : issueIndices) {
|
---|
85 | logger.log(issueIndex);
|
---|
86 | }
|
---|
87 | logger.logLine(util);
|
---|
88 | }
|
---|
89 |
|
---|
90 | logger.close();
|
---|
91 | } catch (FileNotFoundException e) {
|
---|
92 | System.err.println("Problems starting EnumeratorParty logger");
|
---|
93 | e.printStackTrace();
|
---|
94 | } catch (IOException e) {
|
---|
95 | System.err.println("Problems closing EnumeratorParty logger");
|
---|
96 | e.printStackTrace();
|
---|
97 | }
|
---|
98 |
|
---|
99 | System.out.println("Enumeration finished\n");
|
---|
100 | }
|
---|
101 |
|
---|
102 | public Bid generateBid(Integer[] indices) {
|
---|
103 |
|
---|
104 | HashMap<Integer, Value> values = new HashMap<Integer, Value>();
|
---|
105 |
|
---|
106 | for (int i = 0; i < indices.length; i++) {
|
---|
107 | IssueDiscrete issue = (IssueDiscrete) getUtilitySpace().getDomain().getIssues().get(i);
|
---|
108 | values.put(i + 1, issue.getValue(indices[i]));
|
---|
109 | }
|
---|
110 |
|
---|
111 | try {
|
---|
112 | return new Bid(getUtilitySpace().getDomain(), values);
|
---|
113 | } catch (Exception e) {
|
---|
114 | System.err.println("Could not generate offer");
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public Action chooseAction(List<Class<? extends Action>> possibleActions) {
|
---|
121 | return new Offer(getPartyId(), generateRandomBid());
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public void receiveMessage(AgentID sender, Action arguments) {
|
---|
126 | // do nothing
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public String getDescription() {
|
---|
131 | return "Enumerator Party for Debug";
|
---|
132 | }
|
---|
133 | }
|
---|