Skip to content

Commit

Permalink
Legacy ortools behind API - user story 3.1 & 3.2 (#2455)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpier-code authored Oct 23, 2024
1 parent 0bb9b9a commit b94e65e
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 354 deletions.
43 changes: 22 additions & 21 deletions src/packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
include(GNUInstallDirs)
# generate and install export file
set(TARGET_LIBS #No alias
#each "block" of dependency describe the dependency for a target
#not dependency is present since once a dependency is in the export set
#it is available for everything
# each "block" of dependency describe the dependency for a target
# not dependency is present since once a dependency is in the export set
# it is available for everything

solver_api #What we want to export
solver_api # What we want to export

#solver_api
# solver_api
study
study-loader
file-tree-study-loader
antares-solver-simulation
#study
# study
yuni-static-core
array
date
Expand All @@ -37,56 +37,57 @@ set(TARGET_LIBS #No alias
antares-solver-variable
lps

#study-loader
#nothing
# study-loader : nothing

#file-tree-study-loader
# file-tree-study-loader
application

#run-mode
# run-mode
infoCollection

#antares-solver-simulation
# antares-solver-simulation
concurrency
misc
model_antares
antares-solver-ts-generator

#lps
#nothing
# lps : nothing

#array
# array
io
jit
AntaresMemory

#date
# date
logs

#correlation
# correlation
locator

#antares-core
# antares-core
antares-config-lib

#application
# application
solver-lib
sys
signal-handling
antares-solver-variable-info
optimization-options
resources

#model_antares
# model_antares
infeasible_problem_analysis
modeler_api
modeler-ortools-impl

#solver-lib
# solver-lib
args_helper
checks
locale
yuni-static-uuid

antares-solver #executable
# executable
antares-solver
)

install(TARGETS ${TARGET_LIBS}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class ILinearProblem
virtual IMipVariable* addNumVariable(double lb, double ub, const std::string& name) = 0;
/// Create a integer variable
virtual IMipVariable* addIntVariable(double lb, double ub, const std::string& name) = 0;
/// Create a continuous or integer variable
virtual IMipVariable* addVariable(double lb, double ub, bool integer, const std::string& name)
= 0;
virtual IMipVariable* getVariable(const std::string& name) const = 0;
virtual int variableCount() const = 0;

Expand All @@ -68,6 +71,9 @@ class ILinearProblem

/// Solve the problem, returns a IMipSolution
virtual IMipSolution* solve(bool verboseSolver) = 0;

// Definition of infinity
virtual double infinity() const = 0;
};

} // namespace Antares::Solver::Modeler::Api
1 change: 0 additions & 1 deletion src/solver/modeler/api/linearProblemBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

#include <algorithm>
#include <memory>

#include <antares/solver/modeler/api/linearProblemBuilder.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ class MPObjective;
namespace Antares::Solver::Modeler::OrtoolsImpl
{

class OrtoolsLinearProblem final: public Api::ILinearProblem
class OrtoolsLinearProblem: public Api::ILinearProblem
{
public:
OrtoolsLinearProblem(bool isMip, const std::string& solverName);
~OrtoolsLinearProblem() override = default;

OrtoolsMipVariable* addNumVariable(double lb, double ub, const std::string& name) override;
OrtoolsMipVariable* addIntVariable(double lb, double ub, const std::string& name) override;
OrtoolsMipVariable* addVariable(double lb,
double ub,
bool integer,
const std::string& name) override;
OrtoolsMipVariable* getVariable(const std::string& name) const override;
int variableCount() const override;

Expand All @@ -62,10 +66,13 @@ class OrtoolsLinearProblem final: public Api::ILinearProblem

OrtoolsMipSolution* solve(bool verboseSolver) override;

private:
OrtoolsMipVariable* addVariable(double lb, double ub, bool integer, const std::string& name);
double infinity() const override;

protected:
operations_research::MPSolver* MpSolver() const;

std::shared_ptr<operations_research::MPSolver> mpSolver_;
private:
operations_research::MPSolver* mpSolver_;
operations_research::MPObjective* objective_;
operations_research::MPSolverParameters params_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OrtoolsMipSolution final: public Api::IMipSolution
{
public:
OrtoolsMipSolution(operations_research::MPSolver::ResultStatus& responseStatus,
std::shared_ptr<operations_research::MPSolver> solver);
operations_research::MPSolver* solver);

~OrtoolsMipSolution() override = default;

Expand All @@ -47,7 +47,7 @@ class OrtoolsMipSolution final: public Api::IMipSolution

private:
operations_research::MPSolver::ResultStatus status_;
std::shared_ptr<operations_research::MPSolver> mpSolver_;
operations_research::MPSolver* mpSolver_;
std::map<std::string, double> solution_;
};

Expand Down
22 changes: 12 additions & 10 deletions src/solver/modeler/ortoolsImpl/linearProblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,8 @@ namespace Antares::Solver::Modeler::OrtoolsImpl

OrtoolsLinearProblem::OrtoolsLinearProblem(bool isMip, const std::string& solverName)
{
auto* mpSolver = isMip ? MPSolver::CreateSolver(
(OrtoolsUtils::solverMap.at(solverName)).MIPSolverName)
: MPSolver::CreateSolver(
(OrtoolsUtils::solverMap.at(solverName)).LPSolverName);

mpSolver_ = std::unique_ptr<operations_research::MPSolver>(mpSolver);
objective_ = mpSolver->MutableObjective();

params_.SetIntegerParam(MPSolverParameters::SCALING, 0);
params_.SetIntegerParam(MPSolverParameters::PRESOLVE, 0);
mpSolver_ = MPSolverFactory(isMip, solverName);
objective_ = mpSolver_->MutableObjective();
}

class ElemAlreadyExists: public std::exception
Expand Down Expand Up @@ -172,6 +164,11 @@ bool OrtoolsLinearProblem::isMaximization() const
return objective_->maximization();
}

MPSolver* OrtoolsLinearProblem::MpSolver() const
{
return mpSolver_;
}

OrtoolsMipSolution* OrtoolsLinearProblem::solve(bool verboseSolver)
{
if (verboseSolver)
Expand All @@ -185,4 +182,9 @@ OrtoolsMipSolution* OrtoolsLinearProblem::solve(bool verboseSolver)
return solution_.get();
}

double OrtoolsLinearProblem::infinity() const
{
return MPSolver::infinity();
}

} // namespace Antares::Solver::Modeler::OrtoolsImpl
2 changes: 1 addition & 1 deletion src/solver/modeler/ortoolsImpl/mipSolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Antares::Solver::Modeler::OrtoolsImpl
{

OrtoolsMipSolution::OrtoolsMipSolution(operations_research::MPSolver::ResultStatus& status,
std::shared_ptr<operations_research::MPSolver> solver):
operations_research::MPSolver* solver):
status_(status),
mpSolver_(solver)
{
Expand Down
Loading

0 comments on commit b94e65e

Please sign in to comment.