Skip to content

Commit

Permalink
Add attribute enabled to additional constraints, adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes committed Dec 30, 2024
1 parent 8ca3d69 commit 48e4e6a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct SingleAdditionalConstraint
std::set<int> hours;
unsigned int globalIndex = 0;
unsigned int localIndex = 0;

bool isValidHoursRange() const;
};

Expand All @@ -41,8 +40,9 @@ struct AdditionalConstraints
std::string cluster_id;
std::string variable;
std::string operatorType;
bool enabled = true;
// TODO a lot unused entries
//std::array<double, HOURS_PER_YEAR> rhs = {};
// std::array<double, HOURS_PER_YEAR> rhs = {};
std::vector<double> rhs = {};

std::vector<SingleAdditionalConstraint> constraints = {};
Expand All @@ -53,6 +53,9 @@ struct AdditionalConstraints
std::string error_msg;
};

// Number of enabled constraints
std::size_t enabledConstraints() const;

ValidateResult validate() const;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#include "antares/study/parts/short-term-storage/AdditionalConstraints.h"

#include <algorithm>

namespace Antares::Data::ShortTermStorage
Expand Down Expand Up @@ -47,25 +48,21 @@ AdditionalConstraints::ValidateResult AdditionalConstraints::validate() const

return {true, ""};
}

bool SingleAdditionalConstraint::isValidHoursRange() const
{
// `hours` is a sorted set; begin() gives the smallest and prev(end()) gives the largest.
return !hours.empty() && *hours.begin()
>= 1 && *std::prev(
hours.end()) <= 168;
return !hours.empty() && *hours.begin() >= 1 && *std::prev(hours.end()) <= 168;
}

bool AdditionalConstraints::isValidHours() const
{
return std::ranges::all_of(constraints.begin(),
constraints.end(),
[](const auto& constraint)
{
return constraint.isValidHoursRange();
});
{ return constraint.isValidHoursRange(); });
}


bool AdditionalConstraints::isValidVariable() const
{
return variable == "injection" || variable == "withdrawal" || variable == "netting";
Expand All @@ -75,4 +72,9 @@ bool AdditionalConstraints::isValidOperatorType() const
{
return operatorType == "less" || operatorType == "equal" || operatorType == "greater";
}

std::size_t AdditionalConstraints::enabledConstraints() const
{
return enabled ? constraints.size() : 0;
}
} // namespace Antares::Data::ShortTermStorage
30 changes: 16 additions & 14 deletions src/libs/antares/study/parts/short-term-storage/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path)
value.to<std::string>(clusterName);
additional_constraints.cluster_id = transformNameIntoID(clusterName);
}
else if (key == "enabled")
{
value.to<bool>(additional_constraints.enabled);
}
else if (key == "variable")
{
value.to<std::string>(additional_constraints.variable);
Expand Down Expand Up @@ -147,7 +151,7 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path)
{
// Add this group to the `hours` vec
additional_constraints.constraints.push_back(
{.hours = hourSet, .localIndex = localIndex});
{.hours = hourSet, .localIndex = localIndex});
++localIndex;
}
}
Expand All @@ -168,9 +172,7 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path)

auto it = std::ranges::find_if(storagesByIndex,
[&additional_constraints](const STStorageCluster& cluster)
{
return cluster.id == additional_constraints.cluster_id;
});
{ return cluster.id == additional_constraints.cluster_id; });
if (it == storagesByIndex.end())
{
logs.warning() << " from file " << pathIni;
Expand Down Expand Up @@ -234,16 +236,16 @@ std::size_t STStorageInput::cumulativeConstraintCount() const
0,
[](size_t outer_constraint_count, const auto& cluster)
{
return outer_constraint_count + std::accumulate(
cluster.additional_constraints.begin(),
cluster.additional_constraints.end(),
0,
[](size_t inner_constraint_count,
const auto& additional_constraints)
{
return inner_constraint_count +
additional_constraints.constraints.size();
});
return outer_constraint_count
+ std::accumulate(
cluster.additional_constraints.begin(),
cluster.additional_constraints.end(),
0,
[](size_t inner_constraint_count,
const auto& additional_constraints) {
return inner_constraint_count
+ additional_constraints.enabledConstraints();
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ void ShortTermStorageCumulation::add(int pays)
// var = withdrawal for WithdrawalCumulationConstraint
// var = injectionEfficiency * injection - withdrawalEfficiency * withdrawal for Netting
auto constraintHelper = cumulationConstraintFromVariable(
additional_constraints.variable);
additional_constraints.variable);
for (const auto& [hours, globalIndex, localIndex]: additional_constraints.constraints)
{
namer.ShortTermStorageCumulation(
constraintHelper->name(),
builder.data.nombreDeContraintes,
storage.name,
additional_constraints.name + "_" + std::to_string(localIndex));
namer.ShortTermStorageCumulation(constraintHelper->name(),
builder.data.nombreDeContraintes,
storage.name,
additional_constraints.name + "_"
+ std::to_string(localIndex));
const auto index = storage.clusterGlobalIndex;
data.CorrespondanceCntNativesCntOptimHebdomadaires
.ShortTermStorageCumulation[globalIndex]
= builder.data.nombreDeContraintes;
.ShortTermStorageCumulation[globalIndex]
= builder.data.nombreDeContraintes;

for (const auto& hour: hours)
{
Expand Down
14 changes: 9 additions & 5 deletions src/solver/simulation/sim_calcul_economique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ static void importShortTermStorages(
toInsert.penalizeVariationInjection = st.properties.penalizeVariationInjection;
toInsert.penalizeVariationWithdrawal = st.properties.penalizeVariationWithdrawal;
toInsert.name = st.properties.name;
toInsert.additional_constraints = st.additional_constraints;
for (auto& additional_constraints: toInsert.additional_constraints)
for (auto& constr: st.additional_constraints)
{
for (auto& [_, globalIndex,__]: additional_constraints.constraints)
if (constr.enabled)
{
globalIndex = clusterCumulativeConstraintGlobalIndex;
++clusterCumulativeConstraintGlobalIndex;
for (auto c: constr.constraints)
{
c.globalIndex = clusterCumulativeConstraintGlobalIndex;
++clusterCumulativeConstraintGlobalIndex;
}
toInsert.additional_constraints.push_back(constr);
}
}

toInsert.series = st.series;

// TODO add missing properties, or use the same struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

#define WIN32_LEAN_AND_MEAN

#include <absl/strings/internal/str_format/extension.h>
#include <filesystem>
#include <fstream>
#include <absl/strings/internal/str_format/extension.h>

#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

#include <yuni/io/file.h>

#include "antares/antares/constants.h"
#include "antares/study/parts/short-term-storage/container.h"
#include "antares/study/parts/short-term-storage/AdditionalConstraints.h"
#include "antares/study/parts/short-term-storage/container.h"

using namespace std;
using namespace Antares::Data;
Expand Down Expand Up @@ -492,8 +492,7 @@ BOOST_AUTO_TEST_CASE(Validate_InvalidOperatorType)

auto [ok, error_msg] = constraints.validate();
BOOST_CHECK_EQUAL(ok, false);
BOOST_CHECK_EQUAL(error_msg,
"Invalid operator type. Must be 'less', 'equal', or 'greater'.");
BOOST_CHECK_EQUAL(error_msg, "Invalid operator type. Must be 'less', 'equal', or 'greater'.");
}

BOOST_AUTO_TEST_CASE(Validate_InvalidHours)
Expand Down Expand Up @@ -683,8 +682,8 @@ BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_ValidRhs)
HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(storageInput.storagesByIndex[0].additional_constraints[0].rhs[0], 0.0);
BOOST_CHECK_EQUAL(
storageInput.storagesByIndex[0].additional_constraints[0].rhs[HOURS_PER_YEAR - 1],
HOURS_PER_YEAR - 1);
storageInput.storagesByIndex[0].additional_constraints[0].rhs[HOURS_PER_YEAR - 1],
HOURS_PER_YEAR - 1);

std::filesystem::remove_all(testPath);
}
Expand Down Expand Up @@ -730,12 +729,9 @@ BOOST_AUTO_TEST_CASE(Load2ConstraintsFromIniFile)
BOOST_CHECK_EQUAL(constraint1.operatorType, "less");
BOOST_CHECK_EQUAL(constraint1.variable, "injection");
BOOST_CHECK_EQUAL(constraint1.cluster_id, cluster.id);
BOOST_CHECK_EQUAL(constraint1.rhs.size(),
HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(constraint1.rhs.size(), HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(constraint1.rhs[0], 0.0);
BOOST_CHECK_EQUAL(
constraint1.rhs[HOURS_PER_YEAR - 1],
HOURS_PER_YEAR - 1);
BOOST_CHECK_EQUAL(constraint1.rhs[HOURS_PER_YEAR - 1], HOURS_PER_YEAR - 1);

//------- constraint2 ----------

Expand All @@ -745,12 +741,9 @@ BOOST_AUTO_TEST_CASE(Load2ConstraintsFromIniFile)
BOOST_CHECK_EQUAL(constraint2.variable, "withdrawal");
BOOST_CHECK_EQUAL(constraint2.cluster_id, cluster.id);

BOOST_CHECK_EQUAL(constraint2.rhs.size(),
HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(constraint2.rhs.size(), HOURS_PER_YEAR);
BOOST_CHECK_EQUAL(constraint2.rhs[0], 0.0);
BOOST_CHECK_EQUAL(
constraint2.rhs[HOURS_PER_YEAR - 1],
0.0);
BOOST_CHECK_EQUAL(constraint2.rhs[HOURS_PER_YEAR - 1], 0.0);

std::filesystem::remove_all(testPath);
}
Expand Down Expand Up @@ -846,8 +839,8 @@ BOOST_AUTO_TEST_CASE(LoadConstraintsFromIniFile_IncompleteRhsFile)
namespace bdata = boost::unit_test::data;

BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinations,
bdata::make({"injection", "withdrawal", "netting"}) ^
bdata::make({"less", "equal", "greater"}),
bdata::make({"injection", "withdrawal", "netting"})
^ bdata::make({"less", "equal", "greater"}),
variable,
op)
{
Expand All @@ -859,21 +852,21 @@ BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinations,
// Create constraints with valid hours
constraints.constraints.push_back(ShortTermStorage::SingleAdditionalConstraint{{1, 2, 3}});
constraints.constraints.push_back(ShortTermStorage::SingleAdditionalConstraint{{50, 100, 150}});
constraints.constraints.
push_back(ShortTermStorage::SingleAdditionalConstraint{{120, 121, 122}});
constraints.constraints.push_back(
ShortTermStorage::SingleAdditionalConstraint{{120, 121, 122}});

// Validate the constraints
auto [ok, error_msg] = constraints.validate();
BOOST_CHECK_EQUAL(ok, true);
BOOST_CHECK(error_msg.empty());
}


BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinationsFromFile,
bdata::make({"injection", "withdrawal", "netting"}) ^
bdata::make({"less", "equal", "greater"}),
bdata::make({"injection", "withdrawal", "netting"})
* bdata::make({"less", "equal", "greater"}) * bdata::make({true, false}),
variable,
op)
op,
enabled)
{
// Define the path for the test data
std::filesystem::path testPath = std::filesystem::temp_directory_path() / "test_data";
Expand All @@ -885,6 +878,8 @@ BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinationsFromFile,
iniFile << "cluster=clustera\n";
iniFile << "variable=" << variable << "\n";
iniFile << "operator=" << op << "\n";
iniFile << std::boolalpha;
iniFile << "enabled=" << enabled << "\n";
iniFile << "hours=[1,2,3]\n";
iniFile.close();

Expand All @@ -904,14 +899,17 @@ BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinationsFromFile,

// Load constraints from the `.ini` file
bool result = storageInput.LoadConstraintsFromIniFile(testPath);
BOOST_CHECK_EQUAL(storageInput.cumulativeConstraintCount(), enabled ? 1 : 0);

// Assertions
BOOST_CHECK_EQUAL(result, true);

// Validate loaded constraints
auto& built_cluster = storageInput.storagesByIndex[0];

BOOST_REQUIRE_EQUAL(built_cluster.additional_constraints.size(), 1);
const auto& loadedConstraint = built_cluster.additional_constraints[0];
BOOST_CHECK_EQUAL(loadedConstraint.enabled, enabled);
BOOST_CHECK_EQUAL(loadedConstraint.enabledConstraints(), enabled ? 1 : 0);

// Check variable, operator type, and rhs values
BOOST_CHECK_EQUAL(loadedConstraint.variable, variable);
Expand All @@ -929,4 +927,3 @@ BOOST_DATA_TEST_CASE(Validate_AllVariableOperatorCombinationsFromFile,
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 48e4e6a

Please sign in to comment.