00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <string>
00015
00016 #include "base/commandlineflags.h"
00017 #include "base/logging.h"
00018 #include "linear_solver/linear_solver.h"
00019 #include "linear_solver/linear_solver.pb.h"
00020
00021 namespace operations_research {
00022 void BuildLinearProgrammingMaxExample(MPSolver::OptimizationProblemType type) {
00023 const double kObjCoef[] = {10.0, 6.0, 4.0};
00024 const string kVarName[] = {"x1", "x2", "x3"};
00025 const int numVars = 3;
00026 const int kNumConstraints = 3;
00027 const string kConstraintName[] = {"c1", "c2", "c3"};
00028 const double kConstraintCoef1[] = {1.0, 1.0, 1.0};
00029 const double kConstraintCoef2[] = {10.0, 4.0, 5.0};
00030 const double kConstraintCoef3[] = {2.0, 2.0, 6.0};
00031 const double* kConstraintCoef[] = {kConstraintCoef1,
00032 kConstraintCoef2,
00033 kConstraintCoef3};
00034 const double kConstraintUb[] = {100.0, 600.0, 300.0};
00035
00036 const double infinity = MPSolver::infinity();
00037 MPModelProto model_proto;
00038 model_proto.set_name("Max_Example");
00039
00040
00041 for (int j = 0; j < numVars; ++j) {
00042 MPVariableProto* x = model_proto.add_variables();
00043 x->set_id(kVarName[j]);
00044 x->set_lb(0.0);
00045 x->set_ub(infinity);
00046 x->set_integer(false);
00047 MPTermProto* obj_term = model_proto.add_objective_terms();
00048 obj_term->set_variable_id(kVarName[j]);
00049 obj_term->set_coefficient(kObjCoef[j]);
00050 }
00051 model_proto.set_maximize(true);
00052
00053
00054 for (int i = 0; i < kNumConstraints; ++i) {
00055 MPConstraintProto* constraint_proto = model_proto.add_constraints();
00056 constraint_proto->set_id(kConstraintName[i]);
00057 constraint_proto->set_lb(-infinity);
00058 constraint_proto->set_ub(kConstraintUb[i]);
00059 for (int j = 0; j < numVars; ++j) {
00060 MPTermProto* term = constraint_proto->add_terms();
00061 term->set_variable_id(kVarName[j]);
00062 term->set_coefficient(kConstraintCoef[i][j]);
00063 }
00064 }
00065
00066 MPModelRequest model_request;
00067 model_request.mutable_model()->CopyFrom(model_proto);
00068 #if defined(USE_GLPK)
00069 if (type == MPSolver::GLPK_LINEAR_PROGRAMMING) {
00070 model_request.set_problem_type(MPModelRequest::GLPK_LINEAR_PROGRAMMING);
00071 }
00072 #endif // USE_GLPK
00073 #if defined(USE_CLP)
00074 if (type == MPSolver::CLP_LINEAR_PROGRAMMING) {
00075 model_request.set_problem_type(MPModelRequest::CLP_LINEAR_PROGRAMMING);
00076 }
00077 #endif // USE_CLP
00078
00079 MPSolutionResponse solution_response;
00080 MPSolver::SolveWithProtocolBuffers(model_request, &solution_response);
00081
00082
00083 CHECK_EQ(MPSolutionResponse::OPTIMAL, solution_response.result_status());
00084
00085 LOG(INFO) << "objective = " << solution_response.objective_value();
00086 const int num_non_zeros = solution_response.solution_values_size();
00087 for (int j = 0; j < num_non_zeros; ++j) {
00088 MPSolutionValue solution_value = solution_response.solution_values(j);
00089 LOG(INFO) << solution_value.variable_id() << " = "
00090 << solution_value.value();
00091 }
00092 if (num_non_zeros != numVars) {
00093 LOG(INFO) << "All other variables have zero value";
00094 }
00095 }
00096
00097 void RunAllExamples() {
00098 #if defined(USE_GLPK)
00099 LOG(INFO) << "----- Running Max Example with GLPK -----";
00100 BuildLinearProgrammingMaxExample(MPSolver::GLPK_LINEAR_PROGRAMMING);
00101 #endif // USE_GLPK
00102 #if defined(USE_CLP)
00103 LOG(INFO) << "----- Running Max Example with Coin LP -----";
00104 BuildLinearProgrammingMaxExample(MPSolver::CLP_LINEAR_PROGRAMMING);
00105 #endif // USE_CLP
00106 }
00107 }
00108
00109 int main(int argc, char **argv) {
00110 google::ParseCommandLineFlags(&argc, &argv, true);
00111 operations_research::RunAllExamples();
00112 return 0;
00113 }