Skip to content

Commit 1eef8b9

Browse files
committed
More stats
1 parent b01c470 commit 1eef8b9

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

index.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
dataType: 'json',
1616
success: function (data) {
1717
displayGrid(data);
18+
displayStats(data['simulation']);
1819
}
1920
});
2021
};
@@ -52,14 +53,15 @@
5253
ctx.lineWidth = parseInt(gridOptions.LinesSize);
5354
grid_size = 0;
5455
grid_size = parseInt(gridOptions.grid_size);
55-
canvas.width = grid_size * data[0].length;
56-
canvas.height = grid_size * data.length;
57-
for (x = 0; x < data[0].length; x += 1) {
58-
for (y = 0; y < data.length; y += 1) {
56+
var barn = data['barn'];
57+
canvas.width = grid_size * barn[0].length;
58+
canvas.height = grid_size * barn.length;
59+
for (x = 0; x < barn[0].length; x += 1) {
60+
for (y = 0; y < barn.length; y += 1) {
5961
var canvas_x = x * grid_size;
6062
var canvas_y = y * grid_size;
61-
//ctx.fillText(data[x][y], canvas_x, canvas_y);
62-
var agents = data[y][x];
63+
//ctx.fillText(barn[x][y], canvas_x, canvas_y);
64+
var agents = barn[y][x];
6365
if (agents.length === 0) { continue; }
6466
agents.sort((a, b) => a.weight - b.weight);
6567
agent_type = agents[0]['type'];
@@ -73,19 +75,24 @@
7375
if (agent['type'] === 'F') { ctx.drawImage(image('feeder'), canvas_x, canvas_y, grid_size, grid_size); }
7476
if (agent['type'] === 'K') { ctx.drawImage(image('cow'), canvas_x, canvas_y, grid_size, grid_size); }
7577
if (agent['type'] === 'X') { ctx.drawImage(image('cow_dead'), canvas_x, canvas_y, grid_size, grid_size); }
78+
if (agent['type'] === '*') { ctx.fillText("*", canvas_x, canvas_y) ;}
7679
if (agent['debug']) { ctx.fillText("debug-ku", canvas_x, canvas_y); }
7780
}
7881
}
7982
}
8083
}
8184
}
85+
function displayStats(data) {
86+
$('#stats').html(JSON.stringify(data, null, "<br>"));
87+
}
8288
</script>
8389
</head>
8490

8591
<body onload="">
8692
<h2>Cowsim</h2>
87-
<canvas id="barn" width="" height="">
93+
<canvas id="barn" width="" height="" style="border:1px solid black;">
8894
</canvas>
95+
<div id="stats"></div>
8996
<br />
9097
Options:
9198
<select id="" onchange="">

main.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def place_random_agents(agent_type, groups, max_agents, cluster=False):
8888
self.barn.place_agent(Bed(self), (x, y))
8989
self.barn.sleep_positions.append((x,y))
9090

91-
place_random_agents('wall', groups=10, max_agents=10)
91+
place_random_agents('wall', groups=30, max_agents=10)
9292
place_random_agents('grass', groups=5, max_agents=5)
9393
place_random_agents('water', groups=5, max_agents=3)
9494
place_random_agents('feeder', groups=5, max_agents=2)
@@ -107,27 +107,33 @@ def place_random_agents(agent_type, groups, max_agents, cluster=False):
107107
def random_pos(self):
108108
x = random.randrange(config["barn_height"])
109109
y = random.randrange(config["barn_width"])
110+
while not self.barn.is_cell_empty((x,y)):
111+
x = random.randrange(config["barn_height"])
112+
y = random.randrange(config["barn_width"])
110113
return (x, y)
111114

112115
def state(self):
113116
debug_cow = next(cow for cow in self.cows if cow.debug)
114117
dead = len([cow for cow in self.cows if not cow.alive])
115-
water = [cow.water for cow in self.cows]
116-
# print("Mean:", numpy.mean(results), " Median:", numpy.median(results), " Stdev:", numpy.std(results))
118+
water = [cow.water for cow in self.cows if cow.alive]
119+
stuck_recalc = [cow.stuck_recalc for cow in self.cows if cow.alive]
117120
return {
118121
"model": {
119122
"dead": dead,
120123
"water_mean": numpy.mean(water),
121124
"water_median": numpy.median(water),
122125
"water_stdev": numpy.std(water),
126+
"stuck_recalc_mean": numpy.mean(stuck_recalc),
127+
"stuck_recalc_median": numpy.median(stuck_recalc),
128+
"stuck_recalc_stdev": numpy.std(stuck_recalc),
129+
"step": self.step,
123130
},
124-
"barn": self.barn.state(),
125-
"step": self.step,
126131
"debug_cow": debug_cow,
132+
"barn": self.barn.state(),
127133
}
128134

129135
def human_readable_state(self, state):
130-
output = "Step: {}\n".format(state["step"])
136+
output = "Step: {}\n".format(state['model']["step"])
131137
grid = state["barn"]["grid"]
132138
debug_cow_path = state["debug_cow"].current_path
133139
debug_cow_state = state["debug_cow"].state()
@@ -152,19 +158,19 @@ def json_state(self, state):
152158
grid = state["barn"]["grid"]
153159
debug_cow_path = state["debug_cow"].current_path
154160
debug_cow_state = state["debug_cow"].state()
155-
model = state["model"]
156161
w, h = len(grid), len(grid[0])
157-
to_json = [[[] for x in range(h)] for y in range(w)]
162+
grid_json = [[[] for x in range(h)] for y in range(w)]
158163
for x in range(len(grid)):
159164
for y in range(len(grid[x])):
160165
if grid[x][y]:
161-
to_json[x][y] = [a.state() for a in grid[x][y]]
166+
grid_json[x][y] = [a.state() for a in grid[x][y]]
162167
elif (x, y) in debug_cow_path:
163-
to_json[x][y] = []
168+
grid_json[x][y] = [{'type': '*'}]
164169
else:
165-
to_json[x][y] = []
170+
grid_json[x][y] = []
166171

167-
return to_json
172+
return {'barn': grid_json,
173+
'simulation': state['model']}
168174

169175
def run(self):
170176
for s in range(self.config["steps"]):

0 commit comments

Comments
 (0)