Skip to content

Commit

Permalink
Remove getMemberFunction, use factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes committed Dec 16, 2024
1 parent 2d129d8 commit d70370d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
94 changes: 65 additions & 29 deletions src/solver/optimisation/constraints/ShortTermStorageCumulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,84 @@
* 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)
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<std::string,
void (ShortTermStorageCumulation::*)(unsigned int,
const ::ShortTermStorage::PROPERTIES&)>
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<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 false; // TODO custom exception
}

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

Please sign in to comment.