From d70370d2acc59965b6462c28d29f7d9c239b196e Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Mon, 16 Dec 2024 17:58:54 +0100 Subject: [PATCH 1/4] Remove getMemberFunction, use factory pattern --- .../ShortTermStorageCumulation.cpp | 94 +++++++++++++------ .../constraints/ShortTermStorageCumulation.h | 3 - 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp index 7df86685c1..5f1919d728 100644 --- a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp +++ b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp @@ -19,48 +19,84 @@ * 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) +class CumulationConstraint { - builder.ShortTermStorageInjection(index, 1.0); -} +public: + virtual void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES& input) const + = 0; + virtual std::string name() const = 0; +}; -void ShortTermStorageCumulation::Withdrawal(unsigned int index, - const ::ShortTermStorage::PROPERTIES& input) +class WithdrawalCumulationConstraint: public CumulationConstraint { - builder.ShortTermStorageWithdrawal(index, 1.0); -} +public: + void build(ConstraintBuilder& builder, + unsigned int index, + const ::ShortTermStorage::PROPERTIES&) const override + { + builder.ShortTermStorageWithdrawal(index, 1.0); + } -void ShortTermStorageCumulation::Netting(unsigned int index, - const ::ShortTermStorage::PROPERTIES& input) -{ - builder.ShortTermStorageInjection(index, input.injectionEfficiency) - .ShortTermStorageWithdrawal(index, -input.withdrawalEfficiency); -} + std::string name() const override + { + return "WithdrawalSum"; + } +}; -auto getMemberFunction = [](const std::string& name) - -> std::pair +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") +}; + +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 }; +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 false; // TODO custom exception +} + char ConvertSign(const std::string& sign) { if (sign == "greater") @@ -89,8 +125,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 +139,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; From 0f0ad0d9c4d6c204e97df6d2a127332bf0d3badf Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Mon, 16 Dec 2024 18:04:58 +0100 Subject: [PATCH 2/4] Exception --- .../optimisation/constraints/ShortTermStorageCumulation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp index 5f1919d728..b22f27ad1d 100644 --- a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp +++ b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp @@ -94,7 +94,7 @@ std::unique_ptr cumulationConstraintFromVariable(const std { return std::make_unique(); } - throw false; // TODO custom exception + throw std::invalid_argument("Invalid cumulation constraint type"); } char ConvertSign(const std::string& sign) From 1c66b598f1e0aebf9785e862e50d8a99dc5248cc Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Mon, 16 Dec 2024 18:05:42 +0100 Subject: [PATCH 3/4] Headers --- .../optimisation/constraints/ShortTermStorageCumulation.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp index b22f27ad1d..981cba91d0 100644 --- a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp +++ b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp @@ -21,6 +21,9 @@ #include "antares/solver/optimisation/constraints/ShortTermStorageCumulation.h" +#include +#include + class CumulationConstraint { public: From d24ce826d3d021ce0a11f02e7a731de0f104dd76 Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Tue, 17 Dec 2024 10:32:03 +0100 Subject: [PATCH 4/4] Virtual dtors --- .../constraints/ShortTermStorageCumulation.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp index 981cba91d0..f428b113ee 100644 --- a/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp +++ b/src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp @@ -32,6 +32,7 @@ class CumulationConstraint const ::ShortTermStorage::PROPERTIES& input) const = 0; virtual std::string name() const = 0; + virtual ~CumulationConstraint() = default; }; class WithdrawalCumulationConstraint: public CumulationConstraint @@ -48,6 +49,8 @@ class WithdrawalCumulationConstraint: public CumulationConstraint { return "WithdrawalSum"; } + + virtual ~WithdrawalCumulationConstraint() = default; }; class InjectionCumulationConstraint: public CumulationConstraint @@ -64,6 +67,8 @@ class InjectionCumulationConstraint: public CumulationConstraint { return "InjectionSum"; } + + virtual ~InjectionCumulationConstraint() = default; }; class NettingCumulationConstraint: public CumulationConstraint @@ -81,6 +86,8 @@ class NettingCumulationConstraint: public CumulationConstraint { return "NettingSum"; } + + virtual ~NettingCumulationConstraint() = default; }; std::unique_ptr cumulationConstraintFromVariable(const std::string& variable)