From e707ed15b428fc62b2966fd869d0675041e9ec64 Mon Sep 17 00:00:00 2001 From: Vincent Payet Date: Mon, 29 Jul 2024 16:10:00 +0200 Subject: [PATCH] mip objective file, getter for mipvariable --- .../antares/solver/optim/api/mipObjective.h | 6 +- .../antares/solver/optim/api/mipSolution.h | 1 - src/solver/optim/ortoolsImpl/CMakeLists.txt | 2 + .../solver/optim/ortoolsImpl/mipObjective.h | 49 ++++++++++++++ .../solver/optim/ortoolsImpl/mipSolution.h | 9 ++- .../solver/optim/ortoolsImpl/mipVariable.h | 8 ++- src/solver/optim/ortoolsImpl/mipObjective.cpp | 64 +++++++++++++++++++ src/solver/optim/ortoolsImpl/mipSolution.cpp | 13 ++-- src/solver/optim/ortoolsImpl/mipVariable.cpp | 17 +++-- 9 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipObjective.h create mode 100644 src/solver/optim/ortoolsImpl/mipObjective.cpp diff --git a/src/solver/optim/api/include/antares/solver/optim/api/mipObjective.h b/src/solver/optim/api/include/antares/solver/optim/api/mipObjective.h index 5978c3babc..0c55be202f 100644 --- a/src/solver/optim/api/include/antares/solver/optim/api/mipObjective.h +++ b/src/solver/optim/api/include/antares/solver/optim/api/mipObjective.h @@ -28,17 +28,15 @@ namespace Antares::Solver::Optim::Api class MipObjective { - virtual void setCoefficient(MipVariable& var, double coefficient) = 0; + virtual void setCoefficient(MipVariable* var, double coefficient) = 0; virtual void setMaximization() = 0; virtual void setMinimization() = 0; - virtual double getCoefficient(MipVariable& var) = 0; + virtual double getCoefficient(MipVariable* var) = 0; virtual bool getMaximization() = 0; virtual bool getMinimization() = 0; - - virtual double getValue() = 0; }; } // namespace Antares::Solver::Optim::Api diff --git a/src/solver/optim/api/include/antares/solver/optim/api/mipSolution.h b/src/solver/optim/api/include/antares/solver/optim/api/mipSolution.h index 5fe0e43e72..e15225cbb9 100644 --- a/src/solver/optim/api/include/antares/solver/optim/api/mipSolution.h +++ b/src/solver/optim/api/include/antares/solver/optim/api/mipSolution.h @@ -47,7 +47,6 @@ class MipSolution protected: MipStatus responseStatus_; - std::unique_ptr objective_; std::map solution_; }; diff --git a/src/solver/optim/ortoolsImpl/CMakeLists.txt b/src/solver/optim/ortoolsImpl/CMakeLists.txt index 9209a2b561..542f73b9cb 100644 --- a/src/solver/optim/ortoolsImpl/CMakeLists.txt +++ b/src/solver/optim/ortoolsImpl/CMakeLists.txt @@ -3,11 +3,13 @@ set(PROJ optim-ortools-impl) set(HEADERS include/antares/solver/optim/ortoolsImpl/mipVariable.h include/antares/solver/optim/ortoolsImpl/mipSolution.h + include/antares/solver/optim/ortoolsImpl/mipObjective.h ) set(SRC_ORTOOLS_IMPL ${HEADERS} mipVariable.cpp mipSolution.cpp + mipObjective.cpp ) source_group("solver\\optim\\api" FILES ${SRC_ORTOOLS_IMPL}) diff --git a/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipObjective.h b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipObjective.h new file mode 100644 index 0000000000..010e57734f --- /dev/null +++ b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipObjective.h @@ -0,0 +1,49 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include + +#include +#include + +namespace Antares::Solver::Optim::OrtoolsImpl +{ + +class OrtoolsMipObjective final: public virtual Api::MipObjective +{ +public: + OrtoolsMipObjective(operations_research::MPObjective* objective); + ~OrtoolsMipObjective() = default; + + void setCoefficient(Api::MipVariable* var, double coefficient) override; + + void setMaximization() override; + void setMinimization() override; + + double getCoefficient(Api::MipVariable* var) override; + + bool getMaximization() override; + bool getMinimization() override; + + operations_research::MPObjective* objective_; +}; + +} // namespace Antares::Solver::Optim::OrtoolsImpl diff --git a/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipSolution.h b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipSolution.h index 047246b9b4..4d8cb62319 100644 --- a/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipSolution.h +++ b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipSolution.h @@ -29,11 +29,12 @@ namespace Antares::Solver::Optim::OrtoolsImpl { -class OrtoolsMipSolution final: public virtual Api::MipSolution +class OrtoolsMipSolution : virtual public Api::MipSolution { public: - explicit OrtoolsMipSolution(operations_research::MPSolver::ResultStatus responseStatus, - const std::map& solution); + OrtoolsMipSolution(operations_research::MPSolver::ResultStatus responseStatus, + const std::map& solution, + operations_research::MPObjective* objective); ~OrtoolsMipSolution() = default; @@ -41,6 +42,8 @@ class OrtoolsMipSolution final: public virtual Api::MipSolution double getObjectiveValue() override; double getOptimalValue(const Api::MipVariable& var) const override; std::vector getOptimalValue(const std::vector& vars) const override; + + operations_research::MPObjective* objective_; }; } // namespace Antares::Solver::Optim::OrtoolsImpl diff --git a/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipVariable.h b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipVariable.h index 9730b1d712..db51b1f9a7 100644 --- a/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipVariable.h +++ b/src/solver/optim/ortoolsImpl/include/antares/solver/optim/ortoolsImpl/mipVariable.h @@ -37,12 +37,14 @@ class OrtoolsMipVariable: virtual Api::MipVariable double getLb() override; double getUb() override; + operations_research::MPVariable* get(); + + ~OrtoolsMipVariable() = default; private: // TODO: add friend class - explicit OrtoolsMipVariable(operations_research::MPVariable&); - ~OrtoolsMipVariable() = default; + explicit OrtoolsMipVariable(operations_research::MPVariable*); - operations_research::MPVariable& mpVar_; + operations_research::MPVariable* mpVar_; }; } // namespace Antares::Solver::Optim::OrtoolsImpl diff --git a/src/solver/optim/ortoolsImpl/mipObjective.cpp b/src/solver/optim/ortoolsImpl/mipObjective.cpp new file mode 100644 index 0000000000..d14fd5afbd --- /dev/null +++ b/src/solver/optim/ortoolsImpl/mipObjective.cpp @@ -0,0 +1,64 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include +#include + +namespace Antares::Solver::Optim::OrtoolsImpl +{ + +OrtoolsMipObjective::OrtoolsMipObjective(operations_research::MPObjective* objective): + objective_(objective) +{} + +void OrtoolsMipObjective::setCoefficient(Api::MipVariable* var, double coefficient) +{ + auto* mpvar = dynamic_cast(var); + objective_->SetCoefficient(mpvar->get(), coefficient); +} + +void OrtoolsMipObjective::setMaximization() +{ + objective_->SetMaximization(); +} + +void OrtoolsMipObjective::setMinimization() +{ + objective_->SetMinimization(); +} + +double OrtoolsMipObjective::getCoefficient(Api::MipVariable* var) +{ + auto* mpvar = dynamic_cast(var); + return objective_->GetCoefficient(mpvar->get()); +} + +bool OrtoolsMipObjective::getMaximization() +{ + return objective_->maximization(); +} + +bool OrtoolsMipObjective::getMinimization() +{ + return objective_->minimization(); +} + +} // namespace Antares::Solver::Optim::OrtoolsImpl diff --git a/src/solver/optim/ortoolsImpl/mipSolution.cpp b/src/solver/optim/ortoolsImpl/mipSolution.cpp index 858d5b0af5..725fd9f25a 100644 --- a/src/solver/optim/ortoolsImpl/mipSolution.cpp +++ b/src/solver/optim/ortoolsImpl/mipSolution.cpp @@ -20,6 +20,7 @@ */ #include +#include namespace Antares::Solver::Optim::OrtoolsImpl { @@ -27,7 +28,9 @@ namespace Antares::Solver::Optim::OrtoolsImpl using OrMPSolver = operations_research::MPSolver; OrtoolsMipSolution::OrtoolsMipSolution(OrMPSolver::ResultStatus responseStatus, - const std::map& solution) + const std::map& solution, + operations_research::MPObjective* objective): + objective_(objective) { // Only store non-zero values for (const auto& varAndValue : solution) @@ -60,10 +63,10 @@ Api::MipStatus OrtoolsMipSolution::getStatus() return responseStatus_; } -/* double OrtoolsMipSolution::getObjectiveValue() */ -/* { */ - -/* } */ +double OrtoolsMipSolution::getObjectiveValue() +{ + return objective_->Value(); +} double OrtoolsMipSolution::getOptimalValue(const Api::MipVariable& var) const { diff --git a/src/solver/optim/ortoolsImpl/mipVariable.cpp b/src/solver/optim/ortoolsImpl/mipVariable.cpp index c9fa4912ce..aaf0cbcb7c 100644 --- a/src/solver/optim/ortoolsImpl/mipVariable.cpp +++ b/src/solver/optim/ortoolsImpl/mipVariable.cpp @@ -24,34 +24,39 @@ namespace Antares::Solver::Optim::OrtoolsImpl { -OrtoolsMipVariable::OrtoolsMipVariable(operations_research::MPVariable& mpVar): +OrtoolsMipVariable::OrtoolsMipVariable(operations_research::MPVariable* mpVar): mpVar_(mpVar) { } void OrtoolsMipVariable::setLb(double lb) { - mpVar_.SetLB(lb); + mpVar_->SetLB(lb); } void OrtoolsMipVariable::setUb(double ub) { - mpVar_.SetUB(ub); + mpVar_->SetUB(ub); } void OrtoolsMipVariable::setBounds(double lb, double ub) { - mpVar_.SetBounds(lb, ub); + mpVar_->SetBounds(lb, ub); } double OrtoolsMipVariable::getLb() { - return mpVar_.lb(); + return mpVar_->lb(); } double OrtoolsMipVariable::getUb() { - return mpVar_.ub(); + return mpVar_->ub(); +} + +operations_research::MPVariable* OrtoolsMipVariable::get() +{ + return mpVar_; } } // namespace Antares::Solver::Optim::OrtoolsImpl