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
$(MAKE) run SOURCE=examples/cpp/vrp.cc
$(MAKE) run SOURCE=examples/cpp/nurses_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 ##
......
......@@ -20,7 +20,7 @@
// [END import]
namespace operations_research {
void IntegerProgrammingExample() {
void AssignmentMip() {
// Data
// [START data_model]
const std::vector<std::vector<double>> costs{
......@@ -34,7 +34,7 @@ void IntegerProgrammingExample() {
// Solver
// [START solver]
// Create the mip solver with the CBC backend.
MPSolver solver("simple_mip_program",
MPSolver solver("AssignmentMip",
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver]
......@@ -112,7 +112,7 @@ void IntegerProgrammingExample() {
} // namespace operations_research
int main(int argc, char** argv) {
operations_research::IntegerProgrammingExample();
operations_research::AssignmentMip();
return EXIT_SUCCESS;
}
// [END program]
......@@ -41,7 +41,7 @@ void RunConstraintProgrammingExample() {
solver.NewSearch(db);
while (solver.NextSolution()) {
LOG(INFO) << "Solution"
<< ": x = " << x->Value() << "; y = " << x->Value()
<< ": x = " << x->Value() << "; y = " << y->Value()
<< "; z = " << z->Value();
}
solver.EndSearch();
......
......@@ -13,13 +13,25 @@
// Integer programming example that shows how to use the API.
#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h"
namespace operations_research {
void RunIntegerProgrammingExample(
MPSolver::OptimizationProblemType optimization_problem_type) {
MPSolver solver("IntegerProgrammingExample", optimization_problem_type);
const std::string& 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();
// x and y are integer non-negative variables.
MPVariable* const x = solver.MakeIntVar(0.0, infinity, "x");
......@@ -44,9 +56,6 @@ void RunIntegerProgrammingExample(
LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
solver.SetNumThreads(8);
solver.EnableOutput();
const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
......@@ -65,32 +74,21 @@ void RunIntegerProgrammingExample(
}
void RunAllExamples() {
#if defined(USE_CBC)
LOG(INFO) << "---- Integer programming example with CBC ----";
RunIntegerProgrammingExample(MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
#endif
#if defined(USE_GLPK)
LOG(INFO) << "---- Integer programming example with GLPK ----";
RunIntegerProgrammingExample(MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING);
#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
RunIntegerProgrammingExample("CBC");
RunIntegerProgrammingExample("SAT");
RunIntegerProgrammingExample("SCIP");
RunIntegerProgrammingExample("GUROBI");
RunIntegerProgrammingExample("GLPK");
RunIntegerProgrammingExample("CPLEX");
RunIntegerProgrammingExample("XPRESS");
}
} // namespace operations_research
int main(int argc, char** argv) {
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();
return EXIT_SUCCESS;
return 0;
}
......@@ -21,7 +21,7 @@ namespace operations_research {
void IntegerProgrammingExample() {
// [START solver]
// Create the mip solver with the CBC backend.
MPSolver solver("IntegerExample",
MPSolver solver("integer_programming_example",
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver]
......
......@@ -13,58 +13,82 @@
// Linear programming example that shows how to use the API.
#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"
namespace operations_research {
void RunLinearProgrammingExample() {
MPSolver solver("LinearProgrammingExample",
MPSolver::GLOP_LINEAR_PROGRAMMING);
void RunLinearProgrammingExample(const std::string& optimization_problem_type) {
LOG(INFO) << "---- Linear 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();
// x and y are continuous non-negative variables.
MPVariable* const x = solver.MakeNumVar(0.0, infinity, "x");
MPVariable* const y = solver.MakeNumVar(0.0, infinity, "y");
// x1, x2 and x3 are continuous non-negative variables.
MPVariable* const x1 = solver.MakeNumVar(0.0, infinity, "x1");
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();
objective->SetCoefficient(x, 3);
objective->SetCoefficient(y, 4);
objective->SetCoefficient(x1, 10);
objective->SetCoefficient(x2, 6);
objective->SetCoefficient(x3, 4);
objective->SetMaximization();
// x + 2y <= 14.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 14.0);
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 2);
// x1 + x2 + x3 <= 100.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 100.0);
c0->SetCoefficient(x1, 1);
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.
MPConstraint* const c1 = solver.MakeRowConstraint(0.0, infinity);
c1->SetCoefficient(x, 3);
c1->SetCoefficient(y, -1);
// 2 * x1 + 2 * x2 + 6 * x3 <= 300.
MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 300.0);
c2->SetCoefficient(x1, 2);
c2->SetCoefficient(x2, 2);
c2->SetCoefficient(x3, 6);
// x - y <= 2.
MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 2.0);
c2->SetCoefficient(x, 1);
c2->SetCoefficient(y, -1);
// TODO(user): Change example to show = and >= constraints.
LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
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!";
}
LOG(INFO) << "Solution:";
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
// The objective value of the solution.
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) << "Problem solved in " << solver.wall_time() << " milliseconds";
LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations";
LOG(INFO) << "x: reduced cost = " << x->reduced_cost();
LOG(INFO) << "y: reduced cost = " << y->reduced_cost();
LOG(INFO) << "x1: reduced cost = " << x1->reduced_cost();
LOG(INFO) << "x2: reduced cost = " << x2->reduced_cost();
LOG(INFO) << "x3: reduced cost = " << x3->reduced_cost();
const std::vector<double> activities = solver.ComputeConstraintActivities();
LOG(INFO) << "c0: dual value = " << c0->dual_value()
<< " activity = " << activities[c0->index()];
......@@ -73,11 +97,22 @@ void RunLinearProgrammingExample() {
LOG(INFO) << "c2: dual value = " << c2->dual_value()
<< " activity = " << activities[c2->index()];
}
void RunAllExamples() {
RunLinearProgrammingExample("GLOP");
RunLinearProgrammingExample("CLP");
RunLinearProgrammingExample("GUROBI_LP");
RunLinearProgrammingExample("CPLEX_LP");
RunLinearProgrammingExample("GLPK_LP");
RunLinearProgrammingExample("XPRESS_LP");
}
} // namespace operations_research
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = 1;
operations_research::RunLinearProgrammingExample();
absl::SetFlag(&FLAGS_logtostderr, true);
absl::SetFlag(&FLAGS_log_prefix, false);
gflags::ParseCommandLineFlags(&argc, &argv, true);
operations_research::RunAllExamples();
return EXIT_SUCCESS;
}
......@@ -20,7 +20,8 @@
namespace operations_research {
void LinearProgrammingExample() {
// [START solver]
MPSolver solver("LinearExample", MPSolver::GLOP_LINEAR_PROGRAMMING);
MPSolver solver("linear_programming_examples",
MPSolver::GLOP_LINEAR_PROGRAMMING);
// [END solver]
// [START variables]
......
......@@ -33,7 +33,7 @@ struct DataModel {
};
// [END data_model]
void IntegerProgrammingExample() {
void MipVarArray() {
// [START data]
DataModel data;
// [END data]
......@@ -41,8 +41,7 @@ void IntegerProgrammingExample() {
// [START solver]
// Create the mip solver with the CBC backend.
MPSolver solver("simple_mip_program",
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
MPSolver solver("mip_var_array", MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver]
// [START program_part2]
......@@ -96,7 +95,7 @@ void IntegerProgrammingExample() {
} // namespace operations_research