source: src/main/java/genius/core/logging/StatisticsLogger.java

Last change on this file was 1, checked in by Wouter Pasman, 7 years ago

Initial import : Genius 9.0.0

File size: 3.5 KB
Line 
1package genius.core.logging;
2
3import java.io.Closeable;
4import java.io.FileNotFoundException;
5import java.io.OutputStream;
6import java.util.ArrayList;
7
8import javax.xml.bind.JAXBContext;
9import javax.xml.bind.JAXBException;
10import javax.xml.bind.Marshaller;
11import javax.xml.stream.XMLStreamException;
12
13import genius.core.Bid;
14import genius.core.analysis.MultilateralAnalysis;
15import genius.core.events.BrokenPartyException;
16import genius.core.events.NegotiationEvent;
17import genius.core.events.SessionEndedNormallyEvent;
18import genius.core.events.SessionFailedEvent;
19import genius.core.listener.Listener;
20import genius.core.parties.NegotiationPartyInternal;
21import genius.core.session.Participant;
22import genius.core.utility.UtilitySpace;
23
24/**
25 * Keeps track of tournament and creates statistic information.
26 *
27 */
28public 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}
Note: See TracBrowser for help on using the repository browser.