Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::GScipConstraintHandler< ConstraintData > Class Template Reference

#include <gscip_constraint_handler.h>

Public Member Functions

 GScipConstraintHandler (const GScipConstraintHandlerProperties &properties)
 
virtual ~GScipConstraintHandler ()=default
 
const GScipConstraintHandlerPropertiesproperties () const
 
absl::Status Register (GScip *gscip)
 Template implementations.
 
absl::StatusOr< SCIP_CONS * > AddCallbackConstraint (GScip *gscip, const std::string &constraint_name, const ConstraintData *constraint_data, const GScipConstraintOptions &options=DefaultGScipConstraintOptions())
 
virtual absl::StatusOr< GScipCallbackResultEnforceLp (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool solution_infeasible)
 Default callback implementations.
 
virtual absl::StatusOr< GScipCallbackResultEnforcePseudoSolution (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool solution_infeasible, bool objective_infeasible)
 
virtual absl::StatusOr< bool > CheckIsFeasible (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool check_integrality, bool check_lp_rows, bool print_reason, bool check_completely)
 
virtual std::vector< std::pair< SCIP_VAR *, RoundingLockDirection > > RoundingLock (GScip *gscip, const ConstraintData &constraint_data, bool lock_type_is_model)
 
virtual absl::StatusOr< GScipCallbackResultSeparateLp (GScipConstraintHandlerContext context, const ConstraintData &constraint_data)
 
virtual absl::StatusOr< GScipCallbackResultSeparateSolution (GScipConstraintHandlerContext context, const ConstraintData &constraint_data)
 
GScipCallbackResult CallEnforceLp (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool solution_infeasible)
 The functions below wrap each callback function to manage status.
 
GScipCallbackResult CallEnforcePseudoSolution (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool solution_infeasible, bool objective_infeasible)
 
GScipCallbackResult CallCheckIsFeasible (GScipConstraintHandlerContext context, const ConstraintData &constraint_data, bool check_integrality, bool check_lp_rows, bool print_reason, bool check_completely)
 
GScipCallbackResult CallSeparateLp (GScipConstraintHandlerContext context, const ConstraintData &constraint_data)
 
GScipCallbackResult CallSeparateSolution (GScipConstraintHandlerContext context, const ConstraintData &constraint_data)
 

Detailed Description

template<typename ConstraintData>
class operations_research::GScipConstraintHandler< ConstraintData >

Constraint handler class. To implement a constraint handler, the user can inherit this class and implement the desired callback functions. The templated ConstraintData is the equivalent of SCIP's SCIP_CONSHDLRDATA, and can hold the data needed for the constraint. To then use it, the function Register must be called once, and AddCallbackConstraint must be called for each constraint to be added in this constraint handler.

There is a one-to-one mapping between relevant SCIP callback functions and the functions in this class; see SCIP documentation for which types of callbacks to use. Make sure to follow SCIP's rules (e.g. if implementing enforcement, all enforcement and check callbacks must be implemented).

For examples of usage, see gscip_constraint_handler_test.cc.

Implementation details:

  • Default implementations: All callback functions have a default implementation that returns "did not run" or "feasible" accordingly. For rounding lock, the default implementation locks both directions.
  • Status errors: If the user returns an absl::Status error, then the solve is interrupted via SCIPinterruptSolve(), and the status error is ultimately returned by GScip::Solve() after SCIP completes the interruption. The callback function returns SCIP_OKAY to SCIP except for internal errors. We try to avoid returning SCIP_ERROR in the middle of a callback since SCIP might not stay in a fully clean state (e.g. calling SCIPfree might hit an assert).
  • Constraint priority: SCIP informs the callback which subset of constraints are more likely to be violated. The callback is called on those constraints first, and if the highest priority result is kDidNotFind, kDidNotRun, or kFeasible, it is called for the remaining ones.

Supported SCIP callback functions:

  • SCIP_DECL_CONSENFOLP
  • SCIP_DECL_CONSENFOPS
  • SCIP_DECL_CONSCHECK
  • SCIP_DECL_CONSLOCK
  • SCIP_DECL_CONSSEPALP
  • SCIP_DECL_CONSSEPASOL

Used, but not customizable:

  • SCIP_DECL_CONSFREE
  • SCIP_DECL_CONSINIT
  • SCIP_DECL_CONSDELETE

Definition at line 286 of file gscip_constraint_handler.h.

