From 7fa9009ad07f49c5c06457ec02d9192ad95cbd91 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Mon, 16 Dec 2024 17:46:39 +0100 Subject: [PATCH] update --- .../short-term-storage/AdditionalConstraint.h | 18 ++++++++++--- .../AdditionalConstraint.cpp | 27 ++++++++++++++++--- .../parts/short-term-storage/container.cpp | 3 ++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h b/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h index 811c6b6e92..2f3252aeed 100644 --- a/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h +++ b/src/libs/antares/study/include/antares/study/parts/short-term-storage/AdditionalConstraint.h @@ -21,21 +21,33 @@ #pragma once #include -#include +#include namespace Antares::Data::ShortTermStorage { -struct AdditionalConstraint + + struct AdditionalConstraint { std::string name; std::string cluster_id; std::string variable; std::string operatorType; - std::vector hours; + std::set hours; double rhs; unsigned int globalIndex = 0; bool validate() const; + + bool isValidVariable() const; + + bool isValidOperatorType() const; + + bool isValidHoursRange() const; + + mutable std::string error_message = ""; + + std::string getErrorMessage() const { return error_message; } + }; } // namespace Antares::Data::ShortTermStorage diff --git a/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp b/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp index d3ad08e45e..c74e70c812 100644 --- a/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp +++ b/src/libs/antares/study/parts/short-term-storage/AdditionalConstraint.cpp @@ -28,25 +28,44 @@ bool AdditionalConstraint::validate() const { if (cluster_id.empty()) { + error_message = "Cluster ID is empty."; return false; } - if (variable != "injection" && variable != "withdrawal" && variable != "netting") + if (!isValidVariable()) { + error_message = "Invalid variable type. Must be 'injection', 'withdrawal', or 'netting'."; return false; } - if (operatorType != "less" && operatorType != "equal" && operatorType != "greater") + if (!isValidOperatorType()) { + error_message = "Invalid operator type. Must be 'less', 'equal', or 'greater'."; return false; } - if (hours.empty() || *std::min_element(hours.begin(), hours.end()) < 1 - || *std::max_element(hours.begin(), hours.end()) > 168) + if (!isValidHoursRange()) { + error_message = "Hours set contains invalid values. Must be between 1 and 168."; return false; } + error_message.clear(); return true; } + + bool AdditionalConstraint::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; + + } + + bool AdditionalConstraint::isValidVariable() const { + return variable == "injection" || variable == "withdrawal" || variable == "netting"; + } + + bool AdditionalConstraint::isValidOperatorType() const { + return operatorType == "less" || operatorType == "equal" || operatorType == "greater"; + } } // namespace Antares::Data::ShortTermStorage diff --git a/src/libs/antares/study/parts/short-term-storage/container.cpp b/src/libs/antares/study/parts/short-term-storage/container.cpp index 991952d59f..fedc0c98e4 100644 --- a/src/libs/antares/study/parts/short-term-storage/container.cpp +++ b/src/libs/antares/study/parts/short-term-storage/container.cpp @@ -117,7 +117,7 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path) while (std::getline(ss, hour, ',')) { int hourVal = std::stoi(hour); - constraint.hours.push_back(hourVal); + constraint.hours.insert(hourVal); } } else if (key == "rhs") @@ -129,6 +129,7 @@ bool STStorageInput::LoadConstraintsFromIniFile(const fs::path& parent_path) if (!constraint.validate()) { logs.error() << "Invalid constraint in section: " << section->name; + logs.error() << constraint.getErrorMessage(); return false; }