Scippy

GCG

Branch-and-Price & Column Generation for Everyone

scip_misc.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program */
4 /* GCG --- Generic Column Generation */
5 /* a Dantzig-Wolfe decomposition based extension */
6 /* of the branch-cut-and-price framework */
7 /* SCIP --- Solving Constraint Integer Programs */
8 /* */
9 /* Copyright (C) 2010-2021 Operations Research, RWTH Aachen University */
10 /* Zuse Institute Berlin (ZIB) */
11 /* */
12 /* This program is free software; you can redistribute it and/or */
13 /* modify it under the terms of the GNU Lesser General Public License */
14 /* as published by the Free Software Foundation; either version 3 */
15 /* of the License, or (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU Lesser General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
25 /* */
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
28 /**@file scip_misc.c
29  * @brief various SCIP helper methods
30  * @author Martin Bergner
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #include "scip_misc.h"
36 #include "scip/scipdefplugins.h"
37 #include <string.h>
38 #include "scip/cons_indicator.h"
39 
40 /** returns TRUE if variable is relevant, FALSE otherwise */
41 SCIP_Bool GCGisVarRelevant(
42  SCIP_VAR* var /**< variable to test */
43  )
44 {
45  assert(var != NULL);
46  return (SCIPvarGetStatus(var) != SCIP_VARSTATUS_FIXED)
47  && (SCIPvarIsActive(var) || SCIPvarGetStatus(var) == SCIP_VARSTATUS_AGGREGATED || SCIPvarGetStatus(var) == SCIP_VARSTATUS_MULTAGGR || SCIPvarGetStatus(var) == SCIP_VARSTATUS_NEGATED);
48 }
49 
50 /** returns the type of an arbitrary SCIP constraint */
52  SCIP* scip, /**< SCIP data structure */
53  SCIP_CONS* cons /**< constraint to get type for */
54  )
55 {
56  SCIP_CONSHDLR* conshdlr;
57  const char * conshdlrname;
58  assert(cons != NULL);
59  conshdlr = SCIPconsGetHdlr(cons);
60  assert(conshdlr != NULL);
61  conshdlrname = SCIPconshdlrGetName(conshdlr);
62 
63  if( strcmp(conshdlrname, "linear") == 0 )
64  {
65  return linear;
66  }
67  else if( strcmp(conshdlrname, "setppc") == 0 )
68  {
69  switch ( SCIPgetTypeSetppc(scip, cons) ) {
70  case SCIP_SETPPCTYPE_COVERING:
71  return setcovering;
72  case SCIP_SETPPCTYPE_PACKING:
73  return setpacking;
74  case SCIP_SETPPCTYPE_PARTITIONING:
75  return setpartitioning;
76  default:
77  return unknown;
78  }
79  }
80  else if( strcmp(conshdlrname, "logicor") == 0 )
81  {
82  return logicor;
83  }
84  else if( strcmp(conshdlrname, "knapsack") == 0 )
85  {
86  return knapsack;
87  }
88  else if( strcmp(conshdlrname, "varbound") == 0 )
89  {
90  return varbound;
91  }
92  else if( strcmp(conshdlrname, "SOS1") == 0 )
93  {
94  return sos1;
95  }
96  else if( strcmp(conshdlrname, "SOS2") == 0 )
97  {
98  return sos2;
99  }
100  else if( strcmp(conshdlrname, "indicator") == 0 )
101  {
102  return sos2;
103  }
104  return unknown;
105 }
106 
107 /** returns the rhs of an arbitrary SCIP constraint */
108 SCIP_Real GCGconsGetRhs(
109  SCIP* scip, /**< SCIP data structure */
110  SCIP_CONS* cons /**< constraint to get left hand side for */
111  )
112 {
113  int nconsvars;
114  SCIP_VAR** consvars;
115  SCIP_CONSHDLR* conshdlr;
116  const char * conshdlrname;
117  SCIP_Longint * w;
118  SCIP_Real rhs;
119  int i;
120 
121  assert(scip != NULL);
122  assert(cons != NULL);
123  conshdlr = SCIPconsGetHdlr(cons);
124  assert(conshdlr != NULL);
125  conshdlrname = SCIPconshdlrGetName(conshdlr);
126 
127  if( strcmp(conshdlrname, "linear") == 0 )
128  {
129  return SCIPgetRhsLinear(scip, cons);
130  }
131  else if( strcmp(conshdlrname, "setppc") == 0 )
132  {
133  switch ( SCIPgetTypeSetppc(scip, cons) ) {
134  case SCIP_SETPPCTYPE_PARTITIONING: /* fall through desired */
135  case SCIP_SETPPCTYPE_PACKING:
136  rhs = 1.0;
137 
138  nconsvars = SCIPgetNVarsSetppc(scip, cons);
139  consvars = SCIPgetVarsSetppc(scip, cons);
140 
141  for( i = 0; i < nconsvars; i++ )
142  {
143  if( SCIPvarIsNegated(consvars[i]) )
144  rhs -= 1.0;
145  }
146 
147  return rhs;
148 
149  case SCIP_SETPPCTYPE_COVERING:
150  return SCIPinfinity(scip);
151  }
152  }
153  else if( strcmp(conshdlrname, "logicor") == 0 )
154  {
155  return SCIPinfinity(scip);
156  }
157  else if( strcmp(conshdlrname, "knapsack") == 0 )
158  {
159  rhs = SCIPgetCapacityKnapsack(scip, cons);
160 
161  /* copy Longint array to SCIP_Real array */
162  w = SCIPgetWeightsKnapsack(scip, cons);
163  consvars = SCIPgetVarsKnapsack(scip, cons);
164  nconsvars = SCIPgetNVarsKnapsack(scip, cons);
165 
166  for( i = 0; i < nconsvars; i++ )
167  {
168  if( SCIPvarIsNegated(consvars[i]) )
169  rhs -= w[i];
170  }
171 
172  return rhs;
173  }
174  else if( strcmp(conshdlrname, "varbound") == 0 )
175  {
176  rhs = SCIPgetRhsVarbound(scip, cons);
177 
178  if( SCIPvarIsNegated(SCIPgetVarVarbound(scip, cons)) && !SCIPisInfinity(scip, rhs) )
179  rhs -= 1.0;
180 
181  if( SCIPvarIsNegated(SCIPgetVbdvarVarbound(scip, cons)) && !SCIPisInfinity(scip, rhs) )
182  rhs -= SCIPgetVbdcoefVarbound(scip, cons);
183 
184  return rhs;
185  }
186  else if( strcmp(conshdlrname, "SOS1") == 0 )
187  {
188  SCIPdebugMessage("WARNING: SOS1 NOT IMPLEMENTED\n");
189  }
190  else if( strcmp(conshdlrname, "SOS2") == 0 )
191  {
192  SCIPdebugMessage("WARNING: SOS2 NOT IMPLEMENTED\n");
193  }
194  else if( strcmp(conshdlrname, "indicator") == 0 )
195  {
196  return 0.;
197  }
198  else
199  {
200  SCIPdebugMessage("WARNING: NOT IMPLEMENTED");
201  }
202  return -SCIPinfinity(scip);
203 }
204 
205 /** returns the lhs of an arbitrary SCIP constraint */
206 SCIP_Real GCGconsGetLhs(
207  SCIP* scip, /**< SCIP data structure */
208  SCIP_CONS* cons /**< constraint to get left hand side for */
209  )
210 {
211  SCIP_CONSHDLR* conshdlr;
212  const char * conshdlrname;
213  int nconsvars;
214  SCIP_VAR** consvars;
215  SCIP_Real lhs;
216  int i;
217 
218 
219  assert(scip != NULL);
220  assert(cons != NULL);
221  conshdlr = SCIPconsGetHdlr(cons);
222  assert(conshdlr != NULL);
223  conshdlrname = SCIPconshdlrGetName(conshdlr);
224 
225  if( strcmp(conshdlrname, "linear") == 0 )
226  {
227  return SCIPgetLhsLinear(scip, cons);
228  }
229  else if( strcmp(conshdlrname, "setppc") == 0 )
230  {
231  switch ( SCIPgetTypeSetppc(scip, cons) ) {
232  case SCIP_SETPPCTYPE_PARTITIONING: /* fall through desired */
233  case SCIP_SETPPCTYPE_COVERING:
234  lhs = 1.0;
235 
236  nconsvars = SCIPgetNVarsSetppc(scip, cons);
237  consvars = SCIPgetVarsSetppc(scip, cons);
238 
239  for( i = 0; i < nconsvars; i++ )
240  {
241  if( SCIPvarIsNegated(consvars[i]) )
242  lhs -= 1.0;
243  }
244 
245  return lhs;
246 
247  case SCIP_SETPPCTYPE_PACKING:
248  return -SCIPinfinity(scip);
249  }
250  }
251  else if( strcmp(conshdlrname, "logicor") == 0 )
252  {
253  lhs = 1.0;
254 
255  nconsvars = SCIPgetNVarsLogicor(scip, cons);
256  consvars = SCIPgetVarsLogicor(scip, cons);
257 
258  for( i = 0; i < nconsvars; i++ )
259  {
260  if( SCIPvarIsNegated(consvars[i]) )
261  lhs -= 1.0;
262  }
263 
264  return lhs;
265  }
266  else if( strcmp(conshdlrname, "knapsack") == 0 )
267  {
268  return -SCIPinfinity(scip);
269  }
270  else if( strcmp(conshdlrname, "varbound") == 0 )
271  {
272  lhs = SCIPgetLhsVarbound(scip, cons);
273 
274  if( SCIPvarIsNegated(SCIPgetVarVarbound(scip, cons)) && !SCIPisInfinity(scip, -lhs) )
275  lhs -= 1.0;
276 
277  if( SCIPvarIsNegated(SCIPgetVbdvarVarbound(scip, cons)) && !SCIPisInfinity(scip, -lhs) )
278  lhs -= SCIPgetVbdcoefVarbound(scip, cons);
279 
280  return lhs;
281  }
282  else if( strcmp(conshdlrname, "SOS1") == 0 )
283  {
284  SCIPdebugMessage("WARNING: SOS1 NOT IMPLEMENTED\n");
285  }
286  else if( strcmp(conshdlrname, "SOS2") == 0 )
287  {
288  SCIPdebugMessage("WARNING: SOS2 NOT IMPLEMENTED\n");
289  }
290  else if( strcmp(conshdlrname, "indicator") == 0 )
291  {
292  return -SCIPinfinity(scip);
293  }
294  else
295  {
296  SCIPdebugMessage("WARNING: NOT IMPLEMENTED");
297  }
298  return SCIPinfinity(scip);
299 }
300 
301 /** returns the dual farkas sol of an arbitrary SCIP constraint */
302 extern
304  SCIP* scip, /**< SCIP data structure */
305  SCIP_CONS* cons /**< constraint to get left hand side for */
306  )
307 {
308  SCIP_CONSHDLR* conshdlr;
309  const char * conshdlrname;
310 
311  assert(scip != NULL);
312  assert(cons != NULL);
313  conshdlr = SCIPconsGetHdlr(cons);
314  assert(conshdlr != NULL);
315  conshdlrname = SCIPconshdlrGetName(conshdlr);
316 
317  if( strcmp(conshdlrname, "linear") == 0 )
318  {
319  return SCIPgetDualfarkasLinear(scip, cons);
320  }
321  else if( strcmp(conshdlrname, "setppc") == 0 )
322  {
323  return SCIPgetDualfarkasSetppc(scip, cons);
324  }
325  else if( strcmp(conshdlrname, "logicor") == 0 )
326  {
327  return SCIPgetDualfarkasLogicor(scip, cons);
328  }
329  else if( strcmp(conshdlrname, "knapsack") == 0 )
330  {
331  return SCIPgetDualfarkasKnapsack(scip, cons);
332  }
333  else if( strcmp(conshdlrname, "varbound") == 0 )
334  {
335  return SCIPgetDualfarkasVarbound(scip, cons);
336  }
337  else if( strcmp(conshdlrname, "SOS1") == 0 )
338  {
339  SCIPdebugMessage("WARNING: SOS1 NOT IMPLEMENTED\n");
340  }
341  else if( strcmp(conshdlrname, "SOS2") == 0 )
342  {
343  SCIPdebugMessage("WARNING: SOS2 NOT IMPLEMENTED\n");
344  }
345  else if( strcmp(conshdlrname, "origbranch") == 0 )
346  {
347  SCIPdebugMessage("origbranch: return dualfarkas 0\n");
348  return 0.0;
349  }
350  else if( strcmp(conshdlrname, "masterbranch") == 0 )
351  {
352  SCIPdebugMessage("masterbranch: return dualsol 0\n");
353  return 0.0;
354  }
355  else if( strcmp(conshdlrname, "indicator") == 0 )
356  {
357  SCIPdebugMessage("WARNING: indicator conss NOT IMPLEMENTED");
358  }
359  else
360  {
361  SCIPdebugMessage("WARNING: NOT IMPLEMENTED");
362  }
363  return SCIPinfinity(scip);
364 }
365 
366 /** returns the dual sol of an arbitrary SCIP constraint */
367 extern
369  SCIP* scip, /**< SCIP data structure */
370  SCIP_CONS* cons /**< constraint to get left hand side for */
371  )
372 {
373  SCIP_CONSHDLR* conshdlr;
374  const char * conshdlrname;
375 
376  assert(scip != NULL);
377  assert(cons != NULL);
378  conshdlr = SCIPconsGetHdlr(cons);
379  assert(conshdlr != NULL);
380  conshdlrname = SCIPconshdlrGetName(conshdlr);
381 
382  if( strcmp(conshdlrname, "linear") == 0 )
383  {
384  return SCIPgetDualsolLinear(scip, cons);
385  }
386  else if( strcmp(conshdlrname, "setppc") == 0 )
387  {
388  return SCIPgetDualsolSetppc(scip, cons);
389  }
390  else if( strcmp(conshdlrname, "logicor") == 0 )
391  {
392  return SCIPgetDualsolLogicor(scip, cons);
393  }
394  else if( strcmp(conshdlrname, "knapsack") == 0 )
395  {
396  return SCIPgetDualsolKnapsack(scip, cons);
397  }
398  else if( strcmp(conshdlrname, "varbound") == 0 )
399  {
400  return SCIPgetDualsolVarbound(scip, cons);
401  }
402  else if( strcmp(conshdlrname, "SOS1") == 0 )
403  {
404  SCIPdebugMessage("WARNING: SOS1 NOT IMPLEMENTED\n");
405  }
406  else if( strcmp(conshdlrname, "SOS2") == 0 )
407  {
408  SCIPdebugMessage("WARNING: SOS2 NOT IMPLEMENTED\n");
409  }
410  else if( strcmp(conshdlrname, "origbranch") == 0 )
411  {
412  SCIPdebugMessage("origbranch: return Dualsol 0\n");
413  return 0.0;
414  }
415  else if( strcmp(conshdlrname, "masterbranch") == 0 )
416  {
417  SCIPdebugMessage("masterbranch: return dualsol 0\n");
418  return 0.0;
419  }
420  else if( strcmp(conshdlrname, "indicator") == 0 )
421  {
422  SCIPdebugMessage("WARNING: indicator conss NOT IMPLEMENTED");
423  }
424  else
425  {
426  SCIPdebugMessage("WARNING: NOT IMPLEMENTED");
427  }
428  return SCIPinfinity(scip);
429 }
430 
431 
432 
433 /** returns the number of variables in an arbitrary SCIP constraint */
435  SCIP* scip, /**< SCIP data structure */
436  SCIP_CONS* cons /**< constraint to get number of variables */
437  )
438 {
439  SCIP_CONSHDLR* conshdlr;
440  const char * conshdlrname;
441 
442  assert(scip != NULL);
443  assert(cons != NULL);
444  conshdlr = SCIPconsGetHdlr(cons);
445  assert(conshdlr != NULL);
446  conshdlrname = SCIPconshdlrGetName(conshdlr);
447 
448 
449 
450  if( strcmp(conshdlrname, "linear") == 0 )
451  {
452  return SCIPgetNVarsLinear(scip, cons);
453  }
454  else if( strcmp(conshdlrname, "setppc") == 0 )
455  {
456  return SCIPgetNVarsSetppc(scip, cons);
457  }
458  else if( strcmp(conshdlrname, "logicor") == 0 )
459  {
460  return SCIPgetNVarsLogicor(scip, cons);
461  }
462  else if( strcmp(conshdlrname, "knapsack") == 0 )
463  {
464  return SCIPgetNVarsKnapsack(scip, cons);
465  }
466  else if( strcmp(conshdlrname, "varbound") == 0 )
467  {
468  return 2;
469  }
470  else if( strcmp(conshdlrname, "indicator") == 0 )
471  {
472  return 2;
473  }
474  else if( strcmp(conshdlrname, "SOS1") == 0 )
475  {
476  return SCIPgetNVarsSOS1(scip, cons);
477  }
478  else if( strcmp(conshdlrname, "SOS2") == 0 )
479  {
480  return SCIPgetNVarsSOS2(scip, cons);
481  }
482  else
483  {
484  SCIPdebugMessage("WARNING: NOT IMPLEMENTED <%s>\n", conshdlrname);
485  return 0;
486  }
487 }
488 
489 /** returns the variable array of an arbitrary SCIP constraint */
490 SCIP_RETCODE GCGconsGetVars(
491  SCIP* scip, /**< SCIP data structure */
492  SCIP_CONS* cons, /**< constraint to get variables from */
493  SCIP_VAR** vars, /**< array where variables are stored */
494  int nvars /**< size of storage array */
495  )
496 {
497 
498  SCIP_CONSHDLR* conshdlr;
499  const char * conshdlrname;
500  int i;
501 
502  assert(scip != NULL);
503  assert(cons != NULL);
504  assert(vars != NULL);
505  assert(nvars > 0);
506 
507  conshdlr = SCIPconsGetHdlr(cons);
508  assert(conshdlr != NULL);
509  conshdlrname = SCIPconshdlrGetName(conshdlr);
510 
511  if( strcmp(conshdlrname, "linear") == 0 )
512  {
513  if( nvars < SCIPgetNVarsLinear(scip, cons) )
514  return SCIP_INVALIDDATA;
515 
516  BMScopyMemoryArray(vars, SCIPgetVarsLinear(scip, cons), SCIPgetNVarsLinear(scip, cons));
517  }
518  else if( strcmp(conshdlrname, "setppc") == 0 )
519  {
520  SCIP_VAR** consvars;
521  int nconsvars;
522 
523  consvars = SCIPgetVarsSetppc(scip, cons);
524  nconsvars = SCIPgetNVarsSetppc(scip, cons);
525 
526  if( nvars < nconsvars )
527  return SCIP_INVALIDDATA;
528 
529  for( i = 0; i < nconsvars; i++ )
530  if( !SCIPvarIsNegated(consvars[i]) )
531  vars[i] = consvars[i];
532  else
533  vars[i] = SCIPvarGetNegatedVar(consvars[i]);
534  }
535  else if( strcmp(conshdlrname, "logicor") == 0 )
536  {
537  SCIP_VAR** consvars;
538  int nconsvars;
539 
540  consvars = SCIPgetVarsLogicor(scip, cons);
541  nconsvars = SCIPgetNVarsLogicor(scip, cons);
542 
543  if( nvars < nconsvars )
544  return SCIP_INVALIDDATA;
545 
546  for( i = 0; i < nconsvars; i++ )
547  if( !SCIPvarIsNegated(consvars[i]) )
548  vars[i] = consvars[i];
549  else
550  vars[i] = SCIPvarGetNegatedVar(consvars[i]);
551  }
552  else if( strcmp(conshdlrname, "knapsack") == 0 )
553  {
554  SCIP_VAR** consvars;
555  int nconsvars;
556 
557  consvars = SCIPgetVarsKnapsack(scip, cons);
558  nconsvars = SCIPgetNVarsKnapsack(scip, cons);
559 
560  if( nvars < nconsvars )
561  return SCIP_INVALIDDATA;
562 
563  for( i = 0; i < nconsvars; i++ )
564  if( !SCIPvarIsNegated(consvars[i]) )
565  vars[i] = consvars[i];
566  else
567  vars[i] = SCIPvarGetNegatedVar(consvars[i]);
568  }
569  else if( strcmp(conshdlrname, "varbound") == 0 )
570  {
571  if( nvars < 2 )
572  return SCIP_INVALIDDATA;
573 
574  if( !SCIPvarIsNegated(SCIPgetVarVarbound(scip, cons)) )
575  vars[0] = SCIPgetVarVarbound(scip, cons);
576  else
577  vars[0] = SCIPvarGetNegatedVar(SCIPgetVarVarbound(scip, cons));
578 
579  if( !SCIPvarIsNegated(SCIPgetVbdvarVarbound(scip, cons)) )
580  vars[1] = SCIPgetVbdvarVarbound(scip, cons);
581  else
582  vars[1] = SCIPvarGetNegatedVar(SCIPgetVbdvarVarbound(scip, cons));
583  }
584  else if( strcmp(conshdlrname, "SOS1") == 0 )
585  {
586  if( nvars < SCIPgetNVarsSOS1(scip, cons) )
587  return SCIP_INVALIDDATA;
588 
589  BMScopyMemoryArray(vars, SCIPgetVarsSOS1(scip, cons), nvars);
590  }
591  else if( strcmp(conshdlrname, "SOS2") == 0 )
592  {
593  if( nvars < SCIPgetNVarsSOS2(scip, cons) )
594  return SCIP_INVALIDDATA;
595 
596  BMScopyMemoryArray(vars, SCIPgetVarsSOS2(scip, cons), nvars);
597  }
598  else if( strcmp(conshdlrname, "indicator") == 0 )
599  {
600  if( nvars != 2 )
601  return SCIP_INVALIDDATA;
602 
603  /* indicator conss : s - My <= 0 */
604 
605  /* slack variable first */
606  vars[0] = SCIPgetSlackVarIndicator(cons);
607  vars[1] = SCIPgetBinaryVarIndicator(cons);
608  }
609  else
610  {
611  SCIPwarningMessage(scip, "WARNING: NOT IMPLEMENTED <%s>\n", conshdlrname);
612  }
613  return SCIP_OKAY;
614 }
615 
616 
617 /**
618  * Returns the value array of an arbitrary SCIP constraint
619  * @todo SOS1 & SOS2 not implemented yet
620  */
621 SCIP_RETCODE GCGconsGetVals(
622  SCIP* scip, /**< SCIP data structure */
623  SCIP_CONS* cons, /**< constraint to get values from */
624  SCIP_Real* vals, /**< array where values are stored */
625  int nvals /**< size of storage array */
626  )
627 {
628  SCIP_CONSHDLR* conshdlr;
629  const char* conshdlrname;
630  int i;
631  int nvars;
632 
633  assert(scip != NULL);
634  assert(cons != NULL);
635  assert(vals != NULL);
636  assert(nvals > 0);
637 
638  conshdlr = SCIPconsGetHdlr(cons);
639  assert(conshdlr != NULL);
640  conshdlrname = SCIPconshdlrGetName(conshdlr);
641 
642  if( strcmp(conshdlrname, "linear") == 0 )
643  {
644  nvars = SCIPgetNVarsLinear(scip, cons);
645  if( nvals < nvars )
646  return SCIP_INVALIDDATA;
647 
648  BMScopyMemoryArray(vals, SCIPgetValsLinear(scip, cons), nvars);
649  }
650  else if( strcmp(conshdlrname, "setppc") == 0 )
651  {
652  SCIP_VAR** vars;
653  vars = SCIPgetVarsSetppc(scip, cons);
654  nvars = SCIPgetNVarsSetppc(scip, cons);
655  if( nvals < nvars )
656  return SCIP_INVALIDDATA;
657 
658  for( i = 0; i < nvals; i++ )
659  if( !SCIPvarIsNegated(vars[i]) )
660  vals[i] = 1.0;
661  else
662  vals[i] = -1.0;
663  }
664  else if( strcmp(conshdlrname, "logicor") == 0 )
665  {
666  SCIP_VAR** vars;
667  vars = SCIPgetVarsLogicor(scip, cons);
668  nvars = SCIPgetNVarsLogicor(scip, cons);
669  if( nvals < nvars )
670  return SCIP_INVALIDDATA;
671 
672  for( i = 0; i < nvals; i++ )
673  {
674  if( !SCIPvarIsNegated(vars[i]) )
675  vals[i] = 1.0;
676  else
677  vals[i] = -1.0;
678  }
679  }
680  else if( strcmp(conshdlrname, "knapsack") == 0 )
681  {
682  SCIP_VAR** vars;
683 
684  /* copy Longint array to SCIP_Real array */
685  SCIP_Longint * w = SCIPgetWeightsKnapsack(scip, cons);
686  vars = SCIPgetVarsKnapsack(scip, cons);
687  nvars = SCIPgetNVarsKnapsack(scip, cons);
688  if( nvals < nvars )
689  return SCIP_INVALIDDATA;
690 
691  for( i = 0; i < nvars; i++ )
692  {
693  if( !SCIPvarIsNegated(vars[i]) )
694  vals[i] = w[i];
695  else
696  vals[i] = -w[i];
697  }
698  }
699  else if( strcmp(conshdlrname, "varbound") == 0 )
700  {
701  nvars = 2;
702  if( nvals < nvars )
703  return SCIP_INVALIDDATA;
704 
705  if( !SCIPvarIsNegated(SCIPgetVarVarbound(scip, cons)) )
706  vals[0] = 1.0;
707  else
708  vals[0] = -1.0;
709 
710  if( !SCIPvarIsNegated(SCIPgetVbdvarVarbound(scip, cons)) )
711  vals[1] = SCIPgetVbdcoefVarbound(scip, cons);
712  else
713  vals[1] = -SCIPgetVbdcoefVarbound(scip, cons);
714  }
715  else if( strcmp(conshdlrname, "SOS1") == 0 )
716  {
717  /* store constraint */
718  SCIPerrorMessage("WARNING: SOS1 NOT IMPLEMENTED\n");
719  }
720  else if( strcmp(conshdlrname, "SOS2") == 0 )
721  {
722  /* store constraint */
723  SCIPdebugMessage("WARNING: SOS2 NOT IMPLEMENTED\n");
724  }
725  else if( strcmp(conshdlrname, "indicator") == 0 )
726  {
727  if( nvals != 2 )
728  return SCIP_INVALIDDATA;
729 
730  /* indicator conss : s - My <= 0 */
731 
732  /* slack variable first */
733  vals[0] = 1.;
734  vals[1] = SCIPvarGetUbGlobal( SCIPgetSlackVarIndicator(cons) );
735  }
736  else
737  {
738  SCIPdebugMessage("WARNING: UNKNOWN NOT IMPLEMENTED: %s\n", conshdlrname);
739  }
740  return SCIP_OKAY;
741 }
742 
743 /** returns true if the constraint should be a master constraint and false otherwise */
744 SCIP_Bool GCGconsIsRanged(
745  SCIP* scip, /**< SCIP data structure */
746  SCIP_CONS* cons /**< constraint to check */
747 ){
748 
749  SCIP_Real lhs;
750  SCIP_Real rhs;
751 
752  assert(scip != NULL);
753 
754  rhs = GCGconsGetRhs(scip, cons);
755  lhs = GCGconsGetLhs(scip, cons);
756 
757 
758  return !(SCIPisEQ(scip, lhs, rhs) || SCIPisInfinity(scip, -lhs) || SCIPisInfinity(scip, rhs) );
759 }
760 
761 
762 /** returns true if the constraint should be a master constraint and false otherwise */
764  SCIP* scip, /**< SCIP data structure */
765  SCIP_CONS* cons, /**< constraint to check */
766  SCIP_SETPPCTYPE* setppctype /**< returns the type of the constraints */
767  )
768 {
769  SCIP_VAR** vars;
770  SCIP_Real* vals;
771  int i;
772 
773  int nvars;
774  SCIP_Bool relevant = TRUE;
775  assert(scip != NULL);
776  assert(cons != NULL);
777  assert(setppctype != NULL);
778 
779  *setppctype = SCIP_SETPPCTYPE_PACKING;
780  SCIPdebugMessage("cons %s is ", SCIPconsGetName(cons));
781 
782  if( GCGconsGetType(scip, cons) == setcovering || GCGconsGetType(scip, cons) == setpartitioning || GCGconsGetType(scip, cons) == logicor )
783  {
784  SCIPdebugPrintf("setcov, part or logicor.\n");
785  return TRUE;
786  }
787  nvars = GCGconsGetNVars(scip, cons);
788  vars = NULL;
789  vals = NULL;
790  if( nvars > 0 )
791  {
792  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vars, nvars) );
793  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vals, nvars) );
794  SCIP_CALL_ABORT( GCGconsGetVars(scip, cons, vars, nvars) );
795  SCIP_CALL_ABORT( GCGconsGetVals(scip, cons, vals, nvars) );
796  }
797 
798  /* check vars and vals for integrality */
799  for( i = 0; i < nvars && relevant; ++i )
800  {
801  assert(vars != NULL);
802  assert(vals != NULL);
803 
804  if( !SCIPvarIsBinary(vars[i]) )
805  {
806  SCIPdebugPrintf("(%s is not integral) ", SCIPvarGetName(vars[i]) );
807  relevant = FALSE;
808  }
809  if( !SCIPisEQ(scip, vals[i], 1.0) )
810  {
811  SCIPdebugPrintf("(coeff for var %s is %.2f != 1.0) ", SCIPvarGetName(vars[i]), vals[i] );
812  relevant = FALSE;
813  }
814  }
815 
816  if( relevant )
817  {
818  SCIP_Real rhs = GCGconsGetRhs(scip, cons);
819  SCIP_Real lhs = GCGconsGetLhs(scip, cons);
820  SCIPdebugPrintf("(lhs %.2f, rhs %.2f)", lhs, rhs);
821 
822  if( SCIPisEQ(scip, lhs, 1.0) && SCIPisEQ(scip, rhs, 1.0) )
823  {
824  *setppctype = SCIP_SETPPCTYPE_PARTITIONING;
825  }
826  else if( SCIPisEQ(scip, lhs, 1.0) && SCIPisGE(scip, rhs, nvars*1.0) )
827  {
828  *setppctype = SCIP_SETPPCTYPE_COVERING;
829  }
830  else if( SCIPisLE(scip, lhs, 0.0) && SCIPisEQ(scip, rhs, 1.0) )
831  {
832  *setppctype = SCIP_SETPPCTYPE_PACKING;
833  }
834  else
835  {
836  relevant = FALSE;
837  }
838  }
839 
840  /* free temporary data */
841  SCIPfreeBufferArrayNull(scip, &vals);
842  SCIPfreeBufferArrayNull(scip, &vars);
843 
844  SCIPdebugPrintf("%s master\n", relevant ? "in" : "not in");
845  return relevant;
846 }
847 
848 /** returns true if the constraint should be a master constraint and false otherwise */
850  SCIP* scip, /**< SCIP data structure */
851  SCIP_CONS* cons /**< constraint to check */
852 )
853 {
854  SCIP_VAR** vars;
855  SCIP_Real* vals;
856  int i;
857 
858  int nvars;
859  SCIP_Bool relevant = TRUE;
860  assert(scip != NULL);
861  assert(cons != NULL);
862 
863  SCIPdebugMessage("cons %s is ", SCIPconsGetName(cons));
864 
865  if( GCGconsGetType(scip, cons) == setpartitioning )
866  {
867  return TRUE;
868  }
869  nvars = GCGconsGetNVars(scip, cons);
870  vars = NULL;
871  vals = NULL;
872  if( nvars > 0 )
873  {
874  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vars, nvars) );
875  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vals, nvars) );
876  SCIP_CALL_ABORT( GCGconsGetVars(scip, cons, vars, nvars) );
877  SCIP_CALL_ABORT( GCGconsGetVals(scip, cons, vals, nvars) );
878  }
879 
880  /* check vars and vals for integrality */
881  for( i = 0; i < nvars && relevant; ++i )
882  {
883  assert(vars != NULL);
884  assert(vals != NULL);
885 
886 // if( !SCIPvarIsBinary(vars[i]) )
887 // {
888 // SCIPdebugPrintf("(%s is not integral) ", SCIPvarGetName(vars[i]) );
889 // relevant = FALSE;
890 // }
891  if( !SCIPisEQ(scip, vals[i], 1.0) )
892  {
893  SCIPdebugPrintf("(coeff for var %s is %.2f != 1.0) ", SCIPvarGetName(vars[i]), vals[i] );
894  relevant = FALSE;
895  }
896  }
897 
898  if( relevant )
899  {
900  SCIP_Real rhs = GCGconsGetRhs(scip, cons);
901  SCIP_Real lhs = GCGconsGetLhs(scip, cons);
902  SCIPdebugPrintf("(lhs %.2f, rhs %.2f)", lhs, rhs);
903 
904  if(! SCIPisFeasEQ(scip, lhs, rhs) )
905  {
906  relevant = FALSE;
907  }
908  }
909 
910  /* free temporary data */
911  SCIPfreeBufferArrayNull(scip, &vals);
912  SCIPfreeBufferArrayNull(scip, &vars);
913 
914  SCIPdebugPrintf("%s master\n", relevant ? "in" : "not in");
915  return relevant;
916 }
917 
918 
919 
920 /** returns TRUE or FALSE, depending whether we are in the root node or not */
921 SCIP_Bool GCGisRootNode(
922  SCIP* scip /**< SCIP data structure */
923  )
924 {
925  assert(scip != NULL);
926  return (SCIPgetCurrentNode(scip) == SCIPgetRootNode(scip));
927 }
SCIP_Real GCGconsGetRhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:108
SCIP_Real GCGconsGetLhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:206
@ logicor
Definition: scip_misc.h:49
SCIP_Real GCGconsGetDualfarkas(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:303
SCIP_Bool GCGgetConsIsCardinalityCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:849
@ sos2
Definition: scip_misc.h:49
@ knapsack
Definition: scip_misc.h:48
SCIP_Real GCGconsGetDualsol(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:368
SCIP_RETCODE GCGconsGetVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int nvars)
Definition: scip_misc.c:490
SCIP_Bool GCGisVarRelevant(SCIP_VAR *var)
Definition: scip_misc.c:41
SCIP_Bool GCGconsIsRanged(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:744
@ sos1
Definition: scip_misc.h:49
various SCIP helper methods
@ setpacking
Definition: scip_misc.h:48
@ setpartitioning
Definition: scip_misc.h:48
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:51
SCIP_RETCODE GCGconsGetVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int nvals)
Definition: scip_misc.c:621
SCIP_Bool GCGisRootNode(SCIP *scip)
Definition: scip_misc.c:921
int GCGconsGetNVars(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:434
SCIP_Bool GCGgetConsIsSetppc(SCIP *scip, SCIP_CONS *cons, SCIP_SETPPCTYPE *setppctype)
Definition: scip_misc.c:763
@ linear
Definition: scip_misc.h:48
consType
Definition: scip_misc.h:47
@ setcovering
Definition: scip_misc.h:48
@ unknown
Definition: scip_misc.h:49
@ varbound
Definition: scip_misc.h:48