Commit 11ff57e9 authored by Valentin Platzgummer's avatar Valentin Platzgummer Committed by Valentin Platzgummer

or-tools src added

parent 30afb009
# callgrind format
version: 1
creator: callgrind-3.13.0
pid: 24465
cmd: ./QGroundControl.AppImage
part: 1
desc: I1 cache:
desc: D1 cache:
desc: LL cache:
desc: Timerange: Basic block 0 - 206412
desc: Trigger: Program termination
positions: line
events: Ir
summary: 0
totals: 0
# callgrind format
version: 1
creator: callgrind-3.13.0
pid: 24468
cmd: ./QGroundControl.AppImage
part: 1
desc: I1 cache:
desc: D1 cache:
desc: LL cache:
desc: Timerange: Basic block 0 - 366088887
desc: Trigger: Program termination
positions: line
events: Ir
summary: 0
totals: 0
...@@ -269,6 +269,8 @@ test_cc: detect_cc ...@@ -269,6 +269,8 @@ test_cc: detect_cc
$(MAKE) run SOURCE=examples/cpp/vrp.cc $(MAKE) run SOURCE=examples/cpp/vrp.cc
$(MAKE) run SOURCE=examples/cpp/nurses_cp.cc $(MAKE) run SOURCE=examples/cpp/nurses_cp.cc
$(MAKE) run SOURCE=examples/cpp/minimal_jobshop_cp.cc $(MAKE) run SOURCE=examples/cpp/minimal_jobshop_cp.cc
$(MAKE) run SOURCE=examples/cpp/linear_programming.cc
$(MAKE) run SOURCE=examples/cpp/integer_programming.cc
################## ##################
## C++ SOURCE ## ## C++ SOURCE ##
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
// [END import] // [END import]
namespace operations_research { namespace operations_research {
void IntegerProgrammingExample() { void AssignmentMip() {
// Data // Data
// [START data_model] // [START data_model]
const std::vector<std::vector<double>> costs{ const std::vector<std::vector<double>> costs{
...@@ -34,7 +34,7 @@ void IntegerProgrammingExample() { ...@@ -34,7 +34,7 @@ void IntegerProgrammingExample() {
// Solver // Solver
// [START solver] // [START solver]
// Create the mip solver with the CBC backend. // Create the mip solver with the CBC backend.
MPSolver solver("simple_mip_program", MPSolver solver("AssignmentMip",
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING); MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver] // [END solver]
...@@ -112,7 +112,7 @@ void IntegerProgrammingExample() { ...@@ -112,7 +112,7 @@ void IntegerProgrammingExample() {
} // namespace operations_research } // namespace operations_research
int main(int argc, char** argv) { int main(int argc, char** argv) {
operations_research::IntegerProgrammingExample(); operations_research::AssignmentMip();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
// [END program] // [END program]
...@@ -41,7 +41,7 @@ void RunConstraintProgrammingExample() { ...@@ -41,7 +41,7 @@ void RunConstraintProgrammingExample() {
solver.NewSearch(db); solver.NewSearch(db);
while (solver.NextSolution()) { while (solver.NextSolution()) {
LOG(INFO) << "Solution" LOG(INFO) << "Solution"
<< ": x = " << x->Value() << "; y = " << x->Value() << ": x = " << x->Value() << "; y = " << y->Value()
<< "; z = " << z->Value(); << "; z = " << z->Value();
} }
solver.EndSearch(); solver.EndSearch();
......
...@@ -13,13 +13,25 @@ ...@@ -13,13 +13,25 @@
// Integer programming example that shows how to use the API. // Integer programming example that shows how to use the API.
#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h" #include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h" #include "ortools/linear_solver/linear_solver.h"
namespace operations_research { namespace operations_research {
void RunIntegerProgrammingExample( void RunIntegerProgrammingExample(
MPSolver::OptimizationProblemType optimization_problem_type) { const std::string& optimization_problem_type) {
MPSolver solver("IntegerProgrammingExample", optimization_problem_type); LOG(INFO) << "---- Integer programming example with "
<< optimization_problem_type << " ----";
if (!MPSolver::ParseAndCheckSupportForProblemType(
optimization_problem_type)) {
LOG(INFO) << " support for solver not linked in.";
return;
}
MPSolver solver("IntegerProgrammingExample",
MPSolver::ParseSolverTypeOrDie(optimization_problem_type));
const double infinity = solver.infinity(); const double infinity = solver.infinity();
// x and y are integer non-negative variables. // x and y are integer non-negative variables.
MPVariable* const x = solver.MakeIntVar(0.0, infinity, "x"); MPVariable* const x = solver.MakeIntVar(0.0, infinity, "x");
...@@ -44,9 +56,6 @@ void RunIntegerProgrammingExample( ...@@ -44,9 +56,6 @@ void RunIntegerProgrammingExample(
LOG(INFO) << "Number of variables = " << solver.NumVariables(); LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of constraints = " << solver.NumConstraints(); LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
solver.SetNumThreads(8);
solver.EnableOutput();
const MPSolver::ResultStatus result_status = solver.Solve(); const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution. // Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) { if (result_status != MPSolver::OPTIMAL) {
...@@ -65,32 +74,21 @@ void RunIntegerProgrammingExample( ...@@ -65,32 +74,21 @@ void RunIntegerProgrammingExample(
} }
void RunAllExamples() { void RunAllExamples() {
#if defined(USE_CBC) RunIntegerProgrammingExample("CBC");
LOG(INFO) << "---- Integer programming example with CBC ----"; RunIntegerProgrammingExample("SAT");
RunIntegerProgrammingExample(MPSolver::CBC_MIXED_INTEGER_PROGRAMMING); RunIntegerProgrammingExample("SCIP");
#endif RunIntegerProgrammingExample("GUROBI");
#if defined(USE_GLPK) RunIntegerProgrammingExample("GLPK");
LOG(INFO) << "---- Integer programming example with GLPK ----"; RunIntegerProgrammingExample("CPLEX");
RunIntegerProgrammingExample(MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING); RunIntegerProgrammingExample("XPRESS");
#endif
#if defined(USE_SCIP)
LOG(INFO) << "---- Integer programming example with SCIP ----";
RunIntegerProgrammingExample(MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
#endif
#if defined(USE_GUROBI)
LOG(INFO) << "---- Integer programming example with Gurobi ----";
RunIntegerProgrammingExample(MPSolver::GUROBI_MIXED_INTEGER_PROGRAMMING);
#endif // USE_GUROBI
#if defined(USE_CPLEX)
LOG(INFO) << "---- Integer programming example with CPLEX ----";
RunIntegerProgrammingExample(MPSolver::CPLEX_MIXED_INTEGER_PROGRAMMING);
#endif // USE_CPLEX
} }
} // namespace operations_research } // namespace operations_research
int main(int argc, char** argv) { int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]); google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = 1; absl::SetFlag(&FLAGS_logtostderr, true);
absl::SetFlag(&FLAGS_log_prefix, false);
gflags::ParseCommandLineFlags(&argc, &argv, true);
operations_research::RunAllExamples(); operations_research::RunAllExamples();
return EXIT_SUCCESS; return 0;
} }
...@@ -21,7 +21,7 @@ namespace operations_research { ...@@ -21,7 +21,7 @@ namespace operations_research {
void IntegerProgrammingExample() { void IntegerProgrammingExample() {
// [START solver] // [START solver]
// Create the mip solver with the CBC backend. // Create the mip solver with the CBC backend.
MPSolver solver("IntegerExample", MPSolver solver("integer_programming_example",
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING); MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver] // [END solver]
......
...@@ -13,58 +13,82 @@ ...@@ -13,58 +13,82 @@
// Linear programming example that shows how to use the API. // Linear programming example that shows how to use the API.
#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h" #include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h" #include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h" #include "ortools/linear_solver/linear_solver.pb.h"
namespace operations_research { namespace operations_research {
void RunLinearProgrammingExample() { void RunLinearProgrammingExample(const std::string& optimization_problem_type) {
MPSolver solver("LinearProgrammingExample", LOG(INFO) << "---- Linear programming example with "
MPSolver::GLOP_LINEAR_PROGRAMMING); << optimization_problem_type << " ----";
if (!MPSolver::ParseAndCheckSupportForProblemType(
optimization_problem_type)) {
LOG(INFO) << " support for solver not linked in.";
return;
}
MPSolver solver("IntegerProgrammingExample",
MPSolver::ParseSolverTypeOrDie(optimization_problem_type));
const double infinity = solver.infinity(); const double infinity = solver.infinity();
// x and y are continuous non-negative variables. // x1, x2 and x3 are continuous non-negative variables.
MPVariable* const x = solver.MakeNumVar(0.0, infinity, "x"); MPVariable* const x1 = solver.MakeNumVar(0.0, infinity, "x1");
MPVariable* const y = solver.MakeNumVar(0.0, infinity, "y"); MPVariable* const x2 = solver.MakeNumVar(0.0, infinity, "x2");
MPVariable* const x3 = solver.MakeNumVar(0.0, infinity, "x3");
// Objectif function: Maximize 3x + 4y. // Maximize 10 * x1 + 6 * x2 + 4 * x3.
MPObjective* const objective = solver.MutableObjective(); MPObjective* const objective = solver.MutableObjective();
objective->SetCoefficient(x, 3); objective->SetCoefficient(x1, 10);
objective->SetCoefficient(y, 4); objective->SetCoefficient(x2, 6);
objective->SetCoefficient(x3, 4);
objective->SetMaximization(); objective->SetMaximization();
// x + 2y <= 14. // x1 + x2 + x3 <= 100.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 14.0); MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 100.0);
c0->SetCoefficient(x, 1); c0->SetCoefficient(x1, 1);
c0->SetCoefficient(y, 2); c0->SetCoefficient(x2, 1);
c0->SetCoefficient(x3, 1);
// 10 * x1 + 4 * x2 + 5 * x3 <= 600.
MPConstraint* const c1 = solver.MakeRowConstraint(-infinity, 600.0);
c1->SetCoefficient(x1, 10);
c1->SetCoefficient(x2, 4);
c1->SetCoefficient(x3, 5);
// 3x - y >= 0. // 2 * x1 + 2 * x2 + 6 * x3 <= 300.
MPConstraint* const c1 = solver.MakeRowConstraint(0.0, infinity); MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 300.0);
c1->SetCoefficient(x, 3); c2->SetCoefficient(x1, 2);
c1->SetCoefficient(y, -1); c2->SetCoefficient(x2, 2);
c2->SetCoefficient(x3, 6);
// x - y <= 2. // TODO(user): Change example to show = and >= constraints.
MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 2.0);
c2->SetCoefficient(x, 1);
c2->SetCoefficient(y, -1);
LOG(INFO) << "Number of variables = " << solver.NumVariables(); LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of constraints = " << solver.NumConstraints(); LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
const MPSolver::ResultStatus result_status = solver.Solve(); const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution. // Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) { if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!"; LOG(FATAL) << "The problem does not have an optimal solution!";
} }
LOG(INFO) << "Solution:";
LOG(INFO) << "x = " << x->solution_value(); LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
LOG(INFO) << "y = " << y->solution_value();
// The objective value of the solution.
LOG(INFO) << "Optimal objective value = " << objective->Value(); LOG(INFO) << "Optimal objective value = " << objective->Value();
LOG(INFO) << "";
// The value of each variable in the solution.
LOG(INFO) << "x1 = " << x1->solution_value();
LOG(INFO) << "x2 = " << x2->solution_value();
LOG(INFO) << "x3 = " << x3->solution_value();
LOG(INFO) << "Advanced usage:"; LOG(INFO) << "Advanced usage:";
LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations"; LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations";