Skip to content

Commit

Permalink
Remove getMemberFunction, use factory pattern (#2536)
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes authored Dec 17, 2024
1 parent 5fea2ae commit 25f0088
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 32 deletions.
104 changes: 75 additions & 29 deletions src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,94 @@
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#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 <memory>
#include <stdexcept>

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,
void (ShortTermStorageCumulation::*)(unsigned int,
const ::ShortTermStorage::PROPERTIES&)>
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<CumulationConstraint> cumulationConstraintFromVariable(const std::string& variable)
{
if (variable == "withdrawal")
{
return std::make_unique<WithdrawalCumulationConstraint>();
}
else if (variable == "injection")
{
return std::make_unique<InjectionCumulationConstraint>();
}
else if (variable == "netting")
{
return std::make_unique<NettingCumulationConstraint>();
}
throw std::invalid_argument("Invalid cumulation constraint type");
}

char ConvertSign(const std::string& sign)
{
if (sign == "greater")
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 25f0088

Please sign in to comment.