/// @file test_sweep_doe.cpp /// @brief P4-28 / VDE-1253:参数扫描与 DOE 采样(Grid/LHS)+ 批量回调测试 #include #include "vde_param/sweep.h" #include #include #include #include #include using namespace vde::param; namespace { std::vector grid_variables() { return {DesignVariable{"width", 10.0, 20.0, 2, false}, DesignVariable{"height", 5.0, 15.0, 3, false}}; } } // namespace TEST(SweepDoeTest, GridGeneratesFullFactorial) { SweepOptions options; options.method = SamplingMethod::Grid; const auto samples = generate_samples(grid_variables(), options); ASSERT_TRUE(samples.ok()) << samples.error; ASSERT_EQ(samples.points.size(), 6u); // 第一个变量最慢、最后一个最快:首点 = 两变量最小值。 EXPECT_DOUBLE_EQ(samples.points.front().at("width"), 10.0); EXPECT_DOUBLE_EQ(samples.points.front().at("height"), 5.0); EXPECT_DOUBLE_EQ(samples.points[1].at("width"), 10.0); EXPECT_DOUBLE_EQ(samples.points[1].at("height"), 10.0); EXPECT_DOUBLE_EQ(samples.points.back().at("width"), 20.0); EXPECT_DOUBLE_EQ(samples.points.back().at("height"), 15.0); EXPECT_EQ(samples.points[2].at("width"), 10.0); EXPECT_EQ(samples.points[3].at("width"), 20.0); } TEST(SweepDoeTest, GridIntegerValuesAreIntegralAndInsideRange) { DesignVariable count{"count", 1.0, 10.0, 4, true}; SweepOptions options; options.method = SamplingMethod::Grid; const auto samples = generate_samples({count}, options); ASSERT_TRUE(samples.ok()) << samples.error; ASSERT_EQ(samples.points.size(), 4u); for (const auto& point : samples.points) { const double value = point.at("count"); EXPECT_NEAR(value, std::round(value), 1e-12); EXPECT_GE(value, 1.0); EXPECT_LE(value, 10.0); } } TEST(SweepDoeTest, LatinHypercubeIsBoundedStratifiedAndReproducible) { std::vector variables = { {"a", -2.0, 3.0, 2, false}, {"b", 10.0, 20.0, 2, false}, {"c", 0.0, 100.0, 2, false}, }; SweepOptions options; options.method = SamplingMethod::LatinHypercube; options.samples = 32; options.seed = 12345; const auto first = generate_samples(variables, options); const auto second = generate_samples(variables, options); ASSERT_TRUE(first.ok()) << first.error; ASSERT_EQ(first.points.size(), 32u); EXPECT_EQ(first.points, second.points) << "same seed must reproduce samples"; for (const auto& variable : variables) { std::vector strata(32, 0); for (const auto& point : first.points) { const double value = point.at(variable.name); EXPECT_GE(value, variable.min_value); EXPECT_LE(value, variable.max_value); const double t = (value - variable.min_value) / (variable.max_value - variable.min_value); std::size_t bin = static_cast(std::floor(t * 32.0)); if (bin >= 32) bin = 31; ++strata[bin]; } for (const std::size_t count : strata) { EXPECT_EQ(count, 1u) << "each LHS stratum should get one sample for " << variable.name; } } } TEST(SweepDoeTest, InvalidDomainsAreRejected) { SweepOptions options; options.method = SamplingMethod::Grid; const auto empty = generate_samples({}, options); EXPECT_FALSE(empty.ok()); EXPECT_FALSE(empty.error.empty()); EXPECT_FALSE(generate_samples({{"a", 10.0, 5.0, 2, false}}, options).ok()); EXPECT_FALSE(generate_samples({{"a", 0.0, 1.0, 1, false}}, options).ok()); EXPECT_FALSE(generate_samples({{"a", 0.0, 1.0, 2, false}, {"a", 0.0, 1.0, 2, false}}, options).ok()); EXPECT_FALSE(generate_samples({{"a", std::nan(""), 1.0, 2, false}}, options).ok()); SweepOptions lhs; lhs.method = SamplingMethod::LatinHypercube; lhs.samples = 0; EXPECT_FALSE(generate_samples({{"a", 0.0, 1.0, 2, false}}, lhs).ok()); lhs.samples = 10; lhs.max_samples = 5; EXPECT_FALSE(generate_samples({{"a", 0.0, 1.0, 2, false}}, lhs).ok()); options.max_samples = 2; EXPECT_FALSE(generate_samples(grid_variables(), options).ok()); } TEST(SweepDoeTest, RunBatchCollectsSuccessOutputs) { SweepOptions options; options.method = SamplingMethod::Grid; int calls = 0; const auto batch = run_batch(grid_variables(), options, [&calls](const DesignPoint& point) { ++calls; BatchSampleResult result; result.outputs["area"] = point.at("width") * point.at("height"); return result; }); ASSERT_TRUE(batch.setup_error.empty()) << batch.setup_error; EXPECT_TRUE(batch.ok()); EXPECT_EQ(batch.requested, 6u); EXPECT_EQ(batch.completed, 6u); EXPECT_EQ(batch.succeeded, 6u); EXPECT_EQ(batch.failed, 0u); EXPECT_EQ(calls, 6); ASSERT_EQ(batch.results.size(), 6u); EXPECT_DOUBLE_EQ(batch.results.front().outputs.at("area"), 50.0); } TEST(SweepDoeTest, RunBatchIsolatesErrorsAndExceptions) { SweepOptions options; options.method = SamplingMethod::Grid; int calls = 0; const auto batch = run_batch( grid_variables(), options, [&calls](const DesignPoint& point) { ++calls; if (point.at("width") == 20.0 && point.at("height") == 5.0) { throw std::runtime_error("solver failed"); } BatchSampleResult result; if (point.at("width") == 10.0 && point.at("height") == 10.0) { result.ok = false; result.error = "domain rejected"; return result; } result.outputs["ok"] = 1.0; return result; }); EXPECT_FALSE(batch.ok()); EXPECT_EQ(batch.completed, 6u); EXPECT_EQ(calls, 6); EXPECT_EQ(batch.failed, 2u); EXPECT_EQ(batch.succeeded, 4u); ASSERT_EQ(batch.results.size(), 6u); // Grid 顺序:width 最慢、height 最快;index 1 = (10,10),index 3 = (20,5)。 EXPECT_FALSE(batch.results[1].ok); EXPECT_EQ(batch.results[1].error, "domain rejected"); EXPECT_FALSE(batch.results[3].ok); EXPECT_NE(batch.results[3].error.find("exception:"), std::string::npos); } TEST(SweepDoeTest, RunBatchParallelPreservesOrder) { SweepOptions options; options.method = SamplingMethod::Grid; options.max_threads = 3; const auto batch = run_batch(grid_variables(), options, [](const DesignPoint& point) { BatchSampleResult result; result.outputs["area"] = point.at("width") * point.at("height"); return result; }); ASSERT_TRUE(batch.setup_error.empty()) << batch.setup_error; EXPECT_TRUE(batch.ok()); EXPECT_EQ(batch.completed, 6u); EXPECT_EQ(batch.results.size(), 6u); EXPECT_DOUBLE_EQ(batch.results.front().outputs.at("area"), 50.0); EXPECT_DOUBLE_EQ(batch.results.back().outputs.at("area"), 300.0); } TEST(SweepDoeTest, RunBatchHonoursCancellation) { SweepOptions options; options.method = SamplingMethod::Grid; int checks = 0; options.cancel = [&checks]() { return checks++ >= 1; }; const auto batch = run_batch(grid_variables(), options, [](const DesignPoint&) { return BatchSampleResult{}; }); EXPECT_FALSE(batch.ok()); EXPECT_TRUE(batch.cancelled); EXPECT_LE(batch.completed, 2u); EXPECT_EQ(batch.results.size(), batch.completed); EXPECT_EQ(batch.requested, 6u); } TEST(SweepDoeTest, EmptyCallbackIsRejected) { SweepOptions options; options.method = SamplingMethod::Grid; const auto batch = run_batch(grid_variables(), options, BatchEvaluate{}); EXPECT_FALSE(batch.ok()); EXPECT_FALSE(batch.setup_error.empty()); EXPECT_EQ(batch.completed, 0u); }