Use CreateSolver everywhere and check SCIP (Fix #2395)

This commit is contained in:
Mizux Seiha
2021-03-31 16:05:32 +02:00
parent 1f7e6399e9
commit 6faa70e7dc
7 changed files with 77 additions and 52 deletions

View File

@@ -34,7 +34,11 @@ void AssignmentMip() {
// Solver
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("assignment_mip", MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// Variables
@@ -45,7 +49,7 @@ void AssignmentMip() {
num_workers, std::vector<const MPVariable*>(num_tasks));
for (int i = 0; i < num_workers; ++i) {
for (int j = 0; j < num_tasks; ++j) {
x[i][j] = solver.MakeIntVar(0, 1, "");
x[i][j] = solver->MakeIntVar(0, 1, "");
}
}
// [END variables]
@@ -58,7 +62,7 @@ void AssignmentMip() {
for (int j = 0; j < num_tasks; ++j) {
worker_sum += x[i][j];
}
solver.MakeRowConstraint(worker_sum <= 1.0);
solver->MakeRowConstraint(worker_sum <= 1.0);
}
// Each task is assigned to exactly one worker.
for (int j = 0; j < num_tasks; ++j) {
@@ -66,13 +70,13 @@ void AssignmentMip() {
for (int i = 0; i < num_workers; ++i) {
task_sum += x[i][j];
}
solver.MakeRowConstraint(task_sum == 1.0);
solver->MakeRowConstraint(task_sum == 1.0);
}
// [END constraints]
// Objective.
// [START objective]
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
for (int i = 0; i < num_workers; ++i) {
for (int j = 0; j < num_tasks; ++j) {
objective->SetCoefficient(x[i][j], costs[i][j]);
@@ -83,7 +87,7 @@ void AssignmentMip() {
// Solve
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// [END solve]
// Print solution.

View File

@@ -41,7 +41,11 @@ void BinPackingMip() {
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("bin_packing_mip", MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START program_part2]
@@ -50,13 +54,13 @@ void BinPackingMip() {
data.num_items, std::vector<const MPVariable*>(data.num_bins));
for (int i = 0; i < data.num_items; ++i) {
for (int j = 0; j < data.num_bins; ++j) {
x[i][j] = solver.MakeIntVar(0.0, 1.0, "");
x[i][j] = solver->MakeIntVar(0.0, 1.0, "");
}
}
// y[j] = 1 if bin j is used.
std::vector<const MPVariable*> y(data.num_bins);
for (int j = 0; j < data.num_bins; ++j) {
y[j] = solver.MakeIntVar(0.0, 1.0, "");
y[j] = solver->MakeIntVar(0.0, 1.0, "");
}
// [END variables]
@@ -68,7 +72,7 @@ void BinPackingMip() {
for (int j = 0; j < data.num_bins; ++j) {
sum += x[i][j];
}
solver.MakeRowConstraint(sum == 1.0);
solver->MakeRowConstraint(sum == 1.0);
}
// For each bin that is used, the total packed weight can be at most
// the bin capacity.
@@ -77,13 +81,13 @@ void BinPackingMip() {
for (int i = 0; i < data.num_items; ++i) {
weight += data.weights[i] * LinearExpr(x[i][j]);
}
solver.MakeRowConstraint(weight <= LinearExpr(y[j]) * data.bin_capacity);
solver->MakeRowConstraint(weight <= LinearExpr(y[j]) * data.bin_capacity);
}
// [END constraints]
// [START objective]
// Create the objective function.
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
LinearExpr num_bins_used;
for (int j = 0; j < data.num_bins; ++j) {
num_bins_used += y[j];
@@ -92,7 +96,7 @@ void BinPackingMip() {
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// [END solve]
// [START print_solution]

View File

@@ -22,45 +22,48 @@ namespace operations_research {
void IntegerProgrammingExample() {
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("integer_programming_example",
MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START variables]
// x, y, and z are non-negative integer variables.
MPVariable* const x = solver.MakeIntVar(0.0, solver.infinity(), "x");
MPVariable* const y = solver.MakeIntVar(0.0, solver.infinity(), "y");
MPVariable* const z = solver.MakeIntVar(0.0, solver.infinity(), "z");
LOG(INFO) << "Number of variables = " << solver.NumVariables();
MPVariable* const x = solver->MakeIntVar(0.0, solver->infinity(), "x");
MPVariable* const y = solver->MakeIntVar(0.0, solver->infinity(), "y");
MPVariable* const z = solver->MakeIntVar(0.0, solver->infinity(), "z");
LOG(INFO) << "Number of variables = " << solver->NumVariables();
// [END variables]
// [START constraints]
// 2*x + 7*y + 3*z <= 50
MPConstraint* const constraint0 =
solver.MakeRowConstraint(-solver.infinity(), 50);
solver->MakeRowConstraint(-solver->infinity(), 50);
constraint0->SetCoefficient(x, 2);
constraint0->SetCoefficient(y, 7);
constraint0->SetCoefficient(z, 3);
// 3*x - 5*y + 7*z <= 45
MPConstraint* const constraint1 =
solver.MakeRowConstraint(-solver.infinity(), 45);
solver->MakeRowConstraint(-solver->infinity(), 45);
constraint1->SetCoefficient(x, 3);
constraint1->SetCoefficient(y, -5);
constraint1->SetCoefficient(z, 7);
// 5*x + 2*y - 6*z <= 37
MPConstraint* const constraint2 =
solver.MakeRowConstraint(-solver.infinity(), 37);
solver->MakeRowConstraint(-solver->infinity(), 37);
constraint2->SetCoefficient(x, 5);
constraint2->SetCoefficient(y, 2);
constraint2->SetCoefficient(z, -6);
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
// [END constraints]
// [START objective]
// Maximize 2*x + 2*y + 3*z
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
objective->SetCoefficient(x, 2);
objective->SetCoefficient(y, 2);
objective->SetCoefficient(z, 3);
@@ -68,7 +71,7 @@ void IntegerProgrammingExample() {
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";

View File

@@ -21,46 +21,49 @@
namespace operations_research {
void LinearProgrammingExample() {
// [START solver]
MPSolver solver("linear_programming_examples",
MPSolver::GLOP_LINEAR_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START variables]
const double infinity = solver.infinity();
const double infinity = solver->infinity();
// x and y are non-negative variables.
MPVariable* const x = solver.MakeNumVar(0.0, infinity, "x");
MPVariable* const y = solver.MakeNumVar(0.0, infinity, "y");
LOG(INFO) << "Number of variables = " << solver.NumVariables();
MPVariable* const x = solver->MakeNumVar(0.0, infinity, "x");
MPVariable* const y = solver->MakeNumVar(0.0, infinity, "y");
LOG(INFO) << "Number of variables = " << solver->NumVariables();
// [END variables]
// [START constraints]
// x + 2*y <= 14.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 14.0);
MPConstraint* const c0 = solver->MakeRowConstraint(-infinity, 14.0);
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 2);
// 3*x - y >= 0.
MPConstraint* const c1 = solver.MakeRowConstraint(0.0, infinity);
MPConstraint* const c1 = solver->MakeRowConstraint(0.0, infinity);
c1->SetCoefficient(x, 3);
c1->SetCoefficient(y, -1);
// x - y <= 2.
MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 2.0);
MPConstraint* const c2 = solver->MakeRowConstraint(-infinity, 2.0);
c2->SetCoefficient(x, 1);
c2->SetCoefficient(y, -1);
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
// [END constraints]
// [START objective]
// Objective function: 3x + 4y.
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
objective->SetCoefficient(x, 3);
objective->SetCoefficient(y, 4);
objective->SetMaximization();
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";

View File

@@ -41,34 +41,38 @@ void MipVarArray() {
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("mip_var_array", MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START program_part2]
// [START variables]
const double infinity = solver.infinity();
const double infinity = solver->infinity();
// x[j] is an array of non-negative, integer variables.
std::vector<const MPVariable*> x(data.num_vars);
for (int j = 0; j < data.num_vars; ++j) {
x[j] = solver.MakeIntVar(0.0, infinity, "");
x[j] = solver->MakeIntVar(0.0, infinity, "");
}
LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of variables = " << solver->NumVariables();
// [END variables]
// [START constraints]
// Create the constraints.
for (int i = 0; i < data.num_constraints; ++i) {
MPConstraint* constraint = solver.MakeRowConstraint(0, data.bounds[i], "");
MPConstraint* constraint = solver->MakeRowConstraint(0, data.bounds[i], "");
for (int j = 0; j < data.num_vars; ++j) {
constraint->SetCoefficient(x[j], data.constraint_coeffs[i][j]);
}
}
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
// [END constraints]
// [START objective]
// Create the objective function.
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
for (int j = 0; j < data.num_vars; ++j) {
objective->SetCoefficient(x[j], data.obj_coeffs[j]);
}
@@ -76,7 +80,7 @@ void MipVarArray() {
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// [END solve]
// [START print_solution]

View File

@@ -44,8 +44,11 @@ void MultipleKnapsackMip() {
// [START solver]
// Create the mip solver with the SCIP backend.
MPSolver solver("multiple_knapsack_mip",
MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START program_part2]
@@ -54,7 +57,7 @@ void MultipleKnapsackMip() {
data.num_items, std::vector<const MPVariable*>(data.num_bins));
for (int i = 0; i < data.num_items; ++i) {
for (int j = 0; j < data.num_bins; ++j) {
x[i][j] = solver.MakeIntVar(0.0, 1.0, "");
x[i][j] = solver->MakeIntVar(0.0, 1.0, "");
}
}
// [END variables]
@@ -67,7 +70,7 @@ void MultipleKnapsackMip() {
for (int j = 0; j < data.num_bins; ++j) {
sum += x[i][j];
}
solver.MakeRowConstraint(sum <= 1.0);
solver->MakeRowConstraint(sum <= 1.0);
}
// For each bin that is used, the total packed weight can be at most
// the bin capacity.
@@ -76,13 +79,13 @@ void MultipleKnapsackMip() {
for (int i = 0; i < data.num_items; ++i) {
weight += data.weights[i] * LinearExpr(x[i][j]);
}
solver.MakeRowConstraint(weight <= data.bin_capacities[j]);
solver->MakeRowConstraint(weight <= data.bin_capacities[j]);
}
// [END constraints]
// [START objective]
// Create the objective function.
MPObjective* const objective = solver.MutableObjective();
MPObjective* const objective = solver->MutableObjective();
LinearExpr value;
for (int i = 0; i < data.num_items; ++i) {
for (int j = 0; j < data.num_bins; ++j) {
@@ -93,7 +96,7 @@ void MultipleKnapsackMip() {
// [END objective]
// [START solve]
const MPSolver::ResultStatus result_status = solver.Solve();
const MPSolver::ResultStatus result_status = solver->Solve();
// [END solve]
// [START print_solution]

View File

@@ -22,6 +22,10 @@ void SimpleMipProgram() {
// [START solver]
// Create the mip solver with the SCIP backend.
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("SCIP"));
if (!solver) {
LOG(WARNING) << "SCIP solver unavailable.";
return;
}
// [END solver]
// [START variables]