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 RunLinearProgrammingExample(
00022 MPSolver::OptimizationProblemType optimization_problem_type) {
00023 MPSolver solver("LinearProgrammingExample", optimization_problem_type);
00024 const double infinity = solver.infinity();
00025
00026 MPVariable* const x1 = solver.MakeNumVar(0.0, infinity, "x1");
00027 MPVariable* const x2 = solver.MakeNumVar(0.0, infinity, "x2");
00028 MPVariable* const x3 = solver.MakeNumVar(0.0, infinity, "x3");
00029
00030
00031 solver.SetObjectiveCoefficient(x1, 10);
00032 solver.SetObjectiveCoefficient(x2, 6);
00033 solver.SetObjectiveCoefficient(x3, 4);
00034 solver.SetMaximization();
00035
00036
00037 MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 100.0);
00038 c0->SetCoefficient(x1, 1);
00039 c0->SetCoefficient(x2, 1);
00040 c0->SetCoefficient(x3, 1);
00041
00042
00043 MPConstraint* const c1 = solver.MakeRowConstraint(-infinity, 600.0);
00044 c1->SetCoefficient(x1, 10);
00045 c1->SetCoefficient(x2, 4);
00046 c1->SetCoefficient(x3, 5);
00047
00048
00049 MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 300.0);
00050 c2->SetCoefficient(x1, 2);
00051 c2->SetCoefficient(x2, 2);
00052 c2->SetCoefficient(x3, 6);
00053
00054
00055
00056 LOG(INFO) << "Number of variables = " << solver.NumVariables();
00057 LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
00058
00059 const MPSolver::ResultStatus result_status = solver.Solve();
00060
00061
00062 if (result_status != MPSolver::OPTIMAL) {
00063 LOG(FATAL) << "The problem does not have an optimal solution!";
00064 }
00065
00066 LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
00067
00068
00069 LOG(INFO) << "Optimal objective value = " << solver.objective_value();
00070
00071
00072 LOG(INFO) << "x1 = " << x1->solution_value();
00073 LOG(INFO) << "x2 = " << x2->solution_value();
00074 LOG(INFO) << "x3 = " << x3->solution_value();
00075
00076 LOG(INFO) << "Advanced usage:";
00077 LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations";
00078 LOG(INFO) << "x1: reduced cost = " << x1->reduced_cost();
00079 LOG(INFO) << "x2: reduced cost = " << x2->reduced_cost();
00080 LOG(INFO) << "x3: reduced cost = " << x3->reduced_cost();
00081 LOG(INFO) << "c0: dual value = " << c0->dual_value()
00082 << " activity = " << c0->activity();
00083 LOG(INFO) << "c1: dual value = " << c1->dual_value()
00084 << " activity = " << c1->activity();
00085 LOG(INFO) << "c2: dual value = " << c2->dual_value()
00086 << " activity = " << c2->activity();
00087 }
00088
00089 void RunAllExamples() {
00090 #if defined(USE_GLPK)
00091 LOG(INFO) << "---- Linear programming example with GLPK ----";
00092 RunLinearProgrammingExample(MPSolver::GLPK_LINEAR_PROGRAMMING);
00093 #endif // USE_GLPK
00094 #if defined(USE_CLP)
00095 LOG(INFO) << "---- Linear programming example with CLP ----";
00096 RunLinearProgrammingExample(MPSolver::CLP_LINEAR_PROGRAMMING);
00097 #endif // USE_CLP
00098 }
00099 }
00100
00101 int main(int argc, char **argv) {
00102 google::ParseCommandLineFlags(&argc, &argv, true);
00103 operations_research::RunAllExamples();
00104 return 0;
00105 }