Constructor & Destructor Documentation

◆ GScipConstraintHandler()

template<typename ConstraintData >
operations_research::GScipConstraintHandler< ConstraintData >::GScipConstraintHandler ( const GScipConstraintHandlerProperties & properties)
inlineexplicit

Constructs a constraint handler that will be registered using the given properties. It is recommended to set priorities and frequencies manually in properties.

Definition at line 291 of file gscip_constraint_handler.h.

◆ ~GScipConstraintHandler()

template<typename ConstraintData >
virtual operations_research::GScipConstraintHandler< ConstraintData >::~GScipConstraintHandler ( )
virtualdefault

Member Function Documentation

◆ AddCallbackConstraint()

template<typename ConstraintData >
absl::StatusOr< SCIP_CONS * > operations_research::GScipConstraintHandler< ConstraintData >::AddCallbackConstraint ( GScip * gscip,
const std::string & constraint_name,
const ConstraintData * constraint_data,
const GScipConstraintOptions & options = DefaultGScipConstraintOptions() )

Adds a callback constraint to the model. That is, it attaches to the constraint handler a constraint for the given constraint data.

Warning
the user is responsible for ensuring that constraint_data outlives the returned constraint (e.g. until GScip is destroyed or GScip::delete_constraint() is invoked on the returned constraint).
Note
it appears to be safe (from looking at the source and running asan) to free constraint_data before calling ~GScip(), but this is difficult to verify. Any other interaction with GScip after freeing the constraint_data seems very likely to cause memory corruption.

Definition at line 586 of file gscip_constraint_handler.h.

◆ CallCheckIsFeasible()

template<typename ConstraintData >
GScipCallbackResult operations_research::GScipConstraintHandler< ConstraintData >::CallCheckIsFeasible ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool check_integrality,
bool check_lp_rows,
bool print_reason,
bool check_completely )

Definition at line 688 of file gscip_constraint_handler.h.

◆ CallEnforceLp()

template<typename ConstraintData >
GScipCallbackResult operations_research::GScipConstraintHandler< ConstraintData >::CallEnforceLp ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool solution_infeasible )

The functions below wrap each callback function to manage status.

Definition at line 667 of file gscip_constraint_handler.h.

◆ CallEnforcePseudoSolution()

template<typename ConstraintData >
GScipCallbackResult operations_research::GScipConstraintHandler< ConstraintData >::CallEnforcePseudoSolution ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool solution_infeasible,
bool objective_infeasible )

Definition at line 677 of file gscip_constraint_handler.h.

◆ CallSeparateLp()

template<typename ConstraintData >
GScipCallbackResult operations_research::GScipConstraintHandler< ConstraintData >::CallSeparateLp ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data )

Definition at line 706 of file gscip_constraint_handler.h.

◆ CallSeparateSolution()

template<typename ConstraintData >
GScipCallbackResult operations_research::GScipConstraintHandler< ConstraintData >::CallSeparateSolution ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data )

Definition at line 715 of file gscip_constraint_handler.h.

◆ CheckIsFeasible()

template<typename ConstraintData >
absl::StatusOr< bool > operations_research::GScipConstraintHandler< ConstraintData >::CheckIsFeasible ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool check_integrality,
bool check_lp_rows,
bool print_reason,
bool check_completely )
virtual

Callback function called at SCIP's CONSCHECK. Must return true if the current solution stored in the context satisfies all constraints of the constraint handler, or false otherwise. If properties_.feasibility_check_priority < 0, then this only acts on integer solutions.

SCIP CONSCHECK callback arguments:

  • check_integrality: checkintegrality in SCIP, indicates if integrality must be checked. Used to avoid redundant checks in cases where integrality is already checked or implicit.
  • check_lp_rows: checklprows in SCIP, indicates if the constraints represented by rows in the current LP must be checked. Used to avoid redundant checks in cases where row feasibility is already checked or implicit.
  • print_reason: printreason in SCIP, indicates if the reason for the violation should be printed.
  • check_completely: completely in SCIP, indicates if all violations should be checked.

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 616 of file gscip_constraint_handler.h.

◆ EnforceLp()

template<typename ConstraintData >
absl::StatusOr< GScipCallbackResult > operations_research::GScipConstraintHandler< ConstraintData >::EnforceLp ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool solution_infeasible )
virtual

Default callback implementations.

Callback function called at SCIP's CONSENFOLP. Must check if an LP solution at a node is feasible, and if not, resolve the infeasibility if possible by branching, reducing variable domains, or separating the solution with a cutting plane. If properties_.enforcement_priority < 0, then this only acts on integer solutions.

SCIP CONSENFOLP callback arguments:

  • solution_infeasible: solinfeasible in SCIP, indicates if the solution was already declared infeasible by a constraint handler.

It is the user's responsibility to return a valid result for CONSENFOLP; see SCIP's documentation (e.g. type_cons.h).

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 600 of file gscip_constraint_handler.h.

◆ EnforcePseudoSolution()

template<typename ConstraintData >
absl::StatusOr< GScipCallbackResult > operations_research::GScipConstraintHandler< ConstraintData >::EnforcePseudoSolution ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data,
bool solution_infeasible,
bool objective_infeasible )
virtual

Callback function called at SCIP's CONSENFOPS. Must check if a pseudosolution is feasible, and if not, resolve the infeasibility if possible by branching, reducing variable domains, or adding an additional constraint. Separating with a cutting plane is not possible since there is no corresponding LP (i.e. kSeparated cannot be returned). If properties_.enforcement_priority < 0, then this only acts on integer solutions.

SCIP CONSENFOPS callback arguments:

  • solution_infeasible: solinfeasible in SCIP, indicates if the solution was already declared infeasible by a constraint handler.
  • objective_infeasible: objinfeasible in SCIP, indicates if the solution is infeasible due to violating objective bound.

It is the user's responsibility to return a valid result for CONSENFOPS; see SCIP's documentation (e.g. type_cons.h).

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 608 of file gscip_constraint_handler.h.

◆ properties()

template<typename ConstraintData >
const GScipConstraintHandlerProperties & operations_research::GScipConstraintHandler< ConstraintData >::properties ( ) const
inline

Definition at line 297 of file gscip_constraint_handler.h.

◆ Register()

template<typename ConstraintData >
absl::Status operations_research::GScipConstraintHandler< ConstraintData >::Register ( GScip * gscip)

Template implementations.

Registers this constraint handler with GScip. If the handler has already been registered, returns an error.

Definition at line 577 of file gscip_constraint_handler.h.

◆ RoundingLock()

template<typename ConstraintData >
std::vector< std::pair< SCIP_VAR *, RoundingLockDirection > > operations_research::GScipConstraintHandler< ConstraintData >::RoundingLock ( GScip * gscip,
const ConstraintData & constraint_data,
bool lock_type_is_model )
virtual

Callback function called at SCIP's CONSLOCK. Must return, for each variable, whether the constraint may be violated by decreasing and/or increasing the variable value. It is always safe to claim that both directions can violate the constraint, which is the default implementation, but it may affect SCIP's capabilities.

SCIP CONSLOCK callback arguments:

  • lock_type_is_model: if locktype == SCIP_LOCKTYPE_MODEL in SCIP. If true, this callback is called for model constraints, otherwise it is called for conflict constraints.

It is the user's responsibility to return a valid result for CONSLOCK; see SCIP's documentation (e.g. type_cons.h).

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 625 of file gscip_constraint_handler.h.

◆ SeparateLp()

template<typename ConstraintData >
absl::StatusOr< GScipCallbackResult > operations_research::GScipConstraintHandler< ConstraintData >::SeparateLp ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data )
virtual

Callback function called at SCIP's CONSSEPALP. Separates all constraints of the constraint handler for LP solutions.

It is the user's responsibility to return a valid result for CONSSEPALP; see SCIP's documentation (e.g. type_cons.h).

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 637 of file gscip_constraint_handler.h.

◆ SeparateSolution()

template<typename ConstraintData >
absl::StatusOr< GScipCallbackResult > operations_research::GScipConstraintHandler< ConstraintData >::SeparateSolution ( GScipConstraintHandlerContext context,
const ConstraintData & constraint_data )
virtual

Callback function called at SCIP's CONSSEPASOL. Separates all constraints of the constraint handler for solutions that do not come from LP (e.g. relaxators and primal heuristics).

It is the user's responsibility to return a valid result for CONSSEPASOL; see SCIP's documentation (e.g. type_cons.h).

Reimplemented in operations_research::internal::UntypedGScipConstraintHandlerImpl< ConstraintData >.

Definition at line 645 of file gscip_constraint_handler.h.


The documentation for this class was generated from the following file: