From 4ba38c1b3d47fb0f8d8202d9b2bbed0060230373 Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Thu, 12 Nov 2020 22:05:59 +0100 Subject: [PATCH] Add a glop sample --- cmake/glop.cmake | 17 +------ ortools/glop/samples/BUILD | 3 ++ ortools/glop/samples/CMakeLists.txt | 31 ++++++++++++ ortools/glop/samples/code_samples.bzl | 23 +++++++++ ortools/glop/samples/simple_glop_program.cc | 54 +++++++++++++++++++++ 5 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 ortools/glop/samples/BUILD create mode 100644 ortools/glop/samples/CMakeLists.txt create mode 100644 ortools/glop/samples/code_samples.bzl create mode 100644 ortools/glop/samples/simple_glop_program.cc diff --git a/cmake/glop.cmake b/cmake/glop.cmake index 8b577c1e67..915a12a2bc 100644 --- a/cmake/glop.cmake +++ b/cmake/glop.cmake @@ -235,21 +235,6 @@ endif() # ALIAS add_library(${PROJECT_NAME}::glop ALIAS glop) -if(APPLE) - set(CMAKE_INSTALL_RPATH - "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path") -elseif(UNIX) - set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN") -endif() - -add_executable(glopbin "") -file(GENERATE - OUTPUT ${PROJECT_BINARY_DIR}/glop/main.cpp - CONTENT "int main(int, char**) {return 0;}") -target_sources(glopbin PRIVATE ${PROJECT_BINARY_DIR}/glop/main.cpp) - -target_link_libraries(glopbin PRIVATE glop) - # Install rules include(GNUInstallDirs) include(GenerateExportHeader) @@ -365,3 +350,5 @@ install( DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/glop" COMPONENT Devel) +# Build glop samples +add_subdirectory(ortools/glop/samples) diff --git a/ortools/glop/samples/BUILD b/ortools/glop/samples/BUILD new file mode 100644 index 0000000000..ec5baf73f9 --- /dev/null +++ b/ortools/glop/samples/BUILD @@ -0,0 +1,3 @@ +load(":code_samples.bzl", "code_sample_cc") + +code_sample_cc(sample = "simple_glop_program") diff --git a/ortools/glop/samples/CMakeLists.txt b/ortools/glop/samples/CMakeLists.txt new file mode 100644 index 0000000000..ed4e221a49 --- /dev/null +++ b/ortools/glop/samples/CMakeLists.txt @@ -0,0 +1,31 @@ +if(NOT BUILD_SAMPLES AND NOT BUILD_GLOP) + return() +endif() + + +if(BUILD_GLOP) + if(APPLE) + set(CMAKE_INSTALL_RPATH + "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path") + elseif(UNIX) + set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN") + endif() + + add_executable(simple_glop_program simple_glop_program.cc) + target_include_directories(simple_glop_program PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + target_compile_features(simple_glop_program PRIVATE cxx_std_17) + target_link_libraries(simple_glop_program PRIVATE glop) + + install(TARGETS simple_glop_program) + + if(BUILD_TESTING) + add_test(NAME cxx_simple_glop_program COMMAND simple_glop_program) + endif() +endif() + +if(BUILD_CXX_SAMPLES) + file(GLOB CXX_SRCS "*.cc") + foreach(SAMPLE IN LISTS CXX_SRCS) + add_cxx_sample(${SAMPLE}) + endforeach() +endif() diff --git a/ortools/glop/samples/code_samples.bzl b/ortools/glop/samples/code_samples.bzl new file mode 100644 index 0000000000..bbd80e5865 --- /dev/null +++ b/ortools/glop/samples/code_samples.bzl @@ -0,0 +1,23 @@ +def code_sample_cc(sample): + native.cc_binary( + name = sample, + srcs = [sample + ".cc"], + deps = [ + "//ortools/base", + "//ortools/glop:lp_solver", + "//ortools/lp_data", + ], + ) + + native.cc_test( + name = sample+"_test", + size = "small", + srcs = [sample + ".cc"], + deps = [ + ":"+sample, + "//ortools/base", + "//ortools/glop:lp_solver", + "//ortools/lp_data", + ], + ) + diff --git a/ortools/glop/samples/simple_glop_program.cc b/ortools/glop/samples/simple_glop_program.cc new file mode 100644 index 0000000000..b1ee0224dd --- /dev/null +++ b/ortools/glop/samples/simple_glop_program.cc @@ -0,0 +1,54 @@ +#include +#include "ortools/glop/lp_solver.h" +#include "ortools/lp_data/lp_data.h" +#include "ortools/lp_data/lp_types.h" + +namespace operations_research::glop { + int RunLinearExample() { + LinearProgram linear_program; + // Create the variables x and y. + ColIndex col_x = linear_program.FindOrCreateVariable("x"); + linear_program.SetVariableBounds(col_x, 0.0, 1.0); + ColIndex col_y = linear_program.FindOrCreateVariable("y"); + linear_program.SetVariableBounds(col_y, 0.0, 2.0); + + // Create linear constraint: 0 <= x + y <= 2. + RowIndex row_r1 = linear_program.FindOrCreateConstraint("r1"); + linear_program.SetConstraintBounds(row_r1, 0.0, 2.0); + linear_program.SetCoefficient(row_r1, col_x, 1); + linear_program.SetCoefficient(row_r1, col_y, 1); + + // Create objective function: 3 * x + y. + linear_program.SetObjectiveCoefficient(col_x, 3); + linear_program.SetObjectiveCoefficient(col_y, 1); + linear_program.SetMaximizationProblem(true); + + linear_program.CleanUp(); + + std::cout << "Number of variables = " << linear_program.num_variables() << std::endl; + std::cout << "Number of constraints = " << linear_program.num_constraints() << std::endl; + + LPSolver solver; + GlopParameters parameters; + parameters.set_provide_strong_optimal_guarantee(true); + solver.SetParameters(parameters); + + ProblemStatus status = solver.Solve(linear_program); + if (status == ProblemStatus::OPTIMAL) { + std::cout << "Optimal solution found !" << std::endl; + // The objective value of the solution. + std::cout << "Optimal objective value = " << solver.GetObjectiveValue() << std::endl; + // The value of each variable in the solution. + const DenseRow& values = solver.variable_values(); + std::cout << "Solution:" << std::endl + << "x = " << values[col_x] << std::endl + << ", y = " << values[col_y] << std::endl; + return 0; + } else + return 1; + } +} // namespace operations_research::glop + +int main(int argc, char** argv) { + return operations_research::glop::RunLinearExample(); +}