-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
monte_carlo_test.cc
278 lines (239 loc) · 10.9 KB
/
monte_carlo_test.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "drake/systems/analysis/monte_carlo.h"
#include <cmath>
#include <thread>
#include <gtest/gtest.h>
#include "drake/systems/analysis/simulator.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/framework/vector_system.h"
#include "drake/systems/primitives/constant_vector_source.h"
#include "drake/systems/primitives/pass_through.h"
#include "drake/systems/primitives/random_source.h"
namespace drake {
namespace systems {
namespace analysis {
namespace {
// Only use two threads during testing. This value should match the "cpu" tag
// in BUILD.bazel defining this test.
constexpr int kTestConcurrency = 2;
GTEST_TEST(SelectNumberOfThreadsToUseTest, BasicTest) {
const int hardware_concurrency =
static_cast<int>(std::thread::hardware_concurrency());
// When kNoConcurrency is selected, only one thread should be used.
EXPECT_EQ(internal::SelectNumberOfThreadsToUse(kNoConcurrency), 1);
// If kUseHardwareConcurrency is specified, the number of threads should
// match std::thread::hardware_concurrency().
EXPECT_EQ(internal::SelectNumberOfThreadsToUse(kUseHardwareConcurrency),
hardware_concurrency);
// If a value greater than zero is specified, it selects the number of threads
// to use.
EXPECT_EQ(internal::SelectNumberOfThreadsToUse(1), 1);
EXPECT_EQ(internal::SelectNumberOfThreadsToUse(10), 10);
EXPECT_EQ(internal::SelectNumberOfThreadsToUse(100), 100);
// Zero and negative values (that are not kUseHardwareConcurrency) throw.
EXPECT_THROW(internal::SelectNumberOfThreadsToUse(0), std::exception);
EXPECT_THROW(internal::SelectNumberOfThreadsToUse(-10), std::exception);
}
// Checks that RandomSimulation repeatedly produces the same output sample
// when given the same RandomGenerator, but produces different output samples
// when given different generators.
void CheckConsistentReplay(const SimulatorFactory& make_simulator,
const ScalarSystemFunction& output,
double final_time) {
RandomGenerator generator;
// Run a simulation with a snapshot of the generator saved.
const RandomGenerator generator_snapshot(generator);
const double random_output =
RandomSimulation(make_simulator, output, final_time, &generator);
// Run a few more random simulators to mix-up the state. Make sure they
// give different outputs.
for (int i = 0; i < 5; i++) {
EXPECT_NE(RandomSimulation(make_simulator, output, final_time, &generator),
random_output);
}
// Reset the generator to our snapshot and confirm that we got our
// original result:
generator = generator_snapshot;
EXPECT_EQ(RandomSimulation(make_simulator, output, final_time, &generator),
random_output);
}
double GetScalarOutput(const System<double>& system,
const Context<double>& context) {
return system.get_output_port(0)
.Eval<BasicVector<double>>(context)
.GetAtIndex(0);
}
// Check that we get the expected deterministic result if our
// SimulatorFactory is deterministic.
GTEST_TEST(RandomSimulationTest, DeterministicSimulator) {
RandomGenerator generator;
const double final_time = 0.1;
const double value = 1.432;
const SimulatorFactory make_simulator = [value](RandomGenerator*) {
auto system = std::make_unique<ConstantVectorSource<double>>(value);
return std::make_unique<Simulator<double>>(std::move(system));
};
EXPECT_EQ(RandomSimulation(make_simulator, &GetScalarOutput, final_time,
&generator),
value);
}
// Ensure that RandomSimulation provides deterministic results when
// the "randomness" is in the SimulatorFactory.
GTEST_TEST(RandomSimulationTest, WithRandomSimulator) {
// Factory for a simple system output value is different in each simulator
// (but constant over the duration of each simulation).
const SimulatorFactory make_simulator = [](RandomGenerator* generator) {
std::normal_distribution<> distribution;
auto system = std::make_unique<ConstantVectorSource<double>>(
distribution(*generator));
return std::make_unique<Simulator<double>>(std::move(system));
};
const double final_time = 0.1;
CheckConsistentReplay(make_simulator, &GetScalarOutput, final_time);
}
// Simple system that outputs constant scalar, where this scalar is stored in
// the discrete state of the system. The scalar value is randomized in
// SetRandomState().
class RandomContextSystem : public VectorSystem<double> {
public:
RandomContextSystem() : VectorSystem(0, 1) { this->DeclareDiscreteState(1); }
private:
void SetRandomState(const Context<double>& context, State<double>* state,
RandomGenerator* generator) const override {
std::normal_distribution<> distribution;
state->get_mutable_discrete_state(0).SetAtIndex(0,
distribution(*generator));
}
void DoCalcVectorOutput(
const Context<double>& context,
const Eigen::VectorBlock<const VectorX<double>>& input,
const Eigen::VectorBlock<const VectorX<double>>& state,
Eigen::VectorBlock<VectorX<double>>* output) const override {
*output = state;
}
};
// Ensure that RandomSimulation provides correct/deterministic results when
// the "randomness" is in the SetRandomContext.
GTEST_TEST(RandomSimulationTest, WithRandomContext) {
// Factory for a simple system output value is different in each simulator
// (but constant over the duration of each simulation).
const SimulatorFactory make_simulator = [](RandomGenerator*) {
auto system = std::make_unique<RandomContextSystem>();
return std::make_unique<Simulator<double>>(std::move(system));
};
const double final_time = 0.1;
CheckConsistentReplay(make_simulator, &GetScalarOutput, final_time);
}
// Ensure that RandomSimulation provides correct/deterministic results when
// the "randomness" comes from random input ports. (Also provides coverage
// for using a DiagramBuilder in a SimulatorFactory).
GTEST_TEST(RandomSimulationTest, WithRandomInputs) {
const SimulatorFactory make_simulator = [](RandomGenerator*) {
DiagramBuilder<double> builder;
const int kNumOutputs = 1;
const double sampling_interval = 0.1;
auto random_source = builder.AddSystem<RandomSource<double>>(
RandomDistribution::kUniform, kNumOutputs, sampling_interval);
auto pass_through = builder.template AddSystem<PassThrough>(kNumOutputs);
builder.Connect(random_source->get_output_port(0),
pass_through->get_input_port());
builder.ExportOutput(pass_through->get_output_port(), "random");
auto diagram = builder.Build();
return std::make_unique<Simulator<double>>(std::move(diagram));
};
const double final_time = 0.1;
CheckConsistentReplay(make_simulator, &GetScalarOutput, final_time);
}
GTEST_TEST(MonteCarloSimulationTest, BasicTest) {
const SimulatorFactory make_simulator = [](RandomGenerator* generator) {
auto system = std::make_unique<RandomContextSystem>();
return std::make_unique<Simulator<double>>(std::move(system));
};
const double final_time = 0.1;
const int num_samples = 100;
const RandomGenerator prototype_generator;
RandomGenerator serial_generator(prototype_generator);
RandomGenerator parallel_generator(prototype_generator);
const auto serial_results = MonteCarloSimulation(
make_simulator, &GetScalarOutput, final_time, num_samples,
&serial_generator, kNoConcurrency);
const auto parallel_results = MonteCarloSimulation(
make_simulator, &GetScalarOutput, final_time, num_samples,
¶llel_generator, kTestConcurrency);
EXPECT_EQ(serial_results.size(), num_samples);
EXPECT_EQ(parallel_results.size(), num_samples);
// Check that the results were all different. We only check the serial results
// since we check later that serial and parallel results are identical.
std::unordered_set<double> serial_outputs;
for (const auto& serial_result : serial_results) {
serial_outputs.emplace(serial_result.output);
}
EXPECT_EQ(serial_outputs.size(), serial_results.size());
// Confirm that serial and parallel MonteCarloSimulation produce the same
// results, and that they are both reproducible.
for (int sample = 0; sample < num_samples; ++sample) {
const auto& serial_result = serial_results.at(sample);
const auto& parallel_result = parallel_results.at(sample);
EXPECT_EQ(serial_result.output, parallel_result.output);
RandomGenerator serial_reproduction_generator(
serial_result.generator_snapshot);
RandomGenerator parallel_reproduction_generator(
parallel_result.generator_snapshot);
EXPECT_EQ(RandomSimulation(make_simulator, &GetScalarOutput, final_time,
&serial_reproduction_generator),
serial_result.output);
EXPECT_EQ(RandomSimulation(make_simulator, &GetScalarOutput, final_time,
¶llel_reproduction_generator),
parallel_result.output);
}
}
// Simple system that outputs constant scalar, where this scalar is stored in
// the discrete state of the system. The scalar value is randomized in
// SetRandomState(). If the state value (cast to int) is odd, DoCalcVectorOutput
// throws.
class ThrowingRandomContextSystem : public VectorSystem<double> {
public:
ThrowingRandomContextSystem() : VectorSystem(0, 1) {
this->DeclareDiscreteState(1);
}
private:
void SetRandomState(const Context<double>& context, State<double>* state,
RandomGenerator* generator) const override {
std::normal_distribution<> distribution;
state->get_mutable_discrete_state(0).SetAtIndex(0,
distribution(*generator));
}
void DoCalcVectorOutput(
const Context<double>& context,
const Eigen::VectorBlock<const VectorX<double>>& input,
const Eigen::VectorBlock<const VectorX<double>>& state,
Eigen::VectorBlock<VectorX<double>>* output) const override {
if ((static_cast<int>(state(0)) % 2) != 0) {
throw std::runtime_error("State value is odd");
} else {
*output = state;
}
}
};
GTEST_TEST(MonteCarloSimulationExceptionTest, BasicTest) {
const SimulatorFactory make_simulator = [](RandomGenerator* generator) {
auto system = std::make_unique<ThrowingRandomContextSystem>();
return std::make_unique<Simulator<double>>(std::move(system));
};
const double final_time = 0.1;
const int num_samples = 10;
const RandomGenerator prototype_generator;
RandomGenerator serial_generator(prototype_generator);
RandomGenerator parallel_generator(prototype_generator);
EXPECT_THROW(MonteCarloSimulation(
make_simulator, &GetScalarOutput, final_time, num_samples,
&serial_generator, kNoConcurrency),
std::exception);
EXPECT_THROW(MonteCarloSimulation(
make_simulator, &GetScalarOutput, final_time, num_samples,
¶llel_generator, kTestConcurrency),
std::exception);
}
} // namespace
} // namespace analysis
} // namespace systems
} // namespace drake