-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add support for indicator constraints in MPSolver #132
base: main
Are you sure you want to change the base?
Changes from 1 commit
59bb4f2
f243c06
73b8e15
4de1bfc
534c91c
a0e5dfd
52547b1
888963d
71aeeb8
b4f91fb
d68a748
05be41f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1514,19 +1514,27 @@ MPConstraint* MPSolver::MakeRowConstraint(const LinearRange& range, | |
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 || | ||
indicator_variable->ub() != 1) { | ||
LOG(ERROR) << "Variable " << indicator_variable->name() | ||
<< " is not boolean"; | ||
pet-mit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nullptr; | ||
} | ||
const int constraint_index = NumConstraints(); | ||
MPConstraint* const constraint = | ||
new MPConstraint(constraint_index, lb, ub, name, interface_.get()); | ||
// TODO: check that variable is boolean? | ||
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. Choose a reason for hiding this commentThe 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 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. Choose a reason for hiding this commentThe 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" |
||
return nullptr; | ||
} | ||
if (constraint_name_to_index_) { | ||
gtl::InsertOrDie(&*constraint_name_to_index_, constraint->name(), | ||
constraint_index); | ||
} | ||
constraints_.push_back(constraint); | ||
constraint_is_extracted_.push_back(false); | ||
interface_->AddIndicatorConstraint(constraint); | ||
return constraint; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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
andMPConstraint::SetCoefficient
, you'd need the following linesAlthough the 2nd line is currently included in the 1st.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!