14#include <gtest/gtest.h>
32 std::vector<Project<double>> projects = {
33 {0, 100, {},
"Profitable A"},
34 {1, 50, {},
"Profitable B"},
39 EXPECT_DOUBLE_EQ(result.max_profit, 150.0);
40 EXPECT_EQ(result.selected.size(), 2);
45 std::vector<Project<double>> projects = {
46 {0, 100, {},
"Project A"},
47 {1, -30, {},
"Infra"},
48 {2, 50, {1},
"Project B"},
57 EXPECT_GE(result.max_profit, 120.0);
62 std::vector<Project<double>> projects = {
63 {0, -50, {},
"Cost A"},
64 {1, -30, {},
"Cost B"},
69 EXPECT_DOUBLE_EQ(result.max_profit, 0.0);
70 EXPECT_TRUE(result.selected.empty());
75 std::vector<Project<double>> projects;
79 EXPECT_DOUBLE_EQ(result.max_profit, 0.0);
85 std::vector<Project<double>> projects = {
86 {0, 100, {1},
"A needs B"},
87 {1, 50, {0},
"B needs A"},
93 EXPECT_GE(result.max_profit, 150.0);
98 std::vector<Project<double>> projects = {
99 {0, -10, {},
"Foundation"},
100 {1, -10, {0},
"Level 1"},
101 {2, -10, {1},
"Level 2"},
102 {3, 100, {2},
"Payoff"},
108 EXPECT_GE(result.max_profit, 70.0);
122 std::vector<Team> teams(4);
124 teams[0].name =
"Atlanta";
126 teams[0].losses = 71;
127 teams[0].remaining = 8;
128 teams[0].against = {0, 1, 6, 1};
130 teams[1].name =
"Philly";
132 teams[1].losses = 79;
133 teams[1].remaining = 3;
134 teams[1].against = {1, 0, 0, 2};
136 teams[2].name =
"New York";
138 teams[2].losses = 78;
139 teams[2].remaining = 6;
140 teams[2].against = {6, 0, 0, 0};
142 teams[3].name =
"Montreal";
144 teams[3].losses = 82;
145 teams[3].remaining = 3;
146 teams[3].against = {1, 2, 0, 0};
154 auto teams = create_simple_division();
159 EXPECT_FALSE(result.eliminated);
160 EXPECT_EQ(result.max_possible_wins, 91);
165 std::vector<Team> teams(3);
167 teams[0].name =
"Leader";
169 teams[0].remaining = 0;
170 teams[0].against = {0, 0, 0};
172 teams[1].name =
"Middle";
174 teams[1].remaining = 40;
175 teams[1].against = {0, 0, 40};
177 teams[2].name =
"Loser";
179 teams[2].remaining = 50;
180 teams[2].against = {0, 40, 0};
185 EXPECT_TRUE(result.eliminated);
190 auto teams = create_simple_division();
202 auto teams = create_simple_division();
207 EXPECT_FALSE(result.eliminated);
223 std::vector<std::vector<std::array<double, 2>>> data(2,
224 std::vector<std::array<double, 2>>(2));
227 data[0][0] = {100, 10};
230 data[0][1] = {10, 100};
231 data[1][0] = {10, 100};
232 data[1][1] = {10, 100};
236 EXPECT_EQ(result.labels.size(), 2);
237 EXPECT_EQ(result.labels[0].size(), 2);
246 std::vector<std::vector<std::array<double, 2>>> data(3,
247 std::vector<std::array<double, 2>>(3));
249 for (
int i = 0; i < 3; ++i)
250 for (
int j = 0; j < 3; ++j)
251 data[i][j] = {100, 10};
256 for (
int i = 0; i < 3; ++i)
257 for (
int j = 0; j < 3; ++j)
258 EXPECT_EQ(result.labels[i][j], 1);
263 std::vector<std::vector<std::array<double, 2>>> data;
267 EXPECT_TRUE(result.labels.empty());
272 std::vector<std::vector<std::array<double, 2>>> data(1,
273 std::vector<std::array<double, 2>>(1));
275 data[0][0] = {5, 10};
279 EXPECT_EQ(result.labels[0][0], 0);
294 std::vector<SurveyQuestion> questions = {
299 std::vector<SurveyRespondent> respondents = {
306 EXPECT_TRUE(result.feasible);
307 EXPECT_GE(result.assignments.size(), 2);
312 std::vector<SurveyQuestion> questions = {
316 std::vector<SurveyRespondent> respondents = {
322 EXPECT_FALSE(result.feasible);
327 std::vector<SurveyQuestion> questions = {
332 std::vector<SurveyRespondent> respondents = {
340 EXPECT_TRUE(result.feasible);
343 for (
const auto& [
r, q] : result.assignments)
345 bool eligible =
false;
346 for (
size_t eq : respondents[
r].eligible_questions)
349 EXPECT_TRUE(eligible);
355 std::vector<SurveyQuestion> questions;
356 std::vector<SurveyRespondent> respondents;
361 EXPECT_FALSE(result.feasible);
384 [](
auto*) {
return 0.0; },
385 [](
auto*) {
return 0.0; });
387 EXPECT_TRUE(result.feasible);
400 std::map<TestNet::Node*, double> demands;
405 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
406 [](
auto*) {
return 0.0; });
408 EXPECT_TRUE(result.feasible);
409 EXPECT_DOUBLE_EQ(result.excess_flow, 5.0);
412 ASSERT_TRUE(result.flow.contains(arc));
413 EXPECT_DOUBLE_EQ(result.flow[arc], 5.0);
426 std::map<TestNet::Node*, double> demands;
431 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
432 [](
auto*) {
return 0.0; });
434 EXPECT_FALSE(result.feasible);
447 std::map<TestNet::Node*, double> demands;
451 std::map<TestNet::Arc*, double> lower_bounds;
452 lower_bounds[arc] = 3.0;
455 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
456 [&](
auto* a) {
return lower_bounds.count(a) ? lower_bounds[a] : 0.0; });
458 EXPECT_TRUE(result.feasible);
461 ASSERT_TRUE(result.flow.contains(arc));
462 EXPECT_GE(result.flow[arc], 3.0);
463 EXPECT_LE(result.flow[arc], 10.0);
477 std::map<TestNet::Node*, double> demands;
481 std::map<TestNet::Arc*, double> lower_bounds;
482 lower_bounds[arc] = 8.0;
485 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
486 [&](
auto* a) {
return lower_bounds.count(a) ? lower_bounds[a] : 0.0; });
508 [](
auto*) {
return 0.0; },
509 [](
auto*) {
return 0.0; });
511 EXPECT_TRUE(result.feasible);
514 EXPECT_DOUBLE_EQ(result.flow[ab], 0.0);
515 EXPECT_DOUBLE_EQ(result.flow[bc], 0.0);
516 EXPECT_DOUBLE_EQ(result.flow[ca], 0.0);
529 double cap_before = arc->cap;
531 std::map<TestNet::Node*, double> demands;
536 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
537 [](
auto*) {
return 0.0; });
542 EXPECT_DOUBLE_EQ(arc->cap, cap_before);
543 EXPECT_DOUBLE_EQ(arc->flow, 0.0);
565 std::map<TestNet::Node*, double> demands;
570 [&](
auto* n) {
return demands.count(n) ? demands[n] : 0.0; },
571 [](
auto*) {
return 0.0; });
573 EXPECT_TRUE(result.feasible);
574 EXPECT_DOUBLE_EQ(result.excess_flow, 10.0);
577 double total_to_t = result.flow[bt] + result.flow[ct];
578 EXPECT_DOUBLE_EQ(total_to_t, 10.0);
581 double total_from_s = result.flow[sb] + result.flow[sc];
582 EXPECT_DOUBLE_EQ(total_from_s, 10.0);
590 [](
auto*) {
return 0.0; },
591 [](
auto*) {
return 0.0; });
593 EXPECT_TRUE(result.feasible);
599 ::testing::InitGoogleTest(&argc, argv);
600 return RUN_ALL_TESTS();
std::vector< Team > create_simple_division()
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
BaseballEliminationResult check_baseball_elimination(const std::vector< Team > &teams, size_t team_idx)
Check if a team is mathematically eliminated from winning.
ProjectSelectionResult< Value_Type > solve_project_selection(const std::vector< Project< Value_Type > > &projects)
Solve project selection problem using max-flow.
SegmentationResult segment_image(size_t rows, size_t cols, const std::vector< std::vector< std::array< Value_Type, 2 > > > &data_cost, Value_Type smoothness)
Segment image using graph cuts.
SurveyDesignResult design_survey(const std::vector< SurveyQuestion > &questions, const std::vector< SurveyRespondent > &respondents)
Design survey assignment using network flow.
CirculationResult< typename Net::Flow_Type > solve_circulation(Net &net, GetDemand get_demand, GetLower get_lower)
Solve a circulation problem with demands.
Network flow applications.
TEST_F(ProjectSelectionTest, SimpleProjects)
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.