1 | package genius.core.logging;
|
---|
2 |
|
---|
3 | import java.io.Closeable;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.OutputStream;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | import javax.xml.bind.JAXBContext;
|
---|
9 | import javax.xml.bind.JAXBException;
|
---|
10 | import javax.xml.bind.Marshaller;
|
---|
11 | import javax.xml.stream.XMLStreamException;
|
---|
12 |
|
---|
13 | import genius.core.Bid;
|
---|
14 | import genius.core.analysis.MultilateralAnalysis;
|
---|
15 | import genius.core.events.BrokenPartyException;
|
---|
16 | import genius.core.events.NegotiationEvent;
|
---|
17 | import genius.core.events.SessionEndedNormallyEvent;
|
---|
18 | import genius.core.events.SessionFailedEvent;
|
---|
19 | import genius.core.listener.Listener;
|
---|
20 | import genius.core.parties.NegotiationPartyInternal;
|
---|
21 | import genius.core.session.Participant;
|
---|
22 | import genius.core.utility.UtilitySpace;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Keeps track of tournament and creates statistic information.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class StatisticsLogger implements Listener<NegotiationEvent>, Closeable {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * key= agent name, Statistic is the statistical info logged for that agent.
|
---|
32 | */
|
---|
33 | protected AgentsStatistics agentStats = new AgentsStatistics(new ArrayList<AgentStatistics>());
|
---|
34 | private OutputStream outStream;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param out
|
---|
38 | * {@link OutputStream} to write the log to. If this is a file,
|
---|
39 | * we recommend to use the extension ".xml". This logger becomes
|
---|
40 | * owner of this outputstream and will close it eventually.
|
---|
41 | */
|
---|
42 | public StatisticsLogger(OutputStream out) throws FileNotFoundException, XMLStreamException {
|
---|
43 | if (out == null) {
|
---|
44 | throw new NullPointerException("out=null");
|
---|
45 | }
|
---|
46 | this.outStream = out;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void close() {
|
---|
51 | try {
|
---|
52 | JAXBContext jc = JAXBContext.newInstance(AgentsStatistics.class);
|
---|
53 | Marshaller marshaller = jc.createMarshaller();
|
---|
54 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
---|
55 | marshaller.marshal(agentStats, outStream);
|
---|
56 | } catch (JAXBException e) {
|
---|
57 | e.printStackTrace(); // we can't do much here.
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | @SuppressWarnings({ "unchecked", "rawtypes" })
|
---|
62 | @Override
|
---|
63 | public void notifyChange(NegotiationEvent e) {
|
---|
64 | try {
|
---|
65 | if (e instanceof SessionEndedNormallyEvent) {
|
---|
66 | SessionEndedNormallyEvent e1 = (SessionEndedNormallyEvent) e;
|
---|
67 | MultilateralAnalysis analysis = e1.getAnalysis();
|
---|
68 | Bid agreedbid = analysis.getAgreement();
|
---|
69 | double nashdist = analysis.getDistanceToNash();
|
---|
70 | double welfare = analysis.getSocialWelfare();
|
---|
71 | double paretoDist = analysis.getDistanceToPareto();
|
---|
72 |
|
---|
73 | for (NegotiationPartyInternal party : e1.getParties()) {
|
---|
74 | String name = party.getParty().getClass().getCanonicalName();
|
---|
75 | if (agreedbid == null) {
|
---|
76 | agentStats = agentStats.withStatistics(name, 0, 0, nashdist, welfare, paretoDist);
|
---|
77 | } else {
|
---|
78 | agentStats = agentStats.withStatistics(name, party.getUtility(agreedbid),
|
---|
79 | party.getUtilityWithDiscount(agreedbid), nashdist, welfare, paretoDist);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | } else if (e instanceof SessionFailedEvent) {
|
---|
83 | BrokenPartyException e1 = ((SessionFailedEvent) e).getException();
|
---|
84 |
|
---|
85 | for (Participant party : ((SessionFailedEvent) e).getException().getConfiguration().getParties()) {
|
---|
86 | Double reservationvalue = 0d;
|
---|
87 | try {
|
---|
88 | UtilitySpace utilspace = party.getProfile().create();
|
---|
89 | reservationvalue = utilspace.getReservationValue();
|
---|
90 | } catch (Exception ex) {
|
---|
91 | System.out.println("Failed to read profile of " + party + ". using 0");
|
---|
92 | }
|
---|
93 |
|
---|
94 | agentStats = agentStats.withStatistics(party.getStrategy().getClassDescriptor(), reservationvalue,
|
---|
95 | reservationvalue, 1d, 0d, 1d);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | // other events are only giving details we dont need here.
|
---|
99 |
|
---|
100 | } catch (Exception e1) {
|
---|
101 | e1.printStackTrace();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | } |
---|