1 | package genius.core.events;
|
---|
2 |
|
---|
3 |
|
---|
4 | import static java.lang.String.format;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import genius.core.Bid;
|
---|
14 | import genius.core.analysis.MultilateralAnalysis;
|
---|
15 | import genius.core.logging.CsvLogger;
|
---|
16 | import genius.core.parties.NegotiationParty;
|
---|
17 | import genius.core.parties.NegotiationPartyInternal;
|
---|
18 | import genius.core.session.Session;
|
---|
19 | import genius.core.utility.AbstractUtilitySpace;
|
---|
20 | import genius.gui.progress.DataKey;
|
---|
21 | import genius.gui.progress.DataKeyTableModel;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Abstract superclass indicating end of a session. All events happening after
|
---|
25 | * this event are for another session. This is not called if there is a
|
---|
26 | * {@link SessionFailedEvent}.
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class SessionEndedNormallyEvent implements SessionEndedEvent {
|
---|
30 | private Session session;
|
---|
31 | private Bid agreement;
|
---|
32 | private List<NegotiationPartyInternal> parties;
|
---|
33 | private double runTime;
|
---|
34 | private MultilateralAnalysis analysis;
|
---|
35 | private ArrayList<NegotiationParty> ps;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @param session
|
---|
39 | * the session that ended
|
---|
40 | * @param agreement
|
---|
41 | * the bid that was agreed on at the end, or null if no
|
---|
42 | * agreement.
|
---|
43 | * @param parties
|
---|
44 | * list of the involved {@link NegotiationPartyInternal} , in
|
---|
45 | * correct order
|
---|
46 | */
|
---|
47 | public SessionEndedNormallyEvent(Session session, Bid agreement, List<NegotiationPartyInternal> parties) {
|
---|
48 | this.session = session;
|
---|
49 | this.agreement = agreement;
|
---|
50 | this.parties = parties;
|
---|
51 | this.runTime = session.getRuntimeInSeconds();
|
---|
52 | ps = new ArrayList<NegotiationParty>();
|
---|
53 | for (NegotiationPartyInternal p : parties) {
|
---|
54 | ps.add(p.getParty());
|
---|
55 | }
|
---|
56 |
|
---|
57 | analysis = new MultilateralAnalysis(parties, session.getInfo().getProtocol().getCurrentAgreement(session, ps),
|
---|
58 | session.getTimeline().getTime());
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | public Session getSession() {
|
---|
63 | return session;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | *
|
---|
68 | * @return final agreement bid, or null if no agreement was reached
|
---|
69 | */
|
---|
70 | public Bid getAgreement() {
|
---|
71 | return agreement;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public List<NegotiationPartyInternal> getParties() {
|
---|
75 | return parties;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Convert the agreement into a hashmap of < {@link DataKey}, {@link Object}
|
---|
80 | * > pairs. Object will usually be a {@link String}, {@link Number} or
|
---|
81 | * {@link List}. This data can be inserted directly into a
|
---|
82 | * {@link DataKeyTableModel}.
|
---|
83 | *
|
---|
84 | * @return {@link Map} of agreement evaluations.
|
---|
85 | */
|
---|
86 | public Map<DataKey, Object> getValues() {
|
---|
87 | Map<DataKey, Object> values = new HashMap<DataKey, Object>();
|
---|
88 |
|
---|
89 | try {
|
---|
90 | Bid agreement = session.getInfo().getProtocol().getCurrentAgreement(session, ps);
|
---|
91 | values.put(DataKey.RUNTIME, format("%.3f", runTime));
|
---|
92 | values.put(DataKey.ROUND, "" + (session.getRoundNumber() + 1));
|
---|
93 |
|
---|
94 | // deadline
|
---|
95 | values.put(DataKey.DEADLINE, session.getDeadlines().valueString());
|
---|
96 |
|
---|
97 | // discounted and agreement
|
---|
98 | boolean isDiscounted = false;
|
---|
99 | for (NegotiationPartyInternal party : parties)
|
---|
100 | isDiscounted |= (party.getUtilitySpace().discount(1, 1) != 1);
|
---|
101 | values.put(DataKey.IS_AGREEMENT, agreement == null ? "No" : "Yes");
|
---|
102 | values.put(DataKey.IS_DISCOUNT, isDiscounted ? "Yes" : "No");
|
---|
103 |
|
---|
104 | // number of agreeing parties
|
---|
105 | values.put(DataKey.NUM_AGREE, "" + session.getInfo().getProtocol().getNumberOfAgreeingParties(session, ps));
|
---|
106 |
|
---|
107 | // disc. and undisc. utils;
|
---|
108 | List<Double> utils = CsvLogger.getUtils(parties, agreement, false);
|
---|
109 | List<Double> discountedUtils = CsvLogger.getUtils(parties, agreement, true);
|
---|
110 | List<Double> perceivedUtils = CsvLogger.getPerceivedUtils(parties, agreement, true);
|
---|
111 |
|
---|
112 | // user bothers;
|
---|
113 | List<Double> userBothers = CsvLogger.getUserBothers(parties);
|
---|
114 | List<Double> userUtils = CsvLogger.getUserUtilities(parties, agreement, true);
|
---|
115 |
|
---|
116 | // min and max discounted utility
|
---|
117 | values.put(DataKey.MINUTIL, format("%.5f", Collections.min(discountedUtils)));
|
---|
118 | values.put(DataKey.MAXUTIL, format("%.5f", Collections.max(discountedUtils)));
|
---|
119 |
|
---|
120 | // analysis (distances, social welfare, etc)
|
---|
121 | values.put(DataKey.DIST_PARETO, format("%.5f", analysis.getDistanceToPareto()));
|
---|
122 | values.put(DataKey.DIST_NASH, format("%.5f", analysis.getDistanceToNash()));
|
---|
123 | values.put(DataKey.SOCIAL_WELFARE, format("%.5f", analysis.getSocialWelfare()));
|
---|
124 |
|
---|
125 | // enumerate agents names, utils, protocols
|
---|
126 | List<String> agts = new ArrayList<String>();
|
---|
127 |
|
---|
128 | String agentstr = "";
|
---|
129 | for (NegotiationPartyInternal a : parties) {
|
---|
130 | agts.add(a.getID().toString());
|
---|
131 | }
|
---|
132 | values.put(DataKey.AGENTS, agts);
|
---|
133 | values.put(DataKey.UTILS, utils);
|
---|
134 | values.put(DataKey.DISCOUNTED_UTILS, discountedUtils);
|
---|
135 | values.put(DataKey.PERCEIVED_UTILS, perceivedUtils);
|
---|
136 | values.put(DataKey.USER_BOTHERS, userBothers);
|
---|
137 | values.put(DataKey.USER_UTILS, userUtils);
|
---|
138 |
|
---|
139 | List<String> files = new ArrayList<String>();
|
---|
140 | for (NegotiationPartyInternal agent : parties) {
|
---|
141 | String name = "-";
|
---|
142 | if (agent.getUtilitySpace() instanceof AbstractUtilitySpace) {
|
---|
143 | name = new File(((AbstractUtilitySpace) agent.getUtilitySpace()).getFileName()).getName();
|
---|
144 | }
|
---|
145 | files.add(name);
|
---|
146 | }
|
---|
147 | values.put(DataKey.FILES, files);
|
---|
148 |
|
---|
149 | } catch (Exception e) {
|
---|
150 | values.put(DataKey.EXCEPTION, e.toString());
|
---|
151 | }
|
---|
152 | return values;
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | public MultilateralAnalysis getAnalysis() {
|
---|
157 | return analysis;
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|