00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "base/commandlineflags.h"
00017 #include "base/logging.h"
00018 #include "linear_solver/linear_solver.h"
00019
00020 namespace operations_research {
00021 void RunIntegerProgrammingExample(
00022 MPSolver::OptimizationProblemType optimization_problem_type) {
00023 MPSolver solver("IntegerProgrammingExample", optimization_problem_type);
00024 const double infinity = solver.infinity();
00025
00026 MPVariable* const x1 = solver.MakeIntVar(0.0, infinity, "x1");
00027 MPVariable* const x2 = solver.MakeIntVar(0.0, infinity, "x2");
00028
00029
00030 solver.SetObjectiveCoefficient(x1, 1);
00031 solver.SetObjectiveCoefficient(x2, 2);
00032
00033
00034 MPConstraint* const c0 = solver.MakeRowConstraint(17, infinity);
00035 c0->SetCoefficient(x1, 3);
00036 c0->SetCoefficient(x2, 2);
00037
00038 const MPSolver::ResultStatus result_status = solver.Solve();
00039
00040
00041 if (result_status != MPSolver::OPTIMAL) {
00042 LOG(FATAL) << "The problem does not have an optimal solution!";
00043 }
00044
00045 LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
00046
00047
00048 LOG(INFO) << "Optimal objective value = " << solver.objective_value();
00049
00050
00051 LOG(INFO) << "x1 = " << x1->solution_value();
00052 LOG(INFO) << "x2 = " << x2->solution_value();
00053
00054 LOG(INFO) << "Advanced usage:";
00055 LOG(INFO) << "Problem solved in " << solver.nodes()
00056 << " branch-and-bound nodes";
00057 }
00058
00059
00060 void RunAllExamples() {
00061 #if defined(USE_GLPK)
00062 LOG(INFO) << "---- Integer programming example with GLPK ----";
00063 RunIntegerProgrammingExample(MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING);
00064 #endif
00065 #if defined(USE_CBC)
00066 LOG(INFO) << "---- Integer programming example with CBC ----";
00067 RunIntegerProgrammingExample(MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
00068 #endif
00069 #if defined(USE_SCIP)
00070 LOG(INFO) << "---- Integer programming example with SCIP ----";
00071 RunIntegerProgrammingExample(MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
00072 #endif
00073 }
00074 }
00075
00076 int main(int argc, char **argv) {
00077 google::ParseCommandLineFlags(&argc, &argv, true);
00078 operations_research::RunAllExamples();
00079 return 0;
00080 }