export algorithms/

This commit is contained in:
Corentin Le Molgat
2023-06-21 08:42:31 +02:00
parent a52105b8cd
commit bd3bafbd07

View File

@@ -14,73 +14,78 @@
#include "ortools/algorithms/weighted_set_covering.h"
#include "gtest/gtest.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/algorithms/weighted_set_covering_model.h"
#include "ortools/base/logging.h"
namespace operations_research {
namespace {
TEST(SetCoveringTest, InitialValues) {
WeightedSetCovering set_covering;
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(0);
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(1);
set_covering.AddElementToLastSubset(2);
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(1);
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(2);
set_covering.Init();
WeightedSetCoveringModel model;
model.AddEmptySubset(1);
model.AddElementToLastSubset(0);
model.AddEmptySubset(1);
model.AddElementToLastSubset(1);
model.AddElementToLastSubset(2);
model.AddEmptySubset(1);
model.AddElementToLastSubset(1);
model.AddEmptySubset(1);
model.AddElementToLastSubset(2);
EXPECT_TRUE(model.ComputeFeasibility());
WeightedSetCoveringSolver set_covering(model);
set_covering.Initialize();
set_covering.GenerateGreedySolution();
set_covering.Steepest(500);
set_covering.GuidedTabuSearch(500);
// set_covering.GuidedTabuSearch(500);
set_covering.RestoreSolution();
}
TEST(SetCoveringTest, Infeasible) {
WeightedSetCovering set_covering;
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(0);
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(3);
set_covering.Init();
EXPECT_FALSE(set_covering.CheckFeasibility());
WeightedSetCoveringModel model;
model.AddEmptySubset(1);
model.AddElementToLastSubset(0);
model.AddEmptySubset(1);
model.AddElementToLastSubset(3);
EXPECT_FALSE(model.ComputeFeasibility());
}
TEST(SetCoveringTest, KnightsCover) {
const int knight_row_move[] = {2, 1, -1, -2, -2, -1, 1, 2};
const int knight_col_move[] = {1, 2, 2, 1, -1, -2, -2, -1};
WeightedSetCovering set_covering;
const int num_rows = 25;
const int num_cols = 25;
WeightedSetCoveringModel model;
const int num_rows = 30;
const int num_cols = 30;
for (int row = 0; row < num_rows; ++row) {
for (int col = 0; col < num_cols; ++col) {
set_covering.AddEmptySubset(1);
set_covering.AddElementToLastSubset(row * num_cols + col);
model.AddEmptySubset(1);
model.AddElementToLastSubset(row * num_cols + col);
for (int i = 0; i < 8; ++i) {
const int new_row = row + knight_row_move[i];
const int new_col = col + knight_col_move[i];
if (new_row >= 0 && new_row < num_rows && new_col >= 0 &&
new_col < num_cols) {
set_covering.AddElementToLastSubset(new_row * num_cols + new_col);
model.AddElementToLastSubset(new_row * num_cols + new_col);
}
}
}
}
EXPECT_TRUE(set_covering.CheckFeasibility());
set_covering.Init();
set_covering.UseEverything();
EXPECT_TRUE(model.ComputeFeasibility());
WeightedSetCoveringSolver set_covering(model);
set_covering.Initialize();
set_covering.GenerateTrivialSolution();
set_covering.RestoreSolution();
LOG(INFO) << set_covering.GetBestSolution().cost();
EXPECT_TRUE(set_covering.CheckSolution());
set_covering.Init();
set_covering.Initialize();
set_covering.GenerateGreedySolution();
set_covering.Steepest(500);
for (int i = 0; i < 50; ++i) {
set_covering.GuidedTabuSearch(100000);
set_covering.ResetGuidedTabuSearch();
}
set_covering.RestoreSolution();
LOG(INFO) << set_covering.GetBestSolution().cost();
set_covering.Steepest(1000);
set_covering.RestoreSolution();
LOG(INFO) << set_covering.GetBestSolution().cost();
set_covering.GuidedTabuSearch(10);
set_covering.RestoreSolution();
LOG(INFO) << set_covering.GetBestSolution().cost();
EXPECT_TRUE(set_covering.CheckSolution());
}