Skip to content

Add support for indicator constraints in MPSolver#132

Open
pet-mit wants to merge 12 commits intomainfrom
mpsolver_support_indicator_cts
Open

Add support for indicator constraints in MPSolver#132
pet-mit wants to merge 12 commits intomainfrom
mpsolver_support_indicator_cts

Conversation

@pet-mit
Copy link
Copy Markdown
Collaborator

@pet-mit pet-mit commented Jul 12, 2024

No description provided.

pet-mit added 3 commits July 12, 2024 16:09
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>
@pet-mit
Copy link
Copy Markdown
Collaborator Author

pet-mit commented Jul 12, 2024

@flomnes @klorel
do you have initial thoughts on this?
(next step would be adding XPRESS support, in another PR)

pet-mit added 3 commits July 12, 2024 16:27
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>
@pet-mit pet-mit marked this pull request as ready for review July 17, 2024 09:00
@pet-mit pet-mit requested review from flomnes and klorel July 17, 2024 09:00
Copy link
Copy Markdown
Collaborator

@flomnes flomnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if indicator_variable is not a binary variable ?

Comment on lines +446 to +447
/// Creates a named indicator constraint with given bounds and given
/// indicator variable.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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)

Comment on lines +1514 to +1515
MPConstraint* MPSolver::MakeIndicatorConstraint(
double lb, double ub, const std::string& name,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This method should fail if the solver doesn't support integer variables
  2. You can call MPVariable::integer to 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?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

}
constraints_.push_back(constraint);
constraint_is_extracted_.push_back(false);
interface_->AddIndicatorConstraint(constraint);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the return value ?

Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
@pet-mit pet-mit requested a review from flomnes July 17, 2024 12:08
Copy link
Copy Markdown
Collaborator

@flomnes flomnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log improvement ideas

constraint->indicator_variable_ = indicator_variable;
constraint->indicator_value_ = indicator_value;
if (!interface_->AddIndicatorConstraint(constraint)) {
LOG(ERROR) << "Solver doesn't support indicator constraints";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

@pet-mit pet-mit Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

pet-mit and others added 2 commits July 17, 2024 14:57
Co-authored-by: Florian Omnès <florian.omnes@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
@pet-mit pet-mit requested a review from flomnes July 17, 2024 14:03
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 ||
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

pet-mit added 2 commits July 17, 2024 17:33
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
Signed-off-by: Peter Mitri <peter.mitri@rte-france.com>
@pet-mit pet-mit requested a review from flomnes July 17, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants