Skip to content

Commit

Permalink
Make infeasibility more changeable : simplify constraint factory
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpier-code committed Jul 9, 2024
1 parent 436da64 commit 10491bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <regex>
#include <string>
#include <vector>
#include <map>
#include <functional>

namespace Antares::Optimization
{
Expand Down Expand Up @@ -84,17 +86,13 @@ class HydroProduction: public WatchedConstraint
class ConstraintsFactory
{
public:
ConstraintsFactory();
std::shared_ptr<WatchedConstraint> create(std::string regexId) const;
std::regex constraintsFilter();

private:
const std::vector<std::string> regex_ids_ = {"::hourly::",
"::daily::",
"::weekly::",
"^FictiveLoads::",
"^AreaHydroLevel::",
"^Level::",
"^HydroPower::"};
std::map<std::string, std::function<std::shared_ptr<WatchedConstraint>(std::string)>> regex_to_constraints_;
std::vector<std::string> regex_ids_;
};

} // namespace Antares::Optimization
77 changes: 50 additions & 27 deletions src/solver/infeasible-problem-analysis/watched-constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>
#include <ranges>

class StringIsNotWellFormated: public std::runtime_error
{
Expand Down Expand Up @@ -216,37 +217,59 @@ std::string HydroProduction::infeasibilityCause()
// =======================================
// Constraints factory
// =======================================
std::shared_ptr<WatchedConstraint> createHourlyBC(std::string varName)
{
return std::make_shared<HourlyBC>(varName);
}
std::shared_ptr<WatchedConstraint> createDailyBC(std::string varName)
{
return std::make_shared<DailyBC>(varName);
}
std::shared_ptr<WatchedConstraint> createWeeklyBC(std::string varName)
{
return std::make_shared<WeeklyBC>(varName);
}
std::shared_ptr<WatchedConstraint> createFictitiousLoad(std::string varName)
{
return std::make_shared<FictitiousLoad>(varName);
}
std::shared_ptr<WatchedConstraint> createHydroLevel(std::string varName)
{
return std::make_shared<HydroLevel>(varName);
}
std::shared_ptr<WatchedConstraint> createSTS(std::string varName)
{
return std::make_shared<STS>(varName);
}
std::shared_ptr<WatchedConstraint> createHydroProduction(std::string varName)
{
return std::make_shared<HydroProduction>(varName);
}


ConstraintsFactory::ConstraintsFactory()
{
regex_to_constraints_["::hourly::"] = createHourlyBC;
regex_to_constraints_["::daily::"] = createDailyBC;
regex_to_constraints_["::weekly::"] = createWeeklyBC;
regex_to_constraints_["^FictiveLoads::"] = createFictitiousLoad;
regex_to_constraints_["^AreaHydroLevel::"] = createHydroLevel;
regex_to_constraints_["^Level::"] = createSTS;
regex_to_constraints_["^HydroPower::"] = createHydroProduction;

auto keyView = std::views::keys(regex_to_constraints_);
regex_ids_ = { keyView.begin(), keyView.end() };
}

std::shared_ptr<WatchedConstraint> ConstraintsFactory::create(std::string varName) const
{
if (std::regex_search(varName, std::regex("::hourly::")))
{
return std::make_shared<HourlyBC>(varName);
}
if (std::regex_search(varName, std::regex("::daily::")))
{
return std::make_shared<DailyBC>(varName);
}
if (std::regex_search(varName, std::regex("::weekly::")))
{
return std::make_shared<WeeklyBC>(varName);
}
if (std::regex_search(varName, std::regex("^FictiveLoads::")))
{
return std::make_shared<FictitiousLoad>(varName);
}
if (std::regex_search(varName, std::regex("^AreaHydroLevel::")))
{
return std::make_shared<HydroLevel>(varName);
}
if (std::regex_search(varName, std::regex("^Level::")))
{
return std::make_shared<STS>(varName);
}
if (std::regex_search(varName, std::regex("^HydroPower::")))
for (auto& [pattern, createFunction]: regex_to_constraints_)
{
return std::make_shared<HydroProduction>(varName);
if (std::regex_search(varName, std::regex(pattern)))
{
return createFunction(varName);
}
}
return nullptr;
}

std::regex ConstraintsFactory::constraintsFilter()
Expand Down

0 comments on commit 10491bc

Please sign in to comment.