Add support for indicator constraints in MPSolver#132
Conversation
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
flomnes
left a comment
There was a problem hiding this comment.
What happens if indicator_variable is not a binary variable ?
| /// Creates a named indicator constraint with given bounds and given | ||
| /// indicator variable. |
There was a problem hiding this comment.
| /// Creates a named indicator constraint with given bounds and given | |
| /// indicator variable. | |
| /// Creates a named indicator constraint with given bounds and given | |
| /// indicator variable. | |
| /// The constraint is active if and only if *indicator_variable has value indicator_value | |
| /// (Only available for MILP problems) |
| MPConstraint* MPSolver::MakeIndicatorConstraint( | ||
| double lb, double ub, const std::string& name, |
There was a problem hiding this comment.
- This method should fail if the solver doesn't support integer variables
- You can call
MPVariable::integerto check it's an integer variable on the indicator variable, and possibly test it's bounds (0 & 1).
| const int constraint_index = NumConstraints(); | ||
| MPConstraint* const constraint = | ||
| new MPConstraint(constraint_index, lb, ub, name, interface_.get()); | ||
| // TODO: check that variable is boolean? |
| } | ||
| constraints_.push_back(constraint); | ||
| constraint_is_extracted_.push_back(false); | ||
| interface_->AddIndicatorConstraint(constraint); |
| constraint->indicator_variable_ = indicator_variable; | ||
| constraint->indicator_value_ = indicator_value; | ||
| if (!interface_->AddIndicatorConstraint(constraint)) { | ||
| LOG(ERROR) << "Solver doesn't support indicator constraints"; |
There was a problem hiding this comment.
I don't think it's necessary to display a log message here, it's already done in the base version of AddIndicatorConstraint (linear_solver.cc)
virtual bool AddIndicatorConstraint(MPConstraint* const /*ct*/) {
LOG(ERROR) << "Solver doesn't support indicator constraints.";
return false;
}It would be displayed twice. Not terrible, but not great either.
There was a problem hiding this comment.
OK, but then for interfaces that can sometimes handle indicator constraints, sometimes not (for example an interface that can handle MIP or LP problems), we'll have to log the issue before returning "false"
Co-authored-by: Florian Omnès <florian.omnes@rte-france.com>
| MPConstraint* MPSolver::MakeIndicatorConstraint( | ||
| double lb, double ub, const std::string& name, | ||
| const MPVariable* indicator_variable, bool indicator_value) { | ||
| if (!indicator_variable->integer() || indicator_variable->lb() != 0 || |
There was a problem hiding this comment.
To be consistent with e.g MPObjective::SetCoefficient and MPConstraint::SetCoefficient, you'd need the following lines
Although the 2nd line is currently included in the 1st.
DLOG_IF(DFATAL, !interface_->solver_->OwnsVariable(indicator_variable)) << indicator_variable;
if (indicator_variable == nullptr) return nullptr;
No description provided.