-
Notifications
You must be signed in to change notification settings - Fork 24
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
guilpier-code
wants to merge
6
commits into
develop
Choose a base branch
from
feature/legacy-ortools-behind-api-3.1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab11215
Legacy ortools behind API : in CI, run tests with ortools + sirius
guilpier-code 2f704ca
Legacy ortools behind API : mainly preparing the field for further ch…
guilpier-code 0a308a9
Legacy ortools behind API : put solver creation behind or-tools
guilpier-code 51d19d8
Legacy ortools behind API : fix crash in modeler unit tests
guilpier-code 8223c47
Legacy ortools behind API : fix a crash - smart pointer on ortools so…
guilpier-code f588590
Legacy ortools behind API : remove duplicate code
guilpier-code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/solver/modeler/api/include/antares/solver/modeler/api/fillers/LegacyOrtoolsFiller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.