1 | package genius.core.logging;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import javax.xml.bind.annotation.XmlElement;
|
---|
8 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Collects all statistics of all agents. immutable
|
---|
12 | *
|
---|
13 | */
|
---|
14 | @XmlRootElement
|
---|
15 | public class AgentsStatistics {
|
---|
16 | @XmlElement
|
---|
17 | private final List<AgentStatistics> statistics;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Just for serializer
|
---|
21 | */
|
---|
22 | @SuppressWarnings("unused")
|
---|
23 | private AgentsStatistics() {
|
---|
24 | statistics = null; // should be filled by deserializer
|
---|
25 | }
|
---|
26 |
|
---|
27 | public AgentsStatistics(List<AgentStatistics> stats) {
|
---|
28 | if (stats == null) {
|
---|
29 | throw new NullPointerException("stats==null");
|
---|
30 | }
|
---|
31 | this.statistics = stats;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Adds or replaces statistic.
|
---|
36 | *
|
---|
37 | * @param stats
|
---|
38 | * @return new AgentStatistics with the new Statistic added or replaced
|
---|
39 | */
|
---|
40 | public AgentsStatistics withStats(AgentStatistics stats) {
|
---|
41 | List<AgentStatistics> newlist = new ArrayList<AgentStatistics>(statistics);
|
---|
42 | newlist.add(stats);
|
---|
43 | return new AgentsStatistics(newlist);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * See aso {@link #withStatistics(String, double, double)}
|
---|
48 | *
|
---|
49 | * @param agentname
|
---|
50 | * the agent for which statistics are needed.
|
---|
51 | * @return statistic with given agentname, or null if no such elemenet.
|
---|
52 | */
|
---|
53 | public AgentStatistics get(String agentname) {
|
---|
54 | int i = index(agentname);
|
---|
55 | if (i == -1) {
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 | return statistics.get(i);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | *
|
---|
63 | * @param agentname
|
---|
64 | * @return index of given agentname in the array, or -1
|
---|
65 | */
|
---|
66 | private int index(String agentname) {
|
---|
67 | for (int index = 0; index < statistics.size(); index++) {
|
---|
68 | if (agentname.equals(statistics.get(index).getName())) {
|
---|
69 | return index;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return -1;
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | public List<AgentStatistics> getStatistics() {
|
---|
77 | return Collections.unmodifiableList(statistics);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Update the statistic of given agent.
|
---|
82 | *
|
---|
83 | * @param agent
|
---|
84 | * @param undiscounted
|
---|
85 | * @param discounted
|
---|
86 | * @param welfare
|
---|
87 | * @param nashdist
|
---|
88 | */
|
---|
89 | public AgentsStatistics withStatistics(String agent, double undiscounted, double discounted, double nashdist,
|
---|
90 | double welfare, double paretoDist) {
|
---|
91 | AgentStatistics stat = get(agent);
|
---|
92 | if (stat == null) {
|
---|
93 | stat = new AgentStatistics(agent, undiscounted, discounted, nashdist, welfare, paretoDist, 1);
|
---|
94 | } else {
|
---|
95 | stat = stat.withUtility(undiscounted, discounted, nashdist, welfare, paretoDist);
|
---|
96 | }
|
---|
97 | return withStatistics(stat);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @param stat
|
---|
102 | * @return new AgentsStatistics with stat added/updated
|
---|
103 | */
|
---|
104 | public AgentsStatistics withStatistics(AgentStatistics stat) {
|
---|
105 | ArrayList<AgentStatistics> newstatistics = new ArrayList<>(statistics);
|
---|
106 | int index = index(stat.getName());
|
---|
107 | if (index == -1) {
|
---|
108 | newstatistics.add(stat);
|
---|
109 | } else {
|
---|
110 | newstatistics.set(index, stat);
|
---|
111 | }
|
---|
112 | return new AgentsStatistics(newstatistics);
|
---|
113 | }
|
---|
114 | }
|
---|