Scippy

SCIP

Solving Constraint Integer Programs

pub_misc_rowprep.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file pub_misc_rowprep.h
17  * @brief preparation of a linear inequality to become a SCIP_ROW
18  * @ingroup PUBLICCOREAPI
19  * @author Stefan Vigerske
20  * @author Benjamin Mueller
21  * @author Felipe Serrano
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #ifndef __SCIP_PUB_MISC_ROWPREP_H__
27 #define __SCIP_PUB_MISC_ROWPREP_H__
28 
29 #include "scip/type_misc.h"
30 #include "scip/type_cons.h"
31 #include "scip/type_lp.h"
32 #include "scip/type_sepa.h"
33 #include "scip/type_var.h"
34 
35 #ifdef NDEBUG
36 #include "scip/struct_misc.h"
37 #endif
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**@defgroup RowPrep Linear Inequality
44  * @ingroup DataStructures
45  * @brief a linear inequality that is prepared to become a SCIP_ROW
46  *
47  * This data structure helps to work around some limitations of SCIP_ROW's, in particular,
48  * that it rounds coefficients or sides close to integral values without the appropriate care.
49  * On the other hand, a SCIP_ROWPREP is limited to inequalities.
50  *
51  *@{
52  */
53 
54 /** creates a SCIP_ROWPREP datastructure
55  *
56  * Initial row represents 0 ≤ 0.
57  */
58 SCIP_EXPORT
60  SCIP* scip, /**< SCIP data structure */
61  SCIP_ROWPREP** rowprep, /**< buffer to store pointer to rowprep */
62  SCIP_SIDETYPE sidetype, /**< whether cut will be or lower-equal or larger-equal type */
63  SCIP_Bool local /**< whether cut will be valid only locally */
64  );
65 
66 /** frees a SCIP_ROWPREP datastructure */
67 SCIP_EXPORT
68 void SCIPfreeRowprep(
69  SCIP* scip, /**< SCIP data structure */
70  SCIP_ROWPREP** rowprep /**< pointer that stores pointer to rowprep */
71  );
72 
73 /** creates a copy of a SCIP_ROWPREP datastructure */
74 SCIP_EXPORT
76  SCIP* scip, /**< SCIP data structure */
77  SCIP_ROWPREP** target, /**< buffer to store pointer of rowprep copy */
78  SCIP_ROWPREP* source /**< rowprep to copy */
79  );
80 
81 /** gives number of terms in rowprep */
82 SCIP_EXPORT
84  SCIP_ROWPREP* rowprep /**< rowprep */
85  );
86 
87 /** gives variables of rowprep (feel free to modify) */
88 SCIP_EXPORT
90  SCIP_ROWPREP* rowprep /**< rowprep */
91  );
92 
93 /** gives coefficients of rowprep (feel free to modify) */
94 SCIP_EXPORT
96  SCIP_ROWPREP* rowprep /**< rowprep */
97  );
98 
99 /** gives side of rowprep */
100 SCIP_EXPORT
102  SCIP_ROWPREP* rowprep /**< rowprep */
103  );
104 
105 /** gives kind of inequality of rowprep */
106 SCIP_EXPORT
108  SCIP_ROWPREP* rowprep /**< rowprep */
109  );
110 
111 /** returns whether rowprep is locally valid only */
112 SCIP_EXPORT
114  SCIP_ROWPREP* rowprep /**< rowprep */
115  );
116 
117 /** returns name of rowprep (feel free to modify) */
118 SCIP_EXPORT
119 char* SCIProwprepGetName(
120  SCIP_ROWPREP* rowprep /**< rowprep */
121  );
122 
123 /** returns number of variables which coefficients were modified in cleanup */
124 SCIP_EXPORT
126  SCIP_ROWPREP* rowprep /**< rowprep */
127  );
128 
129 /** returns variables which coefficients were modified in cleanup */
130 SCIP_EXPORT
132  SCIP_ROWPREP* rowprep /**< rowprep */
133  );
134 
135 /** resets rowprep to have 0 terms and side 0.0 */
136 SCIP_EXPORT
137 void SCIProwprepReset(
138  SCIP_ROWPREP* rowprep /**< rowprep */
139  );
140 
141 /** adds constant value to side of rowprep */
142 SCIP_EXPORT
143 void SCIProwprepAddSide(
144  SCIP_ROWPREP* rowprep, /**< rowprep */
145  SCIP_Real side /**< constant value to be added to side */
146  );
147 
148 /** adds constant term to rowprep
149  *
150  * Substracts constant from side.
151  */
152 SCIP_EXPORT
154  SCIP_ROWPREP* rowprep, /**< rowprep */
155  SCIP_Real constant /**< constant value to be added */
156  );
157 
158 /** sets side type of rowprep */
159 SCIP_EXPORT
161  SCIP_ROWPREP* rowprep, /**< rowprep */
162  SCIP_SIDETYPE sidetype /**< new side type */
163  );
164 
165 /** sets whether rowprep is local */
166 SCIP_EXPORT
168  SCIP_ROWPREP* rowprep, /**< rowprep */
169  SCIP_Bool islocal /**< whether rowprep is local */
170  );
171 
172 /** enables recording for where modifications were done in cleanup */
173 SCIP_EXPORT
175  SCIP_ROWPREP* rowprep /**< rowprep */
176  );
177 
178 #ifdef NDEBUG
179 /* If NDEBUG is defined, the function calls are overwritten by defines to reduce the number of function calls and
180  * speed up the algorithms.
181  */
182 #define SCIProwprepGetNVars(rowprep) (rowprep)->nvars
183 #define SCIProwprepGetVars(rowprep) (rowprep)->vars
184 #define SCIProwprepGetCoefs(rowprep) (rowprep)->coefs
185 #define SCIProwprepGetSide(rowprep) (rowprep)->side
186 #define SCIProwprepGetSidetype(rowprep) (rowprep)->sidetype
187 #define SCIProwprepIsLocal(rowprep) (rowprep)->local
188 #define SCIProwprepGetName(rowprep) (rowprep)->name
189 #define SCIProwprepGetNModifiedVars(rowprep) (rowprep)->nmodifiedvars
190 #define SCIProwprepGetModifiedVars(rowprep) (rowprep)->modifiedvars
191 #define SCIProwprepAddSide(rowprep, sideadd) ((rowprep)->side += (sideadd))
192 #define SCIProwprepAddConstant(rowprep, constant) SCIProwprepAddSide(rowprep, -(constant))
193 #define SCIProwprepSetSidetype(rowprep, sidetype_) (rowprep)->sidetype = sidetype_
194 #define SCIProwprepSetLocal(rowprep, islocal) (rowprep)->local = islocal
195 #define SCIProwprepRecordModifications(rowprep) (rowprep)->recordmodifications = TRUE
196 #endif
197 
198 /** prints a rowprep */
199 SCIP_EXPORT
200 void SCIPprintRowprep(
201  SCIP* scip, /**< SCIP data structure */
202  SCIP_ROWPREP* rowprep, /**< rowprep to be printed */
203  FILE* file /**< file to print to, or NULL for stdout */
204  );
205 
206 /** prints a rowprep and values in solution */
207 SCIP_EXPORT
209  SCIP* scip, /**< SCIP data structure */
210  SCIP_ROWPREP* rowprep, /**< rowprep to be printed */
211  SCIP_SOL* sol, /**< solution for activity */
212  FILE* file /**< file to print to, or NULL for stdout */
213  );
214 
215 /** ensures that rowprep has space for at least given number of additional terms
216  *
217  * Useful when knowing in advance how many terms will be added.
218  */
219 SCIP_EXPORT
221  SCIP* scip, /**< SCIP data structure */
222  SCIP_ROWPREP* rowprep, /**< rowprep */
223  int size /**< number of additional terms for which to alloc space in rowprep */
224  );
225 
226 /** adds a term coef*var to a rowprep */
227 SCIP_EXPORT
229  SCIP* scip, /**< SCIP data structure */
230  SCIP_ROWPREP* rowprep, /**< rowprep */
231  SCIP_VAR* var, /**< variable to add */
232  SCIP_Real coef /**< coefficient to add */
233  );
234 
235 /** adds several terms coef*var to a rowprep */
236 SCIP_EXPORT
238  SCIP* scip, /**< SCIP data structure */
239  SCIP_ROWPREP* rowprep, /**< rowprep */
240  int nvars, /**< number of terms to add */
241  SCIP_VAR** vars, /**< variables to add */
242  SCIP_Real* coefs /**< coefficients to add */
243  );
244 
245 /** computes violation of rowprep in a given solution
246  *
247  * Can return whether the violation value is reliable from a floating-point accuracy point of view.
248  * The value will not be deemed reliable when its calculation involved the subtraction of large numbers.
249  * To be precise, the violation of an inequality \f$ \sum_i a_ix_i \leq b \f$ in a solution \f$x^*\f$ is deemed
250  * reliable if \f$ |\sum_i a_ix^*_i - b| \geq 2^{-50} \max (|b|, \max_i |a_ix^*_i|) \f$.
251  */
252 SCIP_EXPORT
254  SCIP* scip, /**< SCIP data structure */
255  SCIP_ROWPREP* rowprep, /**< rowprep */
256  SCIP_SOL* sol, /**< solution or NULL for LP solution */
257  SCIP_Bool* reliable /**< buffer to store whether computed violation is reliable (numerically), or NULL if not of interest */
258  );
259 
260 /** computes violation of rowprep in a given solution and reports whether that value seem numerically reliable
261  *
262  * @see SCIPgetRowprepViolation()
263  */
264 SCIP_EXPORT
266  SCIP* scip, /**< SCIP data structure */
267  SCIP_ROWPREP* rowprep, /**< rowprep */
268  SCIP_SOL* sol /**< solution or NULL for LP solution */
269  );
270 
271 /** Merge terms that use same variable and eliminate zero coefficients.
272  *
273  * Removes a variable if its bounds have a relative difference of below epsilon.
274  * Local bounds are checked for local rows, otherwise global bounds are used.
275  * If the bounds are not absolute equal, the bound that relaxes the row is used.
276  *
277  * Terms are sorted by variable (see SCIPvarComp()) after return.
278  */
279 SCIP_EXPORT
281  SCIP* scip, /**< SCIP data structure */
282  SCIP_ROWPREP* rowprep /**< rowprep to be cleaned up */
283  );
284 
285 /** Cleans up and attempts to improve rowprep
286  *
287  * Drops small or large coefficients if their ratio is beyond separating/maxcoefratiofacrowprep / numerics/feastol,
288  * if this can be done by relaxing the row.
289  * Scales coefficients up to reach minimal violation, if possible.
290  * Scaling is omitted if violation is very small (\ref ROWPREP_SCALEUP_VIOLNONZERO) or
291  * maximal coefficient would become huge (\ref ROWPREP_SCALEUP_MAXMAXCOEF).
292  * Scales coefficients and side down if they are large and if the minimal violation is still reached.
293  * Rounds coefficients close to integral values to integrals, if this can be done by relaxing the row.
294  * Rounds side within epsilon of 0 to 0.0 or +/-1.1*epsilon, whichever relaxes the row least.
295  *
296  * After return, the terms in the rowprep will be sorted by absolute value of coefficient, in decreasing order.
297  * Thus, the coefratio can be obtained via `REALABS(rowprep->coefs[0]) / REALABS(rowprep->coefs[rowprep->nvars-1])` (if nvars>0).
298  *
299  * `success` is set to TRUE if and only if the rowprep satisfies the following:
300  * - the coefratio is below separating/maxcoefratiofacrowprep / numerics/feastol
301  * - the violation is at least `minviol`
302  * - the violation is reliable or `minviol` = 0
303  * - the absolute value of coefficients are below SCIPinfinity()
304  * - the absolute value of the side is below SCIPinfinity()
305  */
306 SCIP_EXPORT
308  SCIP* scip, /**< SCIP data structure */
309  SCIP_ROWPREP* rowprep, /**< rowprep to be cleaned */
310  SCIP_SOL* sol, /**< solution that we try to cut off, or NULL for LP solution */
311  SCIP_Real minviol, /**< minimal absolute violation the row should achieve (w.r.t. sol) */
312  SCIP_Real* viol, /**< buffer to store absolute violation of cleaned up cut in sol, or NULL if not of interest */
313  SCIP_Bool* success /**< buffer to store whether cut cleanup was successful, or NULL if not of interest */
314  );
315 
316 /** Cleans up and attempts to improve rowprep without regard for violation
317  *
318  * Drops small or large coefficients if their ratio is beyond separating/maxcoefratiofacrowprep / numerics/feastol,
319  * if this can be done by relaxing the row.
320  * Scales coefficients and side to have maximal coefficient in `[1/maxcoefbound,maxcoefbound]`.
321  * Rounds coefficients close to integral values to integrals, if this can be done by relaxing the row.
322  * Rounds side within epsilon of 0 to 0.0 or +/-1.1*epsilon, whichever relaxes the row least.
323  *
324  * After return, the terms in the rowprep will be sorted by absolute value of coefficient, in decreasing order.
325  * Thus, the coefratio can be obtained via `REALABS(rowprep->coefs[0]) / REALABS(rowprep->coefs[rowprep->nvars-1])` (if nvars>0).
326  *
327  * `success` is set to TRUE if and only if the rowprep satisfies the following:
328  * - the coefratio is below separating/maxcoefratiofacrowprep / numerics/feastol
329  * - the absolute value of coefficients are below SCIPinfinity()
330  * - the absolute value of the side is below SCIPinfinity()
331  *
332  * In difference to SCIPcleanupRowprep(), this function does not scale up the row to increase the absolute violation.
333  */
334 SCIP_EXPORT
336  SCIP* scip, /**< SCIP data structure */
337  SCIP_ROWPREP* rowprep, /**< rowprep to be cleaned */
338  SCIP_SOL* sol, /**< solution that we try to cut off, or NULL for LP solution */
339  SCIP_Real maxcoefbound, /**< bound on absolute value of largest coefficient */
340  SCIP_Bool* success /**< buffer to store whether cut cleanup was successful, or NULL if not of interest */
341  );
342 
343 /** Scales up a rowprep to increase coefficients/sides that are within epsilon to an integer value, if possible.
344  *
345  * Computes the minimal fractionality of all fractional coefficients and the side of the rowprep.
346  * If this fractionality is below epsilon, the rowprep is scaled up such that the fractionality exceeds epsilon,
347  * if this will not put any coefficient or side above SCIPhugeValue().
348  *
349  * This function does not relax the rowprep.
350  *
351  * `success` is set to TRUE if the resulting rowprep can be turned into a SCIP_ROW, that is,
352  * all coefs and the side is below SCIPinfinity() and fractionalities are above epsilon.
353  * If `success` is set to FALSE, then the rowprep will not have been modified.
354  *
355  * @return The applied scaling factor, if `success` is set to TRUE.
356  */
357 SCIP_EXPORT
359  SCIP* scip, /**< SCIP data structure */
360  SCIP_ROWPREP* rowprep, /**< rowprep to be cleaned */
361  SCIP_Real minscaleup, /**< minimal factor by which to scale up row, or <= 1.0 if to be ignored */
362  SCIP_Bool* success /**< buffer to store whether rowprep could be turned into SCIP_ROW without loss, or NULL if not of interest */
363  );
364 
365 /** scales a rowprep by given factor (after some rounding)
366  *
367  * @return Exponent of actually applied scaling factor, if written as \f$2^x\f$.
368  */
369 SCIP_EXPORT
370 int SCIPscaleRowprep(
371  SCIP_ROWPREP* rowprep, /**< rowprep to be scaled */
372  SCIP_Real factor /**< suggested scale factor */
373  );
374 
375 /** generates a SCIP_ROW from a rowprep, setting its origin to given constraint handler */
376 SCIP_EXPORT
378  SCIP* scip, /**< SCIP data structure */
379  SCIP_ROW** row, /**< buffer to store pointer to new row */
380  SCIP_ROWPREP* rowprep, /**< rowprep to be turned into a row */
381  SCIP_CONSHDLR* conshdlr /**< constraint handler */
382  );
383 
384 /** generates a SCIP_ROW from a rowprep, setting its origin to given constraint */
385 SCIP_EXPORT
387  SCIP* scip, /**< SCIP data structure */
388  SCIP_ROW** row, /**< buffer to store pointer to new row */
389  SCIP_ROWPREP* rowprep, /**< rowprep to be turned into a row */
390  SCIP_CONS* cons /**< constraint */
391  );
392 
393 /** generates a SCIP_ROW from a rowprep, setting its origin to given separator */
394 SCIP_EXPORT
396  SCIP* scip, /**< SCIP data structure */
397  SCIP_ROW** row, /**< buffer to store pointer to new row */
398  SCIP_ROWPREP* rowprep, /**< rowprep to be turned into a row */
399  SCIP_SEPA* sepa /**< separator */
400  );
401 
402 /** @} */
403 
404 #ifdef __cplusplus
405 }
406 #endif
407 
408 #endif /* __SCIP_PUB_MISC_ROWPREP_H__ */
void SCIProwprepRecordModifications(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:759
type definitions for miscellaneous datastructures
SCIP_RETCODE SCIPcleanupRowprep2(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_SOL *sol, SCIP_Real maxcoefbound, SCIP_Bool *success)
void SCIPprintRowprep(SCIP *scip, SCIP_ROWPREP *rowprep, FILE *file)
Definition: misc_rowprep.c:769
SCIP_RETCODE SCIPcopyRowprep(SCIP *scip, SCIP_ROWPREP **target, SCIP_ROWPREP *source)
Definition: misc_rowprep.c:574
SCIP_RETCODE SCIPgetRowprepRowSepa(SCIP *scip, SCIP_ROW **row, SCIP_ROWPREP *rowprep, SCIP_SEPA *sepa)
char * SCIProwprepGetName(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:664
miscellaneous datastructures
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_SIDETYPE SCIProwprepGetSidetype(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:644
SCIP_RETCODE SCIPensureRowprepSize(SCIP *scip, SCIP_ROWPREP *rowprep, int size)
Definition: misc_rowprep.c:855
SCIP_RETCODE SCIPgetRowprepRowCons(SCIP *scip, SCIP_ROW **row, SCIP_ROWPREP *rowprep, SCIP_CONS *cons)
type definitions for LP management
SCIP_Real * SCIProwprepGetCoefs(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:624
int SCIProwprepGetNVars(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:604
SCIP_Bool SCIProwprepIsLocal(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:654
SCIP_Real SCIPscaleupRowprep(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_Real minscaleup, SCIP_Bool *success)
void SCIProwprepSetSidetype(SCIP_ROWPREP *rowprep, SCIP_SIDETYPE sidetype)
Definition: misc_rowprep.c:737
SCIP_Bool SCIPisRowprepViolationReliable(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_SOL *sol)
void SCIProwprepReset(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:694
SCIP_RETCODE SCIPgetRowprepRowConshdlr(SCIP *scip, SCIP_ROW **row, SCIP_ROWPREP *rowprep, SCIP_CONSHDLR *conshdlr)
void SCIPfreeRowprep(SCIP *scip, SCIP_ROWPREP **rowprep)
Definition: misc_rowprep.c:558
void SCIProwprepAddSide(SCIP_ROWPREP *rowprep, SCIP_Real side)
Definition: misc_rowprep.c:714
type definitions for problem variables
SCIP_Real SCIPgetRowprepViolation(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_SOL *sol, SCIP_Bool *reliable)
Definition: misc_rowprep.c:940
SCIP_VAR ** SCIProwprepGetModifiedVars(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:684
int SCIProwprepGetNModifiedVars(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:674
#define SCIP_Bool
Definition: def.h:84
void SCIProwprepAddConstant(SCIP_ROWPREP *rowprep, SCIP_Real constant)
Definition: misc_rowprep.c:728
void SCIPmergeRowprepTerms(SCIP *scip, SCIP_ROWPREP *rowprep)
int SCIPscaleRowprep(SCIP_ROWPREP *rowprep, SCIP_Real factor)
void SCIPprintRowprepSol(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_SOL *sol, FILE *file)
Definition: misc_rowprep.c:794
SCIP_Real SCIProwprepGetSide(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:634
type definitions for separators
#define SCIP_Real
Definition: def.h:177
SCIP_RETCODE SCIPcreateRowprep(SCIP *scip, SCIP_ROWPREP **rowprep, SCIP_SIDETYPE sidetype, SCIP_Bool local)
Definition: misc_rowprep.c:538
SCIP_RETCODE SCIPaddRowprepTerm(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_VAR *var, SCIP_Real coef)
Definition: misc_rowprep.c:881
SCIP_RETCODE SCIPcleanupRowprep(SCIP *scip, SCIP_ROWPREP *rowprep, SCIP_SOL *sol, SCIP_Real minviol, SCIP_Real *viol, SCIP_Bool *success)
void SCIProwprepSetLocal(SCIP_ROWPREP *rowprep, SCIP_Bool islocal)
Definition: misc_rowprep.c:748
SCIP_RETCODE SCIPaddRowprepTerms(SCIP *scip, SCIP_ROWPREP *rowprep, int nvars, SCIP_VAR **vars, SCIP_Real *coefs)
Definition: misc_rowprep.c:906
type definitions for constraints and constraint handlers
SCIP_VAR ** SCIProwprepGetVars(SCIP_ROWPREP *rowprep)
Definition: misc_rowprep.c:614
enum SCIP_SideType SCIP_SIDETYPE
Definition: type_lp.h:58