1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the program and library */ 4 /* SCIP --- Solving Constraint Integer Programs */ 5 /* */ 6 /* Copyright (c) 2002-2023 Zuse Institute Berlin (ZIB) */ 7 /* */ 8 /* Licensed under the Apache License, Version 2.0 (the "License"); */ 9 /* you may not use this file except in compliance with the License. */ 10 /* You may obtain a copy of the License at */ 11 /* */ 12 /* http://www.apache.org/licenses/LICENSE-2.0 */ 13 /* */ 14 /* Unless required by applicable law or agreed to in writing, software */ 15 /* distributed under the License is distributed on an "AS IS" BASIS, */ 16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ 17 /* See the License for the specific language governing permissions and */ 18 /* limitations under the License. */ 19 /* */ 20 /* You should have received a copy of the Apache-2.0 license */ 21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */ 22 /* */ 23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 24 25 /**@file objconshdlr.h 26 * @brief C++ wrapper for constraint handlers 27 * @author Tobias Achterberg 28 */ 29 30 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/ 31 32 #ifndef __SCIP_OBJCONSHDLR_H__ 33 #define __SCIP_OBJCONSHDLR_H__ 34 35 36 #include <cassert> 37 #include <cstring> 38 #include <utility> 39 40 #include "scip/scip.h" 41 #include "objscip/objprobcloneable.h" 42 43 namespace scip 44 { 45 46 /** @brief C++ wrapper for constraint handlers 47 * 48 * This class defines the interface for constraint handlers implemented in C++. Note that there are pure virtual 49 * functions (these have to be implemented). These functions are: scip_trans(), scip_enfolp(), scip_enforelax(), 50 * scip_enfops(), scip_check(), and scip_lock(). 51 * 52 * - \ref CONS "Instructions for implementing a constraint handler" 53 * - \ref CONSHDLRS "List of available constraint handlers" 54 * - \ref type_cons.h "Corresponding C interface" 55 */ 56 class ObjConshdlr : public ObjProbCloneable 57 { 58 public: 59 /*lint --e{1540}*/ 60 61 /** SCIP data structure */ 62 SCIP* scip_; 63 64 /** name of the constraint handler */ 65 char* scip_name_; 66 67 /** description of the constraint handler */ 68 char* scip_desc_; 69 70 /** default separation priority of the constraint handler */ 71 const int scip_sepapriority_; 72 73 /** default enforcing priority of the constraint handler */ 74 const int scip_enfopriority_; 75 76 /** default checking priority of the constraint handler */ 77 const int scip_checkpriority_; 78 79 /** default separation frequency of the constraint handler */ 80 const int scip_sepafreq_; 81 82 /** default propagation frequency of the constraint handler */ 83 const int scip_propfreq_; 84 85 /** default frequency of the constraint handler for eager evaluations in separation, propagation and enforcement */ 86 const int scip_eagerfreq_; 87 88 /** maximal number of presolving rounds the constraint handler participates in (-1: no limit) */ 89 const int scip_maxprerounds_; 90 91 /** should separation method be delayed, if other separators found cuts? */ 92 const SCIP_Bool scip_delaysepa_; 93 94 /** should propagation method be delayed, if other propagators found reductions? */ 95 const SCIP_Bool scip_delayprop_; 96 97 /** should the constraint handler be skipped, if no constraints are available? */ 98 const SCIP_Bool scip_needscons_; 99 100 /** positions in the node solving loop where propagation method of constraint handler should be executed */ 101 const SCIP_PROPTIMING scip_proptiming_; 102 103 /**< timing mask of the constraint handler's presolving method */ 104 const SCIP_PRESOLTIMING scip_presoltiming_; 105 106 /** default constructor */ 107 ObjConshdlr( 108 SCIP* scip, /**< SCIP data structure */ 109 const char* name, /**< name of constraint handler */ 110 const char* desc, /**< description of constraint handler */ 111 int sepapriority, /**< priority of the constraint handler for separation */ 112 int enfopriority, /**< priority of the constraint handler for constraint enforcing */ 113 int checkpriority, /**< priority of the constraint handler for checking infeasibility (and propagation) */ 114 int sepafreq, /**< frequency for separating cuts; zero means to separate only in the root node */ 115 int propfreq, /**< frequency for propagating domains; zero means only preprocessing propagation */ 116 int eagerfreq, /**< frequency for using all instead of only the useful constraints in separation, 117 * propagation and enforcement, -1 for no eager evaluations, 0 for first only */ 118 int maxprerounds, /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */ 119 SCIP_Bool delaysepa, /**< should separation method be delayed, if other separators found cuts? */ 120 SCIP_Bool delayprop, /**< should propagation method be delayed, if other propagators found reductions? */ 121 SCIP_Bool needscons, /**< should the constraint handler be skipped, if no constraints are available? */ 122 SCIP_PROPTIMING proptiming, /**< positions in the node solving loop where propagation method of constraint handlers should be executed */ 123 SCIP_PRESOLTIMING presoltiming /**< timing mask of the constraint handler's presolving method */ 124 ) 125 : scip_(scip), 126 scip_name_(0), 127 scip_desc_(0), 128 scip_sepapriority_(sepapriority), 129 scip_enfopriority_(enfopriority), 130 scip_checkpriority_(checkpriority), 131 scip_sepafreq_(sepafreq), 132 scip_propfreq_(propfreq), 133 scip_eagerfreq_(eagerfreq), 134 scip_maxprerounds_(maxprerounds), 135 scip_delaysepa_(delaysepa), 136 scip_delayprop_(delayprop), 137 scip_needscons_(needscons), 138 scip_proptiming_(proptiming), 139 scip_presoltiming_(presoltiming) 140 { 141 /* the macro SCIPduplicateMemoryArray does not need the first argument: */ 142 SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_name_, name, std::strlen(name)+1) ); 143 SCIP_CALL_ABORT( SCIPduplicateMemoryArray(scip_, &scip_desc_, desc, std::strlen(desc)+1) ); 144 } 145 146 /** copy constructor */ 147 ObjConshdlr(const ObjConshdlr& o) 148 : ObjConshdlr(o.scip_, o.scip_name_, o.scip_desc_, o.scip_sepapriority_, o.scip_enfopriority_, 149 o.scip_checkpriority_, o.scip_sepafreq_, o.scip_propfreq_, o.scip_eagerfreq_, o.scip_maxprerounds_, 150 o.scip_delaysepa_, o.scip_delayprop_, o.scip_needscons_, o.scip_proptiming_, o.scip_presoltiming_) 151 { 152 } 153 154 /** move constructor */ 155 ObjConshdlr(ObjConshdlr&& o) 156 : scip_(o.scip_), 157 scip_name_(0), 158 scip_desc_(0), 159 scip_sepapriority_(o.scip_sepapriority_), 160 scip_enfopriority_(o.scip_enfopriority_), 161 scip_checkpriority_(o.scip_checkpriority_), 162 scip_sepafreq_(o.scip_sepafreq_), 163 scip_propfreq_(o.scip_propfreq_), 164 scip_eagerfreq_(o.scip_eagerfreq_), 165 scip_maxprerounds_(o.scip_maxprerounds_), 166 scip_delaysepa_(o.scip_delaysepa_), 167 scip_delayprop_(o.scip_delayprop_), 168 scip_needscons_(o.scip_needscons_), 169 scip_proptiming_(o.scip_proptiming_), 170 scip_presoltiming_(o.scip_presoltiming_) 171 { 172 std::swap(scip_name_, o.scip_name_); 173 std::swap(scip_desc_, o.scip_desc_); 174 } 175 176 /** destructor */ 177 virtual ~ObjConshdlr() 178 { 179 /* the macro SCIPfreeMemoryArray does not need the first argument: */ 180 /*lint --e{64}*/ 181 SCIPfreeMemoryArray(scip_, &scip_name_); 182 SCIPfreeMemoryArray(scip_, &scip_desc_); 183 } 184 185 /** assignment of polymorphic classes causes slicing and is therefore disabled. */ 186 ObjConshdlr& operator=(const ObjConshdlr& o) = delete; 187 188 /** assignment of polymorphic classes causes slicing and is therefore disabled. */ 189 ObjConshdlr& operator=(ObjConshdlr&& o) = delete; 190 191 /** destructor of constraint handler to free user data (called when SCIP is exiting) 192 * 193 * @see SCIP_DECL_CONSFREE(x) in @ref type_cons.h 194 */ 195 virtual SCIP_DECL_CONSFREE(scip_free) 196 { /*lint --e{715}*/ 197 return SCIP_OKAY; 198 } 199 200 /** initialization method of constraint handler (called after problem has been transformed) 201 * 202 * @see SCIP_DECL_CONSINIT(x) in @ref type_cons.h 203 */ 204 virtual SCIP_DECL_CONSINIT(scip_init) 205 { /*lint --e{715}*/ 206 return SCIP_OKAY; 207 } 208 209 /** deinitialization method of constraint handler (called before transformed problem is freed) 210 * 211 * @see SCIP_DECL_CONSEXIT(x) in @ref type_cons.h 212 */ 213 virtual SCIP_DECL_CONSEXIT(scip_exit) 214 { /*lint --e{715}*/ 215 return SCIP_OKAY; 216 } 217 218 /** presolving initialization method of constraint handler (called when presolving is about to begin) 219 * 220 * @see SCIP_DECL_CONSINITPRE(x) in @ref type_cons.h 221 */ 222 virtual SCIP_DECL_CONSINITPRE(scip_initpre) 223 { /*lint --e{715}*/ 224 return SCIP_OKAY; 225 } 226 227 /** presolving deinitialization method of constraint handler (called after presolving has been finished) 228 * 229 * @see SCIP_DECL_CONSEXITPRE(x) in @ref type_cons.h 230 */ 231 virtual SCIP_DECL_CONSEXITPRE(scip_exitpre) 232 { /*lint --e{715}*/ 233 return SCIP_OKAY; 234 } 235 236 /** solving process initialization method of constraint handler (called when branch and bound process is about to begin) 237 * 238 * @see SCIP_DECL_CONSINITSOL(x) in @ref type_cons.h 239 */ 240 virtual SCIP_DECL_CONSINITSOL(scip_initsol) 241 { /*lint --e{715}*/ 242 return SCIP_OKAY; 243 } 244 245 /** solving process deinitialization method of constraint handler (called before branch and bound process data is freed) 246 * 247 * @see SCIP_DECL_CONSEXITSOL(x) in @ref type_cons.h 248 */ 249 virtual SCIP_DECL_CONSEXITSOL(scip_exitsol) 250 { /*lint --e{715}*/ 251 return SCIP_OKAY; 252 } 253 254 /** frees specific constraint data 255 * 256 * @see SCIP_DECL_CONSDELETE(x) in @ref type_cons.h 257 */ 258 virtual SCIP_DECL_CONSDELETE(scip_delete) 259 { /*lint --e{715}*/ 260 return SCIP_OKAY; 261 } 262 263 /** transforms constraint data into data belonging to the transformed problem 264 * 265 * @see SCIP_DECL_CONSTRANS(x) in @ref type_cons.h 266 */ 267 virtual SCIP_DECL_CONSTRANS(scip_trans) = 0; 268 269 /** LP initialization method of constraint handler (called before the initial LP relaxation at a node is solved) 270 * 271 * @see SCIP_DECL_CONSINITLP(x) in @ref type_cons.h 272 */ 273 virtual SCIP_DECL_CONSINITLP(scip_initlp) 274 { /*lint --e{715}*/ 275 return SCIP_OKAY; 276 } 277 278 /** separation method of constraint handler for LP solution 279 * 280 * @see SCIP_DECL_CONSSEPALP(x) in @ref type_cons.h 281 */ 282 virtual SCIP_DECL_CONSSEPALP(scip_sepalp) 283 { /*lint --e{715}*/ 284 assert(result != NULL); 285 *result = SCIP_DIDNOTRUN; 286 return SCIP_OKAY; 287 } 288 289 /** separation method of constraint handler for arbitrary primal solution 290 * 291 * @see SCIP_DECL_CONSSEPASOL(x) in @ref type_cons.h 292 */ 293 virtual SCIP_DECL_CONSSEPASOL(scip_sepasol) 294 { /*lint --e{715}*/ 295 assert(result != NULL); 296 *result = SCIP_DIDNOTRUN; 297 return SCIP_OKAY; 298 } 299 300 /** constraint enforcing method of constraint handler for LP solutions 301 * 302 * @see SCIP_DECL_CONSENFOLP(x) in @ref type_cons.h 303 */ 304 virtual SCIP_DECL_CONSENFOLP(scip_enfolp) = 0; 305 306 /** constraint enforcing method of constraint handler for relaxation solutions 307 * 308 * @see SCIP_DECL_CONSENFORELAX(x) in @ref type_cons.h 309 */ 310 virtual SCIP_DECL_CONSENFORELAX(scip_enforelax) 311 { /*lint --e{715}*/ 312 assert(result != NULL); 313 *result = SCIP_DIDNOTRUN; 314 return SCIP_OKAY; 315 } 316 317 /** constraint enforcing method of constraint handler for pseudo solutions 318 * 319 * @see SCIP_DECL_CONSENFOPS(x) in @ref type_cons.h 320 */ 321 virtual SCIP_DECL_CONSENFOPS(scip_enfops) = 0; 322 323 /** feasibility check method of constraint handler for primal solutions 324 * 325 * @see SCIP_DECL_CONSCHECK(x) in @ref type_cons.h 326 */ 327 virtual SCIP_DECL_CONSCHECK(scip_check) = 0; 328 329 /** domain propagation method of constraint handler 330 * 331 * @see SCIP_DECL_CONSPROP(x) in @ref type_cons.h 332 */ 333 virtual SCIP_DECL_CONSPROP(scip_prop) 334 { /*lint --e{715}*/ 335 assert(result != NULL); 336 *result = SCIP_DIDNOTRUN; 337 return SCIP_OKAY; 338 } 339 340 /** presolving method of constraint handler 341 * 342 * @see SCIP_DECL_CONSPRESOL(x) in @ref type_cons.h 343 */ 344 virtual SCIP_DECL_CONSPRESOL(scip_presol) 345 { /*lint --e{715}*/ 346 assert(result != NULL); 347 *result = SCIP_DIDNOTRUN; 348 return SCIP_OKAY; 349 } 350 351 /** propagation conflict resolving method of constraint handler 352 * 353 * @see SCIP_DECL_CONSRESPROP(x) in @ref type_cons.h 354 */ 355 virtual SCIP_DECL_CONSRESPROP(scip_resprop) 356 { /*lint --e{715}*/ 357 assert(result != NULL); 358 *result = SCIP_DIDNOTFIND; 359 return SCIP_OKAY; 360 } 361 362 /** variable rounding lock method of constraint handler 363 * 364 * @see SCIP_DECL_CONSLOCK(x) in @ref type_cons.h 365 */ 366 virtual SCIP_DECL_CONSLOCK(scip_lock) = 0; 367 368 /** constraint activation notification method of constraint handler 369 * 370 * @see SCIP_DECL_CONSACTIVE(x) in @ref type_cons.h 371 */ 372 virtual SCIP_DECL_CONSACTIVE(scip_active) 373 { /*lint --e{715}*/ 374 return SCIP_OKAY; 375 } 376 377 /** constraint deactivation notification method of constraint handler 378 * 379 * @see SCIP_DECL_CONSDEACTIVE(x) in @ref type_cons.h 380 */ 381 virtual SCIP_DECL_CONSDEACTIVE(scip_deactive) 382 { /*lint --e{715}*/ 383 return SCIP_OKAY; 384 } 385 386 /** constraint enabling notification method of constraint handler 387 * 388 * @see SCIP_DECL_CONSENABLE(x) in @ref type_cons.h 389 */ 390 virtual SCIP_DECL_CONSENABLE(scip_enable) 391 { /*lint --e{715}*/ 392 return SCIP_OKAY; 393 } 394 395 /** constraint disabling notification method of constraint handler 396 * 397 * @see SCIP_DECL_CONSDISABLE(x) in @ref type_cons.h 398 */ 399 virtual SCIP_DECL_CONSDISABLE(scip_disable) 400 { /*lint --e{715}*/ 401 return SCIP_OKAY; 402 } 403 404 /** variable deletion method of constraint handler 405 * 406 * @see SCIP_DECL_CONSDELVARS(x) in @ref type_cons.h 407 */ 408 virtual SCIP_DECL_CONSDELVARS(scip_delvars) 409 { /*lint --e{715}*/ 410 return SCIP_OKAY; 411 } 412 413 /** constraint display method of constraint handler 414 * 415 * @see SCIP_DECL_CONSPRINT(x) in @ref type_cons.h 416 */ 417 virtual SCIP_DECL_CONSPRINT(scip_print) 418 { /*lint --e{715}*/ 419 if ( file == NULL ) 420 fprintf(stdout, "constraint handler <%s> does not support printing constraints\n", SCIPconshdlrGetName(conshdlr)); 421 else 422 fprintf(file, "constraint handler <%s> does not support printing constraints\n", SCIPconshdlrGetName(conshdlr)); 423 return SCIP_OKAY; 424 } 425 426 /** constraint copying method of constraint handler 427 * 428 * @see SCIP_DECL_CONSCOPY(x) in @ref type_cons.h 429 */ 430 virtual SCIP_DECL_CONSCOPY(scip_copy) 431 { /*lint --e{715}*/ 432 assert(valid != NULL); 433 *valid = FALSE; 434 return SCIP_OKAY; 435 } 436 437 /** constraint parsing method of constraint handler 438 * 439 * @see SCIP_DECL_CONSPARSE(x) in @ref type_cons.h 440 */ 441 virtual SCIP_DECL_CONSPARSE(scip_parse) 442 { /*lint --e{715}*/ 443 return SCIP_OKAY; 444 } 445 446 /** constraint method of constraint handler which returns the variables (if possible) 447 * 448 * @see SCIP_DECL_CONSGETVARS(x) in @ref type_cons.h 449 */ 450 virtual SCIP_DECL_CONSGETVARS(scip_getvars) 451 { /*lint --e{715}*/ 452 assert(success != NULL); 453 *success = FALSE; 454 return SCIP_OKAY; 455 } 456 457 /** constraint method of constraint handler which returns the number of variables (if possible) 458 * 459 * @see SCIP_DECL_CONSGETNVARS(x) in @ref type_cons.h 460 */ 461 virtual SCIP_DECL_CONSGETNVARS(scip_getnvars) 462 { /*lint --e{715}*/ 463 assert(nvars != NULL); 464 assert(success != NULL); 465 466 *nvars = 0; 467 *success = FALSE; 468 469 return SCIP_OKAY; 470 } 471 472 /** constraint handler method to suggest dive bound changes during the generic diving algorithm 473 * 474 * @see SCIP_DECL_CONSGETDIVEBDCHGS(x) in @ref type_cons.h 475 */ 476 virtual SCIP_DECL_CONSGETDIVEBDCHGS(scip_getdivebdchgs) 477 { /*lint --e{715}*/ 478 assert(success != NULL); 479 *success = FALSE; 480 return SCIP_OKAY; 481 } 482 483 /** constraint handler method which returns the permutation symmetry detection graph of a constraint (if possible) 484 * 485 * @see SCIP_DECL_CONSGETPERMSYMGRAPH(x) in @ref type_cons.h 486 */ 487 virtual SCIP_DECL_CONSGETPERMSYMGRAPH(scip_getpermsymgraph) 488 { /*lint --e{715}*/ 489 assert(success != NULL); 490 *success = FALSE; 491 return SCIP_OKAY; 492 } 493 494 /** constraint handler method which returns the signed permutation symmetry detection graph of a constraint (if possible) 495 * 496 * @see SCIP_DECL_CONSGETSIGNEDPERMSYMGRAPH(x) in @ref type_cons.h 497 */ 498 virtual SCIP_DECL_CONSGETSIGNEDPERMSYMGRAPH(scip_getsignedpermsymgraph) 499 { /*lint --e{715}*/ 500 assert(success != NULL); 501 *success = FALSE; 502 return SCIP_OKAY; 503 } 504 }; 505 506 } /* namespace scip */ 507 508 509 510 /** creates the constraint handler for the given constraint handler object and includes it in SCIP 511 * 512 * The method should be called in one of the following ways: 513 * 514 * 1. The user is resposible of deleting the object: 515 * SCIP_CALL( SCIPcreate(&scip) ); 516 * ... 517 * MyConshdlr* myconshdlr = new MyConshdlr(...); 518 * SCIP_CALL( SCIPincludeObjConshdlr(scip, &myconshdlr, FALSE) ); 519 * ... 520 * SCIP_CALL( SCIPfree(&scip) ); 521 * delete myconshdlr; // delete conshdlr AFTER SCIPfree() ! 522 * 523 * 2. The object pointer is passed to SCIP and deleted by SCIP in the SCIPfree() call: 524 * SCIP_CALL( SCIPcreate(&scip) ); 525 * ... 526 * SCIP_CALL( SCIPincludeObjConshdlr(scip, new MyConshdlr(...), TRUE) ); 527 * ... 528 * SCIP_CALL( SCIPfree(&scip) ); // destructor of MyConshdlr is called here 529 */ 530 SCIP_EXPORT 531 SCIP_RETCODE SCIPincludeObjConshdlr( 532 SCIP* scip, /**< SCIP data structure */ 533 scip::ObjConshdlr* objconshdlr, /**< constraint handler object */ 534 SCIP_Bool deleteobject /**< should the constraint handler object be deleted when conshdlr is freed? */ 535 ); 536 537 /** returns the conshdlr object of the given name, or 0 if not existing */ 538 SCIP_EXPORT 539 scip::ObjConshdlr* SCIPfindObjConshdlr( 540 SCIP* scip, /**< SCIP data structure */ 541 const char* name /**< name of constraint handler */ 542 ); 543 544 /** returns the conshdlr object for the given constraint handler */ 545 SCIP_EXPORT 546 scip::ObjConshdlr* SCIPgetObjConshdlr( 547 SCIP* scip, /**< SCIP data structure */ 548 SCIP_CONSHDLR* conshdlr /**< constraint handler */ 549 ); 550 551 #endif 552