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

Infeability analyzer : renaming #2225

Merged
merged 3 commits into from
Jul 3, 2024
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
44 changes: 22 additions & 22 deletions src/solver/infeasible-problem-analysis/constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,37 @@ const std::string kUnknown = "<unknown>";
namespace Antares::Optimization
{
Constraint::Constraint(const std::string& input, const double slackValue):
mInput(input),
mSlackValue(slackValue)
name_(input),
slackValue_(slackValue)
{
}

std::size_t Constraint::extractItems()
std::size_t Constraint::extractComponentsFromName()
{
const auto beg = mInput.begin();
const auto end = mInput.end();
const auto beg = name_.begin();
const auto end = name_.end();
std::size_t newPos = 0;
const std::size_t sepSize = 2;
const std::size_t inputSize = mInput.size();
const std::size_t inputSize = name_.size();
for (std::size_t pos = 0; pos < inputSize; pos = newPos + sepSize)
{
newPos = mInput.find("::", pos);
newPos = name_.find("::", pos);
if (newPos == std::string::npos)
{
mItems.emplace_back(beg + pos, end);
nameComponents_.emplace_back(beg + pos, end);
break;
}
if (newPos > pos)
{
mItems.emplace_back(beg + pos, beg + newPos);
nameComponents_.emplace_back(beg + pos, beg + newPos);
}
}
return mItems.size();
return nameComponents_.size();
}

double Constraint::getSlackValue() const
{
return mSlackValue;
return slackValue_;
}

class StringIsNotWellFormated: public std::runtime_error
Expand Down Expand Up @@ -117,7 +117,7 @@ std::string Constraint::getAreaName() const
{
return "<none>";
}
return StringBetweenAngleBrackets(mItems.at(1));
return StringBetweenAngleBrackets(nameComponents_.at(1));
}

std::string Constraint::getTimeStepInYear() const
Expand All @@ -129,36 +129,36 @@ std::string Constraint::getTimeStepInYear() const
case ConstraintType::fictitious_load:
case ConstraintType::hydro_reservoir_level:
case ConstraintType::short_term_storage_level:
return StringBetweenAngleBrackets(mItems.at(mItems.size() - 2));
return StringBetweenAngleBrackets(nameComponents_.at(nameComponents_.size() - 2));
default:
return kUnknown;
}
}

ConstraintType Constraint::getType() const
{
assert(mItems.size() > 1);
if (mItems.at(1) == "hourly")
assert(nameComponents_.size() > 1);
if (nameComponents_.at(1) == "hourly")
{
return ConstraintType::binding_constraint_hourly;
}
if (mItems.at(1) == "daily")
if (nameComponents_.at(1) == "daily")
{
return ConstraintType::binding_constraint_daily;
}
if (mItems.at(1) == "weekly")
if (nameComponents_.at(1) == "weekly")
{
return ConstraintType::binding_constraint_weekly;
}
if (mItems.at(0) == "FictiveLoads")
if (nameComponents_.at(0) == "FictiveLoads")
{
return ConstraintType::fictitious_load;
}
if (mItems.at(0) == "AreaHydroLevel")
if (nameComponents_.at(0) == "AreaHydroLevel")
{
return ConstraintType::hydro_reservoir_level;
}
if (mItems.at(0) == "Level")
if (nameComponents_.at(0) == "Level")
{
return ConstraintType::short_term_storage_level;
}
Expand All @@ -172,7 +172,7 @@ std::string Constraint::getBindingConstraintName() const
case ConstraintType::binding_constraint_hourly:
case ConstraintType::binding_constraint_daily:
case ConstraintType::binding_constraint_weekly:
return mItems.at(0);
return nameComponents_.at(0);
default:
return kUnknown;
}
Expand All @@ -182,7 +182,7 @@ std::string Constraint::getSTSName() const
{
if (getType() == ConstraintType::short_term_storage_level)
{
return StringBetweenAngleBrackets(mItems.at(2));
return StringBetweenAngleBrackets(nameComponents_.at(2));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class Constraint
double getSlackValue() const;

// Extract items, check consistency
std::size_t extractItems();
std::size_t extractComponentsFromName();
std::string prettyPrint() const;
ConstraintType getType() const;

private:
std::string mInput;
std::vector<std::string> mItems;
double mSlackValue;
std::string name_;
std::vector<std::string> nameComponents_;
double slackValue_;

// Get specific items
std::string getAreaName() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class InfeasibleProblemReport
const std::vector<const operations_research::MPVariable*>& slackVariables);
void sortConstraints();
void trimConstraints();
void extractItems();
void sortConstraintsByType();
void logSuspiciousConstraints();

std::vector<Constraint> mConstraints;
std::map<ConstraintType, unsigned int> mTypes;
const unsigned int nbVariables = 10;
std::vector<Constraint> constraints_;
std::map<ConstraintType, unsigned int> nbConstraintsByType_;
const unsigned int nbMaxVariables = 10;
};
} // namespace Antares::Optimization
32 changes: 16 additions & 16 deletions src/solver/infeasible-problem-analysis/report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,62 @@ void InfeasibleProblemReport::turnSlackVarsIntoConstraints(
{
for (const MPVariable* slack: slackVariables)
{
mConstraints.emplace_back(slack->name(), slack->solution_value());
constraints_.emplace_back(slack->name(), slack->solution_value());
}
}

void InfeasibleProblemReport::sortConstraints()
{
std::sort(std::begin(mConstraints), std::end(mConstraints), ::compareSlackSolutions);
std::sort(std::begin(constraints_), std::end(constraints_), ::compareSlackSolutions);
}

void InfeasibleProblemReport::trimConstraints()
{
if (nbVariables <= mConstraints.size())
if (nbMaxVariables <= constraints_.size())
{
mConstraints.resize(nbVariables);
constraints_.resize(nbMaxVariables);
}
}

void InfeasibleProblemReport::extractItems()
void InfeasibleProblemReport::sortConstraintsByType()
{
for (auto& c: mConstraints)
for (auto& c: constraints_)
{
if (c.extractItems() == 0)
if (c.extractComponentsFromName() == 0)
{
return;
}
mTypes[c.getType()]++;
nbConstraintsByType_[c.getType()]++;
}
}

void InfeasibleProblemReport::logSuspiciousConstraints()
{
Antares::logs.error() << "The following constraints are suspicious (first = most suspicious)";
for (const auto& c: mConstraints)
for (const auto& c: constraints_)
{
Antares::logs.error() << c.prettyPrint();
}
Antares::logs.error() << "Possible causes of infeasibility:";
if (mTypes[ConstraintType::hydro_reservoir_level] > 0)
if (nbConstraintsByType_[ConstraintType::hydro_reservoir_level] > 0)
{
Antares::logs.error() << "* Hydro reservoir impossible to manage with cumulative options "
"\"hard bounds without heuristic\"";
}
if (mTypes[ConstraintType::fictitious_load] > 0)
if (nbConstraintsByType_[ConstraintType::fictitious_load] > 0)
{
Antares::logs.error() << "* Last resort shedding status,";
}
if (mTypes[ConstraintType::short_term_storage_level] > 0)
if (nbConstraintsByType_[ConstraintType::short_term_storage_level] > 0)
{
Antares::logs.error()
<< "* Short-term storage reservoir level impossible to manage. Please check inflows, "
"lower & upper curves and initial level (if prescribed),";
}

const unsigned int bcCount = mTypes[ConstraintType::binding_constraint_hourly]
+ mTypes[ConstraintType::binding_constraint_daily]
+ mTypes[ConstraintType::binding_constraint_weekly];
const unsigned int bcCount = nbConstraintsByType_[ConstraintType::binding_constraint_hourly]
+ nbConstraintsByType_[ConstraintType::binding_constraint_daily]
+ nbConstraintsByType_[ConstraintType::binding_constraint_weekly];
if (bcCount > 0)
{
Antares::logs.error() << "* Binding constraints,";
Expand All @@ -118,7 +118,7 @@ void InfeasibleProblemReport::logSuspiciousConstraints()

void InfeasibleProblemReport::prettyPrint()
{
extractItems();
sortConstraintsByType();
logSuspiciousConstraints();
}

Expand Down
Loading