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

Legacy ortools behind API - user story 3.1 #2455

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
34 changes: 17 additions & 17 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ jobs:
run: |
git submodule update --init --remote --recursive src/tests/resources/Antares_Simulator_Tests_NR

- name: Run named mps tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
batch-name: valid-named-mps
os: ${{ env.os }}
variant: "named-mps"
#- name: Run named mps tests
# if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
# uses: ./.github/workflows/run-tests
# with:
# simtest-tag: ${{ env.SIMTEST }}
# batch-name: valid-named-mps
# os: ${{ env.os }}
# variant: "named-mps"

- name: Run unfeasibility-related tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
Expand Down Expand Up @@ -240,13 +240,13 @@ jobs:
with:
feature: "features/short_tests.feature"

- name: Run mps tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
batch-name: valid-mps
os: ${{ env.os }}
#- name: Run mps tests
# if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
# uses: ./.github/workflows/run-tests
# with:
# simtest-tag: ${{ env.SIMTEST }}
# batch-name: valid-mps
# os: ${{ env.os }}

- name: Run tests for adequacy patch (CSR)
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
Expand All @@ -257,7 +257,7 @@ jobs:
os: ${{ env.os }}

- name: Run parallel tests
if: ${{ env.RUN_EXTENDED_TESTS == 'true' && !cancelled() }}
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
Expand All @@ -275,7 +275,7 @@ jobs:
variant: "tsgenerator"

- name: Run medium-tests
if: ${{ env.RUN_EXTENDED_TESTS == 'true' && !cancelled() }}
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
Expand Down
34 changes: 17 additions & 17 deletions .github/workflows/windows-vcpkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ jobs:
run: |
git submodule update --init --remote src/tests/resources/Antares_Simulator_Tests_NR

- name: Run named mps tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && ! cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
batch-name: valid-named-mps
os: ${{ env.os }}
variant: "named-mps"
#- name: Run named mps tests
# if: ${{ env.RUN_SIMPLE_TESTS == 'true' && ! cancelled() }}
# uses: ./.github/workflows/run-tests
# with:
# simtest-tag: ${{ env.SIMTEST }}
# batch-name: valid-named-mps
# os: ${{ env.os }}
# variant: "named-mps"

- name: Run unfeasibility-related tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && ! cancelled() }}
Expand Down Expand Up @@ -250,16 +250,16 @@ jobs:
with:
feature: "features/short_tests.feature"

- name: Run mps tests
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
batch-name: valid-mps
os: ${{ env.os }}
#- name: Run mps tests
# if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
# uses: ./.github/workflows/run-tests
# with:
# simtest-tag: ${{ env.SIMTEST }}
# batch-name: valid-mps
# os: ${{ env.os }}

- name: Run parallel tests
if: ${{ env.RUN_EXTENDED_TESTS == 'true' && !cancelled() }}
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
Expand All @@ -277,7 +277,7 @@ jobs:
variant: "tsgenerator"

