Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mathopt xpress #136

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ortools/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

file(GLOB _SRCS "*.h" "*.cc")
list(FILTER _SRCS EXCLUDE REGEX "/[^/]*_test\\.cc$")
list(FILTER _SRCS EXCLUDE REGEX "/knapsack_solver.*$")
list(FILTER _SRCS EXCLUDE REGEX "/set_cover_mip.cc.*$")

set(NAME ${PROJECT_NAME}_algorithms)

Expand Down
2 changes: 1 addition & 1 deletion ortools/base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ void FixFlagsAndEnvironmentForSwig() {
absl::EnableLogPrefix(false);
}

void KeepAbslSymbols() { absl::SetFlag(&FLAGS_stderrthreshold, 0); }
void KeepAbslSymbols() {}

} // namespace operations_research
4 changes: 2 additions & 2 deletions ortools/math_opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()
add_subdirectory(core)
add_subdirectory(constraints)
add_subdirectory(cpp)
add_subdirectory(io)
#add_subdirectory(io)
add_subdirectory(labs)
add_subdirectory(solvers)
add_subdirectory(storage)
Expand All @@ -29,7 +29,7 @@ add_library(${NAME} OBJECT)
target_sources(${NAME} PUBLIC
$<TARGET_OBJECTS:${NAME}_core>
$<TARGET_OBJECTS:${NAME}_cpp>
$<TARGET_OBJECTS:${NAME}_io>
# $<TARGET_OBJECTS:${NAME}_io>
$<TARGET_OBJECTS:${NAME}_labs>
$<TARGET_OBJECTS:${NAME}_solvers>
$<TARGET_OBJECTS:${NAME}_storage>
Expand Down
33 changes: 32 additions & 1 deletion ortools/math_opt/solvers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ add_library(${NAME} OBJECT)

file(GLOB_RECURSE _SRCS "*.h" "*.cc")
list(FILTER _SRCS EXCLUDE REGEX ".*/.*_test.cc")
list(FILTER _SRCS EXCLUDE REGEX "cp_sat_solver.cc")
list(FILTER _SRCS EXCLUDE REGEX "cp_sat_solver.h")


if(NOT USE_GLPK)
list(FILTER _SRCS EXCLUDE REGEX "/glpk/")
Expand Down Expand Up @@ -45,10 +48,38 @@ set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(${NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>)
target_link_libraries(${NAME} PRIVATE
target_link_libraries(${NAME} PUBLIC
absl::strings
$<$<BOOL:${USE_GLPK}>:GLPK::GLPK>
$<$<BOOL:${USE_HIGHS}>:highs::highs>
$<$<BOOL:${USE_PDLP}>:Eigen3::Eigen>
$<$<BOOL:${USE_SCIP}>:libscip>
${PROJECT_NAMESPACE}::math_opt_proto)

add_executable(simple_xpress_test "simple_xpress_test.cc")
target_link_libraries(simple_xpress_test PRIVATE
absl::base
absl::strings
absl::flags

${PROJECT_NAME}_base
${PROJECT_NAME}_port
${PROJECT_NAME}_math_opt
${PROJECT_NAME}_math_opt_cpp
${PROJECT_NAME}_math_opt_storage
${PROJECT_NAME}_util
${PROJECT_NAME}_glop
${PROJECT_NAME}_pdlp
${PROJECT_NAME}_lp_data
${PROJECT_NAME}_algorithms # MergingPartition

${NAME}

${PROJECT_NAMESPACE}::math_opt_proto
${PROJECT_NAMESPACE}::ortools_proto
protobuf::libprotobuf
GTest::gtest_main
GTest::gmock_main
${PROJECT_NAME}_gscip)

add_test(NAME cxx_unittests_simple_xpress_test COMMAND simple_xpress_test)
42 changes: 42 additions & 0 deletions ortools/math_opt/solvers/simple_xpress_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <ostream>

#include "absl/log/check.h"
#include "absl/status/statusor.h"
#include "gtest/gtest.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/math_opt/cpp/math_opt.h"

using namespace operations_research::math_opt;

TEST(GScipSolver, XpressTest) {
// Build the model.
namespace math_opt = ::operations_research::math_opt;
math_opt::Model lp_model("getting_started_lp");
const math_opt::Variable x = lp_model.AddContinuousVariable(-1.0, 1.5, "x");
const math_opt::Variable y = lp_model.AddContinuousVariable(0.0, 1.0, "y");
lp_model.AddLinearConstraint(x + y <= 1.5, "c");
lp_model.Maximize(x + 2 * y);

// Set parameters, e.g. turn on logging.
math_opt::SolveArguments args;
args.parameters.enable_output = true;

// Solve and ensure an optimal solution was found with no errors.
const absl::StatusOr<math_opt::SolveResult> result =
math_opt::Solve(lp_model, math_opt::SolverType::kGscip, args);
CHECK_OK(result.status());
CHECK_OK(result->termination.EnsureIsOptimal());

// Print some information from the result.
std::cout << "MathOpt solve succeeded" << std::endl;
std::cout << "Objective value: " << result->objective_value() << std::endl;
std::cout << "x: " << result->variable_values().at(x) << std::endl;
std::cout << "y: " << result->variable_values().at(y) << std::endl;
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
}
6 changes: 0 additions & 6 deletions ortools/math_opt/storage/model_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,6 @@ ModelProto ModelStorage::ExportModel(const bool remove_names) const {
// Performance can be improved when remove_names is true by just not
// extracting the names above instead of clearing them below, but this will
// be more code, see discussion on cl/549469633 and prototype in cl/549369764.
if (remove_names) {
RemoveNames(result);
}
return result;
}

Expand Down Expand Up @@ -381,9 +378,6 @@ ModelStorage::UpdateTrackerData::ExportModelUpdate(
*result.mutable_objective_updates() = std::move(primary);
*result.mutable_auxiliary_objectives_updates() = std::move(auxiliary);
}
if (remove_names) {
RemoveNames(result);
}
// Note: Named returned value optimization (NRVO) does not apply here.
return {std::move(result)};
}
Expand Down
7 changes: 0 additions & 7 deletions ortools/math_opt/tools/mathopt_solve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,6 @@ absl::StatusOr<ModelAndHint> ParseModelAndHint() {
model_updates.emplace_back(std::move(update));
}

if (!absl::GetFlag(FLAGS_names)) {
RemoveNames(model_proto);
for (ModelUpdateProto& update : model_updates) {
RemoveNames(update);
}
}

// Parse the problem and the updates.
ASSIGN_OR_RETURN(std::unique_ptr<Model> model,
Model::FromModelProto(model_proto));
Expand Down
3 changes: 1 addition & 2 deletions ortools/pdlp/quadratic_program_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ absl::Status WriteLinearProgramToMps(const QuadraticProgram& linear_program,
"'linear_program' has a quadratic objective");
}
ASSIGN_OR_RETURN(MPModelProto proto, QpToMpModelProto(linear_program));
ASSIGN_OR_RETURN(std::string mps_export, ExportModelAsMpsFormat(proto));
File* file;
RETURN_IF_ERROR(file::Open(mps_file, "w", &file, file::Defaults()));
auto status = file::WriteString(file, mps_export, file::Defaults());
absl::Status status;
status.Update(file->Close(file::Defaults()));
return status;
}
Expand Down