-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateMeanLiving.java
134 lines (101 loc) · 3.82 KB
/
CalculateMeanLiving.java
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
130
131
132
133
134
import org.nlogo.headless.HeadlessWorkspace;
import static java.lang.System.out;
import static java.lang.System.err;
import static java.lang.String.format;
public class CalculateMeanLiving{
/*
* Static variables used to redefined experiments.
*/
// number of steps
static int steps = 250;
// number of runs
static int runs = 200;
// global parameters
public static void main(String[] argv) {
/*
* Command line arguments.
*/
int world_size = Integer.parseInt(argv[0]);
int init_life = Integer.parseInt(argv[1]);
boolean rand = Boolean.parseBoolean(argv[2]);
boolean sync = Boolean.parseBoolean(argv[3]);
int deterministic_period = Integer.parseInt(argv[4]);
double rule_switch_prob = Double.parseDouble(argv[5]);
int second_threshold = Integer.parseInt(argv[6]);
/*
* Main loop.
*/
HeadlessWorkspace world = HeadlessWorkspace.newInstance();
try {
world.open("life-rule-switching.nlogo");
for (int runNo = 0; runNo<runs; runNo++) {
out.println(
format("%d,%d,%s,%s,%d,%3.2f,%d,%7.4f",
world_size, init_life, rand, sync,
deterministic_period, rule_switch_prob, second_threshold,
calcLiving(world, world_size, init_life, rand, sync,
deterministic_period, rule_switch_prob, second_threshold)
)
);
}
world.dispose();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static double calcLiving(
HeadlessWorkspace world,
int world_size, int init_life, boolean rand, boolean sync,
int deterministic_period, double rule_switch_prob, int second_threshold) {
double living = 0.0;
//err.println("[INFO] restarting simulation!");
// name is build from the parsed values
String world_name = new String();
try {
// load the model
// model specific setup
world.command(format("set world-size %d", world_size));
//err.println(format("set world-size %d", world_size));
world.command(format("set init-life %d", init_life));
//err.println(format("set init-life %d", init_life));
world_name += format("_size%d", world_size);
world_name += format("_init%d", init_life);
if (sync) {
world.command("set synchronous true");
world_name += "_sync";
} else {
world.command("set synchronous false");
world_name += "_async";
}
if (rand) {
world_name += "_rand";
world_name += format("_p%3.2f", rule_switch_prob);
world.command("set deterministic false");
//err.println("set deterministic false");
world.command(format("set rule-switch-prob %3.2f", rule_switch_prob));
//err.println(format("set rule-switch-prob %3.2f", rule_switch_prob));
} else {
world.command("set deterministic true");
//err.println("set deterministic true");
world.command(format("set deterministic-period %d", deterministic_period));
//err.println(format("set deterministic-period %d", deterministic_period));
world_name += "_detr";
world_name += format("_dp%d", deterministic_period);
}
world_name += format("_st%d", second_threshold);
world.command(format("set second-threshold %d", second_threshold));
//err.println(format("set second-threshold %d", second_threshold));
// setup the world
world.command("setup");
// run the simulation
world.command(String.format("repeat %d [go]", steps));
// report the percentage of living cells
living = Double.parseDouble(world.report("%living").toString());
world.command("clear-all");
world.command("reset-ticks");
} catch (Exception ex) {
ex.printStackTrace();
}
return living;
}
}