- name: Run medium-tests
if: ${{ env.RUN_EXTENDED_TESTS == 'true' && !cancelled() }}
if: ${{ env.RUN_SIMPLE_TESTS == 'true' && !cancelled() }}
uses: ./.github/workflows/run-tests
with:
simtest-tag: ${{ env.SIMTEST }}
Expand Down
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
2 changes: 2 additions & 0 deletions src/solver/modeler/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set(SRC_API
include/antares/solver/modeler/api/linearProblemFiller.h
include/antares/solver/modeler/api/linearProblemBuilder.h

include/antares/solver/modeler/api/fillers/LegacyOrtoolsFiller.h

linearProblemData.cpp
linearProblemBuilder.cpp
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#pragma once

#include "antares/solver/modeler/api/linearProblemFiller.h"
#include "ortools/linear_solver/linear_solver.h"
#include "antares/solver/utils/named_problem.h"
#include "antares/solver/utils/ortools_utils.h"

using namespace Antares::Optimization;

namespace Antares::Solver::Modeler::Api
{
class LegacyOrtoolsFiller: public LinearProblemFiller
{
public:
explicit LegacyOrtoolsFiller(operations_research::MPSolver* mpSolver,
const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe);
void addVariables(ILinearProblem& pb, LinearProblemData& data) override;
void addConstraints(ILinearProblem& pb, LinearProblemData& data) override;
void addObjective(ILinearProblem& pb, LinearProblemData& data) override;

private:
operations_research::MPSolver* mpSolver_ = nullptr;
const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe_;
Nomenclature variableNameManager_ = Nomenclature('x');
Nomenclature constraintNameManager_ = Nomenclature('c');

void CreateVariable(unsigned idxVar, MPSolver* solver, MPObjective* const objective) const;
void CopyVariables(MPSolver* solver) const;
void UpdateContraints(unsigned idxRow, MPSolver* solver) const;
void CopyRows(MPSolver* solver) const;
void CopyMatrix(const MPSolver* solver) const;
};

LegacyOrtoolsFiller::LegacyOrtoolsFiller(operations_research::MPSolver* mpSolver,
const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) :
mpSolver_(mpSolver),
problemeSimplexe_(problemeSimplexe)
{
if (problemeSimplexe_->UseNamedProblems())
{
variableNameManager_.SetTarget(problemeSimplexe_->VariableNames());
constraintNameManager_.SetTarget(problemeSimplexe_->ConstraintNames());
}
}

void LegacyOrtoolsFiller::addVariables(ILinearProblem& pb, LinearProblemData& data)
{
// Create the variables and set objective cost.
CopyVariables(mpSolver_);
}

void LegacyOrtoolsFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data)
{
// Create constraints and set coefs
CopyRows(mpSolver_);

CopyMatrix(mpSolver_);
}

void LegacyOrtoolsFiller::addObjective(ILinearProblem& pb, LinearProblemData& data)
{
}

void LegacyOrtoolsFiller::CopyMatrix(const MPSolver* solver) const
{
auto variables = solver->variables();
auto constraints = solver->constraints();

for (int idxRow = 0; idxRow < problemeSimplexe_->NombreDeContraintes; ++idxRow)
{
MPConstraint* const ct = constraints[idxRow];
int debutLigne = problemeSimplexe_->IndicesDebutDeLigne[idxRow];
for (int idxCoef = 0; idxCoef < problemeSimplexe_->NombreDeTermesDesLignes[idxRow];
++idxCoef)
{
int pos = debutLigne + idxCoef;
ct->SetCoefficient(variables[problemeSimplexe_->IndicesColonnes[pos]],
problemeSimplexe_->CoefficientsDeLaMatriceDesContraintes[pos]);
}
}
}

void LegacyOrtoolsFiller::CreateVariable(unsigned idxVar,
MPSolver* solver,
MPObjective* const objective) const
{
double min_l = problemeSimplexe_->Xmin[idxVar];
double max_l = problemeSimplexe_->Xmax[idxVar];
bool isIntegerVariable = problemeSimplexe_->IntegerVariable(idxVar);
const MPVariable* var = solver->MakeVar(min_l,
max_l,
isIntegerVariable,
variableNameManager_.GetName(idxVar));
objective->SetCoefficient(var, problemeSimplexe_->CoutLineaire[idxVar]);
}

void LegacyOrtoolsFiller::CopyVariables(MPSolver* solver) const

{
MPObjective* const objective = solver->MutableObjective();
for (int idxVar = 0; idxVar < problemeSimplexe_->NombreDeVariables; ++idxVar)
{
CreateVariable(idxVar, solver, objective);
}
}

void LegacyOrtoolsFiller::UpdateContraints(unsigned idxRow, MPSolver* solver) const
{
double bMin = -MPSolver::infinity(), bMax = MPSolver::infinity();
if (problemeSimplexe_->Sens[idxRow] == '=')
{
bMin = bMax = problemeSimplexe_->SecondMembre[idxRow];
}
else if (problemeSimplexe_->Sens[idxRow] == '<')
{
bMax = problemeSimplexe_->SecondMembre[idxRow];
}
else if (problemeSimplexe_->Sens[idxRow] == '>')
{
bMin = problemeSimplexe_->SecondMembre[idxRow];
}

solver->MakeRowConstraint(bMin, bMax, constraintNameManager_.GetName(idxRow));
}

void LegacyOrtoolsFiller::CopyRows(MPSolver* solver) const
{
for (int idxRow = 0; idxRow < problemeSimplexe_->NombreDeContraintes; ++idxRow)
{
UpdateContraints(idxRow, solver);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ class OrtoolsLinearProblem final: public Api::ILinearProblem
bool isMaximization() const override;

OrtoolsMipSolution* solve(bool verboseSolver) override;
operations_research::MPSolver* MPSolver();

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

std::shared_ptr<operations_research::MPSolver> mpSolver_;
operations_research::MPSolver* mpSolver_;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, mpSolver_ is no longer a shared pointer, at least for now.
The reason is that current Antares code (where the ortools solver are called) is made in such a way that the solver must be shared between optimization and weeks.
So a solver cannot be destroyed too soon, it has to be persistent.
It's planned to handle the life time in the future.

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
Loading
Loading