diff --git a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp index 7df86685c1..f428b113ee 100644 --- a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp +++ b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp @@ -19,48 +19,94 @@ * along with Antares_Simulator. If not, see . */ -#pragma once #include "antares/solver/optimisation/constraints/ShortTermStorageCumulation.h" -void ShortTermStorageCumulation::Injection(unsigned int index, - const ::ShortTermStorage::PROPERTIES& input) -{ - builder.ShortTermStorageInjection(index, 1.0); -} +#include +#include -void ShortTermStorageCumulation::Withdrawal(unsigned int index, - const ::ShortTermStorage::PROPERTIES& input) +class CumulationConstraint { - builder.ShortTermStorageWithdrawal(index, 1.0); -} +public: + virtual void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES& input) const + = 0; + virtual std::string name() const = 0; + virtual ~CumulationConstraint() = default; +}; -void ShortTermStorageCumulation::Netting(unsigned int index, - const ::ShortTermStorage::PROPERTIES& input) +class WithdrawalCumulationConstraint: public CumulationConstraint { - builder.ShortTermStorageInjection(index, input.injectionEfficiency) - .ShortTermStorageWithdrawal(index, -input.withdrawalEfficiency); -} +public: + void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES&) const override + { + builder.ShortTermStorageWithdrawal(index, 1.0); + } -auto getMemberFunction = [](const std::string& name) - -> std::pair + std::string name() const override + { + return "WithdrawalSum"; + } + + virtual ~WithdrawalCumulationConstraint() = default; +}; + +class InjectionCumulationConstraint: public CumulationConstraint { - if (name == "withdrawal") +public: + void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES&) const override + { + builder.ShortTermStorageInjection(index, 1.0); + } + + std::string name() const override { - return {"WithdrawalSum", &ShortTermStorageCumulation::Withdrawal}; + return "InjectionSum"; } - else if (name == "injection") + + virtual ~InjectionCumulationConstraint() = default; +}; + +class NettingCumulationConstraint: public CumulationConstraint +{ +public: + void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES& input) const override { - return {"InjectionSum", &ShortTermStorageCumulation::Injection}; + builder.ShortTermStorageInjection(index, input.injectionEfficiency) + .ShortTermStorageWithdrawal(index, -input.withdrawalEfficiency); } - else if (name == "netting") + + std::string name() const override { - return {"NettingSum", &ShortTermStorageCumulation::Netting}; + return "NettingSum"; } - return {"", nullptr}; // Return null if no match + + virtual ~NettingCumulationConstraint() = default; }; +std::unique_ptr cumulationConstraintFromVariable(const std::string& variable) +{ + if (variable == "withdrawal") + { + return std::make_unique(); + } + else if (variable == "injection") + { + return std::make_unique(); + } + else if (variable == "netting") + { + return std::make_unique(); + } + throw std::invalid_argument("Invalid cumulation constraint type"); +} + char ConvertSign(const std::string& sign) { if (sign == "greater") @@ -89,8 +135,8 @@ void ShortTermStorageCumulation::add(int pays) for (const auto& constraint: storage.additional_constraints) { // sum (var[h]) sign rhs, h in list provied by user - auto [constraintType, memberFunction] = getMemberFunction(constraint.variable); - namer.ShortTermStorageCumulation(constraintType, + auto constraintHelper = cumulationConstraintFromVariable(constraint.variable); + namer.ShortTermStorageCumulation(constraintHelper->name(), builder.data.nombreDeContraintes, storage.name, constraint.name); @@ -103,7 +149,7 @@ void ShortTermStorageCumulation::add(int pays) for (const auto& hour: constraint.hours) { builder.updateHourWithinWeek(hour - 1); - (this->*memberFunction)(index, storage); + constraintHelper->build(builder, index, storage); } builder.SetOperator(ConvertSign(constraint.operatorType)).build(); } diff --git a/src/solver/optimisation/include/antares/solver/optimisation/constraints/ShortTermStorageCumulation.h b/src/solver/optimisation/include/antares/solver/optimisation/constraints/ShortTermStorageCumulation.h index 572ad77821..5aa710b956 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/constraints/ShortTermStorageCumulation.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/constraints/ShortTermStorageCumulation.h @@ -33,9 +33,6 @@ class ShortTermStorageCumulation: private ConstraintFactory } void add(int pays); - void Injection(unsigned int index, const ::ShortTermStorage::PROPERTIES& input); - void Withdrawal(unsigned int index, const ::ShortTermStorage::PROPERTIES& input); - void Netting(unsigned int index, const ::ShortTermStorage::PROPERTIES& input); private: ShortTermStorageCumulativeConstraintData& data;