Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getMemberFunction, use factory pattern #2536

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading