Files
ortools-clone/ortools/linear_solver/samples/simple_mip_program.cc

94 lines
3.0 KiB
C++
Raw Normal View History

2022-06-17 08:40:20 +02:00
// Copyright 2010-2022 Google LLC
2018-11-22 09:32:03 +01:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Mixed Integer programming example that shows how to use the API.
// [START program]
2019-05-13 10:04:00 +02:00
// [START import]
2022-07-22 14:35:40 +02:00
#include <memory>
2018-11-22 09:32:03 +01:00
#include "ortools/linear_solver/linear_solver.h"
2019-05-13 10:04:00 +02:00
// [END import]
2018-11-22 09:32:03 +01:00
namespace operations_research {
2020-06-25 11:14:37 +02:00
void SimpleMipProgram() {
2018-11-22 09:32:03 +01:00
// [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;
}
2018-11-22 09:32:03 +01:00
// [END solver]
// [START variables]
2020-12-07 14:57:58 +01:00
const double infinity = solver->infinity();
2018-11-22 09:32:03 +01:00
// x and y are integer non-negative variables.
2020-12-07 14:57:58 +01:00
MPVariable* const x = solver->MakeIntVar(0.0, infinity, "x");
MPVariable* const y = solver->MakeIntVar(0.0, infinity, "y");
2018-11-22 09:32:03 +01:00
2020-12-07 14:57:58 +01:00
LOG(INFO) << "Number of variables = " << solver->NumVariables();
2018-11-22 09:32:03 +01:00
// [END variables]
// [START constraints]
// x + 7 * y <= 17.5.
2020-12-07 14:57:58 +01:00
MPConstraint* const c0 = solver->MakeRowConstraint(-infinity, 17.5, "c0");
2018-11-22 09:32:03 +01:00
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 7);
// x <= 3.5.
2020-12-07 14:57:58 +01:00
MPConstraint* const c1 = solver->MakeRowConstraint(-infinity, 3.5, "c1");
2018-11-22 09:32:03 +01:00
c1->SetCoefficient(x, 1);
c1->SetCoefficient(y, 0);
2020-12-07 14:57:58 +01:00
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
2018-11-22 09:32:03 +01:00
// [END constraints]
// [START objective]
// Maximize x + 10 * y.
2020-12-07 14:57:58 +01:00
MPObjective* const objective = solver->MutableObjective();
2018-11-22 09:32:03 +01:00
objective->SetCoefficient(x, 1);
objective->SetCoefficient(y, 10);
objective->SetMaximization();
// [END objective]
// [START solve]
2020-12-07 14:57:58 +01:00
const MPSolver::ResultStatus result_status = solver->Solve();
2018-11-22 09:32:03 +01:00
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
}
// [END solve]
// [START print_solution]
LOG(INFO) << "Solution:";
LOG(INFO) << "Objective value = " << objective->Value();
2018-11-22 09:32:03 +01:00
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
// [END print_solution]
// [START advanced]
LOG(INFO) << "\nAdvanced usage:";
2020-12-07 14:57:58 +01:00
LOG(INFO) << "Problem solved in " << solver->wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver->iterations() << " iterations";
LOG(INFO) << "Problem solved in " << solver->nodes()
2018-11-22 09:32:03 +01:00
<< " branch-and-bound nodes";
// [END advanced]
}
} // namespace operations_research
int main(int argc, char** argv) {
2020-06-25 11:14:37 +02:00
operations_research::SimpleMipProgram();
2018-11-22 09:32:03 +01:00
return EXIT_SUCCESS;
}
// [END program]