-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeSeriesGraph.pde
129 lines (113 loc) · 3.62 KB
/
TimeSeriesGraph.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* TimeSeriesGraph -- time is on the x axis
*/
abstract class TimeSeriesGraph extends Graph {
private int numUpdates;
/* Calls Graph constructor Passes argument to the super-class constructor, and sets the number of update calls to 0.
*
* tested visually.
*/
public TimeSeriesGraph(int x, int y, int howWide, int howTall, String xlab, String ylab) {
super(x, y, howWide, howTall, xlab, ylab);
numUpdates = 0;
}
/** In subclasses this will compute the y value of the next point in the graph.
*/
public abstract int nextY(SugarGrid g);
/* Overrides the superclass update method.
* If the "number of updates" -- the number of points updated so far -- is 0, calls the superclass update method.
* Otherwise, calls nextY(g) to get the y-coordinate of the next point in the line.
*
* Draws a 1x1 square (i.e., a point; color is your choice) at the point that would be at
* (number of updates, nextpoint(g)) in the graph that is being plotted.
*
* Increases the number of updates by 1.
*
* If the number of updates exceeds the width of the graph, set the number of updates back to 0
* (erasing the graph on the next call, and starting over).
*
* tested visually.
*/
public void update(SugarGrid g) {
if (numUpdates == 0) {
super.update(g);
}
else {
fill(0,0,255);
rect(xOnScreen(numUpdates),yOnScreen(nextY(g)), 1, 1);
}
if (++numUpdates > howWide) {
numUpdates = 0;
}
}
}
/** A line graph of the number of agents on the SugarScape
*/
class NumberOfAgentsTimeSeriesGraph extends TimeSeriesGraph {
/* Constructor, calls parent constructor
*/
public NumberOfAgentsTimeSeriesGraph(int x, int y, int howWide, int howTall) {
super(x, y, howWide, howTall, "steps", "agents");
}
/*
*/
public int nextY(SugarGrid g) {
return g.getAgents().size();
}
}
/** A line graph of the number of agents on the SugarScape
*/
class AverageAgentSugarTimeSeriesGraph extends TimeSeriesGraph {
/* Constructor, calls parent constructor
*/
public AverageAgentSugarTimeSeriesGraph(int x, int y, int howWide, int howTall) {
super(x, y, howWide, howTall, "Time", "Average Agent Sugar");
}
/*
*/
public int nextY(SugarGrid g) {
ArrayList<Agent> agts = g.getAgents();
int totalSugar = 0;
for (Agent agt: agts) {
totalSugar += agt.getSugarLevel();
}
return totalSugar/agts.size();
}
}
/** A line graph of the number of agents on the SugarScape
*/
class AverageAgentAgeTimeSeriesGraph extends TimeSeriesGraph {
int maxAge;
/* Constructor, calls parent constructor
*/
public AverageAgentAgeTimeSeriesGraph(int x, int y, int howWide, int howTall, int maxAge) {
super(x, y, howWide, howTall, "Time", "Average Agent Age");
this.maxAge = maxAge;
}
/*
*/
public int nextY(SugarGrid g) {
ArrayList<Agent> agts = g.getAgents();
int totalAge = 0;
for (Agent agt: agts) {
totalAge += agt.getAge();
}
int avgAge = totalAge/(agts.size() + 1);
if (avgAge > maxAge) avgAge = maxAge;
return howTall*avgAge/maxAge;
}
}
class ProportionOfTrueCultureTimeSeriesGraph extends TimeSeriesGraph{
public ProportionOfTrueCultureTimeSeriesGraph(int x, int y, int howWide, int howTall){
super( x, y, howWide, howTall, "Time", "Prop. Agents in True Culture");
}
public int nextY(SugarGrid g){
float propTrueCulture = 0;
ArrayList<Agent> agentList = g.getAgents();
for(Agent a: agentList){
if(a.getTribe()) propTrueCulture++;
}
propTrueCulture /= agentList.size();
propTrueCulture *= howTall;
return (int)propTrueCulture;
}
}