Scippy

SCIP

Solving Constraint Integer Programs

cons_cumulative.c
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-2024 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 cons_cumulative.c
26  * @ingroup DEFPLUGINS_CONS
27  * @brief constraint handler for cumulative constraints
28  * @author Timo Berthold
29  * @author Stefan Heinz
30  * @author Jens Schulz
31  *
32  * Given:
33  * - a set of jobs, represented by their integer start time variables \f$S_j\f$, their array of processing times \f$p_j\f$ and of
34  * their demands \f$d_j\f$.
35  * - an integer resource capacity \f$C\f$
36  *
37  * The cumulative constraint ensures that for each point in time \f$t\f$ \f$\sum_{j: S_j \leq t < S_j + p_j} d_j \leq C\f$ holds.
38  *
39  * Separation:
40  * - can be done using binary start time model, see Pritskers, Watters and Wolfe
41  * - or by just separating relatively weak cuts on the integer start time variables
42  *
43  * Propagation:
44  * - time tabling, Klein & Scholl (1999)
45  * - Edge-finding from Petr Vilim, adjusted and simplified for dynamic repropagation
46  * (2009)
47  * - energetic reasoning, see Baptiste, Le Pape, Nuijten (2001)
48  *
49  */
50 
51 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
52 
53 #include <assert.h>
54 #include <string.h>
55 
56 #include "tclique/tclique.h"
57 #include "scip/cons_cumulative.h"
58 #include "scip/cons_linking.h"
59 #include "scip/cons_knapsack.h"
60 #include "scip/scipdefplugins.h"
61 
62 /**@name Constraint handler properties
63  *
64  * @{
65  */
66 
67 /* constraint handler properties */
68 #define CONSHDLR_NAME "cumulative"
69 #define CONSHDLR_DESC "cumulative constraint handler"
70 #define CONSHDLR_SEPAPRIORITY 2100000 /**< priority of the constraint handler for separation */
71 #define CONSHDLR_ENFOPRIORITY -2040000 /**< priority of the constraint handler for constraint enforcing */
72 #define CONSHDLR_CHECKPRIORITY -3030000 /**< priority of the constraint handler for checking feasibility */
73 #define CONSHDLR_SEPAFREQ 1 /**< frequency for separating cuts; zero means to separate only in the root node */
74 #define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
75 #define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
76  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
77 #define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
78 #define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
79 #define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
80 #define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
81 
82 #define CONSHDLR_PRESOLTIMING SCIP_PRESOLTIMING_ALWAYS
83 #define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP
84 
85 /**@} */
86 
87 /**@name Default parameter values
88  *
89  * @{
90  */
91 
92 /* default parameter values */
93 
94 /* separation */
95 #define DEFAULT_USEBINVARS FALSE /**< should the binary representation be used? */
96 #define DEFAULT_LOCALCUTS FALSE /**< should cuts be added only locally? */
97 #define DEFAULT_USECOVERCUTS TRUE /**< should covering cuts be added? */
98 #define DEFAULT_CUTSASCONSS TRUE /**< should the cuts be created as knapsack constraints? */
99 #define DEFAULT_SEPAOLD TRUE /**< shall old sepa algo be applied? */
101 /* propagation */
102 #define DEFAULT_TTINFER TRUE /**< should time-table (core-times) propagator be used to infer bounds? */
103 #define DEFAULT_EFCHECK FALSE /**< should edge-finding be used to detect an overload? */
104 #define DEFAULT_EFINFER FALSE /**< should edge-finding be used to infer bounds? */
105 #define DEFAULT_USEADJUSTEDJOBS FALSE /**< should during edge-finding jobs be adusted which run on the border of the effective time horizon? */
106 #define DEFAULT_TTEFCHECK TRUE /**< should time-table edge-finding be used to detect an overload? */
107 #define DEFAULT_TTEFINFER TRUE /**< should time-table edge-finding be used to infer bounds? */
109 /* presolving */
110 #define DEFAULT_DUALPRESOLVE TRUE /**< should dual presolving be applied? */
111 #define DEFAULT_COEFTIGHTENING FALSE /**< should coeffisient tightening be applied? */
112 #define DEFAULT_NORMALIZE TRUE /**< should demands and capacity be normalized? */
113 #define DEFAULT_PRESOLPAIRWISE TRUE /**< should pairwise constraint comparison be performed in presolving? */
114 #define DEFAULT_DISJUNCTIVE TRUE /**< extract disjunctive constraints? */
115 #define DEFAULT_DETECTDISJUNCTIVE TRUE /**< search for conflict set via maximal cliques to detect disjunctive constraints */
116 #define DEFAULT_DETECTVARBOUNDS TRUE /**< search for conflict set via maximal cliques to detect variable bound constraints */
117 #define DEFAULT_MAXNODES 10000LL /**< number of branch-and-bound nodes to solve an independent cumulative constraint (-1: no limit) */
119 /* enforcement */
120 #define DEFAULT_FILLBRANCHCANDS FALSE /**< should branching candidates be added to storage? */
122 /* conflict analysis */
123 #define DEFAULT_USEBDWIDENING TRUE /**< should bound widening be used during conflict analysis? */
125 /**@} */
126 
127 /**@name Event handler properties
128  *
129  * @{
130  */
131 
132 #define EVENTHDLR_NAME "cumulative"
133 #define EVENTHDLR_DESC "bound change event handler for cumulative constraints"
135 /**@} */
136 
137 /*
138  * Data structures
139  */
140 
141 /** constraint data for cumulative constraints */
142 struct SCIP_ConsData
143 {
144  SCIP_VAR** vars; /**< array of variable representing the start time of each job */
145  SCIP_Bool* downlocks; /**< array to store if the variable has a down lock */
146  SCIP_Bool* uplocks; /**< array to store if the variable has an uplock */
147  SCIP_CONS** linkingconss; /**< array of linking constraints for the integer variables */
148  SCIP_ROW** demandrows; /**< array of rows of linear relaxation of this problem */
149  SCIP_ROW** scoverrows; /**< array of rows of small cover cuts of this problem */
150  SCIP_ROW** bcoverrows; /**< array of rows of big cover cuts of this problem */
151  int* demands; /**< array containing corresponding demands */
152  int* durations; /**< array containing corresponding durations */
153  SCIP_Real resstrength1; /**< stores the resource strength 1*/
154  SCIP_Real resstrength2; /**< stores the resource strength 2 */
155  SCIP_Real cumfactor1; /**< stroes the cumulativeness of the constraint */
156  SCIP_Real disjfactor1; /**< stores the disjunctiveness of the constraint */
157  SCIP_Real disjfactor2; /**< stores the disjunctiveness of the constraint */
158  SCIP_Real estimatedstrength;
159  int nvars; /**< number of variables */
160  int varssize; /**< size of the arrays */
161  int ndemandrows; /**< number of rows of cumulative constrint for linear relaxation */
162  int demandrowssize; /**< size of array rows of demand rows */
163  int nscoverrows; /**< number of rows of small cover cuts */
164  int scoverrowssize; /**< size of array of small cover cuts */
165  int nbcoverrows; /**< number of rows of big cover cuts */
166  int bcoverrowssize; /**< size of array of big cover cuts */
167  int capacity; /**< available cumulative capacity */
168 
169  int hmin; /**< left bound of time axis to be considered (including hmin) */
170  int hmax; /**< right bound of time axis to be considered (not including hmax) */
171 
172  unsigned int signature; /**< constraint signature which is need for pairwise comparison */
173 
174  unsigned int validsignature:1; /**< is the signature valid */
175  unsigned int normalized:1; /**< is the constraint normalized */
176  unsigned int covercuts:1; /**< cover cuts are created? */
177  unsigned int propagated:1; /**< is constraint propagted */
178  unsigned int varbounds:1; /**< bool to store if variable bound strengthening was already preformed */
179  unsigned int triedsolving:1; /**< bool to store if we tried already to solve that constraint as independent subproblem */
180 
181 #ifdef SCIP_STATISTIC
182  int maxpeak;
183 #endif
184 };
185 
186 /** constraint handler data */
187 struct SCIP_ConshdlrData
188 {
189  SCIP_EVENTHDLR* eventhdlr; /**< event handler for bound change events */
190 
191  SCIP_Bool usebinvars; /**< should the binary variables be used? */
192  SCIP_Bool cutsasconss; /**< should the cumulative constraint create cuts as knapsack constraints? */
193  SCIP_Bool ttinfer; /**< should time-table (core-times) propagator be used to infer bounds? */
194  SCIP_Bool efcheck; /**< should edge-finding be used to detect an overload? */
195  SCIP_Bool efinfer; /**< should edge-finding be used to infer bounds? */
196  SCIP_Bool useadjustedjobs; /**< should during edge-finding jobs be adusted which run on the border of the effective time horizon? */
197  SCIP_Bool ttefcheck; /**< should time-table edge-finding be used to detect an overload? */
198  SCIP_Bool ttefinfer; /**< should time-table edge-finding be used to infer bounds? */
199  SCIP_Bool localcuts; /**< should cuts be added only locally? */
200  SCIP_Bool usecovercuts; /**< should covering cuts be added? */
201  SCIP_Bool sepaold; /**< shall old sepa algo be applied? */
202 
203  SCIP_Bool fillbranchcands; /**< should branching candidates be added to storage? */
204 
205  SCIP_Bool dualpresolve; /**< should dual presolving be applied? */
206  SCIP_Bool coeftightening; /**< should coeffisient tightening be applied? */
207  SCIP_Bool normalize; /**< should demands and capacity be normalized? */
208  SCIP_Bool disjunctive; /**< extract disjunctive constraints? */
209  SCIP_Bool detectdisjunctive; /**< search for conflict set via maximal cliques to detect disjunctive constraints */
210  SCIP_Bool detectvarbounds; /**< search for conflict set via maximal cliques to detect variable bound constraints */
211  SCIP_Bool usebdwidening; /**< should bound widening be used during conflict analysis? */
212  SCIP_Bool presolpairwise; /**< should pairwise constraint comparison be performed in presolving? */
213  SCIP_Bool detectedredundant; /**< was detection of redundant constraints already performed? */
214 
215  SCIP_Longint maxnodes; /**< number of branch-and-bound nodes to solve an independent cumulative constraint (-1: no limit) */
216 
217  SCIP_DECL_SOLVECUMULATIVE((*solveCumulative)); /**< method to use a single cumulative condition */
218 
219  /* statistic values which are collected if SCIP_STATISTIC is defined */
220 #ifdef SCIP_STATISTIC
221  SCIP_Longint nlbtimetable; /**< number of times the lower bound was tightened by the time-table propagator */
222  SCIP_Longint nubtimetable; /**< number of times the upper bound was tightened by the time-table propagator */
223  SCIP_Longint ncutofftimetable; /**< number of times the a cutoff was detected due to time-table propagator */
224  SCIP_Longint nlbedgefinder; /**< number of times the lower bound was tightened by the edge-finder propagator */
225  SCIP_Longint nubedgefinder; /**< number of times the upper bound was tightened by the edge-finder propagator */
226  SCIP_Longint ncutoffedgefinder; /**< number of times the a cutoff was detected due to edge-finder propagator */
227  SCIP_Longint ncutoffoverload; /**< number of times the a cutoff was detected due to overload checking via edge-finding */
228  SCIP_Longint nlbTTEF; /**< number of times the lower bound was tightened by time-table edge-finding */
229  SCIP_Longint nubTTEF; /**< number of times the upper bound was tightened by time-table edge-finding */
230  SCIP_Longint ncutoffoverloadTTEF;/**< number of times the a cutoff was detected due to overload checking via time-table edge-finding */
231 
232  int nirrelevantjobs; /**< number of time a irrelevant/redundant jobs was removed form a constraint */
233  int nalwaysruns; /**< number of time a job removed form a constraint which run completely during the effective horizon */
234  int nremovedlocks; /**< number of times a up or down lock was removed */
235  int ndualfixs; /**< number of times a dual fix was performed by a single constraint */
236  int ndecomps; /**< number of times a constraint was decomposed */
237  int ndualbranchs; /**< number of times a dual branch was discoverd and applicable via probing */
238  int nallconsdualfixs; /**< number of times a dual fix was performed due to knowledge of all cumulative constraints */
239  int naddedvarbounds; /**< number of added variable bounds constraints */
240  int naddeddisjunctives; /**< number of added disjunctive constraints */
241 
242  SCIP_Bool iscopy; /**< Boolean to store if constraint handler is part of a copy */
243 #endif
244 };
245 
246 /**@name Inference Information Methods
247  *
248  * An inference information can be passed with each domain reduction to SCIP. This information is passed back to the
249  * constraint handler if the corresponding bound change has to be explained. It can be used to store information which
250  * help to construct a reason/explanation for a bound change. The inference information is limited to size of integer.
251  *
252  * In case of the cumulative constraint handler we store the used propagation algorithms for that particular bound
253  * change and the earliest start and latest completion time of all jobs in the conflict set.
254  *
255  * @{
256  */
257 
258 /** Propagation rules */
259 enum Proprule
260 {
261  PROPRULE_0_INVALID = 0, /**< invalid inference information */
262  PROPRULE_1_CORETIMES = 1, /**< core-time propagator */
263  PROPRULE_2_EDGEFINDING = 2, /**< edge-finder */
264  PROPRULE_3_TTEF = 3 /**< time-table edeg-finding */
265 };
266 typedef enum Proprule PROPRULE;
268 /** inference information */
269 struct InferInfo
270 {
271  union
272  {
273  /** struct to use the inference information */
274  struct
275  {
276  unsigned int proprule:2; /**< propagation rule that was applied */
277  unsigned int data1:15; /**< data field one */
278  unsigned int data2:15; /**< data field two */
279  } asbits;
280  int asint; /**< inference information as a single int value */
281  } val;
282 };
283 typedef struct InferInfo INFERINFO;
285 /** converts an integer into an inference information */
286 static
287 INFERINFO intToInferInfo(
288  int i /**< integer to convert */
289  )
290 {
291  INFERINFO inferinfo;
292 
293  inferinfo.val.asint = i;
294 
295  return inferinfo;
296 }
297 
298 /** converts an inference information into an int */
299 static
300 int inferInfoToInt(
301  INFERINFO inferinfo /**< inference information to convert */
302  )
303 {
304  return inferinfo.val.asint;
305 }
306 
307 /** returns the propagation rule stored in the inference information */
308 static
310  INFERINFO inferinfo /**< inference information to convert */
311  )
312 {
313  return (PROPRULE) inferinfo.val.asbits.proprule;
314 }
315 
316 /** returns data field one of the inference information */
317 static
319  INFERINFO inferinfo /**< inference information to convert */
320  )
321 {
322  return (int) inferinfo.val.asbits.data1;
323 }
324 
325 /** returns data field two of the inference information */
326 static
328  INFERINFO inferinfo /**< inference information to convert */
329  )
330 {
331  return (int) inferinfo.val.asbits.data2;
332 }
333 
334 /** returns whether the inference information is valid */
335 static
337  INFERINFO inferinfo /**< inference information to convert */
338  )
339 {
340  return (inferinfo.val.asint != 0);
341 }
342 
343 
344 /** constructs an inference information out of a propagation rule, an earliest start and a latest completion time */
345 static
346 INFERINFO getInferInfo(
347  PROPRULE proprule, /**< propagation rule that deduced the value */
348  int data1, /**< data field one */
349  int data2 /**< data field two */
350  )
351 {
352  INFERINFO inferinfo;
353 
354  /* check that the data members are in the range of the available bits */
355  if( proprule == PROPRULE_0_INVALID || data1 < 0 || data1 >= (1<<15) || data2 < 0 || data2 >= (1<<15) )
356  {
357  inferinfo.val.asint = 0;
358  assert(inferInfoGetProprule(inferinfo) == PROPRULE_0_INVALID);
359  assert(inferInfoIsValid(inferinfo) == FALSE);
360  }
361  else
362  {
363  inferinfo.val.asbits.proprule = proprule; /*lint !e641*/
364  inferinfo.val.asbits.data1 = (unsigned int) data1; /*lint !e732*/
365  inferinfo.val.asbits.data2 = (unsigned int) data2; /*lint !e732*/
366  assert(inferInfoIsValid(inferinfo) == TRUE);
367  }
368 
369  return inferinfo;
370 }
371 
372 /**@} */
373 
374 /*
375  * Local methods
376  */
377 
378 /**@name Miscellaneous Methods
379  *
380  * @{
381  */
382 
383 #ifndef NDEBUG
384 
385 /** compute the core of a job which lies in certain interval [begin, end) */
386 static
388  int begin, /**< begin of the interval */
389  int end, /**< end of the interval */
390  int ect, /**< earliest completion time */
391  int lst /**< latest start time */
392  )
393 {
394  int core;
395 
396  core = MAX(0, MIN(end, ect) - MAX(lst, begin));
397 
398  return core;
399 }
400 #else
401 #define computeCoreWithInterval(begin, end, ect, lst) (MAX(0, MIN((end), (ect)) - MAX((lst), (begin))))
402 #endif
403 
404 /** returns the implied earliest start time */ /*lint -e{715}*/
405 static
407  SCIP* scip, /**< SCIP data structure */
408  SCIP_VAR* var, /**< variable for which the implied est should be returned */
409  SCIP_HASHMAP* addedvars, /**< hash map containig the variable which are already added */
410  int* est /**< pointer to store the implied earliest start time */
411  )
412 { /*lint --e{715}*/
413 #if 0
414  SCIP_VAR** vbdvars;
415  SCIP_VAR* vbdvar;
416  SCIP_Real* vbdcoefs;
417  SCIP_Real* vbdconsts;
418  void* image;
419  int nvbdvars;
420  int v;
421 #endif
422 
423  (*est) = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
424 
425 #if 0
426  /* the code contains a bug; we need to check if an implication forces that the jobs do not run in parallel */
427 
428  nvbdvars = SCIPvarGetNVlbs(var);
429  vbdvars = SCIPvarGetVlbVars(var);
430  vbdcoefs = SCIPvarGetVlbCoefs(var);
431  vbdconsts = SCIPvarGetVlbConstants(var);
432 
433  for( v = 0; v < nvbdvars; ++v )
434  {
435  vbdvar = vbdvars[v];
436  assert(vbdvar != NULL);
437 
438  image = SCIPhashmapGetImage(addedvars, (void*)vbdvar);
439 
440  if( image != NULL && SCIPisEQ(scip, vbdcoefs[v], 1.0 ) )
441  {
442  int duration;
443  int vbdconst;
444 
445  duration = (int)(size_t)image;
446  vbdconst = SCIPconvertRealToInt(scip, vbdconsts[v]);
447 
448  SCIPdebugMsg(scip, "check implication <%s>[%g,%g] >= <%s>[%g,%g] + <%g>\n",
450  SCIPvarGetName(vbdvar), SCIPvarGetLbLocal(vbdvar), SCIPvarGetUbLocal(vbdvar), vbdconsts[v]);
451 
452  if( duration >= vbdconst )
453  {
454  int impliedest;
455 
456  impliedest = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vbdvar)) + duration;
457 
458  if( (*est) < impliedest )
459  {
460  (*est) = impliedest;
461 
462  SCIP_CALL( SCIPhashmapRemove(addedvars, (void*)vbdvar) );
463  }
464  }
465  }
466  }
467 #endif
468 
469  return SCIP_OKAY;
470 }
471 
472 /** returns the implied latest completion time */ /*lint -e{715}*/
473 static
475  SCIP* scip, /**< SCIP data structure */
476  SCIP_VAR* var, /**< variable for which the implied est should be returned */
477  int duration, /**< duration of the given job */
478  SCIP_HASHMAP* addedvars, /**< hash map containig the variable which are already added */
479  int* lct /**< pointer to store the implied latest completion time */
480  )
481 { /*lint --e{715}*/
482 #if 0
483  SCIP_VAR** vbdvars;
484  SCIP_VAR* vbdvar;
485  SCIP_Real* vbdcoefs;
486  SCIP_Real* vbdconsts;
487  int nvbdvars;
488  int v;
489 #endif
490 
491  (*lct) = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var)) + duration;
492 
493 #if 0
494  /* the code contains a bug; we need to check if an implication forces that the jobs do not run in parallel */
495 
496  nvbdvars = SCIPvarGetNVubs(var);
497  vbdvars = SCIPvarGetVubVars(var);
498  vbdcoefs = SCIPvarGetVubCoefs(var);
499  vbdconsts = SCIPvarGetVubConstants(var);
500 
501  for( v = 0; v < nvbdvars; ++v )
502  {
503  vbdvar = vbdvars[v];
504  assert(vbdvar != NULL);
505 
506  if( SCIPhashmapExists(addedvars, (void*)vbdvar) && SCIPisEQ(scip, vbdcoefs[v], 1.0 ) )
507  {
508  int vbdconst;
509 
510  vbdconst = SCIPconvertRealToInt(scip, -vbdconsts[v]);
511 
512  SCIPdebugMsg(scip, "check implication <%s>[%g,%g] <= <%s>[%g,%g] + <%g>\n",
514  SCIPvarGetName(vbdvar), SCIPvarGetLbLocal(vbdvar), SCIPvarGetUbLocal(vbdvar), vbdconsts[v]);
515 
516  if( duration >= -vbdconst )
517  {
518  int impliedlct;
519 
520  impliedlct = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vbdvar));
521 
522  if( (*lct) > impliedlct )
523  {
524  (*lct) = impliedlct;
525 
526  SCIP_CALL( SCIPhashmapRemove(addedvars, (void*)vbdvar) );
527  }
528  }
529  }
530  }
531 #endif
532 
533  return SCIP_OKAY;
534 }
535 
536 /** collects all necessary binary variables to represent the jobs which can be active at time point of interest */
537 static
539  SCIP* scip, /**< SCIP data structure */
540  SCIP_CONSDATA* consdata, /**< constraint data */
541  SCIP_VAR*** vars, /**< pointer to the array to store the binary variables */
542  int** coefs, /**< pointer to store the coefficients */
543  int* nvars, /**< number if collect binary variables */
544  int* startindices, /**< permutation with rspect to the start times */
545  int curtime, /**< current point in time */
546  int nstarted, /**< number of jobs that start before the curtime or at curtime */
547  int nfinished /**< number of jobs that finished before curtime or at curtime */
548  )
549 {
550  int nrowvars;
551  int startindex;
552  int size;
553 
554  size = 10;
555  nrowvars = 0;
556  startindex = nstarted - 1;
557 
558  SCIP_CALL( SCIPallocBufferArray(scip, vars, size) );
559  SCIP_CALL( SCIPallocBufferArray(scip, coefs, size) );
560 
561  /* search for the (nstarted - nfinished) jobs which are active at curtime */
562  while( nstarted - nfinished > nrowvars )
563  {
564  SCIP_VAR* var;
565  int endtime;
566  int duration;
567  int demand;
568  int varidx;
569 
570  /* collect job information */
571  varidx = startindices[startindex];
572  assert(varidx >= 0 && varidx < consdata->nvars);
573 
574  var = consdata->vars[varidx];
575  duration = consdata->durations[varidx];
576  demand = consdata->demands[varidx];
577  assert(var != NULL);
578 
579  endtime = SCIPconvertRealToInt(scip, SCIPvarGetUbGlobal(var)) + duration;
580 
581  /* check the end time of this job is larger than the curtime; in this case the job is still running */
582  if( endtime > curtime )
583  {
584  SCIP_VAR** binvars;
585  SCIP_Real* vals;
586  int nbinvars;
587  int start;
588  int end;
589  int b;
590 
591  /* check if the linking constraints exists */
592  assert(SCIPexistsConsLinking(scip, var));
593  assert(SCIPgetConsLinking(scip, var) != NULL);
594  assert(SCIPgetConsLinking(scip, var) == consdata->linkingconss[varidx]);
595 
596  /* collect linking constraint information */
597  SCIP_CALL( SCIPgetBinvarsLinking(scip, consdata->linkingconss[varidx], &binvars, &nbinvars) );
598  vals = SCIPgetValsLinking(scip, consdata->linkingconss[varidx]);
599 
600  start = curtime - duration + 1;
601  end = MIN(curtime, endtime - duration);
602 
603  for( b = 0; b < nbinvars; ++b )
604  {
605  if( vals[b] < start )
606  continue;
607 
608  if( vals[b] > end )
609  break;
610 
611  assert(binvars[b] != NULL);
612 
613  /* ensure array proper array size */
614  if( size == *nvars )
615  {
616  size *= 2;
617  SCIP_CALL( SCIPreallocBufferArray(scip, vars, size) );
618  SCIP_CALL( SCIPreallocBufferArray(scip, coefs, size) );
619  }
620 
621  (*vars)[*nvars] = binvars[b];
622  (*coefs)[*nvars] = demand;
623  (*nvars)++;
624  }
625  nrowvars++;
626  }
627 
628  startindex--;
629  }
630 
631  return SCIP_OKAY;
632 }
633 
634 /** collect all integer variable which belong to jobs which can run at the point of interest */
635 static
637  SCIP* scip, /**< SCIP data structure */
638  SCIP_CONSDATA* consdata, /**< constraint data */
639  SCIP_VAR*** activevars, /**< jobs that are currently running */
640  int* startindices, /**< permutation with rspect to the start times */
641  int curtime, /**< current point in time */
642  int nstarted, /**< number of jobs that start before the curtime or at curtime */
643  int nfinished, /**< number of jobs that finished before curtime or at curtime */
644  SCIP_Bool lower, /**< shall cuts be created due to lower or upper bounds? */
645  int* lhs /**< lhs for the new row sum of lbs + minoffset */
646  )
647 {
648  SCIP_VAR* var;
649  int startindex;
650  int endtime;
651  int duration;
652  int starttime;
653 
654  int varidx;
655  int sumofstarts;
656  int mindelta;
657  int counter;
658 
659  assert(curtime >= consdata->hmin);
660  assert(curtime < consdata->hmax);
661 
662  counter = 0;
663  sumofstarts = 0;
664 
665  mindelta = INT_MAX;
666 
667  startindex = nstarted - 1;
668 
669  /* search for the (nstarted - nfinished) jobs which are active at curtime */
670  while( nstarted - nfinished > counter )
671  {
672  assert(startindex >= 0);
673 
674  /* collect job information */
675  varidx = startindices[startindex];
676  assert(varidx >= 0 && varidx < consdata->nvars);
677 
678  var = consdata->vars[varidx];
679  duration = consdata->durations[varidx];
680  assert(duration > 0);
681  assert(var != NULL);
682 
683  if( lower )
684  starttime = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
685  else
686  starttime = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
687 
688  endtime = MIN(starttime + duration, consdata->hmax);
689 
690  /* check the end time of this job is larger than the curtime; in this case the job is still running */
691  if( endtime > curtime )
692  {
693  (*activevars)[counter] = var;
694  sumofstarts += starttime;
695  mindelta = MIN(mindelta, endtime - curtime); /* this amount of schifting holds for lb and ub */
696  counter++;
697  }
698 
699  startindex--;
700  }
701 
702  assert(mindelta > 0);
703  *lhs = lower ? sumofstarts + mindelta : sumofstarts - mindelta;
704 
705  return SCIP_OKAY;
706 }
707 
708 /** initialize the sorted event point arrays */
709 static
711  SCIP* scip, /**< SCIP data structure */
712  int nvars, /**< number of start time variables (activities) */
713  SCIP_VAR** vars, /**< array of start time variables */
714  int* durations, /**< array of durations per start time variable */
715  int* starttimes, /**< array to store sorted start events */
716  int* endtimes, /**< array to store sorted end events */
717  int* startindices, /**< permutation with rspect to the start times */
718  int* endindices, /**< permutation with rspect to the end times */
719  SCIP_Bool local /**< shall local bounds be used */
720  )
721 {
722  SCIP_VAR* var;
723  int j;
724 
725  assert(vars != NULL || nvars == 0);
726 
727  /* assign variables, start and endpoints to arrays */
728  for ( j = 0; j < nvars; ++j )
729  {
730  assert(vars != NULL);
731 
732  var = vars[j];
733  assert(var != NULL);
734 
735  if( local )
736  starttimes[j] = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
737  else
738  starttimes[j] = SCIPconvertRealToInt(scip, SCIPvarGetLbGlobal(var));
739 
740  startindices[j] = j;
741 
742  if( local )
743  endtimes[j] = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var)) + durations[j];
744  else
745  endtimes[j] = SCIPconvertRealToInt(scip, SCIPvarGetUbGlobal(var)) + durations[j];
746 
747  endindices[j] = j;
748  }
749 
750  /* sort the arrays not-decreasing according to startsolvalues and endsolvalues (and sort the indices in the same way) */
751  SCIPsortIntInt(starttimes, startindices, j);
752  SCIPsortIntInt(endtimes, endindices, j);
753 }
754 
755 /** initialize the sorted event point arrays w.r.t. the given primal solutions */
756 static
758  SCIP* scip, /**< SCIP data structure */
759  SCIP_SOL* sol, /**< solution */
760  int nvars, /**< number of start time variables (activities) */
761  SCIP_VAR** vars, /**< array of start time variables */
762  int* durations, /**< array of durations per start time variable */
763  int* starttimes, /**< array to store sorted start events */
764  int* endtimes, /**< array to store sorted end events */
765  int* startindices, /**< permutation with rspect to the start times */
766  int* endindices /**< permutation with rspect to the end times */
767  )
768 {
769  SCIP_VAR* var;
770  int j;
771 
772  assert(vars != NULL || nvars == 0);
773 
774  /* assign variables, start and endpoints to arrays */
775  for ( j = 0; j < nvars; ++j )
776  {
777  assert(vars != NULL);
778 
779  var = vars[j];
780  assert(var != NULL);
781 
782  starttimes[j] = SCIPconvertRealToInt(scip, SCIPgetSolVal(scip, sol, var));
783  startindices[j] = j;
784 
785  endtimes[j] = SCIPconvertRealToInt(scip, SCIPgetSolVal(scip, sol, var)) + durations[j];
786  endindices[j] = j;
787  }
788 
789  /* sort the arrays not-decreasing according to startsolvalues and endsolvalues (and sort the indices in the same way) */
790  SCIPsortIntInt(starttimes, startindices, j);
791  SCIPsortIntInt(endtimes, endindices, j);
792 }
793 
794 /** initialize the sorted event point arrays
795  *
796  * @todo Check the separation process!
797  */
798 static
800  SCIP* scip, /**< SCIP data structure */
801  SCIP_CONSDATA* consdata, /**< constraint data */
802  SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
803  int* starttimes, /**< array to store sorted start events */
804  int* endtimes, /**< array to store sorted end events */
805  int* startindices, /**< permutation with rspect to the start times */
806  int* endindices, /**< permutation with rspect to the end times */
807  int* nvars, /**< number of variables that are integral */
808  SCIP_Bool lower /**< shall the constraints be derived for lower or upper bounds? */
809  )
810 {
811  SCIP_VAR* var;
812  int tmpnvars;
813  int j;
814 
815  tmpnvars = consdata->nvars;
816  *nvars = 0;
817 
818  /* assign variables, start and endpoints to arrays */
819  for ( j = 0; j < tmpnvars; ++j )
820  {
821  var = consdata->vars[j];
822  assert(var != NULL);
823  assert(consdata->durations[j] > 0);
824  assert(consdata->demands[j] > 0);
825 
826  if( lower )
827  {
828  /* only consider jobs that are at their lower or upper bound */
829  if( !SCIPisFeasIntegral(scip, SCIPgetSolVal(scip, sol, var))
830  || !SCIPisFeasEQ(scip, SCIPgetSolVal(scip, sol, var), SCIPvarGetLbLocal(var)) )
831  continue;
832 
833  starttimes[*nvars] = SCIPconvertRealToInt(scip, SCIPgetSolVal(scip, sol, var));
834  startindices[*nvars] = j;
835 
836  endtimes[*nvars] = starttimes[*nvars] + consdata->durations[j];
837  endindices[*nvars] = j;
838 
839  SCIPdebugMsg(scip, "%d: variable <%s>[%g,%g] (sol %g, duration %d) starttime %d, endtime = %d, demand = %d\n",
840  *nvars, SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), SCIPgetSolVal(scip, sol, var),
841  consdata->durations[j],
842  starttimes[*nvars], starttimes[*nvars] + consdata->durations[startindices[*nvars]],
843  consdata->demands[startindices[*nvars]]);
844 
845  (*nvars)++;
846  }
847  else
848  {
849  if( !SCIPisFeasIntegral(scip, SCIPgetSolVal(scip, sol, var))
850  || !SCIPisFeasEQ(scip, SCIPgetSolVal(scip, sol, var), SCIPvarGetUbLocal(var)) )
851  continue;
852 
853  starttimes[*nvars] = SCIPconvertRealToInt(scip, SCIPgetSolVal(scip, sol, var));
854  startindices[*nvars] = j;
855 
856  endtimes[*nvars] = starttimes[*nvars] + consdata->durations[j];
857  endindices[*nvars] = j;
858 
859  SCIPdebugMsg(scip, "%d: variable <%s>[%g,%g] (sol %g, duration %d) starttime %d, endtime = %d, demand = %d\n",
860  *nvars, SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), SCIPgetSolVal(scip, sol, var),
861  consdata->durations[j],
862  starttimes[*nvars], starttimes[*nvars] + consdata->durations[startindices[*nvars]],
863  consdata->demands[startindices[*nvars]]);
864 
865  (*nvars)++;
866  }
867  }
868 
869  /* sort the arrays not-decreasing according to startsolvalues and endsolvalues (and sort the indices in the same way) */
870  SCIPsortIntInt(starttimes, startindices, *nvars);
871  SCIPsortIntInt(endtimes, endindices, *nvars);
872 
873 #ifdef SCIP_DEBUG
874  SCIPdebugMsg(scip, "sorted output %d\n", *nvars);
875 
876  for ( j = 0; j < *nvars; ++j )
877  {
878  SCIPdebugMsg(scip, "%d: job[%d] starttime %d, endtime = %d, demand = %d\n", j,
879  startindices[j], starttimes[j], starttimes[j] + consdata->durations[startindices[j]],
880  consdata->demands[startindices[j]]);
881  }
882 
883  for ( j = 0; j < *nvars; ++j )
884  {
885  SCIPdebugMsg(scip, "%d: job[%d] endtime %d, demand = %d\n", j, endindices[j], endtimes[j],
886  consdata->demands[endindices[j]]);
887  }
888 #endif
889 }
890 
891 #ifdef SCIP_STATISTIC
892 /** this method checks for relevant intervals for energetic reasoning */
893 static
894 SCIP_RETCODE computeRelevantEnergyIntervals(
895  SCIP* scip, /**< SCIP data structure */
896  int nvars, /**< number of start time variables (activities) */
897  SCIP_VAR** vars, /**< array of start time variables */
898  int* durations, /**< array of durations */
899  int* demands, /**< array of demands */
900  int capacity, /**< cumulative capacity */
901  int hmin, /**< left bound of time axis to be considered (including hmin) */
902  int hmax, /**< right bound of time axis to be considered (not including hmax) */
903  int** timepoints, /**< array to store relevant points in time */
904  SCIP_Real** cumulativedemands, /**< array to store the estimated cumulative demand for each point in time */
905  int* ntimepoints, /**< pointer to store the number of timepoints */
906  int* maxdemand, /**< pointer to store maximum over all demands */
907  SCIP_Real* minfreecapacity /**< pointer to store the minimum free capacity */
908  )
909 {
910  int* starttimes; /* stores when each job is starting */
911  int* endtimes; /* stores when each job ends */
912  int* startindices; /* we will sort the startsolvalues, thus we need to know wich index of a job it corresponds to */
913  int* endindices; /* we will sort the endsolvalues, thus we need to know wich index of a job it corresponds to */
914 
915  SCIP_Real totaldemand;
916  int curtime; /* point in time which we are just checking */
917  int endindex; /* index of endsolvalues with: endsolvalues[endindex] > curtime */
918 
919  int j;
920 
921  assert( scip != NULL );
922  assert(durations != NULL);
923  assert(demands != NULL);
924  assert(capacity >= 0);
925 
926  /* if no activities are associated with this cumulative then this constraint is redundant */
927  if( nvars == 0 )
928  return SCIP_OKAY;
929 
930  assert(vars != NULL);
931 
932  SCIP_CALL( SCIPallocBufferArray(scip, &starttimes, nvars) );
933  SCIP_CALL( SCIPallocBufferArray(scip, &endtimes, nvars) );
934  SCIP_CALL( SCIPallocBufferArray(scip, &startindices, nvars) );
935  SCIP_CALL( SCIPallocBufferArray(scip, &endindices, nvars) );
936 
937  /* create event point arrays */
938  createSortedEventpoints(scip, nvars, vars, durations, starttimes, endtimes, startindices, endindices, TRUE);
939 
940  endindex = 0;
941  totaldemand = 0.0;
942 
943  *ntimepoints = 0;
944  (*timepoints)[0] = starttimes[0];
945  (*cumulativedemands)[0] = 0;
946  *maxdemand = 0;
947 
948  /* check each startpoint of a job whether the capacity is kept or not */
949  for( j = 0; j < nvars; ++j )
950  {
951  int lct;
952  int idx;
953 
954  curtime = starttimes[j];
955 
956  if( curtime >= hmax )
957  break;
958 
959  /* free all capacity usages of jobs the are no longer running */
960  while( endindex < nvars && endtimes[endindex] <= curtime )
961  {
962  int est;
963 
964  if( (*timepoints)[*ntimepoints] < endtimes[endindex] )
965  {
966  (*ntimepoints)++;
967  (*timepoints)[*ntimepoints] = endtimes[endindex];
968  (*cumulativedemands)[*ntimepoints] = 0;
969  }
970 
971  idx = endindices[endindex];
972  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[idx]));
973  totaldemand -= (SCIP_Real) demands[idx] * durations[idx] / (endtimes[endindex] - est);
974  endindex++;
975 
976  (*cumulativedemands)[*ntimepoints] = totaldemand;
977  }
978 
979  idx = startindices[j];
980  lct = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[idx]) + durations[idx]);
981  totaldemand += (SCIP_Real) demands[idx] * durations[idx] / (lct - starttimes[j]);
982 
983  if( (*timepoints)[*ntimepoints] < curtime )
984  {
985  (*ntimepoints)++;
986  (*timepoints)[*ntimepoints] = curtime;
987  (*cumulativedemands)[*ntimepoints] = 0;
988  }
989 
990  (*cumulativedemands)[*ntimepoints] = totaldemand;
991 
992  /* add the relative capacity requirements for all job which start at the curtime */
993  while( j+1 < nvars && starttimes[j+1] == curtime )
994  {
995  ++j;
996  idx = startindices[j];
997  lct = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[idx]) + durations[idx]);
998  totaldemand += (SCIP_Real) demands[idx] * durations[idx] / (lct - starttimes[j]);
999 
1000  (*cumulativedemands)[*ntimepoints] = totaldemand;
1001  }
1002  } /*lint --e{850}*/
1003 
1004  /* free all capacity usages of jobs that are no longer running */
1005  while( endindex < nvars/* && endtimes[endindex] < hmax*/)
1006  {
1007  int est;
1008  int idx;
1009 
1010  if( (*timepoints)[*ntimepoints] < endtimes[endindex] )
1011  {
1012  (*ntimepoints)++;
1013  (*timepoints)[*ntimepoints] = endtimes[endindex];
1014  (*cumulativedemands)[*ntimepoints] = 0;
1015  }
1016 
1017  idx = endindices[endindex];
1018  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[idx]));
1019  totaldemand -= (SCIP_Real) demands[idx] * durations[idx] / (endtimes[endindex] - est);
1020  (*cumulativedemands)[*ntimepoints] = totaldemand;
1021 
1022  ++endindex;
1023  }
1024 
1025  (*ntimepoints)++;
1026  /* compute minimum free capacity */
1027  (*minfreecapacity) = INT_MAX;
1028  for( j = 0; j < *ntimepoints; ++j )
1029  {
1030  if( (*timepoints)[j] >= hmin && (*timepoints)[j] < hmax )
1031  *minfreecapacity = MIN( *minfreecapacity, (SCIP_Real)capacity - (*cumulativedemands)[j] );
1032  }
1033 
1034  /* free buffer arrays */
1035  SCIPfreeBufferArray(scip, &endindices);
1036  SCIPfreeBufferArray(scip, &startindices);
1037  SCIPfreeBufferArray(scip, &endtimes);
1038  SCIPfreeBufferArray(scip, &starttimes);
1039 
1040  return SCIP_OKAY;
1041 }
1042 
1043 /** evaluates the cumulativeness and disjointness factor of a cumulative constraint */
1044 static
1045 SCIP_RETCODE evaluateCumulativeness(
1046  SCIP* scip, /**< pointer to scip */
1047  SCIP_CONS* cons /**< cumulative constraint */
1048  )
1049 {
1050  SCIP_CONSDATA* consdata;
1051  int nvars;
1052  int v;
1053  int capacity;
1054 
1055  /* output values: */
1056  SCIP_Real disjfactor2; /* (peak-capacity)/capacity * (large demands/nvars_t) */
1057  SCIP_Real cumfactor1;
1058  SCIP_Real resstrength1; /* overall strength */
1059  SCIP_Real resstrength2; /* timepoint wise maximum */
1060 
1061  /* helpful variables: */
1062  SCIP_Real globalpeak;
1063  SCIP_Real globalmaxdemand;
1064 
1065  /* get constraint data structure */
1066  consdata = SCIPconsGetData(cons);
1067  assert(consdata != NULL);
1068 
1069  nvars = consdata->nvars;
1070  capacity = consdata->capacity;
1071  globalpeak = 0.0;
1072  globalmaxdemand = 0.0;
1073 
1074  disjfactor2 = 0.0;
1075  cumfactor1 = 0.0;
1076  resstrength2 = 0.0;
1077 
1078  /* check each starting time (==each job, but inefficient) */
1079  for( v = 0; v < nvars; ++v )
1080  {
1081  SCIP_Real peak;
1082  SCIP_Real maxdemand;
1083  SCIP_Real deltademand;
1084  int ndemands;
1085  int nlarge;
1086 
1087  int timepoint;
1088  int j;
1089  timepoint = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(consdata->vars[v]));
1090  peak = consdata->demands[v];
1091  ndemands = 1;
1092  maxdemand = 0;
1093  nlarge = 0;
1094 
1095  if( consdata->demands[v] > capacity / 3 )
1096  nlarge++;
1097 
1098  for( j = 0; j < nvars; ++j )
1099  {
1100  int lb;
1101 
1102  if( j == v )
1103  continue;
1104 
1105  maxdemand = 0.0;
1106  lb = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(consdata->vars[j]));
1107 
1108  if( lb <= timepoint && lb + consdata->durations[j] > timepoint )
1109  {
1110  peak += consdata->demands[j];
1111  ndemands++;
1112 
1113  if( consdata->demands[j] > consdata->capacity / 3 )
1114  nlarge++;
1115  }
1116  }
1117 
1118  deltademand = (SCIP_Real)peak / (SCIP_Real)ndemands;
1119  globalpeak = MAX(globalpeak, peak);
1120  globalmaxdemand = MAX(globalmaxdemand, maxdemand);
1121 
1122  if( peak > capacity )
1123  {
1124  disjfactor2 = MAX( disjfactor2, (peak-(SCIP_Real)capacity)/peak * (nlarge/(SCIP_Real)ndemands) );
1125  cumfactor1 = MAX( cumfactor1, (peak-capacity)/peak * (capacity-deltademand)/(SCIP_Real)capacity );
1126  resstrength2 = MAX(resstrength2, (capacity-maxdemand)/(peak-maxdemand) );
1127  }
1128  }
1129 
1130  resstrength1 = (capacity-globalmaxdemand) / (globalpeak-globalmaxdemand);
1131 
1132  consdata->maxpeak = SCIPconvertRealToInt(scip, globalpeak);
1133  consdata->disjfactor2 = disjfactor2;
1134  consdata->cumfactor1 = cumfactor1;
1135  consdata->resstrength2 = resstrength2;
1136  consdata->resstrength1 = resstrength1;
1137 
1138  /* get estimated res strength */
1139  {
1140  int* timepoints;
1141  SCIP_Real* estimateddemands;
1142  int ntimepoints;
1143  int maxdemand;
1144  SCIP_Real minfreecapacity;
1145 
1146  SCIP_CALL( SCIPallocBufferArray(scip, &timepoints, 2*nvars) );
1147  SCIP_CALL( SCIPallocBufferArray(scip, &estimateddemands, 2*nvars) );
1148 
1149  ntimepoints = 0;
1150  minfreecapacity = INT_MAX;
1151 
1152  SCIP_CALL( computeRelevantEnergyIntervals(scip, nvars, consdata->vars,
1153  consdata->durations, consdata->demands,
1154  capacity, consdata->hmin, consdata->hmax, &timepoints, &estimateddemands,
1155  &ntimepoints, &maxdemand, &minfreecapacity) );
1156 
1157  /* free buffer arrays */
1158  SCIPfreeBufferArray(scip, &estimateddemands);
1159  SCIPfreeBufferArray(scip, &timepoints);
1160 
1161  consdata->estimatedstrength = (SCIP_Real)(capacity - minfreecapacity) / (SCIP_Real) capacity;
1162  }
1163 
1164  SCIPstatisticPrintf("cumulative constraint<%s>: DISJ1=%g, DISJ2=%g, CUM=%g, RS1 = %g, RS2 = %g, EST = %g\n",
1165  SCIPconsGetName(cons), consdata->disjfactor1, disjfactor2, cumfactor1, resstrength1, resstrength2,
1166  consdata->estimatedstrength);
1167 
1168  return SCIP_OKAY;
1169 }
1170 #endif
1171 
1172 /** gets the active variables together with the constant */
1173 static
1175  SCIP* scip, /**< SCIP data structure */
1176  SCIP_VAR** var, /**< pointer to store the active variable */
1177  int* scalar, /**< pointer to store the scalar */
1178  int* constant /**< pointer to store the constant */
1179  )
1180 {
1181  if( !SCIPvarIsActive(*var) )
1182  {
1183  SCIP_Real realscalar;
1184  SCIP_Real realconstant;
1185 
1186  realscalar = 1.0;
1187  realconstant = 0.0;
1188 
1190 
1191  /* transform variable to active variable */
1192  SCIP_CALL( SCIPgetProbvarSum(scip, var, &realscalar, &realconstant) );
1193  assert(!SCIPisZero(scip, realscalar));
1194  assert(SCIPvarIsActive(*var));
1195 
1196  if( realconstant < 0.0 )
1197  (*constant) = -SCIPconvertRealToInt(scip, -realconstant);
1198  else
1199  (*constant) = SCIPconvertRealToInt(scip, realconstant);
1200 
1201  if( realscalar < 0.0 )
1202  (*scalar) = -SCIPconvertRealToInt(scip, -realscalar);
1203  else
1204  (*scalar) = SCIPconvertRealToInt(scip, realscalar);
1205  }
1206  else
1207  {
1208  (*scalar) = 1;
1209  (*constant) = 0;
1210  }
1211 
1212  assert(*scalar != 0);
1213 
1214  return SCIP_OKAY;
1215 }
1216 
1217 /** computes the total energy of all jobs */
1218 static
1220  int* durations, /**< array of job durations */
1221  int* demands, /**< array of job demands */
1222  int njobs /**< number of jobs */
1223  )
1224 {
1225  SCIP_Longint energy;
1226  int j;
1227 
1228  energy = 0;
1229 
1230  for( j = 0; j < njobs; ++j )
1231  energy += (SCIP_Longint) durations[j] * demands[j];
1232 
1233  return energy;
1234 }
1235 
1236 /**@} */
1237 
1238 /**@name Default method to solve a cumulative condition
1239  *
1240  * @{
1241  */
1242 
1243 /** setup and solve subscip to solve single cumulative condition */
1244 static
1246  SCIP* subscip, /**< subscip data structure */
1247  SCIP_Real* objvals, /**< array of objective coefficients for each job (linear objective function), or NULL if none */
1248  int* durations, /**< array of durations */
1249  int* demands, /**< array of demands */
1250  int njobs, /**< number of jobs (activities) */
1251  int capacity, /**< cumulative capacity */
1252  int hmin, /**< left bound of time axis to be considered (including hmin) */
1253  int hmax, /**< right bound of time axis to be considered (not including hmax) */
1254  SCIP_Longint maxnodes, /**< maximum number of branch-and-bound nodes (-1: no limit) */
1255  SCIP_Real timelimit, /**< time limit for solving in seconds */
1256  SCIP_Real memorylimit, /**< memory limit for solving in mega bytes (MB) */
1257  SCIP_Real* ests, /**< array of earliest start times for each job */
1258  SCIP_Real* lsts, /**< array of latest start times for each job */
1259  SCIP_Bool* infeasible, /**< pointer to store if the subproblem was infeasible */
1260  SCIP_Bool* unbounded, /**< pointer to store if the problem is unbounded */
1261  SCIP_Bool* solved, /**< pointer to store if the problem is solved (to optimality) */
1262  SCIP_Bool* error /**< pointer to store if an error occurred */
1263  )
1264 {
1265  SCIP_VAR** subvars;
1266  SCIP_CONS* cons;
1267 
1268  char name[SCIP_MAXSTRLEN];
1269  int v;
1270  SCIP_RETCODE retcode;
1271 
1272  assert(subscip != NULL);
1273 
1274  /* copy all plugins */
1276 
1277  /* create the subproblem */
1278  SCIP_CALL( SCIPcreateProbBasic(subscip, "cumulative") );
1279 
1280  SCIP_CALL( SCIPallocBlockMemoryArray(subscip, &subvars, njobs) );
1281 
1282  /* create for each job a start time variable */
1283  for( v = 0; v < njobs; ++v )
1284  {
1285  SCIP_Real objval;
1286 
1287  /* construct variable name */
1288  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "job%d", v);
1289 
1290  if( objvals == NULL )
1291  objval = 0.0;
1292  else
1293  objval = objvals[v];
1294 
1295  SCIP_CALL( SCIPcreateVarBasic(subscip, &subvars[v], name, ests[v], lsts[v], objval, SCIP_VARTYPE_INTEGER) );
1296  SCIP_CALL( SCIPaddVar(subscip, subvars[v]) );
1297  }
1298 
1299  /* create cumulative constraint */
1300  SCIP_CALL( SCIPcreateConsBasicCumulative(subscip, &cons, "cumulative",
1301  njobs, subvars, durations, demands, capacity) );
1302 
1303  /* set effective horizon */
1304  SCIP_CALL( SCIPsetHminCumulative(subscip, cons, hmin) );
1305  SCIP_CALL( SCIPsetHmaxCumulative(subscip, cons, hmax) );
1306 
1307  /* add cumulative constraint */
1308  SCIP_CALL( SCIPaddCons(subscip, cons) );
1309  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
1310 
1311  /* set CP solver settings
1312  *
1313  * @note This "meta" setting has to be set first since this call overwrite all parameters including for example the
1314  * time limit.
1315  */
1317 
1318  /* do not abort subproblem on CTRL-C */
1319  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
1320 
1321  /* disable output to console */
1322  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
1323 
1324  /* set limits for the subproblem */
1325  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", maxnodes) );
1326  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
1327  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
1328 
1329  /* forbid recursive call of heuristics and separators solving subMIPs */
1330  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
1331 
1332  /* solve single cumulative constraint by branch and bound */
1333  retcode = SCIPsolve(subscip);
1334 
1335  if( retcode != SCIP_OKAY )
1336  (*error) = TRUE;
1337  else
1338  {
1339  SCIPdebugMsg(subscip, "solved single cumulative condition with status %d\n", SCIPgetStatus(subscip));
1340 
1341  /* evaluated solution status */
1342  switch( SCIPgetStatus(subscip) )
1343  {
1344  case SCIP_STATUS_INFORUNBD:
1346  (*infeasible) = TRUE;
1347  (*solved) = TRUE;
1348  break;
1349  case SCIP_STATUS_UNBOUNDED:
1350  (*unbounded) = TRUE;
1351  (*solved) = TRUE;
1352  break;
1353  case SCIP_STATUS_OPTIMAL:
1354  {
1355  SCIP_SOL* sol;
1356  SCIP_Real solval;
1357 
1358  sol = SCIPgetBestSol(subscip);
1359  assert(sol != NULL);
1360 
1361  for( v = 0; v < njobs; ++v )
1362  {
1363  solval = SCIPgetSolVal(subscip, sol, subvars[v]);
1364 
1365  ests[v] = solval;
1366  lsts[v] = solval;
1367  }
1368  (*solved) = TRUE;
1369  break;
1370  }
1371  case SCIP_STATUS_NODELIMIT:
1373  case SCIP_STATUS_TIMELIMIT:
1374  case SCIP_STATUS_MEMLIMIT:
1376  case SCIP_STATUS_TERMINATE:
1377  /* transfer the global bound changes */
1378  for( v = 0; v < njobs; ++v )
1379  {
1380  ests[v] = SCIPvarGetLbGlobal(subvars[v]);
1381  lsts[v] = SCIPvarGetUbGlobal(subvars[v]);
1382  }
1383  (*solved) = FALSE;
1384  break;
1385 
1386  case SCIP_STATUS_UNKNOWN:
1388  case SCIP_STATUS_GAPLIMIT:
1389  case SCIP_STATUS_SOLLIMIT:
1392  SCIPerrorMessage("invalid status code <%d>\n", SCIPgetStatus(subscip));
1393  return SCIP_INVALIDDATA;
1394  }
1395  }
1396 
1397  /* release all variables */
1398  for( v = 0; v < njobs; ++v )
1399  {
1400  SCIP_CALL( SCIPreleaseVar(subscip, &subvars[v]) );
1401  }
1402 
1403  SCIPfreeBlockMemoryArray(subscip, &subvars, njobs);
1404 
1405  return SCIP_OKAY;
1406 }
1407 
1408 /** solve single cumulative condition using SCIP and a single cumulative constraint */
1409 static
1410 SCIP_DECL_SOLVECUMULATIVE(solveCumulativeViaScipCp)
1412  SCIP* subscip;
1413 
1414  SCIP_RETCODE retcode;
1415 
1416  assert(njobs > 0);
1417 
1418  (*solved) = FALSE;
1419  (*infeasible) = FALSE;
1420  (*unbounded) = FALSE;
1421  (*error) = FALSE;
1422 
1423  SCIPdebugMessage("solve independent cumulative condition with %d variables\n", njobs);
1424 
1425  /* initialize the sub-problem */
1426  SCIP_CALL( SCIPcreate(&subscip) );
1427 
1428  /* create and solve the subproblem. catch possible errors */
1429  retcode = setupAndSolveCumulativeSubscip(subscip, objvals, durations, demands,
1430  njobs, capacity, hmin, hmax,
1431  maxnodes, timelimit, memorylimit,
1432  ests, lsts,
1433  infeasible, unbounded, solved, error);
1434 
1435  /* free the subscip in any case */
1436  SCIP_CALL( SCIPfree(&subscip) );
1437 
1438  SCIP_CALL( retcode );
1439 
1440  return SCIP_OKAY;
1441 }
1442 
1443 #if 0
1444 /** solve single cumulative condition using SCIP and the time indexed formulation */
1445 static
1446 SCIP_DECL_SOLVECUMULATIVE(solveCumulativeViaScipMip)
1447 {
1448  SCIP* subscip;
1449  SCIP_VAR*** binvars;
1450  SCIP_RETCODE retcode;
1451  char name[SCIP_MAXSTRLEN];
1452  int minest;
1453  int maxlct;
1454  int t;
1455  int v;
1456 
1457  assert(njobs > 0);
1458 
1459  (*solved) = FALSE;
1460  (*infeasible) = FALSE;
1461  (*unbounded) = FALSE;
1462  (*error) = FALSE;
1463 
1464  SCIPdebugMsg(scip, "solve independent cumulative condition with %d variables\n", njobs);
1465 
1466  /* initialize the sub-problem */
1467  SCIP_CALL( SCIPcreate(&subscip) );
1468 
1469  /* copy all plugins */
1471 
1472  /* create the subproblem */
1473  SCIP_CALL( SCIPcreateProbBasic(subscip, "cumulative") );
1474 
1475  SCIP_CALL( SCIPallocBufferArray(subscip, &binvars, njobs) );
1476 
1477  minest = INT_MAX;
1478  maxlct = INT_MIN;
1479 
1480  /* create for each job and time step a binary variable which is one if this jobs starts at this time point and a set
1481  * partitioning constrain which forces that job starts
1482  */
1483  for( v = 0; v < njobs; ++v )
1484  {
1485  SCIP_CONS* cons;
1486  SCIP_Real objval;
1487  int timeinterval;
1488  int est;
1489  int lst;
1490 
1491  if( objvals == NULL )
1492  objval = 0.0;
1493  else
1494  objval = objvals[v];
1495 
1496  est = ests[v];
1497  lst = lsts[v];
1498 
1499  /* compute number of possible start points */
1500  timeinterval = lst - est + 1;
1501  assert(timeinterval > 0);
1502 
1503  /* compute the smallest earliest start time and largest latest completion time */
1504  minest = MIN(minest, est);
1505  maxlct = MAX(maxlct, lst + durations[v]);
1506 
1507  /* construct constraint name */
1508  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "job_%d", v);
1509 
1510  SCIP_CALL( SCIPcreateConsBasicSetpart(subscip, &cons, name, 0, NULL) );
1511 
1512  SCIP_CALL( SCIPallocBufferArray(subscip, &binvars[v], timeinterval) );
1513 
1514  for( t = 0; t < timeinterval; ++t )
1515  {
1516  SCIP_VAR* binvar;
1517 
1518  /* construct varibale name */
1519  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "job_%d_time_%d", v, t + est);
1520 
1521  SCIP_CALL( SCIPcreateVarBasic(subscip, &binvar, name, 0.0, 1.0, objval, SCIP_VARTYPE_BINARY) );
1522  SCIP_CALL( SCIPaddVar(subscip, binvar) );
1523 
1524  /* add binary varibale to the set partitioning constraint which ensures that the job is started */
1525  SCIP_CALL( SCIPaddCoefSetppc(subscip, cons, binvar) );
1526 
1527  binvars[v][t] = binvar;
1528  }
1529 
1530  /* add and release the set partitioning constraint */
1531  SCIP_CALL( SCIPaddCons(subscip, cons) );
1532  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
1533  }
1534 
1535  /* adjusted the smallest earliest start time and the largest latest completion time with the effective horizon */
1536  hmin = MAX(hmin, minest);
1537  hmax = MIN(hmax, maxlct);
1538  assert(hmin > INT_MIN);
1539  assert(hmax < INT_MAX);
1540  assert(hmin < hmax);
1541 
1542  /* create for each time a knapsack constraint which ensures that the resource capacity is not exceeded */
1543  for( t = hmin; t < hmax; ++t )
1544  {
1545  SCIP_CONS* cons;
1546 
1547  /* construct constraint name */
1548  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "time_%d", t);
1549 
1550  /* create an empty knapsack constraint */
1551  SCIP_CALL( SCIPcreateConsBasicKnapsack(subscip, &cons, name, 0, NULL, NULL, (SCIP_Longint)capacity) );
1552 
1553  /* add all jobs which potentially can be processed at that time point */
1554  for( v = 0; v < njobs; ++v )
1555  {
1556  int duration;
1557  int demand;
1558  int start;
1559  int end;
1560  int est;
1561  int lst;
1562  int k;
1563 
1564  est = ests[v];
1565  lst = lsts[v] ;
1566 
1567  duration = durations[v];
1568  assert(duration > 0);
1569 
1570  /* check if the varibale is processed potentially at time point t */
1571  if( t < est || t >= lst + duration )
1572  continue;
1573 
1574  demand = demands[v];
1575  assert(demand >= 0);
1576 
1577  start = MAX(t - duration + 1, est);
1578  end = MIN(t, lst);
1579 
1580  assert(start <= end);
1581 
1582  for( k = start; k <= end; ++k )
1583  {
1584  assert(binvars[v][k] != NULL);
1585  SCIP_CALL( SCIPaddCoefKnapsack(subscip, cons, binvars[v][k], (SCIP_Longint) demand) );
1586  }
1587  }
1588 
1589  /* add and release the knapsack constraint */
1590  SCIP_CALL( SCIPaddCons(subscip, cons) );
1591  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
1592  }
1593 
1594  /* do not abort subproblem on CTRL-C */
1595  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
1596 
1597  /* disable output to console */
1598  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
1599 
1600  /* set limits for the subproblem */
1601  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", maxnodes) );
1602  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
1603  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
1604 
1605  /* solve single cumulative constraint by branch and bound */
1606  retcode = SCIPsolve(subscip);
1607 
1608  if( retcode != SCIP_OKAY )
1609  (*error) = TRUE;
1610  else
1611  {
1612  SCIPdebugMsg(scip, "solved single cumulative condition with status %d\n", SCIPgetStatus(subscip));
1613 
1614  /* evaluated solution status */
1615  switch( SCIPgetStatus(subscip) )
1616  {
1617  case SCIP_STATUS_INFORUNBD:
1619  (*infeasible) = TRUE;
1620  (*solved) = TRUE;
1621  break;
1622  case SCIP_STATUS_UNBOUNDED:
1623  (*unbounded) = TRUE;
1624  (*solved) = TRUE;
1625  break;
1626  case SCIP_STATUS_OPTIMAL:
1627  {
1628  SCIP_SOL* sol;
1629 
1630  sol = SCIPgetBestSol(subscip);
1631  assert(sol != NULL);
1632 
1633  for( v = 0; v < njobs; ++v )
1634  {
1635  int timeinterval;
1636  int est;
1637  int lst;
1638 
1639  est = ests[v];
1640  lst = lsts[v];
1641 
1642  /* compute number of possible start points */
1643  timeinterval = lst - est + 1;
1644 
1645  /* check which binary varibale is set to one */
1646  for( t = 0; t < timeinterval; ++t )
1647  {
1648  if( SCIPgetSolVal(subscip, sol, binvars[v][t]) > 0.5 )
1649  {
1650  ests[v] = est + t;
1651  lsts[v] = est + t;
1652  break;
1653  }
1654  }
1655  }
1656 
1657  (*solved) = TRUE;
1658  break;
1659  }
1660  case SCIP_STATUS_NODELIMIT:
1662  case SCIP_STATUS_TIMELIMIT:
1663  case SCIP_STATUS_MEMLIMIT:
1665  /* transfer the global bound changes */
1666  for( v = 0; v < njobs; ++v )
1667  {
1668  int timeinterval;
1669  int est;
1670  int lst;
1671 
1672  est = ests[v];
1673  lst = lsts[v];
1674 
1675  /* compute number of possible start points */
1676  timeinterval = lst - est + 1;
1677 
1678  /* check which binary varibale is the first binary varibale which is not globally fixed to zero */
1679  for( t = 0; t < timeinterval; ++t )
1680  {
1681  if( SCIPvarGetUbGlobal(binvars[v][t]) > 0.5 )
1682  {
1683  ests[v] = est + t;
1684  break;
1685  }
1686  }
1687 
1688  /* check which binary varibale is the last binary varibale which is not globally fixed to zero */
1689  for( t = timeinterval - 1; t >= 0; --t )
1690  {
1691  if( SCIPvarGetUbGlobal(binvars[v][t]) > 0.5 )
1692  {
1693  lsts[v] = est + t;
1694  break;
1695  }
1696  }
1697  }
1698  (*solved) = FALSE;
1699  break;
1700 
1701  case SCIP_STATUS_UNKNOWN:
1703  case SCIP_STATUS_GAPLIMIT:
1704  case SCIP_STATUS_SOLLIMIT:
1706  SCIPerrorMessage("invalid status code <%d>\n", SCIPgetStatus(subscip));
1707  return SCIP_INVALIDDATA;
1708  }
1709  }
1710 
1711  /* release all variables */
1712  for( v = 0; v < njobs; ++v )
1713  {
1714  int timeinterval;
1715  int est;
1716  int lst;
1717 
1718  est = ests[v];
1719  lst = lsts[v];
1720 
1721  /* compute number of possible start points */
1722  timeinterval = lst - est + 1;
1723 
1724  for( t = 0; t < timeinterval; ++t )
1725  {
1726  SCIP_CALL( SCIPreleaseVar(subscip, &binvars[v][t]) );
1727  }
1728  SCIPfreeBufferArray(subscip, &binvars[v]);
1729  }
1730 
1731  SCIPfreeBufferArray(subscip, &binvars);
1732 
1733  SCIP_CALL( SCIPfree(&subscip) );
1734 
1735  return SCIP_OKAY;
1736 }
1737 #endif
1738 
1739 /**@} */
1740 
1741 /**@name Constraint handler data
1742  *
1743  * Method used to create and free the constraint handler data when including and removing the cumulative constraint
1744  * handler.
1745  *
1746  * @{
1747  */
1748 
1749 /** creates constaint handler data for cumulative constraint handler */
1750 static
1752  SCIP* scip, /**< SCIP data structure */
1753  SCIP_CONSHDLRDATA** conshdlrdata, /**< pointer to store the constraint handler data */
1754  SCIP_EVENTHDLR* eventhdlr /**< event handler */
1755  )
1756 {
1757  /* create precedence constraint handler data */
1758  assert(scip != NULL);
1759  assert(conshdlrdata != NULL);
1760  assert(eventhdlr != NULL);
1761 
1762  SCIP_CALL( SCIPallocBlockMemory(scip, conshdlrdata) );
1763 
1764  /* set event handler for checking if bounds of start time variables are tighten */
1765  (*conshdlrdata)->eventhdlr = eventhdlr;
1766 
1767  /* set default methed for solving single cumulative conditions using SCIP and a CP model */
1768  (*conshdlrdata)->solveCumulative = solveCumulativeViaScipCp;
1769 
1770 #ifdef SCIP_STATISTIC
1771  (*conshdlrdata)->nlbtimetable = 0;
1772  (*conshdlrdata)->nubtimetable = 0;
1773  (*conshdlrdata)->ncutofftimetable = 0;
1774  (*conshdlrdata)->nlbedgefinder = 0;
1775  (*conshdlrdata)->nubedgefinder = 0;
1776  (*conshdlrdata)->ncutoffedgefinder = 0;
1777  (*conshdlrdata)->ncutoffoverload = 0;
1778  (*conshdlrdata)->ncutoffoverloadTTEF = 0;
1779 
1780  (*conshdlrdata)->nirrelevantjobs = 0;
1781  (*conshdlrdata)->nalwaysruns = 0;
1782  (*conshdlrdata)->nremovedlocks = 0;
1783  (*conshdlrdata)->ndualfixs = 0;
1784  (*conshdlrdata)->ndecomps = 0;
1785  (*conshdlrdata)->ndualbranchs = 0;
1786  (*conshdlrdata)->nallconsdualfixs = 0;
1787  (*conshdlrdata)->naddedvarbounds = 0;
1788  (*conshdlrdata)->naddeddisjunctives = 0;
1789 #endif
1790 
1791  return SCIP_OKAY;
1792 }
1793 
1794 /** frees constraint handler data for logic or constraint handler */
1795 static
1796 void conshdlrdataFree(
1797  SCIP* scip, /**< SCIP data structure */
1798  SCIP_CONSHDLRDATA** conshdlrdata /**< pointer to the constraint handler data */
1799  )
1800 {
1801  assert(conshdlrdata != NULL);
1802  assert(*conshdlrdata != NULL);
1803 
1804  SCIPfreeBlockMemory(scip, conshdlrdata);
1805 }
1806 
1807 /**@} */
1808 
1809 
1810 /**@name Constraint data methods
1811  *
1812  * @{
1813  */
1814 
1815 /** catches bound change events for all variables in transformed cumulative constraint */
1816 static
1818  SCIP* scip, /**< SCIP data structure */
1819  SCIP_CONSDATA* consdata, /**< cumulative constraint data */
1820  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
1821  )
1822 {
1823  int v;
1824 
1825  assert(scip != NULL);
1826  assert(consdata != NULL);
1827  assert(eventhdlr != NULL);
1828 
1829  /* catch event for every single variable */
1830  for( v = 0; v < consdata->nvars; ++v )
1831  {
1832  SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[v],
1833  SCIP_EVENTTYPE_BOUNDTIGHTENED, eventhdlr, (SCIP_EVENTDATA*)consdata, NULL) );
1834  }
1835 
1836  return SCIP_OKAY;
1837 }
1838 
1839 /** drops events for variable at given position */
1840 static
1842  SCIP* scip, /**< SCIP data structure */
1843  SCIP_CONSDATA* consdata, /**< cumulative constraint data */
1844  SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
1845  int pos /**< array position of variable to catch bound change events for */
1846  )
1847 {
1848  assert(scip != NULL);
1849  assert(consdata != NULL);
1850  assert(eventhdlr != NULL);
1851  assert(0 <= pos && pos < consdata->nvars);
1852  assert(consdata->vars[pos] != NULL);
1853 
1854  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[pos],
1855  SCIP_EVENTTYPE_BOUNDTIGHTENED, eventhdlr, (SCIP_EVENTDATA*)consdata, -1) );
1856 
1857  return SCIP_OKAY;
1858 }
1859 
1860 /** drops bound change events for all variables in transformed linear constraint */
1861 static
1863  SCIP* scip, /**< SCIP data structure */
1864  SCIP_CONSDATA* consdata, /**< linear constraint data */
1865  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
1866  )
1867 {
1868  int v;
1869 
1870  assert(scip != NULL);
1871  assert(consdata != NULL);
1872 
1873  /* drop event of every single variable */
1874  for( v = 0; v < consdata->nvars; ++v )
1875  {
1876  SCIP_CALL( consdataDropEvents(scip, consdata, eventhdlr, v) );
1877  }
1878 
1879  return SCIP_OKAY;
1880 }
1881 
1882 /** initialize variable lock data structure */
1883 static
1884 void initializeLocks(
1885  SCIP_CONSDATA* consdata, /**< constraint data */
1886  SCIP_Bool locked /**< should the variable be locked? */
1887  )
1888 {
1889  int nvars;
1890  int v;
1891 
1892  nvars = consdata->nvars;
1893 
1894  /* initialize locking arrays */
1895  for( v = 0; v < nvars; ++v )
1896  {
1897  consdata->downlocks[v] = locked;
1898  consdata->uplocks[v] = locked;
1899  }
1900 }
1901 
1902 /** creates constraint data of cumulative constraint */
1903 static
1905  SCIP* scip, /**< SCIP data structure */
1906  SCIP_CONSDATA** consdata, /**< pointer to consdata */
1907  SCIP_VAR** vars, /**< array of integer variables */
1908  SCIP_CONS** linkingconss, /**< array of linking constraints for the integer variables, or NULL */
1909  int* durations, /**< array containing corresponding durations */
1910  int* demands, /**< array containing corresponding demands */
1911  int nvars, /**< number of variables */
1912  int capacity, /**< available cumulative capacity */
1913  int hmin, /**< left bound of time axis to be considered (including hmin) */
1914  int hmax, /**< right bound of time axis to be considered (not including hmax) */
1915  SCIP_Bool check /**< is the corresponding constraint a check constraint */
1916  )
1917 {
1918  int v;
1919 
1920  assert(scip != NULL);
1921  assert(consdata != NULL);
1922  assert(vars != NULL || nvars > 0);
1923  assert(demands != NULL);
1924  assert(durations != NULL);
1925  assert(capacity >= 0);
1926  assert(hmin >= 0);
1927  assert(hmin < hmax);
1928 
1929  /* create constraint data */
1930  SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
1931 
1932  (*consdata)->hmin = hmin;
1933  (*consdata)->hmax = hmax;
1934 
1935  (*consdata)->capacity = capacity;
1936  (*consdata)->demandrows = NULL;
1937  (*consdata)->demandrowssize = 0;
1938  (*consdata)->ndemandrows = 0;
1939  (*consdata)->scoverrows = NULL;
1940  (*consdata)->nscoverrows = 0;
1941  (*consdata)->scoverrowssize = 0;
1942  (*consdata)->bcoverrows = NULL;
1943  (*consdata)->nbcoverrows = 0;
1944  (*consdata)->bcoverrowssize = 0;
1945  (*consdata)->nvars = nvars;
1946  (*consdata)->varssize = nvars;
1947  (*consdata)->signature = 0;
1948  (*consdata)->validsignature = FALSE;
1949  (*consdata)->normalized = FALSE;
1950  (*consdata)->covercuts = FALSE;
1951  (*consdata)->propagated = FALSE;
1952  (*consdata)->varbounds = FALSE;
1953  (*consdata)->triedsolving = FALSE;
1954 
1955  if( nvars > 0 )
1956  {
1957  assert(vars != NULL); /* for flexelint */
1958 
1959  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->vars, vars, nvars) );
1960  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->demands, demands, nvars) );
1961  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->durations, durations, nvars) );
1962  (*consdata)->linkingconss = NULL;
1963 
1964  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->downlocks, nvars) );
1965  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->uplocks, nvars) );
1966 
1967  /* initialize variable lock data structure; the locks are only used if the constraint is a check constraint */
1968  initializeLocks(*consdata, check);
1969 
1970  if( linkingconss != NULL )
1971  {
1972  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->linkingconss, linkingconss, nvars) );
1973  }
1974 
1975  /* transform variables, if they are not yet transformed */
1976  if( SCIPisTransformed(scip) )
1977  {
1978  SCIPdebugMsg(scip, "get tranformed variables and constraints\n");
1979 
1980  /* get transformed variables and do NOT captures these */
1981  SCIP_CALL( SCIPgetTransformedVars(scip, (*consdata)->nvars, (*consdata)->vars, (*consdata)->vars) );
1982 
1983  /* multi-aggregated variables cannot be replaced by active variable; therefore we mark all variables for not
1984  * been multi-aggregated
1985  */
1986  for( v = 0; v < nvars; ++v )
1987  {
1988  SCIP_CALL( SCIPmarkDoNotMultaggrVar(scip, (*consdata)->vars[v]) );
1989  }
1990 
1991  if( linkingconss != NULL )
1992  {
1993  /* get transformed constraints and captures these */
1994  SCIP_CALL( SCIPtransformConss(scip, (*consdata)->nvars, (*consdata)->linkingconss, (*consdata)->linkingconss) );
1995 
1996  for( v = 0; v < nvars; ++v )
1997  assert(SCIPgetConsLinking(scip, (*consdata)->vars[v]) == (*consdata)->linkingconss[v]);
1998  }
1999  }
2000 
2001 #ifndef NDEBUG
2002  /* only binary and integer variables can be used in cumulative constraints
2003  * for fractional variable values, the constraint cannot be checked
2004  */
2005  for( v = 0; v < (*consdata)->nvars; ++v )
2006  assert(SCIPvarGetType((*consdata)->vars[v]) <= SCIP_VARTYPE_INTEGER);
2007 #endif
2008  }
2009  else
2010  {
2011  (*consdata)->vars = NULL;
2012  (*consdata)->downlocks = NULL;
2013  (*consdata)->uplocks = NULL;
2014  (*consdata)->demands = NULL;
2015  (*consdata)->durations = NULL;
2016  (*consdata)->linkingconss = NULL;
2017  }
2018 
2019  /* initialize values for running propagation algorithms efficiently */
2020  (*consdata)->resstrength1 = -1.0;
2021  (*consdata)->resstrength2 = -1.0;
2022  (*consdata)->cumfactor1 = -1.0;
2023  (*consdata)->disjfactor1 = -1.0;
2024  (*consdata)->disjfactor2 = -1.0;
2025  (*consdata)->estimatedstrength = -1.0;
2026 
2027  SCIPstatistic( (*consdata)->maxpeak = -1 );
2028 
2029  return SCIP_OKAY;
2030 }
2031 
2032 /** releases LP rows of constraint data and frees rows array */
2033 static
2035  SCIP* scip, /**< SCIP data structure */
2036  SCIP_CONSDATA** consdata /**< constraint data */
2037  )
2038 {
2039  int r;
2040 
2041  assert(consdata != NULL);
2042  assert(*consdata != NULL);
2043 
2044  for( r = 0; r < (*consdata)->ndemandrows; ++r )
2045  {
2046  assert((*consdata)->demandrows[r] != NULL);
2047  SCIP_CALL( SCIPreleaseRow(scip, &(*consdata)->demandrows[r]) );
2048  }
2049 
2050  SCIPfreeBlockMemoryArrayNull(scip, &(*consdata)->demandrows, (*consdata)->demandrowssize);
2051 
2052  (*consdata)->ndemandrows = 0;
2053  (*consdata)->demandrowssize = 0;
2054 
2055  /* free rows of cover cuts */
2056  for( r = 0; r < (*consdata)->nscoverrows; ++r )
2057  {
2058  assert((*consdata)->scoverrows[r] != NULL);
2059  SCIP_CALL( SCIPreleaseRow(scip, &(*consdata)->scoverrows[r]) );
2060  }
2061 
2062  SCIPfreeBlockMemoryArrayNull(scip, &(*consdata)->scoverrows, (*consdata)->scoverrowssize);
2063 
2064  (*consdata)->nscoverrows = 0;
2065  (*consdata)->scoverrowssize = 0;
2066 
2067  for( r = 0; r < (*consdata)->nbcoverrows; ++r )
2068  {
2069  assert((*consdata)->bcoverrows[r] != NULL);
2070  SCIP_CALL( SCIPreleaseRow(scip, &(*consdata)->bcoverrows[r]) );
2071  }
2072 
2073  SCIPfreeBlockMemoryArrayNull(scip, &(*consdata)->bcoverrows, (*consdata)->bcoverrowssize);
2074 
2075  (*consdata)->nbcoverrows = 0;
2076  (*consdata)->bcoverrowssize = 0;
2077 
2078  (*consdata)->covercuts = FALSE;
2079 
2080  return SCIP_OKAY;
2081 }
2082 
2083 /** frees a cumulative constraint data */
2084 static
2086  SCIP* scip, /**< SCIP data structure */
2087  SCIP_CONSDATA** consdata /**< pointer to linear constraint data */
2088  )
2089 {
2090  int varssize;
2091  int nvars;
2092 
2093  assert(consdata != NULL);
2094  assert(*consdata != NULL);
2095 
2096  nvars = (*consdata)->nvars;
2097  varssize = (*consdata)->varssize;
2098 
2099  if( varssize > 0 )
2100  {
2101  int v;
2102 
2103  /* release and free the rows */
2104  SCIP_CALL( consdataFreeRows(scip, consdata) );
2105 
2106  /* release the linking constraints if they were generated */
2107  if( (*consdata)->linkingconss != NULL )
2108  {
2109  for( v = nvars-1; v >= 0; --v )
2110  {
2111  assert((*consdata)->linkingconss[v] != NULL );
2112  SCIP_CALL( SCIPreleaseCons(scip, &(*consdata)->linkingconss[v]) );
2113  }
2114 
2115  SCIPfreeBlockMemoryArray(scip, &(*consdata)->linkingconss, varssize);
2116  }
2117 
2118  /* free arrays */
2119  SCIPfreeBlockMemoryArray(scip, &(*consdata)->downlocks, varssize);
2120  SCIPfreeBlockMemoryArray(scip, &(*consdata)->uplocks, varssize);
2121  SCIPfreeBlockMemoryArray(scip, &(*consdata)->durations, varssize);
2122  SCIPfreeBlockMemoryArray(scip, &(*consdata)->demands, varssize);
2123  SCIPfreeBlockMemoryArray(scip, &(*consdata)->vars, varssize);
2124  }
2125 
2126  /* free memory */
2127  SCIPfreeBlockMemory(scip, consdata);
2128 
2129  return SCIP_OKAY;
2130 }
2131 
2132 /** prints cumulative constraint to file stream */
2133 static
2134 void consdataPrint(
2135  SCIP* scip, /**< SCIP data structure */
2136  SCIP_CONSDATA* consdata, /**< cumulative constraint data */
2137  FILE* file /**< output file (or NULL for standard output) */
2138  )
2139 {
2140  int v;
2141 
2142  assert(consdata != NULL);
2143 
2144  /* print coefficients */
2145  SCIPinfoMessage( scip, file, "cumulative(");
2146 
2147  for( v = 0; v < consdata->nvars; ++v )
2148  {
2149  assert(consdata->vars[v] != NULL);
2150  if( v > 0 )
2151  SCIPinfoMessage(scip, file, ", ");
2152  SCIPinfoMessage(scip, file, "<%s>[%g,%g](%d)[%d]", SCIPvarGetName(consdata->vars[v]),
2153  SCIPvarGetLbGlobal(consdata->vars[v]), SCIPvarGetUbGlobal(consdata->vars[v]),
2154  consdata->durations[v], consdata->demands[v]);
2155  }
2156  SCIPinfoMessage(scip, file, ")[%d,%d) <= %d", consdata->hmin, consdata->hmax, consdata->capacity);
2157 }
2158 
2159 /** deletes coefficient at given position from constraint data */
2160 static
2162  SCIP* scip, /**< SCIP data structure */
2163  SCIP_CONSDATA* consdata, /**< cumulative constraint data */
2164  SCIP_CONS* cons, /**< knapsack constraint */
2165  int pos /**< position of coefficient to delete */
2166  )
2167 {
2168  SCIP_CONSHDLR* conshdlr;
2169  SCIP_CONSHDLRDATA* conshdlrdata;
2170 
2171  assert(scip != NULL);
2172  assert(consdata != NULL);
2173  assert(cons != NULL);
2174  assert(SCIPconsIsTransformed(cons));
2175  assert(!SCIPinProbing(scip));
2176 
2177  SCIPdebugMsg(scip, "cumulative constraint <%s>: remove variable <%s>\n",
2178  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[pos]));
2179 
2180  /* remove the rounding locks for the deleted variable */
2181  SCIP_CALL( SCIPunlockVarCons(scip, consdata->vars[pos], cons, consdata->downlocks[pos], consdata->uplocks[pos]) );
2182 
2183  consdata->downlocks[pos] = FALSE;
2184  consdata->uplocks[pos] = FALSE;
2185 
2186  if( consdata->linkingconss != NULL )
2187  {
2188  SCIP_CALL( SCIPreleaseCons(scip, &consdata->linkingconss[pos]) );
2189  }
2190 
2191  /* get event handler */
2192  conshdlr = SCIPconsGetHdlr(cons);
2193  assert(conshdlr != NULL);
2194  conshdlrdata = SCIPconshdlrGetData(conshdlr);
2195  assert(conshdlrdata != NULL);
2196  assert(conshdlrdata->eventhdlr != NULL);
2197 
2198  /* drop events */
2199  SCIP_CALL( consdataDropEvents(scip, consdata, conshdlrdata->eventhdlr, pos) );
2200 
2201  SCIPdebugMsg(scip, "remove variable <%s>[%g,%g] from cumulative constraint <%s>\n",
2202  SCIPvarGetName(consdata->vars[pos]), SCIPvarGetLbGlobal(consdata->vars[pos]), SCIPvarGetUbGlobal(consdata->vars[pos]), SCIPconsGetName(cons));
2203 
2204  /* in case the we did not remove the variable in the last slot of the arrays we move the current last to this
2205  * position
2206  */
2207  if( pos != consdata->nvars - 1 )
2208  {
2209  consdata->vars[pos] = consdata->vars[consdata->nvars-1];
2210  consdata->downlocks[pos] = consdata->downlocks[consdata->nvars-1];
2211  consdata->uplocks[pos] = consdata->uplocks[consdata->nvars-1];
2212  consdata->demands[pos] = consdata->demands[consdata->nvars-1];
2213  consdata->durations[pos] = consdata->durations[consdata->nvars-1];
2214 
2215  if( consdata->linkingconss != NULL )
2216  {
2217  consdata->linkingconss[pos]= consdata->linkingconss[consdata->nvars-1];
2218  }
2219  }
2220 
2221  consdata->nvars--;
2222  consdata->validsignature = FALSE;
2223  consdata->normalized = FALSE;
2224 
2225  return SCIP_OKAY;
2226 }
2227 
2228 /** collect linking constraints for each integer variable */
2229 static
2231  SCIP* scip, /**< SCIP data structure */
2232  SCIP_CONSDATA* consdata /**< pointer to consdata */
2233  )
2234 {
2235  int nvars;
2236  int v;
2237 
2238  assert(scip != NULL);
2239  assert(consdata != NULL);
2240 
2241  nvars = consdata->nvars;
2242  assert(nvars > 0);
2243  assert(consdata->linkingconss == NULL);
2244 
2245  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &consdata->linkingconss, consdata->varssize) );
2246 
2247  for( v = 0; v < nvars; ++v )
2248  {
2249  SCIP_CONS* cons;
2250  SCIP_VAR* var;
2251 
2252  var = consdata->vars[v];
2253  assert(var != NULL);
2254 
2255  SCIPdebugMsg(scip, "linking constraint (%d of %d) for variable <%s>\n", v+1, nvars, SCIPvarGetName(var));
2256 
2257  /* create linking constraint if it does not exist yet */
2258  if( !SCIPexistsConsLinking(scip, var) )
2259  {
2260  char name[SCIP_MAXSTRLEN];
2261 
2262  (void)SCIPsnprintf(name, SCIP_MAXSTRLEN, "link(%s)", SCIPvarGetName(var));
2263 
2264  /* creates and captures an linking constraint */
2265  SCIP_CALL( SCIPcreateConsLinking(scip, &cons, name, var, NULL, 0, 0,
2266  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE /*TRUE*/, FALSE) );
2267  SCIP_CALL( SCIPaddCons(scip, cons) );
2268  consdata->linkingconss[v] = cons;
2269  }
2270  else
2271  {
2272  consdata->linkingconss[v] = SCIPgetConsLinking(scip, var);
2273  SCIP_CALL( SCIPcaptureCons(scip, consdata->linkingconss[v]) );
2274  }
2275 
2276  assert(SCIPexistsConsLinking(scip, var));
2277  assert(consdata->linkingconss[v] != NULL);
2278  assert(strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(consdata->linkingconss[v])), "linking") == 0 );
2279  assert(SCIPgetConsLinking(scip, var) == consdata->linkingconss[v]);
2280  }
2281 
2282  return SCIP_OKAY;
2283 }
2284 
2285 /**@} */
2286 
2287 
2288 /**@name Check methods
2289  *
2290  * @{
2291  */
2292 
2293 /** check for the given starting time variables with their demands and durations if the cumulative conditions for the
2294  * given solution is satisfied
2295  */
2296 static
2298  SCIP* scip, /**< SCIP data structure */
2299  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
2300  int nvars, /**< number of variables (jobs) */
2301  SCIP_VAR** vars, /**< array of integer variable which corresponds to starting times for a job */
2302  int* durations, /**< array containing corresponding durations */
2303  int* demands, /**< array containing corresponding demands */
2304  int capacity, /**< available cumulative capacity */
2305  int hmin, /**< left bound of time axis to be considered (including hmin) */
2306  int hmax, /**< right bound of time axis to be considered (not including hmax) */
2307  SCIP_Bool* violated, /**< pointer to store if the cumulative condition is violated */
2308  SCIP_CONS* cons, /**< constraint which is checked */
2309  SCIP_Bool printreason /**< should the reason for the violation be printed? */
2310  )
2311 {
2312  int* startsolvalues; /* stores when each job is starting */
2313  int* endsolvalues; /* stores when each job ends */
2314  int* startindices; /* we will sort the startsolvalues, thus we need to know which index of a job it corresponds to */
2315  int* endindices; /* we will sort the endsolvalues, thus we need to know which index of a job it corresponds to */
2316 
2317  int freecapacity;
2318  int curtime; /* point in time which we are just checking */
2319  int endindex; /* index of endsolvalues with: endsolvalues[endindex] > curtime */
2320  int j;
2321 
2322  SCIP_Real absviol;
2323  SCIP_Real relviol;
2324 
2325  assert(scip != NULL);
2326  assert(violated != NULL);
2327 
2328  (*violated) = FALSE;
2329 
2330  if( nvars == 0 )
2331  return SCIP_OKAY;
2332 
2333  assert(vars != NULL);
2334  assert(demands != NULL);
2335  assert(durations != NULL);
2336 
2337  /* compute time points where we have to check whether capacity constraint is infeasible or not */
2338  SCIP_CALL( SCIPallocBufferArray(scip, &startsolvalues, nvars) );
2339  SCIP_CALL( SCIPallocBufferArray(scip, &endsolvalues, nvars) );
2340  SCIP_CALL( SCIPallocBufferArray(scip, &startindices, nvars) );
2341  SCIP_CALL( SCIPallocBufferArray(scip, &endindices, nvars) );
2342 
2343  /* assign variables, start and endpoints to arrays */
2344  for ( j = 0; j < nvars; ++j )
2345  {
2346  int solvalue;
2347 
2348  /* the constraint of the cumulative constraint handler should be called after the integrality check */
2349  assert(SCIPisFeasIntegral(scip, SCIPgetSolVal(scip, sol, vars[j])));
2350 
2351  solvalue = SCIPconvertRealToInt(scip, SCIPgetSolVal(scip, sol, vars[j]));
2352 
2353  /* we need to ensure that we check at least one time point during the effective horizon; therefore we project all
2354  * jobs which start before hmin to hmin
2355  */
2356  startsolvalues[j] = MAX(solvalue, hmin);
2357  startindices[j] = j;
2358 
2359  endsolvalues[j] = MAX(solvalue + durations[j], hmin);
2360  endindices[j] = j;
2361  }
2362 
2363  /* sort the arrays not-decreasing according to start solution values and end solution values (and sort the
2364  * corresponding indices in the same way)
2365  */
2366  SCIPsortIntInt(startsolvalues, startindices, nvars);
2367  SCIPsortIntInt(endsolvalues, endindices, nvars);
2368 
2369  endindex = 0;
2370  freecapacity = capacity;
2371  absviol = 0.0;
2372  relviol = 0.0;
2373 
2374  /* check each start point of a job whether the capacity is kept or not */
2375  for( j = 0; j < nvars; ++j )
2376  {
2377  /* only check intervals [hmin,hmax) */
2378  curtime = startsolvalues[j];
2379 
2380  if( curtime >= hmax )
2381  break;
2382 
2383  /* subtract all capacity needed up to this point */
2384  freecapacity -= demands[startindices[j]];
2385  while( j+1 < nvars && startsolvalues[j+1] == curtime )
2386  {
2387  j++;
2388  freecapacity -= demands[startindices[j]];
2389  }
2390 
2391  /* free all capacity usages of jobs that are no longer running */
2392  while( endindex < nvars && curtime >= endsolvalues[endindex] )
2393  {
2394  freecapacity += demands[endindices[endindex]];
2395  ++endindex;
2396  }
2397  assert(freecapacity <= capacity);
2398 
2399  /* update absolute and relative violation */
2400  if( absviol < (SCIP_Real) (-freecapacity) )
2401  {
2402  absviol = -freecapacity;
2403  relviol = SCIPrelDiff((SCIP_Real)(capacity - freecapacity), (SCIP_Real)capacity);
2404  }
2405 
2406  /* check freecapacity to be smaller than zero */
2407  if( freecapacity < 0 && curtime >= hmin )
2408  {
2409  SCIPdebugMsg(scip, "freecapacity = %3d\n", freecapacity);
2410  (*violated) = TRUE;
2411 
2412  if( printreason )
2413  {
2414  int i;
2415 
2416  /* first state the violated constraints */
2417  SCIP_CALL( SCIPprintCons(scip, cons, NULL) );
2418 
2419  /* second state the reason */
2420  SCIPinfoMessage(scip, NULL,
2421  ";\nviolation: at time point %d available capacity = %d, needed capacity = %d\n",
2422  curtime, capacity, capacity - freecapacity);
2423 
2424  for( i = 0; i <= j; ++i )
2425  {
2426  if( startsolvalues[i] + durations[startindices[i]] > curtime )
2427  {
2428  SCIPinfoMessage(scip, NULL, "activity %s, start = %i, duration = %d, demand = %d \n",
2429  SCIPvarGetName(vars[startindices[i]]), startsolvalues[i], durations[startindices[i]],
2430  demands[startindices[i]]);
2431  }
2432  }
2433  }
2434  break;
2435  }
2436  } /*lint --e{850}*/
2437 
2438  /* update constraint violation in solution */
2439  if( sol != NULL )
2440  SCIPupdateSolConsViolation(scip, sol, absviol, relviol);
2441 
2442  /* free all buffer arrays */
2443  SCIPfreeBufferArray(scip, &endindices);
2444  SCIPfreeBufferArray(scip, &startindices);
2445  SCIPfreeBufferArray(scip, &endsolvalues);
2446  SCIPfreeBufferArray(scip, &startsolvalues);
2447 
2448  return SCIP_OKAY;
2449 }
2450 
2451 /** check if the given constrait is valid; checks each starting point of a job whether the remaining capacity is at
2452  * least zero or not. If not (*violated) is set to TRUE
2453  */
2454 static
2456  SCIP* scip, /**< SCIP data structure */
2457  SCIP_CONS* cons, /**< constraint to be checked */
2458  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
2459  SCIP_Bool* violated, /**< pointer to store if the constraint is violated */
2460  SCIP_Bool printreason /**< should the reason for the violation be printed? */
2461  )
2462 {
2463  SCIP_CONSDATA* consdata;
2464 
2465  assert(scip != NULL);
2466  assert(cons != NULL);
2467  assert(violated != NULL);
2468 
2469  SCIPdebugMsg(scip, "check cumulative constraints <%s>\n", SCIPconsGetName(cons));
2470 
2471  consdata = SCIPconsGetData(cons);
2472  assert(consdata != NULL);
2473 
2474  /* check the cumulative condition */
2475  SCIP_CALL( checkCumulativeCondition(scip, sol, consdata->nvars, consdata->vars,
2476  consdata->durations, consdata->demands, consdata->capacity, consdata->hmin, consdata->hmax,
2477  violated, cons, printreason) );
2478 
2479  return SCIP_OKAY;
2480 }
2481 
2482 /**@} */
2483 
2484 /**@name Conflict analysis
2485  *
2486  * @{
2487  */
2488 
2489 /** resolves the propagation of the core time algorithm */
2490 static
2492  SCIP* scip, /**< SCIP data structure */
2493  int nvars, /**< number of start time variables (activities) */
2494  SCIP_VAR** vars, /**< array of start time variables */
2495  int* durations, /**< array of durations */
2496  int* demands, /**< array of demands */
2497  int capacity, /**< cumulative capacity */
2498  int hmin, /**< left bound of time axis to be considered (including hmin) */
2499  int hmax, /**< right bound of time axis to be considered (not including hmax) */
2500  SCIP_VAR* infervar, /**< inference variable */
2501  int inferdemand, /**< demand of the inference variable */
2502  int inferpeak, /**< time point which causes the propagation */
2503  int relaxedpeak, /**< relaxed time point which would be sufficient to be proved */
2504  SCIP_BDCHGIDX* bdchgidx, /**< the index of the bound change, representing the point of time where the change took place */
2505  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
2506  int* provedpeak, /**< pointer to store the actually proved peak, or NULL */
2507  SCIP_Bool* explanation /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
2508  )
2509 {
2510  SCIP_VAR* var;
2511  SCIP_Bool* reported;
2512  int duration;
2513  int maxlst;
2514  int minect;
2515  int ect;
2516  int lst;
2517  int j;
2518 
2519  assert(SCIPgetStage(scip) == SCIP_STAGE_SOLVING || SCIPinProbing(scip));
2520 
2521  SCIPdebugMsg(scip, "variable <%s>: (demand %d) resolve propagation of core time algorithm (peak %d)\n",
2522  SCIPvarGetName(infervar), inferdemand, inferpeak);
2523  assert(nvars > 0);
2524 
2525  /* adjusted capacity */
2526  capacity -= inferdemand;
2527  maxlst = INT_MIN;
2528  minect = INT_MAX;
2529 
2530  SCIP_CALL( SCIPallocBufferArray(scip, &reported, nvars) );
2531  BMSclearMemoryArray(reported, nvars);
2532 
2533  /* first we loop over all variables and adjust the capacity with those jobs which provide a global core at the
2534  * inference peak and those where the current conflict bounds provide a core at the inference peak
2535  */
2536  for( j = 0; j < nvars && capacity >= 0; ++j )
2537  {
2538  var = vars[j];
2539  assert(var != NULL);
2540 
2541  /* skip inference variable */
2542  if( var == infervar )
2543  continue;
2544 
2545  duration = durations[j];
2546  assert(duration > 0);
2547 
2548  /* compute cores of jobs; if core overlaps interval of inference variable add this job to the array */
2549  assert(!SCIPvarIsActive(var) || SCIPisFeasEQ(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, TRUE), SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE)));
2550  assert(SCIPisFeasIntegral(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, TRUE)));
2551  assert(!SCIPvarIsActive(var) || SCIPisFeasEQ(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, TRUE), SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)));
2552  assert(SCIPisFeasIntegral(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, TRUE)));
2553 
2554  SCIPdebugMsg(scip, "variable <%s>: glb=[%g,%g] conflict=[%g,%g] (duration %d, demand %d)\n",
2556  SCIPgetConflictVarLb(scip, var), SCIPgetConflictVarUb(scip, var), duration, demands[j]);
2557 
2558  ect = SCIPconvertRealToInt(scip, SCIPvarGetLbGlobal(var)) + duration;
2559  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbGlobal(var));
2560 
2561  /* check if the inference peak is part of the global bound core; if so we decreasing the capacity by the demand of
2562  * that job without adding it the explanation
2563  */
2564  if( inferpeak < ect && lst <= inferpeak )
2565  {
2566  capacity -= demands[j];
2567  reported[j] = TRUE;
2568 
2569  maxlst = MAX(maxlst, lst);
2570  minect = MIN(minect, ect);
2571  assert(maxlst < minect);
2572 
2573  if( explanation != NULL )
2574  explanation[j] = TRUE;
2575 
2576  continue;
2577  }
2578 
2579  /* collect the conflict bound core (the conflict bounds are those bounds which are already part of the conflict)
2580  * hence these bound are already reported by other resolve propation steps. In case a bound (lower or upper) is
2581  * not part of the conflict yet we get the global bounds back.
2582  */
2583  ect = SCIPconvertRealToInt(scip, SCIPgetConflictVarLb(scip, var)) + duration;
2584  lst = SCIPconvertRealToInt(scip, SCIPgetConflictVarUb(scip, var));
2585 
2586  /* check if the inference peak is part of the conflict bound core; if so we decreasing the capacity by the demand
2587  * of that job without and collect the job as part of the explanation
2588  *
2589  * @note we do not need to reported that job to SCIP since the required bounds are already reported
2590  */
2591  if( inferpeak < ect && lst <= inferpeak )
2592  {
2593  capacity -= demands[j];
2594  reported[j] = TRUE;
2595 
2596  maxlst = MAX(maxlst, lst);
2597  minect = MIN(minect, ect);
2598  assert(maxlst < minect);
2599 
2600  if( explanation != NULL )
2601  explanation[j] = TRUE;
2602  }
2603  }
2604 
2605  if( capacity >= 0 )
2606  {
2607  int* cands;
2608  int* canddemands;
2609  int ncands;
2610  int c;
2611 
2612  SCIP_CALL( SCIPallocBufferArray(scip, &cands, nvars) );
2613  SCIP_CALL( SCIPallocBufferArray(scip, &canddemands, nvars) );
2614  ncands = 0;
2615 
2616  /* collect all cores of the variables which lay in the considered time window except the inference variable */
2617  for( j = 0; j < nvars; ++j )
2618  {
2619  var = vars[j];
2620  assert(var != NULL);
2621 
2622  /* skip inference variable */
2623  if( var == infervar || reported[j] )
2624  continue;
2625 
2626  duration = durations[j];
2627  assert(duration > 0);
2628 
2629  /* compute cores of jobs; if core overlaps interval of inference variable add this job to the array */
2630  assert(!SCIPvarIsActive(var) || SCIPisFeasEQ(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, TRUE), SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE)));
2631  assert(SCIPisFeasIntegral(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, TRUE)));
2632  assert(!SCIPvarIsActive(var) || SCIPisFeasEQ(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, TRUE), SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)));
2633  assert(SCIPisFeasIntegral(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, TRUE)));
2634 
2635  /* collect local core information */
2636  ect = SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)) + duration;
2637  lst = SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE));
2638 
2639  SCIPdebugMsg(scip, "variable <%s>: loc=[%g,%g] glb=[%g,%g] (duration %d, demand %d)\n",
2640  SCIPvarGetName(var), SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE), SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE),
2641  SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), duration, demands[j]);
2642 
2643  /* check if the inference peak is part of the core */
2644  if( inferpeak < ect && lst <= inferpeak )
2645  {
2646  cands[ncands] = j;
2647  canddemands[ncands] = demands[j];
2648  ncands++;
2649 
2650  capacity -= demands[j];
2651  }
2652  }
2653 
2654  /* sort candidates indices w.r.t. their demands */
2655  SCIPsortDownIntInt(canddemands, cands, ncands);
2656 
2657  assert(capacity < 0);
2658  assert(ncands > 0);
2659 
2660  /* greedily remove candidates form the list such that the needed capacity is still exceeded */
2661  while( capacity + canddemands[ncands-1] < 0 )
2662  {
2663  ncands--;
2664  capacity += canddemands[ncands];
2665  assert(ncands > 0);
2666  }
2667 
2668  /* compute the size (number of time steps) of the job cores */
2669  for( c = 0; c < ncands; ++c )
2670  {
2671  var = vars[cands[c]];
2672  assert(var != NULL);
2673 
2674  duration = durations[cands[c]];
2675 
2676  ect = SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)) + duration;
2677  lst = SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE));
2678 
2679  maxlst = MAX(maxlst, lst);
2680  minect = MIN(minect, ect);
2681  assert(maxlst < minect);
2682  }
2683 
2684  SCIPdebugMsg(scip, "infer peak %d, relaxed peak %d, lst %d, ect %d\n", inferpeak, relaxedpeak, maxlst, minect);
2685  assert(inferpeak >= maxlst);
2686  assert(inferpeak < minect);
2687 
2688  /* check if the collect variable are sufficient to prove the relaxed bound (relaxedpeak) */
2689  if( relaxedpeak < inferpeak )
2690  {
2691  inferpeak = MAX(maxlst, relaxedpeak);
2692  }
2693  else if( relaxedpeak > inferpeak )
2694  {
2695  inferpeak = MIN(minect-1, relaxedpeak);
2696  }
2697  assert(inferpeak >= hmin);
2698  assert(inferpeak < hmax);
2699  assert(inferpeak >= maxlst);
2700  assert(inferpeak < minect);
2701 
2702  /* post all necessary bound changes */
2703  for( c = 0; c < ncands; ++c )
2704  {
2705  var = vars[cands[c]];
2706  assert(var != NULL);
2707 
2708  if( usebdwidening )
2709  {
2710  duration = durations[cands[c]];
2711 
2712  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, var, bdchgidx, (SCIP_Real)(inferpeak - duration + 1)) );
2713  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, var, bdchgidx, (SCIP_Real)inferpeak) );
2714  }
2715  else
2716  {
2717  SCIP_CALL( SCIPaddConflictLb(scip, var, bdchgidx) );
2718  SCIP_CALL( SCIPaddConflictUb(scip, var, bdchgidx) );
2719  }
2720 
2721  if( explanation != NULL )
2722  explanation[cands[c]] = TRUE;
2723  }
2724 
2725  SCIPfreeBufferArray(scip, &canddemands);
2726  SCIPfreeBufferArray(scip, &cands);
2727  }
2728 
2729  SCIPfreeBufferArray(scip, &reported);
2730 
2731  if( provedpeak != NULL )
2732  *provedpeak = inferpeak;
2733 
2734  return SCIP_OKAY;
2735 }
2736 
2737 #if 0
2738 /** repropagation of edge finding algorithm simplified version from Petr Vilim only a small subset is reported such that
2739  * energy in total and for bound change is enough
2740  */
2741 static
2742 SCIP_RETCODE resolvePropagationEdgeFinding(
2743  SCIP* scip, /**< SCIP data structure */
2744  int nvars, /**< number of start time variables (activities) */
2745  SCIP_VAR** vars, /**< array of start time variables */
2746  int* durations, /**< array of durations */
2747  int hmin, /**< left bound of time axis to be considered (including hmin) */
2748  int hmax, /**< right bound of time axis to be considered (not including hmax) */
2749  SCIP_VAR* infervar, /**< variable whose bound change is to be explained */
2750  INFERINFO inferinfo, /**< inference info containing position of correct bdchgids */
2751  SCIP_BOUNDTYPE boundtype, /**< the type of the changed bound (lower or upper bound) */
2752  SCIP_BDCHGIDX* bdchgidx, /**< the index of the bound change, representing the point of time where the change took place */
2753  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
2754  SCIP_Bool* explanation /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
2755  )
2756 {
2757  int est;
2758  int lct;
2759  int j;
2760 
2761  /* ???????????????????? do bound widening */
2762 
2763  assert(scip != NULL);
2764  assert(nvars > 0);
2765  assert(inferInfoGetProprule(inferinfo) == PROPRULE_2_EDGEFINDING);
2766 
2767  SCIPdebugMsg(scip, "repropagate edge-finding with short reasons for variable <%s>\n", SCIPvarGetName(infervar));
2768 
2769  if( boundtype == SCIP_BOUNDTYPE_LOWER )
2770  {
2771  SCIP_CALL( SCIPaddConflictLb(scip, infervar, bdchgidx) );
2772  }
2773  else
2774  {
2775  SCIP_CALL( SCIPaddConflictUb(scip, infervar, bdchgidx) );
2776  }
2777 
2778  est = inferInfoGetData1(inferinfo);
2779  lct = inferInfoGetData2(inferinfo);
2780  assert(est < lct);
2781 
2782  /* collect the energies of all variables in [est_omega, lct_omega] */
2783  for( j = 0; j < nvars; ++j )
2784  {
2785  SCIP_VAR* var;
2786  SCIP_Bool left;
2787  SCIP_Bool right;
2788  int duration;
2789  int lb;
2790  int ub;
2791 
2792  var = vars[j];
2793  assert(var != NULL);
2794 
2795  if( var == infervar )
2796  {
2797  if( explanation != NULL )
2798  explanation[j] = TRUE;
2799 
2800  continue;
2801  }
2802 
2803  lb = SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE));
2804  ub = SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE));
2805 
2806  duration = durations[j];
2807  assert(duration > 0);
2808 
2809  /* in case the earliest start time is equal to hmin we have to also consider the jobs which run in that region
2810  * since we use adjusted jobs during the propagation
2811  */
2812  left = (est == hmin && lb + duration > hmin) || lb >= est;
2813 
2814  /* in case the latest completion time is equal to hmax we have to also consider the jobs which run in that region
2815  * since we use adjusted jobs during the propagation
2816  */
2817  right = (lct == hmax && ub < hmax) || ub + duration <= lct;
2818 
2819  /* store all jobs running in [est_omega; lct_omega] */
2820  if( left && right )
2821  {
2822  /* check if bound widening should be used */
2823  if( usebdwidening )
2824  {
2825  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, var, bdchgidx, (SCIP_Real)(lct - duration)) );
2826  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, var, bdchgidx, (SCIP_Real)(est)) );
2827  }
2828  else
2829  {
2830  SCIP_CALL( SCIPaddConflictLb(scip, var, bdchgidx) );
2831  SCIP_CALL( SCIPaddConflictUb(scip, var, bdchgidx) );
2832  }
2833 
2834  if( explanation != NULL )
2835  explanation[j] = TRUE;
2836  }
2837  }
2838 
2839  return SCIP_OKAY;
2840 }
2841 #endif
2842 
2843 /** compute the minimum overlaps w.r.t. the duration of the job and the time window [begin,end) */
2844 static
2845 int computeOverlap(
2846  int begin, /**< begin of the times interval */
2847  int end, /**< end of time interval */
2848  int est, /**< earliest start time */
2849  int lst, /**< latest start time */
2850  int duration /**< duration of the job */
2851  )
2852 {
2853  int left;
2854  int right;
2855  int ect;
2856  int lct;
2857 
2858  ect = est + duration;
2859  lct = lst + duration;
2860 
2861  /* check if job runs completely within [begin,end) */
2862  if( lct <= end && est >= begin )
2863  return duration;
2864 
2865  assert(lst <= end && ect >= begin);
2866 
2867  left = ect - begin;
2868  assert(left > 0);
2869 
2870  right = end - lst;
2871  assert(right > 0);
2872 
2873  return MIN3(left, right, end - begin);
2874 }
2875 
2876 /** an overload was detected due to the time-time edge-finding propagate; initialized conflict analysis, add an initial
2877  * reason
2878  *
2879  * @note the conflict analysis is not performend, only the initialized SCIP_Bool pointer is set to TRUE
2880  */
2881 static
2883  SCIP* scip, /**< SCIP data structure */
2884  int nvars, /**< number of start time variables (activities) */
2885  SCIP_VAR** vars, /**< array of start time variables */
2886  int* durations, /**< array of durations */
2887  int* demands, /**< array of demands */
2888  int capacity, /**< capacity of the cumulative condition */
2889  int begin, /**< begin of the time window */
2890  int end, /**< end of the time window */
2891  SCIP_VAR* infervar, /**< variable which was propagate, or NULL */
2892  SCIP_BOUNDTYPE boundtype, /**< the type of the changed bound (lower or upper bound) */
2893  SCIP_BDCHGIDX* bdchgidx, /**< the index of the bound change, representing the point of time where the change took place */
2894  SCIP_Real relaxedbd, /**< the relaxed bound which is sufficient to be explained */
2895  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
2896  SCIP_Bool* explanation /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
2897  )
2898 {
2899  int* locenergies;
2900  int* overlaps;
2901  int* idxs;
2902 
2903  SCIP_Longint requiredenergy;
2904  int v;
2905 
2906  SCIP_CALL( SCIPallocBufferArray(scip, &locenergies, nvars) );
2907  SCIP_CALL( SCIPallocBufferArray(scip, &overlaps, nvars) );
2908  SCIP_CALL( SCIPallocBufferArray(scip, &idxs, nvars) );
2909 
2910  /* energy which needs be explained */
2911  requiredenergy = ((SCIP_Longint) end - begin) * capacity;
2912 
2913  SCIPdebugMsg(scip, "analysis energy load in [%d,%d) (capacity %d, energy %" SCIP_LONGINT_FORMAT ")\n", begin, end, capacity, requiredenergy);
2914 
2915  /* collect global contribution and adjusted the required energy by the amount of energy the inference variable
2916  * takes
2917  */
2918  for( v = 0; v < nvars; ++v )
2919  {
2920  SCIP_VAR* var;
2921  int glbenergy;
2922  int duration;
2923  int demand;
2924  int est;
2925  int lst;
2926 
2927  var = vars[v];
2928  assert(var != NULL);
2929 
2930  locenergies[v] = 0;
2931  overlaps[v] = 0;
2932  idxs[v] = v;
2933 
2934  demand = demands[v];
2935  assert(demand > 0);
2936 
2937  duration = durations[v];
2938  assert(duration > 0);
2939 
2940  /* check if the variable equals the inference variable (the one which was propagated) */
2941  if( infervar == var )
2942  {
2943  int overlap;
2944  int right;
2945  int left;
2946 
2947  assert(relaxedbd != SCIP_UNKNOWN); /*lint !e777*/
2948 
2949  SCIPdebugMsg(scip, "inference variable <%s>[%g,%g] %s %g (duration %d, demand %d)\n",
2950  SCIPvarGetName(var), SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE), SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE),
2951  boundtype == SCIP_BOUNDTYPE_LOWER ? ">=" : "<=", relaxedbd, duration, demand);
2952 
2953  /* compute the amount of energy which needs to be available for enforcing the propagation and report the bound
2954  * which is necessary from the inference variable
2955  */
2956  if( boundtype == SCIP_BOUNDTYPE_UPPER )
2957  {
2958  int lct;
2959 
2960  /* get the latest start time of the infer start time variable before the propagation took place */
2961  lst = SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE));
2962 
2963  /* the latest start time of the inference start time variable before the propagation needs to be smaller as
2964  * the end of the time interval; meaning the job needs be overlap with the time interval in case the job is
2965  * scheduled w.r.t. its latest start time
2966  */
2967  assert(lst < end);
2968 
2969  /* compute the overlap of the job in case it would be scheduled w.r.t. its latest start time and the time
2970  * interval (before the propagation)
2971  */
2972  right = MIN3(end - lst, end - begin, duration);
2973 
2974  /* the job needs to overlap with the interval; otherwise the propagation w.r.t. this time window is not valid */
2975  assert(right > 0);
2976 
2977  lct = SCIPconvertRealToInt(scip, relaxedbd) + duration;
2978  assert(begin <= lct);
2979  assert(bdchgidx == NULL || SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, TRUE)) < begin);
2980 
2981  /* compute the overlap of the job after the propagation but considering the relaxed bound */
2982  left = MIN(lct - begin + 1, end - begin);
2983  assert(left > 0);
2984 
2985  /* compute the minimum overlap; */
2986  overlap = MIN(left, right);
2987  assert(overlap > 0);
2988  assert(overlap <= end - begin);
2989  assert(overlap <= duration);
2990 
2991  if( usebdwidening )
2992  {
2993  assert(SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE)) <= (end - overlap));
2994  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, var, bdchgidx, (SCIP_Real)(end - overlap)) );
2995  }
2996  else
2997  {
2998  SCIP_CALL( SCIPaddConflictUb(scip, var, bdchgidx) );
2999  }
3000  }
3001  else
3002  {
3003  int ect;
3004 
3005  assert(boundtype == SCIP_BOUNDTYPE_LOWER);
3006 
3007  /* get the earliest completion time of the infer start time variable before the propagation took place */
3008  ect = SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)) + duration;
3009 
3010  /* the earliest start time of the inference start time variable before the propagation needs to be larger as
3011  * than the beginning of the time interval; meaning the job needs be overlap with the time interval in case
3012  * the job is scheduled w.r.t. its earliest start time
3013  */
3014  assert(ect > begin);
3015 
3016  /* compute the overlap of the job in case it would be scheduled w.r.t. its earliest start time and the time
3017  * interval (before the propagation)
3018  */
3019  left = MIN3(ect - begin, end - begin, duration);
3020 
3021  /* the job needs to overlap with the interval; otherwise the propagation w.r.t. this time window is not valid */
3022  assert(left > 0);
3023 
3024  est = SCIPconvertRealToInt(scip, relaxedbd);
3025  assert(end >= est);
3026  assert(bdchgidx == NULL || end - SCIPgetVarLbAtIndex(scip, var, bdchgidx, TRUE) < duration);
3027 
3028  /* compute the overlap of the job after the propagation but considering the relaxed bound */
3029  right = MIN(end - est + 1, end - begin);
3030  assert(right > 0);
3031 
3032  /* compute the minimum overlap */
3033  overlap = MIN(left, right);
3034  assert(overlap > 0);
3035  assert(overlap <= end - begin);
3036  assert(overlap <= duration);
3037 
3038  if( usebdwidening )
3039  {
3040  assert(SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE)) >= (begin + overlap - duration));
3041  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, var, bdchgidx, (SCIP_Real)(begin + overlap - duration)) );
3042  }
3043  else
3044  {
3045  SCIP_CALL( SCIPaddConflictLb(scip, var, bdchgidx) );
3046  }
3047  }
3048 
3049  /* subtract the amount of energy which is available due to the overlap of the inference start time */
3050  requiredenergy -= (SCIP_Longint) overlap * demand;
3051 
3052  if( explanation != NULL )
3053  explanation[v] = TRUE;
3054 
3055  continue;
3056  }
3057 
3058  /* global time points */
3059  est = SCIPconvertRealToInt(scip, SCIPvarGetLbGlobal(var));
3060  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbGlobal(var));
3061 
3062  glbenergy = 0;
3063 
3064  /* check if the has any overlap w.r.t. global bound; meaning some parts of the job will run for sure within the
3065  * time window
3066  */
3067  if( est + duration > begin && lst < end )
3068  {
3069  /* evaluated global contribution */
3070  glbenergy = computeOverlap(begin, end, est, lst, duration) * demand;
3071 
3072  /* remove the globally available energy form the required energy */
3073  requiredenergy -= glbenergy;
3074 
3075  if( explanation != NULL )
3076  explanation[v] = TRUE;
3077  }
3078 
3079  /* local time points */
3080  est = SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, var, bdchgidx, FALSE));
3081  lst = SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, var, bdchgidx, FALSE));
3082 
3083  /* check if the job has any overlap w.r.t. local bound; meaning some parts of the job will run for sure within the
3084  * time window
3085  */
3086  if( est + duration > begin && lst < end )
3087  {
3088  overlaps[v] = computeOverlap(begin, end, est, lst, duration);
3089 
3090  /* evaluated additionally local energy contribution */
3091  locenergies[v] = overlaps[v] * demand - glbenergy;
3092  assert(locenergies[v] >= 0);
3093  }
3094  }
3095 
3096  /* sort the variable contributions w.r.t. additional local energy contributions */
3097  SCIPsortDownIntIntInt(locenergies, overlaps, idxs, nvars);
3098 
3099  /* add local energy contributions until an overload is implied */
3100  for( v = 0; v < nvars && requiredenergy >= 0; ++v )
3101  {
3102  SCIP_VAR* var;
3103  int duration;
3104  int overlap;
3105  int relaxlb;
3106  int relaxub;
3107  int idx;
3108 
3109  idx = idxs[v];
3110  assert(idx >= 0 && idx < nvars);
3111 
3112  var = vars[idx];
3113  assert(var != NULL);
3114  assert(var != infervar);
3115 
3116  duration = durations[idx];
3117  assert(duration > 0);
3118 
3119  overlap = overlaps[v];
3120  assert(overlap > 0);
3121 
3122  requiredenergy -= locenergies[v];
3123 
3124  if( requiredenergy < -1 )
3125  {
3126  int demand;
3127 
3128  demand = demands[idx];
3129  assert(demand > 0);
3130 
3131  overlap += (int)((requiredenergy + 1) / demand);
3132 
3133 #ifndef NDEBUG
3134  requiredenergy += locenergies[v];
3135  requiredenergy -= (SCIP_Longint) overlap * demand;
3136  assert(requiredenergy < 0);
3137 #endif
3138  }
3139  assert(overlap > 0);
3140 
3141  relaxlb = begin - duration + overlap;
3142  relaxub = end - overlap;
3143 
3144  SCIPdebugMsg(scip, "variable <%s> glb=[%g,%g] loc=[%g,%g], conf=[%g,%g], added=[%d,%d] (demand %d, duration %d)\n",
3145  SCIPvarGetName(var),
3148  SCIPgetConflictVarLb(scip, var), SCIPgetConflictVarUb(scip, var),
3149  relaxlb, relaxub, demands[idx], duration);
3150 
3151  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, var, bdchgidx, (SCIP_Real)relaxlb) );
3152  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, var, bdchgidx, (SCIP_Real)relaxub) );
3153 
3154  if( explanation != NULL )
3155  explanation[idx] = TRUE;
3156  }
3157 
3158  assert(requiredenergy < 0);
3159 
3160  SCIPfreeBufferArray(scip, &idxs);
3161  SCIPfreeBufferArray(scip, &overlaps);
3162  SCIPfreeBufferArray(scip, &locenergies);
3163 
3164  return SCIP_OKAY;
3165 }
3166 
3167 /** resolve propagation w.r.t. the cumulative condition */
3168 static
3170  SCIP* scip, /**< SCIP data structure */
3171  int nvars, /**< number of start time variables (activities) */
3172  SCIP_VAR** vars, /**< array of start time variables */
3173  int* durations, /**< array of durations */
3174  int* demands, /**< array of demands */
3175  int capacity, /**< cumulative capacity */
3176  int hmin, /**< left bound of time axis to be considered (including hmin) */
3177  int hmax, /**< right bound of time axis to be considered (not including hmax) */
3178  SCIP_VAR* infervar, /**< the conflict variable whose bound change has to be resolved */
3179  INFERINFO inferinfo, /**< the user information */
3180  SCIP_BOUNDTYPE boundtype, /**< the type of the changed bound (lower or upper bound) */
3181  SCIP_BDCHGIDX* bdchgidx, /**< the index of the bound change, representing the point of time where the change took place */
3182  SCIP_Real relaxedbd, /**< the relaxed bound which is sufficient to be explained */
3183  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
3184  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
3185  SCIP_RESULT* result /**< pointer to store the result of the propagation conflict resolving call */
3186  )
3187 {
3188  switch( inferInfoGetProprule(inferinfo) )
3189  {
3190  case PROPRULE_1_CORETIMES:
3191  {
3192  int inferdemand;
3193  int inferduration;
3194  int inferpos;
3195  int inferpeak;
3196  int relaxedpeak;
3197  int provedpeak;
3198 
3199  /* get the position of the inferred variable in the vars array */
3200  inferpos = inferInfoGetData1(inferinfo);
3201  if( inferpos >= nvars || vars[inferpos] != infervar )
3202  {
3203  /* find inference variable in constraint */
3204  for( inferpos = 0; inferpos < nvars && vars[inferpos] != infervar; ++inferpos )
3205  {}
3206  }
3207  assert(inferpos < nvars);
3208  assert(vars[inferpos] == infervar);
3209 
3210  inferdemand = demands[inferpos];
3211  inferduration = durations[inferpos];
3212 
3213  if( boundtype == SCIP_BOUNDTYPE_UPPER )
3214  {
3215  /* we propagated the latest start time (upper bound) step wise with a step length of at most the duration of
3216  * the inference variable
3217  */
3218  assert(SCIPgetVarUbAtIndex(scip, infervar, bdchgidx, FALSE) - SCIPgetVarUbAtIndex(scip, infervar, bdchgidx, TRUE) < inferduration + 0.5);
3219 
3220  SCIPdebugMsg(scip, "variable <%s>: upper bound changed from %g to %g (relaxed %g)\n",
3221  SCIPvarGetName(infervar), SCIPgetVarUbAtIndex(scip, infervar, bdchgidx, FALSE),
3222  SCIPgetVarUbAtIndex(scip, infervar, bdchgidx, TRUE), relaxedbd);
3223 
3224  /* get the inference peak that the time point which lead to the that propagtion */
3225  inferpeak = inferInfoGetData2(inferinfo);
3226  /* the bound passed back to be resolved might be tighter as the bound propagted by the core time propagator;
3227  * this can happen if the variable is not activ and aggregated to an activ variable with a scale != 1.0
3228  */
3229  assert(SCIPconvertRealToInt(scip, SCIPgetVarUbAtIndex(scip, infervar, bdchgidx, TRUE)) + inferduration <= inferpeak);
3230  relaxedpeak = SCIPconvertRealToInt(scip, relaxedbd) + inferduration;
3231 
3232  /* make sure that the relaxed peak is part of the effective horizon */
3233  relaxedpeak = MIN(relaxedpeak, hmax-1);
3234 
3235  /* make sure that relaxed peak is not larger than the infer peak
3236  *
3237  * This can happen in case the variable is not an active variable!
3238  */
3239  relaxedpeak = MAX(relaxedpeak, inferpeak);
3240  assert(relaxedpeak >= inferpeak);
3241  assert(relaxedpeak >= hmin);
3242  }
3243  else
3244  {
3245  assert(boundtype == SCIP_BOUNDTYPE_LOWER);
3246 
3247  SCIPdebugMsg(scip, "variable <%s>: lower bound changed from %g to %g (relaxed %g)\n",
3248  SCIPvarGetName(infervar), SCIPgetVarLbAtIndex(scip, infervar, bdchgidx, FALSE),
3249  SCIPgetVarLbAtIndex(scip, infervar, bdchgidx, TRUE), relaxedbd);
3250 
3251  /* get the time interval where the job could not be scheduled */
3252  inferpeak = inferInfoGetData2(inferinfo);
3253  /* the bound passed back to be resolved might be tighter as the bound propagted by the core time propagator;
3254  * this can happen if the variable is not activ and aggregated to an activ variable with a scale != 1.0
3255  */
3256  assert(SCIPconvertRealToInt(scip, SCIPgetVarLbAtIndex(scip, infervar, bdchgidx, TRUE)) - 1 >= inferpeak);
3257  relaxedpeak = SCIPconvertRealToInt(scip, relaxedbd) - 1;
3258 
3259  /* make sure that the relaxed peak is part of the effective horizon */
3260  relaxedpeak = MAX(relaxedpeak, hmin);
3261 
3262  /* make sure that relaxed peak is not larger than the infer peak
3263  *
3264  * This can happen in case the variable is not an active variable!
3265  */
3266  relaxedpeak = MIN(relaxedpeak, inferpeak);
3267  assert(relaxedpeak < hmax);
3268  }
3269 
3270  /* resolves the propagation of the core time algorithm */
3271  SCIP_CALL( resolvePropagationCoretimes(scip, nvars, vars, durations, demands, capacity, hmin, hmax,
3272  infervar, inferdemand, inferpeak, relaxedpeak, bdchgidx, usebdwidening, &provedpeak, explanation) );
3273 
3274  if( boundtype == SCIP_BOUNDTYPE_UPPER )
3275  {
3276  if( usebdwidening )
3277  {
3278  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, infervar, NULL, (SCIP_Real)provedpeak) );
3279  }
3280  else
3281  {
3282  /* old upper bound of variable itself is part of the explanation */
3283  SCIP_CALL( SCIPaddConflictUb(scip, infervar, bdchgidx) );
3284  }
3285  }
3286  else
3287  {
3288  assert(boundtype == SCIP_BOUNDTYPE_LOWER);
3289 
3290  if( usebdwidening )
3291  {
3292  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, infervar, bdchgidx, (SCIP_Real)(provedpeak - inferduration + 1)) );
3293  }
3294  else
3295  {
3296  /* old lower bound of variable itself is part of the explanation */
3297  SCIP_CALL( SCIPaddConflictLb(scip, infervar, bdchgidx) );
3298  }
3299  }
3300 
3301  if( explanation != NULL )
3302  explanation[inferpos] = TRUE;
3303 
3304  break;
3305  }
3307  case PROPRULE_3_TTEF:
3308  {
3309  int begin;
3310  int end;
3311 
3312  begin = inferInfoGetData1(inferinfo);
3313  end = inferInfoGetData2(inferinfo);
3314  assert(begin < end);
3315 
3316  begin = MAX(begin, hmin);
3317  end = MIN(end, hmax);
3318 
3319  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
3320  begin, end, infervar, boundtype, bdchgidx, relaxedbd, usebdwidening, explanation) );
3321 
3322  break;
3323  }
3324 
3325  case PROPRULE_0_INVALID:
3326  default:
3327  SCIPerrorMessage("invalid inference information %d\n", inferInfoGetProprule(inferinfo));
3328  SCIPABORT();
3329  return SCIP_INVALIDDATA; /*lint !e527*/
3330  }
3331 
3332  (*result) = SCIP_SUCCESS;
3333 
3334  return SCIP_OKAY;
3335 }
3336 
3337 /**@} */
3338 
3339 
3340 /**@name Enforcement methods
3341  *
3342  * @{
3343  */
3344 
3345 /** apply all fixings which are given by the alternative bounds */
3346 static
3348  SCIP* scip, /**< SCIP data structure */
3349  SCIP_VAR** vars, /**< array of active variables */
3350  int nvars, /**< number of active variables */
3351  int* alternativelbs, /**< alternative lower bounds */
3352  int* alternativeubs, /**< alternative lower bounds */
3353  int* downlocks, /**< number of constraints with down lock participating by the computation */
3354  int* uplocks, /**< number of constraints with up lock participating by the computation */
3355  SCIP_Bool* branched /**< pointer to store if a branching was applied */
3356  )
3357 {
3358  int v;
3359 
3360  for( v = 0; v < nvars; ++v )
3361  {
3362  SCIP_VAR* var;
3363  SCIP_Real objval;
3364 
3365  var = vars[v];
3366  assert(var != NULL);
3367 
3368  objval = SCIPvarGetObj(var);
3369 
3370  if( SCIPvarGetNLocksDownType(var, SCIP_LOCKTYPE_MODEL) == downlocks[v] && !SCIPisNegative(scip, objval) )
3371  {
3372  int ub;
3373 
3374  ub = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
3375 
3376  if( alternativelbs[v] <= ub )
3377  {
3378  SCIP_CALL( SCIPbranchVarHole(scip, var, SCIPvarGetLbLocal(var), (SCIP_Real)alternativelbs[v], NULL, NULL) );
3379  (*branched) = TRUE;
3380 
3381  SCIPdebugMsg(scip, "variable <%s> branched domain hole (%g,%d)\n", SCIPvarGetName(var),
3382  SCIPvarGetLbLocal(var), alternativelbs[v]);
3383 
3384  return SCIP_OKAY;
3385  }
3386  }
3387 
3388  if( SCIPvarGetNLocksUpType(var, SCIP_LOCKTYPE_MODEL) == uplocks[v] && !SCIPisPositive(scip, objval) )
3389  {
3390  int lb;
3391 
3392  lb = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
3393 
3394  if( alternativeubs[v] >= lb )
3395  {
3396  SCIP_CALL( SCIPbranchVarHole(scip, var, (SCIP_Real)alternativeubs[v], SCIPvarGetUbLocal(var), NULL, NULL) );
3397  (*branched) = TRUE;
3398 
3399  SCIPdebugMsg(scip, "variable <%s> branched domain hole (%d,%g)\n", SCIPvarGetName(var),
3400  alternativeubs[v], SCIPvarGetUbLocal(var));
3401 
3402  return SCIP_OKAY;
3403  }
3404  }
3405  }
3406 
3407  return SCIP_OKAY;
3408 }
3409 
3410 /** remove the capacity requirments for all job which start at the curtime */
3411 static
3413  SCIP_CONSDATA* consdata, /**< constraint data */
3414  int curtime, /**< current point in time */
3415  int* starttimes, /**< array of start times */
3416  int* startindices, /**< permutation with respect to the start times */
3417  int* freecapacity, /**< pointer to store the resulting free capacity */
3418  int* idx, /**< pointer to index in start time array */
3419  int nvars /**< number of vars in array of starttimes and startindices */
3420  )
3421 {
3422 #if defined SCIP_DEBUG && !defined NDEBUG
3423  int oldidx;
3424 
3425  assert(idx != NULL);
3426  oldidx = *idx;
3427 #else
3428  assert(idx != NULL);
3429 #endif
3430 
3431  assert(starttimes != NULL);
3432  assert(starttimes != NULL);
3433  assert(freecapacity != NULL);
3434  assert(starttimes[*idx] == curtime);
3435  assert(consdata->demands != NULL);
3436  assert(freecapacity != idx);
3437 
3438  /* subtract all capacity needed up to this point */
3439  (*freecapacity) -= consdata->demands[startindices[*idx]];
3440 
3441  while( (*idx)+1 < nvars && starttimes[(*idx)+1] == curtime )
3442  {
3443  ++(*idx);
3444  (*freecapacity) -= consdata->demands[startindices[(*idx)]];
3445  assert(freecapacity != idx);
3446  }
3447 #ifdef SCIP_DEBUG
3448  assert(oldidx <= *idx);
3449 #endif
3450 }
3451 
3452 /** add the capacity requirments for all job which end at the curtime */
3453 static
3454 void addEndingJobDemands(
3455  SCIP_CONSDATA* consdata, /**< constraint data */
3456  int curtime, /**< current point in time */
3457  int* endtimes, /**< array of end times */
3458  int* endindices, /**< permutation with rspect to the end times */
3459  int* freecapacity, /**< pointer to store the resulting free capacity */
3460  int* idx, /**< pointer to index in end time array */
3461  int nvars /**< number of vars in array of starttimes and startindices */
3462  )
3463 {
3464 #if defined SCIP_DEBUG && !defined NDEBUG
3465  int oldidx;
3466  oldidx = *idx;
3467 #endif
3468 
3469  /* free all capacity usages of jobs the are no longer running */
3470  while( endtimes[*idx] <= curtime && *idx < nvars)
3471  {
3472  (*freecapacity) += consdata->demands[endindices[*idx]];
3473  ++(*idx);
3474  }
3475 
3476 #ifdef SCIP_DEBUG
3477  assert(oldidx <= *idx);
3478 #endif
3479 }
3480 
3481 /** computes a point in time when the capacity is exceeded returns hmax if this does not happen */
3482 static
3484  SCIP* scip, /**< SCIP data structure */
3485  SCIP_CONSDATA* consdata, /**< constraint handler data */
3486  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
3487  int* timepoint /**< pointer to store the time point of the peak */
3488  )
3489 {
3490  int* starttimes; /* stores when each job is starting */
3491  int* endtimes; /* stores when each job ends */
3492  int* startindices; /* we will sort the startsolvalues, thus we need to know wich index of a job it corresponds to */
3493  int* endindices; /* we will sort the endsolvalues, thus we need to know wich index of a job it corresponds to */
3494 
3495  int nvars; /* number of activities for this constraint */
3496  int freecapacity; /* remaining capacity */
3497  int curtime; /* point in time which we are just checking */
3498  int endindex; /* index of endsolvalues with: endsolvalues[endindex] > curtime */
3499 
3500  int hmin;
3501  int hmax;
3502 
3503  int j;
3504 
3505  assert(consdata != NULL);
3506 
3507  nvars = consdata->nvars;
3508  assert(nvars > 0);
3509 
3510  *timepoint = consdata->hmax;
3511 
3512  assert(consdata->vars != NULL);
3513 
3514  SCIP_CALL( SCIPallocBufferArray(scip, &starttimes, nvars) );
3515  SCIP_CALL( SCIPallocBufferArray(scip, &endtimes, nvars) );
3516  SCIP_CALL( SCIPallocBufferArray(scip, &startindices, nvars) );
3517  SCIP_CALL( SCIPallocBufferArray(scip, &endindices, nvars) );
3518 
3519  /* create event point arrays */
3520  createSortedEventpointsSol(scip, sol, consdata->nvars, consdata->vars, consdata->durations,
3521  starttimes, endtimes, startindices, endindices);
3522 
3523  endindex = 0;
3524  freecapacity = consdata->capacity;
3525  hmin = consdata->hmin;
3526  hmax = consdata->hmax;
3527 
3528  /* check each startpoint of a job whether the capacity is kept or not */
3529  for( j = 0; j < nvars; ++j )
3530  {
3531  curtime = starttimes[j];
3532  SCIPdebugMsg(scip, "look at %d-th job with start %d\n", j, curtime);
3533 
3534  if( curtime >= hmax )
3535  break;
3536 
3537  /* remove the capacity requirments for all job which start at the curtime */
3538  subtractStartingJobDemands(consdata, curtime, starttimes, startindices, &freecapacity, &j, nvars);
3539 
3540  /* add the capacity requirments for all job which end at the curtime */
3541  addEndingJobDemands(consdata, curtime, endtimes, endindices, &freecapacity, &endindex, nvars);
3542 
3543  assert(freecapacity <= consdata->capacity);
3544  assert(endindex <= nvars);
3545 
3546  /* endindex - points to the next job which will finish */
3547  /* j - points to the last job that has been released */
3548 
3549  /* if free capacity is smaller than zero, then add branching candidates */
3550  if( freecapacity < 0 && curtime >= hmin )
3551  {
3552  *timepoint = curtime;
3553  break;
3554  }
3555  } /*lint --e{850}*/
3556 
3557  /* free all buffer arrays */
3558  SCIPfreeBufferArray(scip, &endindices);
3559  SCIPfreeBufferArray(scip, &startindices);
3560  SCIPfreeBufferArray(scip, &endtimes);
3561  SCIPfreeBufferArray(scip, &starttimes);
3562 
3563  return SCIP_OKAY;
3564 }
3565 
3566 /** checks all cumulative constraints for infeasibility and add branching candidates to storage */
3567 static
3569  SCIP* scip, /**< SCIP data structure */
3570  SCIP_CONS** conss, /**< constraints to be processed */
3571  int nconss, /**< number of constraints */
3572  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
3573  int* nbranchcands /**< pointer to store the number of branching variables */
3574  )
3575 {
3576  SCIP_HASHTABLE* collectedvars;
3577  int c;
3578 
3579  assert(scip != NULL);
3580  assert(conss != NULL);
3581 
3582  /* create a hash table */
3583  SCIP_CALL( SCIPhashtableCreate(&collectedvars, SCIPblkmem(scip), SCIPgetNVars(scip),
3584  SCIPvarGetHashkey, SCIPvarIsHashkeyEq, SCIPvarGetHashkeyVal, NULL) );
3585 
3586  assert(scip != NULL);
3587  assert(conss != NULL);
3588 
3589  for( c = 0; c < nconss; ++c )
3590  {
3591  SCIP_CONS* cons;
3592  SCIP_CONSDATA* consdata;
3593 
3594  int curtime;
3595  int j;
3596 
3597  cons = conss[c];
3598  assert(cons != NULL);
3599 
3600  if( !SCIPconsIsActive(cons) )
3601  continue;
3602 
3603  consdata = SCIPconsGetData(cons);
3604  assert(consdata != NULL);
3605 
3606  /* get point in time when capacity is exceeded */
3607  SCIP_CALL( computePeak(scip, consdata, sol, &curtime) );
3608 
3609  if( curtime < consdata->hmin || curtime >= consdata->hmax )
3610  continue;
3611 
3612  /* report all variables that are running at that point in time */
3613  for( j = 0; j < consdata->nvars; ++j )
3614  {
3615  SCIP_VAR* var;
3616  int lb;
3617  int ub;
3618 
3619  var = consdata->vars[j];
3620  assert(var != NULL);
3621 
3622  /* check if the variable was already added */
3623  if( SCIPhashtableExists(collectedvars, (void*)var) )
3624  continue;
3625 
3626  lb = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
3627  ub = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
3628 
3629  if( lb <= curtime && ub + consdata->durations[j] > curtime && lb < ub )
3630  {
3631  SCIP_Real solval;
3632  SCIP_Real score;
3633 
3634  solval = SCIPgetSolVal(scip, sol, var);
3635  score = MIN(solval - lb, ub - solval) / ((SCIP_Real)ub-lb);
3636 
3637  SCIPdebugMsg(scip, "add var <%s> to branch cand storage\n", SCIPvarGetName(var));
3638  SCIP_CALL( SCIPaddExternBranchCand(scip, var, score, lb + (ub - lb) / 2.0 + 0.2) );
3639  (*nbranchcands)++;
3640 
3641  SCIP_CALL( SCIPhashtableInsert(collectedvars, var) );
3642  }
3643  }
3644  }
3645 
3646  SCIPhashtableFree(&collectedvars);
3647 
3648  SCIPdebugMsg(scip, "found %d branching candidates\n", *nbranchcands);
3649 
3650  return SCIP_OKAY;
3651 }
3652 
3653 /** enforcement of an LP, pseudo, or relaxation solution */
3654 static
3656  SCIP* scip, /**< SCIP data structure */
3657  SCIP_CONS** conss, /**< constraints to be processed */
3658  int nconss, /**< number of constraints */
3659  SCIP_SOL* sol, /**< solution to enforce (NULL for LP or pseudo solution) */
3660  SCIP_Bool branch, /**< should branching candidates be collected */
3661  SCIP_RESULT* result /**< pointer to store the result */
3662  )
3663 {
3664  if( branch )
3665  {
3666  int nbranchcands;
3667 
3668  nbranchcands = 0;
3669  SCIP_CALL( collectBranchingCands(scip, conss, nconss, sol, &nbranchcands) );
3670 
3671  if( nbranchcands > 0 )
3672  (*result) = SCIP_INFEASIBLE;
3673  }
3674  else
3675  {
3676  SCIP_Bool violated;
3677  int c;
3678 
3679  violated = FALSE;
3680 
3681  /* first check if a constraints is violated */
3682  for( c = 0; c < nconss && !violated; ++c )
3683  {
3684  SCIP_CONS* cons;
3685 
3686  cons = conss[c];
3687  assert(cons != NULL);
3688 
3689  SCIP_CALL( checkCons(scip, cons, sol, &violated, FALSE) );
3690  }
3691 
3692  if( violated )
3693  (*result) = SCIP_INFEASIBLE;
3694  }
3695 
3696  return SCIP_OKAY;
3697 }
3698 
3699 /**@} */
3700 
3701 /**@name Propagation
3702  *
3703  * @{
3704  */
3705 
3706 /** check if cumulative constraint is independently of all other constraints */
3707 static
3709  SCIP_CONS* cons /**< cumulative constraint */
3710  )
3711 {
3712  SCIP_CONSDATA* consdata;
3713  SCIP_VAR** vars;
3714  SCIP_Bool* downlocks;
3715  SCIP_Bool* uplocks;
3716  int nvars;
3717  int v;
3718 
3719  consdata = SCIPconsGetData(cons);
3720  assert(consdata != NULL);
3721 
3722  nvars = consdata->nvars;
3723  vars = consdata->vars;
3724  downlocks = consdata->downlocks;
3725  uplocks = consdata->uplocks;
3726 
3727  /* check if the cumulative constraint has the only locks on the involved variables */
3728  for( v = 0; v < nvars; ++v )
3729  {
3730  SCIP_VAR* var;
3731 
3732  var = vars[v];
3733  assert(var != NULL);
3734 
3735  if( SCIPvarGetNLocksDownType(var, SCIP_LOCKTYPE_MODEL) > (int)downlocks[v]
3736  || SCIPvarGetNLocksUpType(var, SCIP_LOCKTYPE_MODEL) > (int)uplocks[v] )
3737  return FALSE;
3738  }
3739 
3740  return TRUE;
3741 }
3742 
3743 /** in case the cumulative constraint is independent of every else, solve the cumulative problem and apply the fixings
3744  * (dual reductions)
3745  */
3746 static
3748  SCIP* scip, /**< SCIP data structure */
3749  SCIP_CONS* cons, /**< cumulative constraint */
3750  SCIP_Longint maxnodes, /**< number of branch-and-bound nodes to solve an independent cumulative constraint (-1: no limit) */
3751  int* nchgbds, /**< pointer to store the number changed variable bounds */
3752  int* nfixedvars, /**< pointer to count number of fixings */
3753  int* ndelconss, /**< pointer to count number of deleted constraints */
3754  SCIP_Bool* cutoff, /**< pointer to store if the constraint is infeasible */
3755  SCIP_Bool* unbounded /**< pointer to store if the constraint is unbounded */
3756  )
3757 {
3758  SCIP_CONSDATA* consdata;
3759  SCIP_VAR** vars;
3760  SCIP_Real* objvals;
3761  SCIP_Real* lbs;
3762  SCIP_Real* ubs;
3763  SCIP_Real timelimit;
3764  SCIP_Real memorylimit;
3765  SCIP_Bool solved;
3766  SCIP_Bool error;
3767 
3768  int ncheckconss;
3769  int nvars;
3770  int v;
3771 
3772  assert(scip != NULL);
3773  assert(!SCIPconsIsModifiable(cons));
3774  assert(SCIPgetNConss(scip) > 0);
3775 
3776  /* if SCIP is in probing mode or repropagation we cannot perform this dual reductions since this dual reduction
3777  * would/could end in an implication which can lead to cutoff of the/all optimal solution
3778  */
3779  if( SCIPinProbing(scip) || SCIPinRepropagation(scip) )
3780  return SCIP_OKAY;
3781 
3782  /* constraints for which the check flag is set to FALSE, did not contribute to the lock numbers; therefore, we cannot
3783  * use the locks to decide for a dual reduction using this constraint;
3784  */
3785  if( !SCIPconsIsChecked(cons) )
3786  return SCIP_OKAY;
3787 
3788  ncheckconss = SCIPgetNCheckConss(scip);
3789 
3790  /* if the cumulative constraint is the only constraint of the original problem or the only check constraint in the
3791  * presolved problem do nothing execpt to change the parameter settings
3792  */
3793  if( ncheckconss == 1 )
3794  {
3795  /* shrink the minimal maximum value for the conflict length */
3796  SCIP_CALL( SCIPsetIntParam(scip, "conflict/minmaxvars", 10) );
3797 
3798  /* use only first unique implication point */
3799  SCIP_CALL( SCIPsetIntParam(scip, "conflict/fuiplevels", 1) );
3800 
3801  /* do not use reconversion conflicts */
3802  SCIP_CALL( SCIPsetIntParam(scip, "conflict/reconvlevels", 0) );
3803 
3804  /* after 250 conflict we force a restart since then the variable statistics are reasonable initialized */
3805  SCIP_CALL( SCIPsetIntParam(scip, "conflict/restartnum", 250) );
3806 
3807  /* increase the number of conflicts which induce a restart */
3808  SCIP_CALL( SCIPsetRealParam(scip, "conflict/restartfac", 2.0) );
3809 
3810  /* weight the variable which made into a conflict */
3811  SCIP_CALL( SCIPsetRealParam(scip, "conflict/conflictweight", 1.0) );
3812 
3813  /* do not check pseudo solution (for performance reasons) */
3814  SCIP_CALL( SCIPsetBoolParam(scip, "constraints/disableenfops", TRUE) );
3815 
3816  /* use value based history to detect a reasonable branching point */
3817  SCIP_CALL( SCIPsetBoolParam(scip, "history/valuebased", TRUE) );
3818 
3819  /* turn of LP relaxation */
3820  SCIP_CALL( SCIPsetIntParam(scip, "lp/solvefreq", -1) );
3821 
3822  /* prefer the down branch in case the value based history does not suggest something */
3823  SCIP_CALL( SCIPsetCharParam(scip, "nodeselection/childsel", 'd') );
3824 
3825  /* accept any bound change */
3826  SCIP_CALL( SCIPsetRealParam(scip, "numerics/boundstreps", 1e-6) );
3827 
3828  /* allow for at most 10 restart, after that the value based history should be reliable */
3829  SCIP_CALL( SCIPsetIntParam(scip, "presolving/maxrestarts", 10) );
3830 
3831  /* set priority for depth first search to highest possible value */
3832  SCIP_CALL( SCIPsetIntParam(scip, "nodeselection/dfs/stdpriority", INT_MAX/4) );
3833 
3834  return SCIP_OKAY;
3835  }
3836 
3837  consdata = SCIPconsGetData(cons);
3838  assert(consdata != NULL);
3839 
3840  /* check if already tried to solve that constraint as independent sub problem; we do not want to try it again if we
3841  * fail on the first place
3842  */
3843  if( consdata->triedsolving )
3844  return SCIP_OKAY;
3845 
3846  /* check if constraint is independently */
3847  if( !isConsIndependently(cons) )
3848  return SCIP_OKAY;
3849 
3850  /* mark the constraint to be tried of solving it as independent sub problem; in case that is successful the
3851  * constraint is deleted; otherwise, we want to ensure that we do not try that again
3852  */
3853  consdata->triedsolving = TRUE;
3854 
3855  SCIPdebugMsg(scip, "the cumulative constraint <%s> is independent from rest of the problem (%d variables, %d constraints)\n",
3856  SCIPconsGetName(cons), SCIPgetNVars(scip), SCIPgetNConss(scip));
3857  SCIPdebugPrintCons(scip, cons, NULL);
3858 
3859  nvars = consdata->nvars;
3860  vars = consdata->vars;
3861 
3862  SCIP_CALL( SCIPallocBufferArray(scip, &lbs, nvars) );
3863  SCIP_CALL( SCIPallocBufferArray(scip, &ubs, nvars) );
3864  SCIP_CALL( SCIPallocBufferArray(scip, &objvals, nvars) );
3865 
3866  for( v = 0; v < nvars; ++v )
3867  {
3868  SCIP_VAR* var;
3869 
3870  /* if a variables array is given, use the variable bounds otherwise the default values stored in the ests and lsts
3871  * array
3872  */
3873  var = vars[v];
3874  assert(var != NULL);
3875 
3876  lbs[v] = SCIPvarGetLbLocal(var);
3877  ubs[v] = SCIPvarGetUbLocal(var);
3878 
3879  objvals[v] = SCIPvarGetObj(var);
3880  }
3881 
3882  /* check whether there is enough time and memory left */
3883  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
3884  if( !SCIPisInfinity(scip, timelimit) )
3885  timelimit -= SCIPgetSolvingTime(scip);
3886  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
3887 
3888  /* substract the memory already used by the main SCIP and the estimated memory usage of external software */
3889  if( !SCIPisInfinity(scip, memorylimit) )
3890  {
3891  memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
3892  memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
3893  }
3894 
3895  /* solve the cumulative condition separately */
3896  SCIP_CALL( SCIPsolveCumulative(scip, nvars, lbs, ubs, objvals, consdata->durations, consdata->demands, consdata->capacity,
3897  consdata->hmin, consdata->hmax, timelimit, memorylimit, maxnodes, &solved, cutoff, unbounded, &error) );
3898 
3899  if( !(*cutoff) && !(*unbounded) && !error )
3900  {
3901  SCIP_Bool infeasible;
3902  SCIP_Bool tightened;
3903  SCIP_Bool allfixed;
3904 
3905  allfixed = TRUE;
3906 
3907  for( v = 0; v < nvars; ++v )
3908  {
3909  /* check if variable is fixed */
3910  if( lbs[v] + 0.5 > ubs[v] )
3911  {
3912  SCIP_CALL( SCIPfixVar(scip, vars[v], lbs[v], &infeasible, &tightened) );
3913  assert(!infeasible);
3914 
3915  if( tightened )
3916  {
3917  (*nfixedvars)++;
3918  consdata->triedsolving = FALSE;
3919  }
3920  }
3921  else
3922  {
3923  SCIP_CALL( SCIPtightenVarLb(scip, vars[v], lbs[v], TRUE, &infeasible, &tightened) );
3924  assert(!infeasible);
3925 
3926  if( tightened )
3927  {
3928  (*nchgbds)++;
3929  consdata->triedsolving = FALSE;
3930  }
3931 
3932  SCIP_CALL( SCIPtightenVarUb(scip, vars[v], ubs[v], TRUE, &infeasible, &tightened) );
3933  assert(!infeasible);
3934 
3935  if( tightened )
3936  {
3937  (*nchgbds)++;
3938  consdata->triedsolving = FALSE;
3939  }
3940 
3941  allfixed = FALSE;
3942  }
3943  }
3944 
3945  /* if all variables are fixed, remove the cumulative constraint since it is redundant */
3946  if( allfixed )
3947  {
3948  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
3949  (*ndelconss)++;
3950  }
3951  }
3952 
3953  SCIPfreeBufferArray(scip, &objvals);
3954  SCIPfreeBufferArray(scip, &ubs);
3955  SCIPfreeBufferArray(scip, &lbs);
3956 
3957  return SCIP_OKAY;
3958 }
3959 
3960 /** start conflict analysis to analysis the core insertion which is infeasible */
3961 static
3963  SCIP* scip, /**< SCIP data structure */
3964  int nvars, /**< number of start time variables (activities) */
3965  SCIP_VAR** vars, /**< array of start time variables */
3966  int* durations, /**< array of durations */
3967  int* demands, /**< array of demands */
3968  int capacity, /**< cumulative capacity */
3969  int hmin, /**< left bound of time axis to be considered (including hmin) */
3970  int hmax, /**< right bound of time axis to be considered (not including hmax) */
3971  SCIP_VAR* infervar, /**< start time variable which lead to the infeasibilty */
3972  int inferduration, /**< duration of the start time variable */
3973  int inferdemand, /**< demand of the start time variable */
3974  int inferpeak, /**< profile preak which causes the infeasibilty */
3975  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
3976  SCIP_Bool* initialized, /**< pointer to store if the conflict analysis was initialized */
3977  SCIP_Bool* explanation /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
3978  )
3979 {
3980  SCIPdebugMsg(scip, "detected infeasibility due to adding a core to the core resource profile\n");
3981  SCIPdebugMsg(scip, "variable <%s>[%g,%g] (demand %d, duration %d)\n", SCIPvarGetName(infervar),
3982  SCIPvarGetLbLocal(infervar), SCIPvarGetUbLocal(infervar), inferdemand, inferduration);
3983 
3984  /* initialize conflict analysis if conflict analysis is applicable */
3986  {
3988 
3989  SCIP_CALL( resolvePropagationCoretimes(scip, nvars, vars, durations, demands, capacity, hmin, hmax,
3990  infervar, inferdemand, inferpeak, inferpeak, NULL, usebdwidening, NULL, explanation) );
3991 
3992  SCIPdebugMsg(scip, "add lower and upper bounds of variable <%s>\n", SCIPvarGetName(infervar));
3993 
3994  /* add both bound of the inference variable since these biuld the core which we could not inserted */
3995  if( usebdwidening )
3996  {
3997  SCIP_CALL( SCIPaddConflictRelaxedLb(scip, infervar, NULL, (SCIP_Real)(inferpeak - inferduration + 1)) );
3998  SCIP_CALL( SCIPaddConflictRelaxedUb(scip, infervar, NULL, (SCIP_Real)inferpeak) );
3999  }
4000  else
4001  {
4002  SCIP_CALL( SCIPaddConflictLb(scip, infervar, NULL) );
4003  SCIP_CALL( SCIPaddConflictUb(scip, infervar, NULL) );
4004  }
4005 
4006  *initialized = TRUE;
4007  }
4008 
4009  return SCIP_OKAY;
4010 }
4011 
4012 /** We are using the core resource profile which contains all core except the one of the start time variable which we
4013  * want to propagate, to incease the earliest start time. This we are doing in steps of length at most the duration of
4014  * the job. The reason for that is, that this makes it later easier to resolve this propagation during the conflict
4015  * analysis
4016  */
4017 static
4019  SCIP* scip, /**< SCIP data structure */
4020  int nvars, /**< number of start time variables (activities) */
4021  SCIP_VAR** vars, /**< array of start time variables */
4022  int* durations, /**< array of durations */
4023  int* demands, /**< array of demands */
4024  int capacity, /**< cumulative capacity */
4025  int hmin, /**< left bound of time axis to be considered (including hmin) */
4026  int hmax, /**< right bound of time axis to be considered (not including hmax) */
4027  SCIP_CONS* cons, /**< constraint which is propagated */
4028  SCIP_PROFILE* profile, /**< resource profile */
4029  int idx, /**< position of the variable to propagate */
4030  int* nchgbds, /**< pointer to store the number of bound changes */
4031  SCIP_Bool usebdwidening, /**< should bound widening be used during conflict analysis? */
4032  SCIP_Bool* initialized, /**< was conflict analysis initialized */
4033  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
4034  SCIP_Bool* infeasible /**< pointer to store if the constraint is infeasible */
4035  )
4036 {
4037  SCIP_VAR* var;
4038  int ntimepoints;
4039  int duration;
4040  int demand;
4041  int peak;
4042  int newlb;
4043  int est;
4044  int lst;
4045  int pos;
4046 
4047  var = vars[idx];
4048  assert(var != NULL);
4049 
4050  duration = durations[idx];
4051  assert(duration > 0);
4052 
4053  demand = demands[idx];
4054  assert(demand > 0);
4055 
4056  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
4057  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
4058  ntimepoints = SCIPprofileGetNTimepoints(profile);
4059 
4060  /* first we find left position of earliest start time (lower bound) in resource profile; this position gives us the
4061  * load which we have at the earliest start time (lower bound)
4062  */
4063  (void) SCIPprofileFindLeft(profile, est, &pos);
4064 
4065  SCIPdebugMsg(scip, "propagate earliest start time (lower bound) (pos %d)\n", pos);
4066 
4067  /* we now trying to move the earliest start time in steps of at most "duration" length */
4068  do
4069  {
4070  INFERINFO inferinfo;
4071  SCIP_Bool tightened;
4072  int ect;
4073 
4074 #ifndef NDEBUG
4075  {
4076  /* in debug mode we check that we adjust the search position correctly */
4077  int tmppos;
4078 
4079  (void)SCIPprofileFindLeft(profile, est, &tmppos);
4080  assert(pos == tmppos);
4081  }
4082 #endif
4083  ect = est + duration;
4084  peak = -1;
4085 
4086  /* we search for a peak within the core profile which conflicts with the demand of the start time variable; we
4087  * want a peak which is closest to the earliest completion time
4088  */
4089  do
4090  {
4091  /* check if the profile load conflicts with the demand of the start time variable */
4092  if( SCIPprofileGetLoad(profile, pos) + demand > capacity )
4093  peak = pos;
4094 
4095  pos++;
4096  }
4097  while( pos < ntimepoints && SCIPprofileGetTime(profile, pos) < ect );
4098 
4099  /* if we found no peak that means current the job could be scheduled at its earliest start time without
4100  * conflicting to the core resource profile
4101  */
4102  /* coverity[check_after_sink] */
4103  if( peak == -1 )
4104  break;
4105 
4106  /* the peak position gives us a time point where the start time variable is in conflict with the resource
4107  * profile. That means we have to move it to the next time point in the resource profile but at most to the
4108  * earliest completion time (the remaining move will done in the next loop)
4109  */
4110  newlb = SCIPprofileGetTime(profile, peak+1);
4111  newlb = MIN(newlb, ect);
4112 
4113  /* if the earliest start time is greater than the lst we detected an infeasibilty */
4114  if( newlb > lst )
4115  {
4116  SCIPdebugMsg(scip, "variable <%s>: cannot be scheduled\n", SCIPvarGetName(var));
4117 
4118  /* use conflict analysis to analysis the core insertion which was infeasible */
4119  SCIP_CALL( analyseInfeasibelCoreInsertion(scip, nvars, vars, durations, demands, capacity, hmin, hmax,
4120  var, duration, demand, newlb-1, usebdwidening, initialized, explanation) );
4121 
4122  if( explanation != NULL )
4123  explanation[idx] = TRUE;
4124 
4125  *infeasible = TRUE;
4126 
4127  break;
4128  }
4129 
4130  /* construct the inference information which we are using with the conflict analysis to resolve that particular
4131  * bound change
4132  */
4133  inferinfo = getInferInfo(PROPRULE_1_CORETIMES, idx, newlb-1);
4134 
4135  /* perform the bound lower bound change */
4136  if( inferInfoIsValid(inferinfo) )
4137  {
4138  SCIP_CALL( SCIPinferVarLbCons(scip, var, (SCIP_Real)newlb, cons, inferInfoToInt(inferinfo), TRUE, infeasible, &tightened) );
4139  }
4140  else
4141  {
4142  SCIP_CALL( SCIPtightenVarLb(scip, var, (SCIP_Real)newlb, TRUE, infeasible, &tightened) );
4143  }
4144  assert(tightened);
4145  assert(!(*infeasible));
4146 
4147  SCIPdebugMsg(scip, "variable <%s> new lower bound <%d> -> <%d>\n", SCIPvarGetName(var), est, newlb);
4148  (*nchgbds)++;
4149 
4150  /* for the statistic we count the number of times a lower bound was tightened due the the time-table algorithm */
4152 
4153  /* adjust the earliest start time
4154  *
4155  * @note We are taking the lower of the start time variable on purpose instead of newlb. This is due the fact that
4156  * the proposed lower bound might be even strength by be the core which can be the case if aggregations are
4157  * involved.
4158  */
4159  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
4160  assert(est >= newlb);
4161 
4162  /* adjust the search position for the resource profile for the next step */
4163  if( est == SCIPprofileGetTime(profile, peak+1) )
4164  pos = peak + 1;
4165  else
4166  pos = peak;
4167  }
4168  while( est < lst );
4169 
4170  return SCIP_OKAY;
4171 }
4172 
4173 /** We are using the core resource profile which contains all core except the one of the start time variable which we
4174  * want to propagate, to decrease the latest start time. This we are doing in steps of length at most the duration of
4175  * the job. The reason for that is, that this makes it later easier to resolve this propagation during the conflict
4176  * analysis
4177  */
4178 static
4180  SCIP* scip, /**< SCIP data structure */
4181  SCIP_VAR* var, /**< start time variable to propagate */
4182  int duration, /**< duration of the job */
4183  int demand, /**< demand of the job */
4184  int capacity, /**< cumulative capacity */
4185  SCIP_CONS* cons, /**< constraint which is propagated */
4186  SCIP_PROFILE* profile, /**< resource profile */
4187  int idx, /**< position of the variable to propagate */
4188  int* nchgbds /**< pointer to store the number of bound changes */
4189  )
4190 {
4191  int ntimepoints;
4192  int newub;
4193  int peak;
4194  int pos;
4195  int est;
4196  int lst;
4197  int lct;
4198 
4199  assert(var != NULL);
4200  assert(duration > 0);
4201  assert(demand > 0);
4202 
4203  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
4204  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
4205 
4206  /* in case the start time variable is fixed do nothing */
4207  if( est == lst )
4208  return SCIP_OKAY;
4209 
4210  ntimepoints = SCIPprofileGetNTimepoints(profile);
4211 
4212  lct = lst + duration;
4213 
4214  /* first we find left position of latest completion time minus 1 (upper bound + duration) in resource profile; That
4215  * is the last time point where the job would run if schedule it at its latest start time (upper bound). This
4216  * position gives us the load which we have at the latest completion time minus one
4217  */
4218  (void) SCIPprofileFindLeft(profile, lct - 1, &pos);
4219 
4220  SCIPdebugMsg(scip, "propagate upper bound (pos %d)\n", pos);
4221  SCIPdebug( SCIPprofilePrint(profile, SCIPgetMessagehdlr(scip), NULL) );
4222 
4223  if( pos == ntimepoints-1 && SCIPprofileGetTime(profile, pos) == lst )
4224  return SCIP_OKAY;
4225 
4226  /* we now trying to move the latest start time in steps of at most "duration" length */
4227  do
4228  {
4229  INFERINFO inferinfo;
4230  SCIP_Bool tightened;
4231  SCIP_Bool infeasible;
4232 
4233  peak = -1;
4234 
4235 #ifndef NDEBUG
4236  {
4237  /* in debug mode we check that we adjust the search position correctly */
4238  int tmppos;
4239 
4240  (void)SCIPprofileFindLeft(profile, lct - 1, &tmppos);
4241  assert(pos == tmppos);
4242  }
4243 #endif
4244 
4245  /* we search for a peak within the core profile which conflicts with the demand of the start time variable; we
4246  * want a peak which is closest to the latest start time
4247  */
4248  do
4249  {
4250  if( SCIPprofileGetLoad(profile, pos) + demand > capacity )
4251  peak = pos;
4252 
4253  pos--;
4254  }
4255  while( pos >= 0 && SCIPprofileGetTime(profile, pos+1) > lst);
4256 
4257  /* if we found no peak that means the current job could be scheduled at its latest start time without conflicting
4258  * to the core resource profile
4259  */
4260  /* coverity[check_after_sink] */
4261  if( peak == -1 )
4262  break;
4263 
4264  /* the peak position gives us a time point where the start time variable is in conflict with the resource
4265  * profile. That means the job has be done until that point. Hence that gives us the latest completion
4266  * time. Note that that we want to move the bound by at most the duration length (the remaining move we are
4267  * doing in the next loop)
4268  */
4269  newub = SCIPprofileGetTime(profile, peak);
4270  newub = MAX(newub, lst) - duration;
4271  assert(newub >= est);
4272 
4273  /* construct the inference information which we are using with the conflict analysis to resolve that particular
4274  * bound change
4275  */
4276  inferinfo = getInferInfo(PROPRULE_1_CORETIMES, idx, newub+duration);
4277 
4278  /* perform the bound upper bound change */
4279  if( inferInfoIsValid(inferinfo) )
4280  {
4281  SCIP_CALL( SCIPinferVarUbCons(scip, var, (SCIP_Real)newub, cons, inferInfoToInt(inferinfo), TRUE, &infeasible, &tightened) );
4282  }
4283  else
4284  {
4285  SCIP_CALL( SCIPtightenVarUb(scip, var, (SCIP_Real)newub, TRUE, &infeasible, &tightened) );
4286  }
4287  assert(tightened);
4288  assert(!infeasible);
4289 
4290  SCIPdebugMsg(scip, "variable <%s>: new upper bound <%d> -> <%d>\n", SCIPvarGetName(var), lst, newub);
4291  (*nchgbds)++;
4292 
4293  /* for the statistic we count the number of times a upper bound was tightened due the the time-table algorithm */
4295 
4296  /* adjust the latest start and completion time
4297  *
4298  * @note We are taking the upper of the start time variable on purpose instead of newub. This is due the fact that
4299  * the proposed upper bound might be even strength by be the core which can be the case if aggregations are
4300  * involved.
4301  */
4302  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
4303  assert(lst <= newub);
4304  lct = lst + duration;
4305 
4306  /* adjust the search position for the resource profile for the next step */
4307  if( SCIPprofileGetTime(profile, peak) == lct )
4308  pos = peak - 1;
4309  else
4310  pos = peak;
4311  }
4312  while( est < lst );
4313 
4314  return SCIP_OKAY;
4315 }
4316 
4317 /** compute for the different earliest start and latest completion time the core energy of the corresponding time
4318  * points
4319  */
4320 static
4322  SCIP_PROFILE* profile, /**< core profile */
4323  int nvars, /**< number of start time variables (activities) */
4324  int* ests, /**< array of sorted earliest start times */
4325  int* lcts, /**< array of sorted latest completion times */
4326  int* coreEnergyAfterEst, /**< array to store the core energy after the earliest start time of each job */
4327  int* coreEnergyAfterLct /**< array to store the core energy after the latest completion time of each job */
4328  )
4329 {
4330  int ntimepoints;
4331  int energy;
4332  int t;
4333  int v;
4334 
4335  ntimepoints = SCIPprofileGetNTimepoints(profile);
4336  t = ntimepoints - 1;
4337  energy = 0;
4338 
4339  /* compute core energy after the earliest start time of each job */
4340  for( v = nvars-1; v >= 0; --v )
4341  {
4342  while( t > 0 && SCIPprofileGetTime(profile, t-1) >= ests[v] )
4343  {
4344  assert(SCIPprofileGetLoad(profile, t-1) >= 0);
4345  assert(SCIPprofileGetTime(profile, t) - SCIPprofileGetTime(profile, t-1)>= 0);
4346  energy += SCIPprofileGetLoad(profile, t-1) * (SCIPprofileGetTime(profile, t) - SCIPprofileGetTime(profile, t-1));
4347  t--;
4348  }
4349  assert(SCIPprofileGetTime(profile, t) >= ests[v] || t == ntimepoints-1);
4350 
4351  /* maybe ests[j] is in-between two timepoints */
4352  if( SCIPprofileGetTime(profile, t) - ests[v] > 0 )
4353  {
4354  assert(t > 0);
4355  coreEnergyAfterEst[v] = energy + SCIPprofileGetLoad(profile, t-1) * (SCIPprofileGetTime(profile, t) - ests[v]);
4356  }
4357  else
4358  coreEnergyAfterEst[v] = energy;
4359  }
4360 
4361  t = ntimepoints - 1;
4362  energy = 0;
4363 
4364  /* compute core energy after the latest completion time of each job */
4365  for( v = nvars-1; v >= 0; --v )
4366  {
4367  while( t > 0 && SCIPprofileGetTime(profile, t-1) >= lcts[v] )
4368  {
4369  assert(SCIPprofileGetLoad(profile, t-1) >= 0);
4370  assert(SCIPprofileGetTime(profile, t) - SCIPprofileGetTime(profile, t-1)>= 0);
4371  energy += SCIPprofileGetLoad(profile, t-1) * (SCIPprofileGetTime(profile, t) - SCIPprofileGetTime(profile, t-1));
4372  t--;
4373  }
4374  assert(SCIPprofileGetTime(profile, t) >= lcts[v] || t == ntimepoints-1);
4375 
4376  /* maybe lcts[j] is in-between two timepoints */
4377  if( SCIPprofileGetTime(profile, t) - lcts[v] > 0 )
4378  {
4379  assert(t > 0);
4380  coreEnergyAfterLct[v] = energy + SCIPprofileGetLoad(profile, t-1) * (SCIPprofileGetTime(profile, t) - lcts[v]);
4381  }
4382  else
4383  coreEnergyAfterLct[v] = energy;
4384  }
4385 }
4386 
4387 /** collect earliest start times, latest completion time, and free energy contributions */
4388 static
4389 void collectDataTTEF(
4390  SCIP* scip, /**< SCIP data structure */
4391  int nvars, /**< number of start time variables (activities) */
4392  SCIP_VAR** vars, /**< array of start time variables */
4393  int* durations, /**< array of durations */
4394  int* demands, /**< array of demands */
4395  int hmin, /**< left bound of time axis to be considered (including hmin) */
4396  int hmax, /**< right bound of time axis to be considered (not including hmax) */
4397  int* permests, /**< array to store the variable positions */
4398  int* ests, /**< array to store earliest start times */
4399  int* permlcts, /**< array to store the variable positions */
4400  int* lcts, /**< array to store latest completion times */
4401  int* ects, /**< array to store earliest completion times of the flexible part of the job */
4402  int* lsts, /**< array to store latest start times of the flexible part of the job */
4403  int* flexenergies /**< array to store the flexible energies of each job */
4404  )
4405 {
4406  int v;
4407 
4408  for( v = 0; v < nvars; ++ v)
4409  {
4410  int duration;
4411  int leftadjust;
4412  int rightadjust;
4413  int core;
4414  int est;
4415  int lct;
4416  int ect;
4417  int lst;
4418 
4419  duration = durations[v];
4420  assert(duration > 0);
4421 
4422  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[v]));
4423  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[v]));
4424  ect = est + duration;
4425  lct = lst + duration;
4426 
4427  ests[v] = est;
4428  lcts[v] = lct;
4429  permests[v] = v;
4430  permlcts[v] = v;
4431 
4432  /* compute core time window which lies within the effective horizon */
4433  core = (int) computeCoreWithInterval(hmin, hmax, ect, lst);
4434 
4435  /* compute the number of time steps the job could run before the effective horizon */
4436  leftadjust = MAX(0, hmin - est);
4437 
4438  /* compute the number of time steps the job could run after the effective horizon */
4439  rightadjust = MAX(0, lct - hmax);
4440 
4441  /* compute for each job the energy which is flexible; meaning not part of the core */
4442  flexenergies[v] = duration - leftadjust - rightadjust - core;
4443  flexenergies[v] = MAX(0, flexenergies[v]);
4444  flexenergies[v] *= demands[v];
4445  assert(flexenergies[v] >= 0);
4446 
4447  /* the earliest completion time of the flexible energy */
4448  ects[v] = MIN(ect, lst);
4449 
4450  /* the latest start time of the flexible energy */
4451  lsts[v] = MAX(ect, lst);
4452  }
4453 }
4454 
4455 /** try to tighten the lower bound of the given variable */
4456 static
4458  SCIP* scip, /**< SCIP data structure */
4459  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
4460  int nvars, /**< number of start time variables (activities) */
4461  SCIP_VAR** vars, /**< array of start time variables */
4462  int* durations, /**< array of durations */
4463  int* demands, /**< array of demands */
4464  int capacity, /**< cumulative capacity */
4465  int hmin, /**< left bound of time axis to be considered (including hmin) */
4466  int hmax, /**< right bound of time axis to be considered (not including hmax) */
4467  SCIP_VAR* var, /**< variable to be considered for upper bound tightening */
4468  int duration, /**< duration of the job */
4469  int demand, /**< demand of the job */
4470  int est, /**< earliest start time of the job */
4471  int ect, /**< earliest completion time of the flexible part of the job */
4472  int lct, /**< latest completion time of the job */
4473  int begin, /**< begin of the time window under investigation */
4474  int end, /**< end of the time window under investigation */
4475  SCIP_Longint energy, /**< available energy for the flexible part of the hob within the time window */
4476  int* bestlb, /**< pointer to strope the best lower bound change */
4477  int* inferinfos, /**< pointer to store the inference information which is need for the (best) lower bound change */
4478  SCIP_Bool* initialized, /**< was conflict analysis initialized */
4479  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
4480  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
4481  )
4482 {
4483  int newlb;
4484 
4485  assert(begin >= hmin);
4486  assert(end <= hmax);
4487 
4488  /* check if the time-table edge-finding should infer bounds */
4489  if( !conshdlrdata->ttefinfer )
4490  return SCIP_OKAY;
4491 
4492  /* if the job can be processed completely before or after the time window, nothing can be tightened */
4493  if( est >= end || ect <= begin )
4494  return SCIP_OKAY;
4495 
4496  /* if flexible part runs completely within the time window (assuming it is scheduled on its earliest start time), we
4497  * skip since the overload check will do the job
4498  */
4499  if( est >= begin && ect <= end )
4500  return SCIP_OKAY;
4501 
4502  /* check if the available energy in the time window is to small to handle the flexible part if it is schedule on its
4503  * earliest start time
4504  */
4505  if( energy >= demand * ((SCIP_Longint) MAX(begin, est) - MIN(end, ect)) )
4506  return SCIP_OKAY;
4507 
4508  /* adjust the available energy for the job; the given available energy assumes that the core of the considered job is
4509  * present; therefore, we need to add the core;
4510  *
4511  * @note the variable ect define the earliest completion time of the flexible part of the job; hence we need to
4512  * compute the earliest completion time of the (whole) job
4513  */
4514  energy += computeCoreWithInterval(begin, end, est + duration, lct - duration) * demand;
4515 
4516  /* compute a latest start time (upper bound) such that the job consums at most the available energy
4517  *
4518  * @note we can round down the compute duration w.r.t. the available energy
4519  */
4520  newlb = end - (int) (energy / demand);
4521 
4522  /* check if we detected an infeasibility which is the case if the new lower bound is larger than the current upper
4523  * bound (latest start time); meaning it is not possible to schedule the job
4524  */
4525  if( newlb > lct - duration )
4526  {
4527  /* initialize conflict analysis if conflict analysis is applicable */
4529  {
4530  SCIP_Real relaxedbd;
4531 
4532  assert(SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var)) < newlb);
4533 
4534  /* it is enough to overshoot the upper bound of the variable by one */
4535  relaxedbd = SCIPvarGetUbLocal(var) + 1.0;
4536 
4537  /* initialize conflict analysis */
4539 
4540  /* added to upper bound (which was overcut be new lower bound) of the variable */
4541  SCIP_CALL( SCIPaddConflictUb(scip, var, NULL) );
4542 
4543  /* analyze the infeasible */
4544  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
4545  begin, end, var, SCIP_BOUNDTYPE_LOWER, NULL, relaxedbd, conshdlrdata->usebdwidening, explanation) );
4546 
4547  (*initialized) = TRUE;
4548  }
4549 
4550  (*cutoff) = TRUE;
4551  }
4552  else if( newlb > (*bestlb) )
4553  {
4554  INFERINFO inferinfo;
4555 
4556  assert(newlb > begin);
4557 
4558  inferinfo = getInferInfo(PROPRULE_3_TTEF, begin, end);
4559 
4560  /* construct inference information */
4561  (*inferinfos) = inferInfoToInt(inferinfo);
4562  (*bestlb) = newlb;
4563  }
4564 
4565  return SCIP_OKAY;
4566 }
4567 
4568 /** try to tighten the upper bound of the given variable */
4569 static
4571  SCIP* scip, /**< SCIP data structure */
4572  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
4573  int nvars, /**< number of start time variables (activities) */
4574  SCIP_VAR** vars, /**< array of start time variables */
4575  int* durations, /**< array of durations */
4576  int* demands, /**< array of demands */
4577  int capacity, /**< cumulative capacity */
4578  int hmin, /**< left bound of time axis to be considered (including hmin) */
4579  int hmax, /**< right bound of time axis to be considered (not including hmax) */
4580  SCIP_VAR* var, /**< variable to be considered for upper bound tightening */
4581  int duration, /**< duration of the job */
4582  int demand, /**< demand of the job */
4583  int est, /**< earliest start time of the job */
4584  int lst, /**< latest start time of the flexible part of the job */
4585  int lct, /**< latest completion time of the job */
4586  int begin, /**< begin of the time window under investigation */
4587  int end, /**< end of the time window under investigation */
4588  SCIP_Longint energy, /**< available energy for the flexible part of the hob within the time window */
4589  int* bestub, /**< pointer to strope the best upper bound change */
4590  int* inferinfos, /**< pointer to store the inference information which is need for the (best) upper bound change */
4591  SCIP_Bool* initialized, /**< was conflict analysis initialized */
4592  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
4593  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
4594  )
4595 {
4596  int newub;
4597 
4598  assert(begin >= hmin);
4599  assert(end <= hmax);
4600  assert(est < begin);
4601 
4602  /* check if the time-table edge-finding should infer bounds */
4603  if( !conshdlrdata->ttefinfer )
4604  return SCIP_OKAY;
4605 
4606  /* if flexible part of the job can be processed completely before or after the time window, nothing can be tightened */
4607  if( lst >= end || lct <= begin )
4608  return SCIP_OKAY;
4609 
4610  /* if flexible part runs completely within the time window (assuming it is scheduled on its latest start time), we
4611  * skip since the overload check will do the job
4612  */
4613  if( lst >= begin && lct <= end )
4614  return SCIP_OKAY;
4615 
4616  /* check if the available energy in the time window is to small to handle the flexible part of the job */
4617  if( energy >= demand * ((SCIP_Longint) MIN(end, lct) - MAX(begin, lst)) )
4618  return SCIP_OKAY;
4619 
4620  /* adjust the available energy for the job; the given available energy assumes that the core of the considered job is
4621  * present; therefore, we need to add the core;
4622  *
4623  * @note the variable lst define the latest start time of the flexible part of the job; hence we need to compute the
4624  * latest start of the (whole) job
4625  */
4626  energy += computeCoreWithInterval(begin, end, est + duration, lct - duration) * demand;
4627  assert(energy >= 0);
4628 
4629  /* compute a latest start time (upper bound) such that the job consums at most the available energy
4630  *
4631  * @note we can round down the compute duration w.r.t. the available energy
4632  */
4633  assert(demand > 0);
4634  newub = begin - duration + (int) (energy / demand);
4635 
4636  /* check if we detected an infeasibility which is the case if the new upper bound is smaller than the current lower
4637  * bound (earliest start time); meaning it is not possible to schedule the job
4638  */
4639  if( newub < est )
4640  {
4641  /* initialize conflict analysis if conflict analysis is applicable */
4643  {
4644  SCIP_Real relaxedbd;
4645 
4646  assert(SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var)) > newub);
4647 
4648  /* it is enough to undershoot the lower bound of the variable by one */
4649  relaxedbd = SCIPvarGetLbLocal(var) - 1.0;
4650 
4651  /* initialize conflict analysis */
4653 
4654  /* added to lower bound (which was undercut be new upper bound) of the variable */
4655  SCIP_CALL( SCIPaddConflictLb(scip, var, NULL) );
4656 
4657  /* analyze the infeasible */
4658  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
4659  begin, end, var, SCIP_BOUNDTYPE_UPPER, NULL, relaxedbd, conshdlrdata->usebdwidening, explanation) );
4660 
4661  (*initialized) = TRUE;
4662  }
4663 
4664  (*cutoff) = TRUE;
4665  }
4666  else if( newub < (*bestub) )
4667  {
4668  INFERINFO inferinfo;
4669 
4670  assert(newub < begin);
4671 
4672  inferinfo = getInferInfo(PROPRULE_3_TTEF, begin, end);
4673 
4674  /* construct inference information */
4675  (*inferinfos) = inferInfoToInt(inferinfo);
4676  (*bestub) = newub;
4677  }
4678 
4679  return SCIP_OKAY;
4680 }
4681 
4682 /** propagate the upper bounds and "opportunistically" the lower bounds using the time-table edge-finding algorithm */
4683 static
4685  SCIP* scip, /**< SCIP data structure */
4686  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
4687  int nvars, /**< number of start time variables (activities) */
4688  SCIP_VAR** vars, /**< array of start time variables */
4689  int* durations, /**< array of durations */
4690  int* demands, /**< array of demands */
4691  int capacity, /**< cumulative capacity */
4692  int hmin, /**< left bound of time axis to be considered (including hmin) */
4693  int hmax, /**< right bound of time axis to be considered (not including hmax) */
4694  int* newlbs, /**< array to buffer new lower bounds */
4695  int* newubs, /**< array to buffer new upper bounds */
4696  int* lbinferinfos, /**< array to store the inference information for the lower bound changes */
4697  int* ubinferinfos, /**< array to store the inference information for the upper bound changes */
4698  int* lsts, /**< array of latest start time of the flexible part in the same order as the variables */
4699  int* flexenergies, /**< array of flexible energies in the same order as the variables */
4700  int* perm, /**< permutation of the variables w.r.t. the non-decreasing order of the earliest start times */
4701  int* ests, /**< array with earliest strart times sorted in non-decreasing order */
4702  int* lcts, /**< array with latest completion times sorted in non-decreasing order */
4703  int* coreEnergyAfterEst, /**< core energy after the earliest start times */
4704  int* coreEnergyAfterLct, /**< core energy after the latest completion times */
4705  SCIP_Bool* initialized, /**< was conflict analysis initialized */
4706  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
4707  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
4708  )
4709 {
4710  int coreEnergyAfterEnd;
4711  SCIP_Longint maxavailable;
4712  SCIP_Longint minavailable;
4713  SCIP_Longint totalenergy;
4714  int nests;
4715  int est;
4716  int lct;
4717  int start;
4718  int end;
4719  int v;
4720 
4721  est = INT_MAX;
4722  lct = INT_MIN;
4723 
4724  /* compute earliest start and latest completion time of all jobs */
4725  for( v = 0; v < nvars; ++v )
4726  {
4727  start = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[v]));
4728  end = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[v])) + durations[v];
4729 
4730  est = MIN(est, start);
4731  lct = MAX(lct, end);
4732  }
4733 
4734  /* adjust the effective time horizon */
4735  hmin = MAX(hmin, est);
4736  hmax = MIN(hmax, lct);
4737 
4738  end = hmax + 1;
4739  coreEnergyAfterEnd = -1;
4740 
4741  maxavailable = ((SCIP_Longint) hmax - hmin) * capacity;
4742  minavailable = maxavailable;
4743  totalenergy = computeTotalEnergy(durations, demands, nvars);
4744 
4745  /* check if the smallest interval has a size such that the total energy fits, if so we can skip the propagator */
4746  if( ((SCIP_Longint) lcts[0] - ests[nvars-1]) * capacity >= totalenergy )
4747  return SCIP_OKAY;
4748 
4749  nests = nvars;
4750 
4751  /* loop over all variable in non-increasing order w.r.t. the latest completion time; thereby, the latest completion
4752  * times define the end of the time interval under investigation
4753  */
4754  for( v = nvars-1; v >= 0 && !(*cutoff); --v )
4755  {
4756  int flexenergy;
4757  int minbegin;
4758  int lbenergy;
4759  int lbcand;
4760  int i;
4761 
4762  lct = lcts[v];
4763 
4764  /* if the latest completion time is larger then hmax an infeasibility cannot be detected, since after hmax an
4765  * infinity capacity is available; hence we skip that
4766  */
4767  if( lct > hmax )
4768  continue;
4769 
4770  /* if the latest completion time is smaller then hmin we have to stop */
4771  if( lct <= hmin )
4772  {
4773  assert(v == 0 || lcts[v-1] <= lcts[v]);
4774  break;
4775  }
4776 
4777  /* if the latest completion time equals to previous end time, we can continue since this particular interval
4778  * induced by end was just analyzed
4779  */
4780  if( lct == end )
4781  continue;
4782 
4783  assert(lct < end);
4784 
4785  /* In case we only want to detect an overload (meaning no bound propagation) we can skip the interval; this is
4786  * the case if the free energy (the energy which is not occupied by any core) is smaller than the previous minimum
4787  * free energy; if so it means that in the next iterate the free-energy cannot be negative
4788  */
4789  if( !conshdlrdata->ttefinfer && end <= hmax && minavailable < maxavailable )
4790  {
4791  SCIP_Longint freeenergy;
4792 
4793  assert(coreEnergyAfterLct[v] >= coreEnergyAfterEnd);
4794  assert(coreEnergyAfterEnd >= 0);
4795 
4796  /* compute the energy which is not consumed by the cores with in the interval [lct, end) */
4797  freeenergy = capacity * ((SCIP_Longint) end - lct) - coreEnergyAfterLct[v] + coreEnergyAfterEnd;
4798 
4799  if( freeenergy <= minavailable )
4800  {
4801  SCIPdebugMsg(scip, "skip latest completion time <%d> (minimum available energy <%" SCIP_LONGINT_FORMAT ">, free energy <%" SCIP_LONGINT_FORMAT ">)\n", lct, minavailable, freeenergy);
4802  continue;
4803  }
4804  }
4805 
4806  SCIPdebugMsg(scip, "check intervals ending with <%d>\n", lct);
4807 
4808  end = lct;
4809  coreEnergyAfterEnd = coreEnergyAfterLct[v];
4810 
4811  flexenergy = 0;
4812  minavailable = maxavailable;
4813  minbegin = hmax;
4814  lbcand = -1;
4815  lbenergy = 0;
4816 
4817  /* loop over the job in non-increasing order w.r.t. the earliest start time; these earliest start time are
4818  * defining the beginning of the time interval under investigation; Thereby, the time interval gets wider and
4819  * wider
4820  */
4821  for( i = nests-1; i >= 0; --i )
4822  {
4823  SCIP_VAR* var;
4824  SCIP_Longint freeenergy;
4825  int duration;
4826  int demand;
4827  int begin;
4828  int idx;
4829  int lst;
4830 
4831  idx = perm[i];
4832  assert(idx >= 0);
4833  assert(idx < nvars);
4834  assert(!(*cutoff));
4835 
4836  /* the earliest start time of the job */
4837  est = ests[i];
4838 
4839  /* if the job starts after the current end, we can skip it and do not need to consider it again since the
4840  * latest completion times (which define end) are scant in non-increasing order
4841  */
4842  if( end <= est )
4843  {
4844  nests--;
4845  continue;
4846  }
4847 
4848  /* check if the interval has a size such that the total energy fits, if so we can skip all intervals with the
4849  * current ending time
4850  */
4851  if( ((SCIP_Longint) end - est) * capacity >= totalenergy )
4852  break;
4853 
4854  var = vars[idx];
4855  assert(var != NULL);
4856 
4857  duration = durations[idx];
4858  assert(duration > 0);
4859 
4860  demand = demands[idx];
4861  assert(demand > 0);
4862 
4863  lct = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var)) + duration;
4864 
4865  /* the latest start time of the free part of the job */
4866  lst = lsts[idx];
4867 
4868  /* in case the earliest start time is equal to minbegin, the job lies completely within the time window under
4869  * investigation; hence the overload check will do the the job
4870  */
4871  assert(est <= minbegin);
4872  if( minavailable < maxavailable && est < minbegin )
4873  {
4874  assert(!(*cutoff));
4875 
4876  /* try to tighten the upper bound */
4877  SCIP_CALL( tightenUbTTEF(scip, conshdlrdata, nvars, vars, durations, demands, capacity, hmin, hmax,
4878  var, duration, demand, est, lst, lct, minbegin, end, minavailable, &(newubs[idx]), &(ubinferinfos[idx]),
4879  initialized, explanation, cutoff) );
4880 
4881  if( *cutoff )
4882  break;
4883  }
4884 
4885  SCIPdebugMsg(scip, "check variable <%s>[%g,%g] (duration %d, demands %d, est <%d>, lst of free part <%d>\n",
4886  SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), duration, demand, est, lst);
4887 
4888  begin = est;
4889  assert(SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var)) == est);
4890 
4891  /* if the earliest start time is smaller than hmin we can stop here since the next job will not decrease the
4892  * free energy
4893  */
4894  if( begin < hmin )
4895  break;
4896 
4897  /* compute the contribution to the flexible energy */
4898  if( lct <= end )
4899  {
4900  /* if the jobs has to finish before the end, all the energy has to be scheduled */
4901  assert(lst >= begin);
4902  assert(flexenergies[idx] >= 0);
4903  flexenergy += flexenergies[idx];
4904  }
4905  else
4906  {
4907  /* the job partly overlaps with the end */
4908  int candenergy;
4909  int energy;
4910 
4911  /* compute the flexible energy which is part of the time interval for sure if the job is scheduled
4912  * w.r.t. latest start time
4913  *
4914  * @note we need to be aware of the effective horizon
4915  */
4916  energy = MIN(flexenergies[idx], demands[idx] * MAX(0, (end - lst)));
4917  assert(end - lst < duration);
4918  assert(energy >= 0);
4919 
4920  /* adjust the flexible energy of the time interval */
4921  flexenergy += energy;
4922 
4923  /* compute the flexible energy of the job which is not part of flexible energy of the time interval */
4924  candenergy = MIN(flexenergies[idx], demands[idx] * (end - begin)) - energy;
4925  assert(candenergy >= 0);
4926 
4927  /* check if we found a better candidate */
4928  if( candenergy > lbenergy )
4929  {
4930  lbenergy = candenergy;
4931  lbcand = idx;
4932  }
4933  }
4934 
4935  SCIPdebugMsg(scip, "time window [%d,%d) flexible energy <%d>\n", begin, end, flexenergy);
4936  assert(coreEnergyAfterEst[i] >= coreEnergyAfterEnd);
4937 
4938  /* compute the energy which is not used yet */
4939  freeenergy = capacity * ((SCIP_Longint) end - begin) - flexenergy - coreEnergyAfterEst[i] + coreEnergyAfterEnd;
4940 
4941  /* check overload */
4942  if( freeenergy < 0 )
4943  {
4944  SCIPdebugMsg(scip, "analyze overload within time window [%d,%d) capacity %d\n", begin, end, capacity);
4945 
4946  /* initialize conflict analysis if conflict analysis is applicable */
4948  {
4949  /* analyze infeasibilty */
4951 
4952  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
4953  begin, end, NULL, SCIP_BOUNDTYPE_UPPER, NULL, SCIP_UNKNOWN,
4954  conshdlrdata->usebdwidening, explanation) );
4955 
4956  (*initialized) = TRUE;
4957  }
4958 
4959  (*cutoff) = TRUE;
4960 
4961  /* for the statistic we count the number of times a cutoff was detected due the time-time-edge-finding */
4962  SCIPstatistic( SCIPconshdlrGetData(SCIPfindConshdlr(scip, CONSHDLR_NAME))->ncutoffoverloadTTEF++ );
4963 
4964  break;
4965  }
4966 
4967  /* check if the available energy is not sufficent to schedule the flexible energy of the best candidate job */
4968  if( lbenergy > 0 && freeenergy < lbenergy )
4969  {
4970  SCIP_Longint energy;
4971  int newlb;
4972  int ect;
4973 
4974  ect = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[lbcand])) + durations[lbcand];
4975  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[lbcand]));
4976 
4977  /* remove the energy of our job from the ... */
4978  energy = freeenergy + (computeCoreWithInterval(begin, end, ect, lst) + MAX(0, (SCIP_Longint) end - lsts[lbcand])) * demands[lbcand];
4979 
4980  newlb = end - (int)(energy / demands[lbcand]);
4981 
4982  if( newlb > lst )
4983  {
4984  /* initialize conflict analysis if conflict analysis is applicable */
4986  {
4987  SCIP_Real relaxedbd;
4988 
4989  /* analyze infeasibilty */
4991 
4992  relaxedbd = lst + 1.0;
4993 
4994  /* added to upper bound (which was overcut be new lower bound) of the variable */
4995  SCIP_CALL( SCIPaddConflictUb(scip, vars[lbcand], NULL) );
4996 
4997  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
4998  begin, end, vars[lbcand], SCIP_BOUNDTYPE_LOWER, NULL, relaxedbd,
4999  conshdlrdata->usebdwidening, explanation) );
5000 
5001  (*initialized) = TRUE;
5002  }
5003 
5004  (*cutoff) = TRUE;
5005  break;
5006  }
5007  else if( newlb > newlbs[lbcand] )
5008  {
5009  INFERINFO inferinfo;
5010 
5011  /* construct inference information */
5012  inferinfo = getInferInfo(PROPRULE_3_TTEF, begin, end);
5013 
5014  /* buffer upper bound change */
5015  lbinferinfos[lbcand] = inferInfoToInt(inferinfo);
5016  newlbs[lbcand] = newlb;
5017  }
5018  }
5019 
5020  /* check if the current interval has a smaller free energy */
5021  if( minavailable > freeenergy )
5022  {
5023  minavailable = freeenergy;
5024  minbegin = begin;
5025  }
5026  assert(minavailable >= 0);
5027  }
5028  }
5029 
5030  return SCIP_OKAY;
5031 }
5032 
5033 /** propagate the lower bounds and "opportunistically" the upper bounds using the time-table edge-finding algorithm */
5034 static
5036  SCIP* scip, /**< SCIP data structure */
5037  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
5038  int nvars, /**< number of start time variables (activities) */
5039  SCIP_VAR** vars, /**< array of start time variables */
5040  int* durations, /**< array of durations */
5041  int* demands, /**< array of demands */
5042  int capacity, /**< cumulative capacity */
5043  int hmin, /**< left bound of time axis to be considered (including hmin) */
5044  int hmax, /**< right bound of time axis to be considered (not including hmax) */
5045  int* newlbs, /**< array to buffer new lower bounds */
5046  int* newubs, /**< array to buffer new upper bounds */
5047  int* lbinferinfos, /**< array to store the inference information for the lower bound changes */
5048  int* ubinferinfos, /**< array to store the inference information for the upper bound changes */
5049  int* ects, /**< array of earliest completion time of the flexible part in the same order as the variables */
5050  int* flexenergies, /**< array of flexible energies in the same order as the variables */
5051  int* perm, /**< permutation of the variables w.r.t. the non-decreasing order of the latest completion times */
5052  int* ests, /**< array with earliest strart times sorted in non-decreasing order */
5053  int* lcts, /**< array with latest completion times sorted in non-decreasing order */
5054  int* coreEnergyAfterEst, /**< core energy after the earliest start times */
5055  int* coreEnergyAfterLct, /**< core energy after the latest completion times */
5056  SCIP_Bool* initialized, /**< was conflict analysis initialized */
5057  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
5058  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
5059  )
5060 {
5061  int coreEnergyAfterStart;
5062  SCIP_Longint maxavailable;
5063  SCIP_Longint minavailable;
5064  SCIP_Longint totalenergy;
5065  int nlcts;
5066  int begin;
5067  int minest;
5068  int maxlct;
5069  int start;
5070  int end;
5071  int v;
5072 
5073  if( *cutoff )
5074  return SCIP_OKAY;
5075 
5076  begin = hmin - 1;
5077 
5078  minest = INT_MAX;
5079  maxlct = INT_MIN;
5080 
5081  /* compute earliest start and latest completion time of all jobs */
5082  for( v = 0; v < nvars; ++v )
5083  {
5084  start = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[v]));
5085  end = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[v])) + durations[v];
5086 
5087  minest = MIN(minest, start);
5088  maxlct = MAX(maxlct, end);
5089  }
5090 
5091  /* adjust the effective time horizon */
5092  hmin = MAX(hmin, minest);
5093  hmax = MIN(hmax, maxlct);
5094 
5095  maxavailable = ((SCIP_Longint) hmax - hmin) * capacity;
5096  totalenergy = computeTotalEnergy(durations, demands, nvars);
5097 
5098  /* check if the smallest interval has a size such that the total energy fits, if so we can skip the propagator */
5099  if( ((SCIP_Longint) lcts[0] - ests[nvars-1]) * capacity >= totalenergy )
5100  return SCIP_OKAY;
5101 
5102  nlcts = 0;
5103 
5104  /* loop over all variable in non-decreasing order w.r.t. the earliest start times; thereby, the earliest start times
5105  * define the start of the time interval under investigation
5106  */
5107  for( v = 0; v < nvars; ++v )
5108  {
5109  int flexenergy;
5110  int minend;
5111  int ubenergy;
5112  int ubcand;
5113  int est;
5114  int i;
5115 
5116  est = ests[v];
5117 
5118  /* if the earliest start time is smaller then hmin an infeasibility cannot be detected, since before hmin an
5119  * infinity capacity is available; hence we skip that
5120  */
5121  if( est < hmin )
5122  continue;
5123 
5124  /* if the earliest start time is larger or equal then hmax we have to stop */
5125  if( est >= hmax )
5126  break;
5127 
5128  /* if the latest earliest start time equals to previous start time, we can continue since this particular interval
5129  * induced by start was just analyzed
5130  */
5131  if( est == begin )
5132  continue;
5133 
5134  assert(est > begin);
5135 
5136  SCIPdebugMsg(scip, "check intervals starting with <%d>\n", est);
5137 
5138  begin = est;
5139  coreEnergyAfterStart = coreEnergyAfterEst[v];
5140 
5141  flexenergy = 0;
5142  minavailable = maxavailable;
5143  minend = hmin;
5144  ubcand = -1;
5145  ubenergy = 0;
5146 
5147  /* loop over the job in non-decreasing order w.r.t. the latest completion time; these latest completion times are
5148  * defining the ending of the time interval under investigation; thereby, the time interval gets wider and wider
5149  */
5150  for( i = nlcts; i < nvars; ++i )
5151  {
5152  SCIP_VAR* var;
5153  SCIP_Longint freeenergy;
5154  int duration;
5155  int demand;
5156  int idx;
5157  int lct;
5158  int ect;
5159 
5160  idx = perm[i];
5161  assert(idx >= 0);
5162  assert(idx < nvars);
5163  assert(!(*cutoff));
5164 
5165  /* the earliest start time of the job */
5166  lct = lcts[i];
5167 
5168  /* if the job has a latest completion time before the the current start, we can skip it and do not need to
5169  * consider it again since the earliest start times (which define the start) are scant in non-decreasing order
5170  */
5171  if( lct <= begin )
5172  {
5173  nlcts++;
5174  continue;
5175  }
5176 
5177  /* check if the interval has a size such that the total energy fits, if so we can skip all intervals which
5178  * start with current beginning time
5179  */
5180  if( ((SCIP_Longint) lct - begin) * capacity >= totalenergy )
5181  break;
5182 
5183  var = vars[idx];
5184  assert(var != NULL);
5185 
5186  duration = durations[idx];
5187  assert(duration > 0);
5188 
5189  demand = demands[idx];
5190  assert(demand > 0);
5191 
5192  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
5193 
5194  /* the earliest completion time of the flexible part of the job */
5195  ect = ects[idx];
5196 
5197  /* in case the latest completion time is equal to minend, the job lies completely within the time window under
5198  * investigation; hence the overload check will do the the job
5199  */
5200  assert(lct >= minend);
5201  if( minavailable < maxavailable && lct > minend )
5202  {
5203  assert(!(*cutoff));
5204 
5205  /* try to tighten the upper bound */
5206  SCIP_CALL( tightenLbTTEF(scip, conshdlrdata, nvars, vars, durations, demands, capacity, hmin, hmax,
5207  var, duration, demand, est, ect, lct, begin, minend, minavailable, &(newlbs[idx]), &(lbinferinfos[idx]),
5208  initialized, explanation, cutoff) );
5209 
5210  if( *cutoff )
5211  return SCIP_OKAY;
5212  }
5213 
5214  SCIPdebugMsg(scip, "check variable <%s>[%g,%g] (duration %d, demands %d, est <%d>, ect of free part <%d>\n",
5215  SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), duration, demand, est, ect);
5216 
5217  end = lct;
5218  assert(SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var)) + duration == lct);
5219 
5220  /* if the latest completion time is larger than hmax we can stop here since the next job will not decrease the
5221  * free energy
5222  */
5223  if( end > hmax )
5224  break;
5225 
5226  /* compute the contribution to the flexible energy */
5227  if( est >= begin )
5228  {
5229  /* if the jobs has to finish before the end, all the energy has to be scheduled */
5230  assert(ect <= end);
5231  assert(flexenergies[idx] >= 0);
5232  flexenergy += flexenergies[idx];
5233  }
5234  else
5235  {
5236  /* the job partly overlaps with the end */
5237  int candenergy;
5238  int energy;
5239 
5240  /* compute the flexible energy which is part of the time interval for sure if the job is scheduled
5241  * w.r.t. latest start time
5242  *
5243  * @note we need to be aware of the effective horizon
5244  */
5245  energy = MIN(flexenergies[idx], demands[idx] * MAX(0, (ect - begin)));
5246  assert(ect - begin < duration);
5247  assert(energy >= 0);
5248 
5249  /* adjust the flexible energy of the time interval */
5250  flexenergy += energy;
5251 
5252  /* compute the flexible energy of the job which is not part of flexible energy of the time interval */
5253  candenergy = MIN(flexenergies[idx], demands[idx] * (end - begin)) - energy;
5254  assert(candenergy >= 0);
5255 
5256  /* check if we found a better candidate */
5257  if( candenergy > ubenergy )
5258  {
5259  ubenergy = candenergy;
5260  ubcand = idx;
5261  }
5262  }
5263 
5264  SCIPdebugMsg(scip, "time window [%d,%d) flexible energy <%d>\n", begin, end, flexenergy);
5265  assert(coreEnergyAfterLct[i] <= coreEnergyAfterStart);
5266 
5267  /* compute the energy which is not used yet */
5268  freeenergy = capacity * ((SCIP_Longint) end - begin) - flexenergy - coreEnergyAfterStart + coreEnergyAfterLct[i];
5269 
5270  /* check overload */
5271  if( freeenergy < 0 )
5272  {
5273  SCIPdebugMsg(scip, "analyze overload within time window [%d,%d) capacity %d\n", begin, end, capacity);
5274 
5275  /* initialize conflict analysis if conflict analysis is applicable */
5277  {
5278  /* analyze infeasibilty */
5280 
5281  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
5282  begin, end, NULL, SCIP_BOUNDTYPE_UPPER, NULL, SCIP_UNKNOWN,
5283  conshdlrdata->usebdwidening, explanation) );
5284 
5285  (*initialized) = TRUE;
5286  }
5287 
5288  (*cutoff) = TRUE;
5289 
5290  /* for the statistic we count the number of times a cutoff was detected due the time-time-edge-finding */
5291  SCIPstatistic( SCIPconshdlrGetData(SCIPfindConshdlr(scip, CONSHDLR_NAME))->ncutoffoverloadTTEF++ );
5292 
5293  return SCIP_OKAY;
5294  }
5295 
5296  /* check if the available energy is not sufficent to schedule the flexible energy of the best candidate job */
5297  if( ubenergy > 0 && freeenergy < ubenergy )
5298  {
5299  SCIP_Longint energy;
5300  int newub;
5301  int lst;
5302 
5303  duration = durations[ubcand];
5304  assert(duration > 0);
5305 
5306  ect = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[ubcand])) + duration;
5307  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[ubcand]));
5308 
5309  /* remove the energy of our job from the ... */
5310  energy = freeenergy + (computeCoreWithInterval(begin, end, ect, lst) + MAX(0, (SCIP_Longint) ects[ubcand] - begin)) * demands[ubcand];
5311 
5312  newub = begin - duration + (int)(energy / demands[ubcand]);
5313 
5314  if( newub < ect - duration )
5315  {
5316  /* initialize conflict analysis if conflict analysis is applicable */
5318  {
5319  SCIP_Real relaxedbd;
5320  /* analyze infeasibilty */
5322 
5323  relaxedbd = ect - duration - 1.0;
5324 
5325  /* added to lower bound (which was undercut be new upper bound) of the variable */
5326  SCIP_CALL( SCIPaddConflictUb(scip, vars[ubcand], NULL) );
5327 
5328  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
5329  begin, end, vars[ubcand], SCIP_BOUNDTYPE_UPPER, NULL, relaxedbd,
5330  conshdlrdata->usebdwidening, explanation) );
5331 
5332  (*initialized) = TRUE;
5333  }
5334 
5335  (*cutoff) = TRUE;
5336  return SCIP_OKAY;
5337  }
5338  else if( newub < newubs[ubcand] )
5339  {
5340  INFERINFO inferinfo;
5341 
5342  /* construct inference information */
5343  inferinfo = getInferInfo(PROPRULE_3_TTEF, begin, end);
5344 
5345  /* buffer upper bound change */
5346  ubinferinfos[ubcand] = inferInfoToInt(inferinfo);
5347  newubs[ubcand] = newub;
5348  }
5349  }
5350 
5351  /* check if the current interval has a smaller free energy */
5352  if( minavailable > freeenergy )
5353  {
5354  minavailable = freeenergy;
5355  minend = end;
5356  }
5357  assert(minavailable >= 0);
5358  }
5359  }
5360 
5361  return SCIP_OKAY;
5362 }
5363 
5364 /** checks whether the instance is infeasible due to a overload within a certain time frame using the idea of time-table
5365  * edge-finding
5366  *
5367  * @note The algorithm is based on the following two papers:
5368  * - Petr Vilim, "Timetable Edge Finding Filtering Algorithm for Discrete Cumulative Resources", In: Tobias
5369  * Achterberg and J. Christopher Beck (Eds.), Integration of AI and OR Techniques in Constraint Programming for
5370  * Combinatorial Optimization Problems (CPAIOR 2011), LNCS 6697, pp 230--245
5371  * - Andreas Schutt, Thibaut Feydy, and Peter J. Stuckey, "Explaining Time-Table-Edge-Finding Propagation for the
5372  * Cumulative Resource Constraint (submitted to CPAIOR 2013)
5373  */
5374 static
5376  SCIP* scip, /**< SCIP data structure */
5377  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
5378  SCIP_PROFILE* profile, /**< current core profile */
5379  int nvars, /**< number of start time variables (activities) */
5380  SCIP_VAR** vars, /**< array of start time variables */
5381  int* durations, /**< array of durations */
5382  int* demands, /**< array of demands */
5383  int capacity, /**< cumulative capacity */
5384  int hmin, /**< left bound of time axis to be considered (including hmin) */
5385  int hmax, /**< right bound of time axis to be considered (not including hmax) */
5386  SCIP_CONS* cons, /**< constraint which is propagated (needed to SCIPinferVar**Cons()) */
5387  int* nchgbds, /**< pointer to store the number of bound changes */
5388  SCIP_Bool* initialized, /**< was conflict analysis initialized */
5389  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
5390  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
5391  )
5392 {
5393  int* coreEnergyAfterEst;
5394  int* coreEnergyAfterLct;
5395  int* flexenergies;
5396  int* permests;
5397  int* permlcts;
5398  int* lcts;
5399  int* ests;
5400  int* ects;
5401  int* lsts;
5402 
5403  int* newlbs;
5404  int* newubs;
5405  int* lbinferinfos;
5406  int* ubinferinfos;
5407 
5408  int v;
5409 
5410  /* check if a cutoff was already detected */
5411  if( (*cutoff) )
5412  return SCIP_OKAY;
5413 
5414  /* check if at least the basic overload checking should be perfomed */
5415  if( !conshdlrdata->ttefcheck )
5416  return SCIP_OKAY;
5417 
5418  SCIPdebugMsg(scip, "run time-table edge-finding overload checking\n");
5419 
5420  SCIP_CALL( SCIPallocBufferArray(scip, &coreEnergyAfterEst, nvars) );
5421  SCIP_CALL( SCIPallocBufferArray(scip, &coreEnergyAfterLct, nvars) );
5422  SCIP_CALL( SCIPallocBufferArray(scip, &flexenergies, nvars) );
5423  SCIP_CALL( SCIPallocBufferArray(scip, &permlcts, nvars) );
5424  SCIP_CALL( SCIPallocBufferArray(scip, &permests, nvars) );
5425  SCIP_CALL( SCIPallocBufferArray(scip, &lcts, nvars) );
5426  SCIP_CALL( SCIPallocBufferArray(scip, &ests, nvars) );
5427  SCIP_CALL( SCIPallocBufferArray(scip, &ects, nvars) );
5428  SCIP_CALL( SCIPallocBufferArray(scip, &lsts, nvars) );
5429 
5430  SCIP_CALL( SCIPallocBufferArray(scip, &newlbs, nvars) );
5431  SCIP_CALL( SCIPallocBufferArray(scip, &newubs, nvars) );
5432  SCIP_CALL( SCIPallocBufferArray(scip, &lbinferinfos, nvars) );
5433  SCIP_CALL( SCIPallocBufferArray(scip, &ubinferinfos, nvars) );
5434 
5435  /* we need to buffer the bound changes since the propagation algorithm cannot handle new bound dynamically */
5436  for( v = 0; v < nvars; ++v )
5437  {
5438  newlbs[v] = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(vars[v]));
5439  newubs[v] = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(vars[v]));
5440  lbinferinfos[v] = 0;
5441  ubinferinfos[v] = 0;
5442  }
5443 
5444  /* collect earliest start times, latest completion time, and free energy contributions */
5445  collectDataTTEF(scip, nvars, vars, durations, demands, hmin, hmax, permests, ests, permlcts, lcts, ects, lsts, flexenergies);
5446 
5447  /* sort the earliest start times and latest completion in non-decreasing order */
5448  SCIPsortIntInt(ests, permests, nvars);
5449  SCIPsortIntInt(lcts, permlcts, nvars);
5450 
5451  /* compute for the different earliest start and latest completion time the core energy of the corresponding time
5452  * points
5453  */
5454  computeCoreEnergyAfter(profile, nvars, ests, lcts, coreEnergyAfterEst, coreEnergyAfterLct);
5455 
5456  /* propagate the upper bounds and "opportunistically" the lower bounds */
5457  SCIP_CALL( propagateUbTTEF(scip, conshdlrdata, nvars, vars, durations, demands, capacity, hmin, hmax,
5458  newlbs, newubs, lbinferinfos, ubinferinfos, lsts, flexenergies,
5459  permests, ests, lcts, coreEnergyAfterEst, coreEnergyAfterLct, initialized, explanation, cutoff) );
5460 
5461  /* propagate the lower bounds and "opportunistically" the upper bounds */
5462  SCIP_CALL( propagateLbTTEF(scip, conshdlrdata, nvars, vars, durations, demands, capacity, hmin, hmax,
5463  newlbs, newubs, lbinferinfos, ubinferinfos, ects, flexenergies,
5464  permlcts, ests, lcts, coreEnergyAfterEst, coreEnergyAfterLct, initialized, explanation, cutoff) );
5465 
5466  /* apply the buffer bound changes */
5467  for( v = 0; v < nvars && !(*cutoff); ++v )
5468  {
5469  SCIP_Bool infeasible;
5470  SCIP_Bool tightened;
5471 
5472  if( inferInfoIsValid(intToInferInfo(lbinferinfos[v])) )
5473  {
5474  SCIP_CALL( SCIPinferVarLbCons(scip, vars[v], (SCIP_Real)newlbs[v], cons, lbinferinfos[v],
5475  TRUE, &infeasible, &tightened) );
5476  }
5477  else
5478  {
5479  SCIP_CALL( SCIPtightenVarLb(scip, vars[v], (SCIP_Real)newlbs[v], TRUE, &infeasible, &tightened) );
5480  }
5481 
5482  /* since we change first the lower bound of the variable an infeasibilty should not be detected */
5483  assert(!infeasible);
5484 
5485  if( tightened )
5486  {
5487  (*nchgbds)++;
5488 
5489  /* for the statistic we count the number of times a cutoff was detected due the time-time */
5491  }
5492 
5493  if( inferInfoIsValid(intToInferInfo(ubinferinfos[v])) )
5494  {
5495  SCIP_CALL( SCIPinferVarUbCons(scip, vars[v], (SCIP_Real)newubs[v], cons, ubinferinfos[v],
5496  TRUE, &infeasible, &tightened) );
5497  }
5498  else
5499  {
5500  SCIP_CALL( SCIPtightenVarUb(scip, vars[v], (SCIP_Real)newubs[v], TRUE, &infeasible, &tightened) );
5501  }
5502 
5503  /* since upper bound was compute w.r.t. the "old" bound the previous lower bound update together with this upper
5504  * bound update can be infeasible
5505  */
5506  if( infeasible )
5507  {
5508  /* a small performance improvement is possible here: if the tighten...TEFF and propagate...TEFF methods would
5509  * return not only the inferinfos, but the actual begin and end values, then the infeasibility here could also
5510  * be analyzed in the case when begin and end exceed the 15 bit limit
5511  */
5512  if( SCIPisConflictAnalysisApplicable(scip) && inferInfoIsValid(intToInferInfo(ubinferinfos[v])) )
5513  {
5514  INFERINFO inferinfo;
5515  SCIP_VAR* var;
5516  int begin;
5517  int end;
5518 
5519  var = vars[v];
5520  assert(var != NULL);
5521 
5522  /* initialize conflict analysis */
5524 
5525  /* convert int to inference information */
5526  inferinfo = intToInferInfo(ubinferinfos[v]);
5527 
5528  /* collect time window from inference information */
5529  begin = inferInfoGetData1(inferinfo);
5530  end = inferInfoGetData2(inferinfo);
5531  assert(begin < end);
5532 
5533  /* added to lower bound (which was undercut be new upper bound) of the variable */
5534  SCIP_CALL( SCIPaddConflictLb(scip, var, NULL) );
5535 
5536  /* analysis the upper bound change */
5537  SCIP_CALL( analyzeEnergyRequirement(scip, nvars, vars, durations, demands, capacity,
5538  begin, end, var, SCIP_BOUNDTYPE_UPPER, NULL, SCIPvarGetLbLocal(vars[v]) - 1.0,
5539  conshdlrdata->usebdwidening, explanation) );
5540 
5541  (*initialized) = TRUE;
5542  }
5543 
5544  /* for the statistic we count the number of times a cutoff was detected due the time-time */
5545  SCIPstatistic( SCIPconshdlrGetData(SCIPfindConshdlr(scip, CONSHDLR_NAME))->ncutoffoverloadTTEF++ );
5546 
5547  (*cutoff) = TRUE;
5548  break;
5549  }
5550 
5551  if( tightened )
5552  {
5553  (*nchgbds)++;
5554 
5555  /* for the statistic we count the number of times a cutoff was detected due the time-time */
5557  }
5558  }
5559 
5560  SCIPfreeBufferArray(scip, &ubinferinfos);
5561  SCIPfreeBufferArray(scip, &lbinferinfos);
5562  SCIPfreeBufferArray(scip, &newubs);
5563  SCIPfreeBufferArray(scip, &newlbs);
5564 
5565  /* free buffer arrays */
5566  SCIPfreeBufferArray(scip, &lsts);
5567  SCIPfreeBufferArray(scip, &ects);
5568  SCIPfreeBufferArray(scip, &ests);
5569  SCIPfreeBufferArray(scip, &lcts);
5570  SCIPfreeBufferArray(scip, &permests);
5571  SCIPfreeBufferArray(scip, &permlcts);
5572  SCIPfreeBufferArray(scip, &flexenergies);
5573  SCIPfreeBufferArray(scip, &coreEnergyAfterLct);
5574  SCIPfreeBufferArray(scip, &coreEnergyAfterEst);
5575 
5576  return SCIP_OKAY;
5577 }
5578 
5579 /** a cumulative condition is not satisfied if its capacity is exceeded at a time where jobs cannot be shifted (core)
5580  * anymore we build up a cumulative profile of all cores of jobs and try to improve bounds of all jobs; also known as
5581  * time table propagator
5582  */
5583 static
5585  SCIP* scip, /**< SCIP data structure */
5586  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
5587  SCIP_PROFILE* profile, /**< core profile */
5588  int nvars, /**< number of start time variables (activities) */
5589  SCIP_VAR** vars, /**< array of start time variables */
5590  int* durations, /**< array of durations */
5591  int* demands, /**< array of demands */
5592  int capacity, /**< cumulative capacity */
5593  int hmin, /**< left bound of time axis to be considered (including hmin) */
5594  int hmax, /**< right bound of time axis to be considered (not including hmax) */
5595  SCIP_CONS* cons, /**< constraint which is propagated (needed to SCIPinferVar**Cons()) */
5596  int* nchgbds, /**< pointer to store the number of bound changes */
5597  SCIP_Bool* initialized, /**< was conflict analysis initialized */
5598  SCIP_Bool* explanation, /**< bool array which marks the variable which are part of the explanation if a cutoff was detected, or NULL */
5599  SCIP_Bool* cutoff /**< pointer to store if the constraint is infeasible */
5600  )
5601 {
5602  SCIP_Bool infeasible;
5603  int v;
5604 
5605  assert(scip != NULL);
5606  assert(nvars > 0);
5607  assert(cons != NULL);
5608  assert(cutoff != NULL);
5609 
5610  /* check if already a cutoff was detected */
5611  if( (*cutoff) )
5612  return SCIP_OKAY;
5613 
5614  /* check if the time tabling should infer bounds */
5615  if( !conshdlrdata->ttinfer )
5616  return SCIP_OKAY;
5617 
5618  assert(*initialized == FALSE);
5619 
5620  SCIPdebugMsg(scip, "propagate cores of cumulative condition of constraint <%s>[%d,%d) <= %d\n",
5621  SCIPconsGetName(cons), hmin, hmax, capacity);
5622 
5623  infeasible = FALSE;
5624 
5625  /* if core profile is empty; nothing to do */
5626  if( SCIPprofileGetNTimepoints(profile) <= 1 )
5627  return SCIP_OKAY;
5628 
5629  /* start checking each job whether the bounds can be improved */
5630  for( v = 0; v < nvars; ++v )
5631  {
5632  SCIP_VAR* var;
5633  int demand;
5634  int duration;
5635  int begin;
5636  int end;
5637  int est;
5638  int lst;
5639 
5640  var = vars[v];
5641  assert(var != NULL);
5642 
5643  duration = durations[v];
5644  assert(duration > 0);
5645 
5646  /* collect earliest and latest start time */
5647  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
5648  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
5649 
5650  /* check if the start time variables is already fixed; in that case we can ignore the job */
5651  if( est == lst )
5652  continue;
5653 
5654  /* check if the job runs completely outside of the effective horizon [hmin, hmax); if so skip it */
5655  if( lst + duration <= hmin || est >= hmax )
5656  continue;
5657 
5658  /* compute core interval w.r.t. effective time horizon */
5659  begin = MAX(hmin, lst);
5660  end = MIN(hmax, est + duration);
5661 
5662  demand = demands[v];
5663  assert(demand > 0);
5664 
5665  /* if the job has a core, remove it first */
5666  if( begin < end )
5667  {
5668  SCIPdebugMsg(scip, "variable <%s>[%g,%g] (duration %d, demand %d): remove core [%d,%d)\n",
5669  SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), duration, demand, begin, end);
5670 
5671  SCIP_CALL( SCIPprofileDeleteCore(profile, begin, end, demand) );
5672  }
5673 
5674  /* first try to update the earliest start time */
5675  SCIP_CALL( coretimesUpdateLb(scip, nvars, vars, durations, demands, capacity, hmin, hmax, cons,
5676  profile, v, nchgbds, conshdlrdata->usebdwidening, initialized, explanation, cutoff) );
5677 
5678  if( *cutoff )
5679  break;
5680 
5681  /* second try to update the latest start time */
5682  SCIP_CALL( coretimesUpdateUb(scip, var, duration, demand, capacity, cons,
5683  profile, v, nchgbds) );
5684 
5685  if( *cutoff )
5686  break;
5687 
5688  /* collect the potentially updated earliest and latest start time */
5689  est = SCIPconvertRealToInt(scip, SCIPvarGetLbLocal(var));
5690  lst = SCIPconvertRealToInt(scip, SCIPvarGetUbLocal(var));
5691 
5692  /* compute core interval w.r.t. effective time horizon */
5693  begin = MAX(hmin, lst);
5694  end = MIN(hmax, est + duration);
5695 
5696  /* after updating the bound we might have a new core */
5697  if( begin < end )
5698  {
5699  int pos;
5700 
5701  SCIPdebugMsg(scip, "variable <%s>[%d,%d] (duration %d, demand %d): add core [%d,%d)\n",
5702  SCIPvarGetName(var), est, lst, duration, demand, begin, end);
5703 
5704  SCIP_CALL( SCIPprofileInsertCore(profile, begin, end, demand, &pos, &infeasible) );
5705 
5706  if( infeasible )
5707  {
5708  /* use conflict analysis to analysis the core insertion which was infeasible */
5709  SCIP_CALL( analyseInfeasibelCoreInsertion(scip, nvars, vars, durations, demands, capacity, hmin, hmax,
5710  var, duration, demand, SCIPprofileGetTime(profile, pos), conshdlrdata->usebdwidening, initialized, explanation) );
5711 
5712  if( explanation != NULL )
5713  explanation[v] = TRUE;
5714 
5715  (*cutoff) = TRUE;
5716 
5717  /* for the statistic we count the number of times a cutoff was detected due the time-time */
5718  SCIPstatistic( SCIPconshdlrGetData(SCIPfindConshdlr(scip, CONSHDLR_NAME))->ncutofftimetable++ );
5719 
5720  break;
5721  }
5722  }
5723  }
5724 
5725  return SCIP_OKAY;
5726 }
5727 
5728 
5729 /** node data structure for the binary tree used for edgefinding (with overload checking) */
5730 struct SCIP_NodeData
5731 {
5732  SCIP_VAR* var; /**< start time variable of the job if the node data belongs to a leaf, otherwise NULL */
5733  SCIP_Real key; /**< key which is to insert the corresponding search node */
5734  int est; /**< earliest start time if the node data belongs to a leaf */
5735  int lct; /**< latest completion time if the node data belongs to a leaf */
5736  int demand; /**< demand of the job if the node data belongs to a leaf */
5737  int duration; /**< duration of the job if the node data belongs to a leaf */
5738  int leftadjust; /**< left adjustments of the duration w.r.t. hmin */
5739  int rightadjust; /**< right adjustments of the duration w.r.t. hmax */
5740  SCIP_Longint enveloptheta; /**< the maximal energy of a subset of jobs part of the theta set */
5741  int energytheta; /**< energy of the subset of the jobs which are part of theta set */
5742  int energylambda;
5743  SCIP_Longint enveloplambda;
5744  int idx; /**< index of the start time variable in the (global) variable array */
5745  SCIP_Bool intheta; /**< belongs the node to the theta set (otherwise to the lambda set) */
5746 };
5747 typedef struct SCIP_NodeData SCIP_NODEDATA;
5749 
5750 /** update node data structure starting from the given node along the path to the root node */
5751 static
5752 void updateEnvelope(
5753  SCIP* scip, /**< SCIP data structure */
5754  SCIP_BTNODE* node /**< search node which inserted */
5755  )
5756 {
5757  SCIP_BTNODE* left;
5758  SCIP_BTNODE* right;
5759  SCIP_NODEDATA* nodedata;
5760  SCIP_NODEDATA* leftdata;
5761  SCIP_NODEDATA* rightdata;
5762 
5763  SCIPdebugMsg(scip, "update envelop starting from node <%p>\n", (void*)node);
5764 
5765  if( SCIPbtnodeIsLeaf(node) )
5766  node = SCIPbtnodeGetParent(node);
5767 
5768  while( node != NULL )
5769  {
5770  /* get node data */
5771  nodedata = (SCIP_NODEDATA*)SCIPbtnodeGetData(node);
5772  assert(nodedata != NULL);
5773 
5774  /* collect node data from left node */
5775  left = SCIPbtnodeGetLeftchild(node);
5776  assert(left != NULL);
5777  leftdata = (SCIP_NODEDATA*)SCIPbtnodeGetData(left);
5778  assert(leftdata != NULL);
5779 
5780  /* collect node data from right node */
5781  right = SCIPbtnodeGetRightchild(node);
5782  assert(right != NULL);
5783  rightdata = (SCIP_NODEDATA*)SCIPbtnodeGetData(right);
5784  assert(rightdata != NULL);
5785 
5786  /* update envelop and energy */
5787  if( leftdata->enveloptheta >= 0 )
5788  {
5789  assert(rightdata->energytheta != -1);
5790  nodedata->enveloptheta = MAX(leftdata->enveloptheta + rightdata->energytheta, rightdata->enveloptheta);
5791  }
5792  else
5793  nodedata->enveloptheta = rightdata->enveloptheta;
5794 
5795  assert(leftdata->energytheta != -1);
5796  assert(rightdata->energytheta != -1);
5797  nodedata->energytheta = leftdata->energytheta + rightdata->energytheta;
5798 
5799  if( leftdata->enveloplambda >= 0 )
5800  {
5801  assert(rightdata->energytheta != -1);
5802  nodedata->enveloplambda = MAX(leftdata->enveloplambda + rightdata->energytheta, rightdata->enveloplambda);
5803  }
5804  else
5805  nodedata->enveloplambda = rightdata->enveloplambda;
5806 
5807  if( leftdata->enveloptheta >= 0 && rightdata->energylambda >= 0 )
5808  nodedata->enveloplambda = MAX(nodedata->enveloplambda, leftdata->enveloptheta + rightdata->energylambda);
5809 
5810  SCIPdebugMsg(scip, "node <%p> lambda envelop %" SCIP_LONGINT_FORMAT "\n", (void*)node, nodedata->enveloplambda);
5811 
5812  if( leftdata->energylambda >= 0 && rightdata->energylambda >= 0 )
5813  {
5814  assert(rightdata->energytheta != -1);
5815  assert(leftdata->energytheta != -1);
5816  nodedata->energylambda = MAX(leftdata->energylambda + rightdata->energytheta, leftdata->energytheta + rightdata->energylambda);
5817  }
5818  else if( rightdata->energylambda >= 0 )
5819  {
5820  assert(leftdata->energytheta != -1);
5821  nodedata->energylambda = leftdata->energytheta + rightdata->energylambda;
5822  }
5823  else if( leftdata->energylambda >= 0 )
5824  {
5825  assert(rightdata->energytheta != -1);
5826  nodedata->energylambda = leftdata->energylambda + rightdata->energytheta;
5827  }
5828  else
5829  nodedata->energylambda = -1;
5830 
5831  /* go to parent */
5832  node = SCIPbtnodeGetParent(node);
5833  }
5834 
5835  SCIPdebugMsg(scip, "updating done\n");
5836 }
5837 
5838 /** updates the key of the first parent on the trace which comes from left */
5839 static
5840 void updateKeyOnTrace(
5841  SCIP_BTNODE* node, /**< node to start the trace */
5842  SCIP_Real key /**< update search key */
5843  )
5844 {
5845  assert(node != NULL);
5846 
5847  while( !SCIPbtnodeIsRoot(node) )
5848  {
5849  SCIP_BTNODE* parent;
5850 
5851  parent = SCIPbtnodeGetParent(node);
5852  assert(parent != NULL);
5853 
5854  if( SCIPbtnodeIsLeftchild(node) )
5855  {
5856  SCIP_NODEDATA* nodedata;
5857 
5858  nodedata = (SCIP_NODEDATA*)SCIPbtnodeGetData(parent);
5859  assert(nodedata != NULL);
5860 
5861  nodedata->key = key;
5862  return;
5863  }
5864 
5865  node = parent;
5866  }
5867 }
5868 
5869 
5870 /** deletes the given node and updates all envelops */
5871 static
5873  SCIP* scip, /**< SCIP data structure */
5874  SCIP_BT* tree, /**< binary tree */
5875  SCIP_BTNODE* node /**< node to be deleted */
5876  )
5877 {
5878  SCIP_BTNODE* parent;
5879  SCIP_BTNODE* grandparent;
5880  SCIP_BTNODE* sibling;
5881 
5882  assert(scip != NULL);
5883  assert(tree != NULL);
5884  assert(node != NULL);
5885 
5886  assert(SCIPbtnodeIsLeaf(node));
5887  assert(!SCIPbtnodeIsRoot(node));
5888 
5889  SCIPdebugMsg(scip, "delete node <%p>\n", (void*)node);
5890 
5891  parent = SCIPbtnodeGetParent(node);
5892  assert(parent != NULL);
5893  if( SCIPbtnodeIsLeftchild(node) )
5894  {
5895  sibling = SCIPbtnodeGetRightchild(parent);
5896  SCIPbtnodeSetRightchild(parent, NULL);
5897  }
5898  else
5899  {
5900  sibling = SCIPbtnodeGetLeftchild(parent);
5901  SCIPbtnodeSetLeftchild(parent, NULL);
5902  }
5903  assert(sibling != NULL);
5904 
5905  grandparent = SCIPbtnodeGetParent(parent);
5906 
5907  if( grandparent != NULL )
5908  {
5909  /* reset parent of sibling */
5910  SCIPbtnodeSetParent(sibling, grandparent);
5911 
5912  /* reset child of grandparent to sibling */
5913  if( SCIPbtnodeIsLeftchild(parent) )
5914  {
5915  SCIPbtnodeSetLeftchild(grandparent, sibling);
5916  }
5917  else
5918  {
5919  SCIP_NODEDATA* nodedata;
5920 
5921  assert(SCIPbtnodeIsRightchild(parent));
5922  SCIPbtnodeSetRightchild(grandparent, sibling);
5923 
5924  nodedata = (SCIP_NODEDATA*)SCIPbtnodeGetData(sibling);
5925 
5926  updateKeyOnTrace(grandparent, nodedata->key);
5927  }
5928 
5929  updateEnvelope(scip, grandparent);
5930  }
5931  else
5932  {
5933  SCIPbtnodeSetParent(sibling, NULL);
5934 
5935  SCIPbtSetRoot(tree, sibling);
5936  }
5937 
5938  SCIPbtnodeFree(tree, &parent);
5939 
5940  return SCIP_OKAY;
5941 }
5942 
5943 /** moves a node form the theta set into the lambda set and updates the envelops */
5944 static
5946  SCIP* scip, /**< SCIP data structure */
5947  SCIP_BT* tree, /**< binary tree */
5948  SCIP_BTNODE* node /**< node to move into the lambda set */
5949  )
5950 {
5951  SCIP_NODEDATA* nodedata;
5952 
5953  assert(scip != NULL);
5954  assert(tree != NULL);
5955  assert(node != NULL);
5956 
5957  nodedata = (SCIP_NODEDATA*)SCIPbtnodeGetData(node);
5958  assert(nodedata != NULL);
5959  assert(nodedata->intheta);
5960 
5961  /* move the contributions form the theta set into the lambda set */
5962  assert(nodedata->enveloptheta != -1);
5963  assert(nodedata->energytheta != -1);
5964  assert(nodedata->enveloplambda == -1);
5965  assert(nodedata->energylambda == -1);
5966  nodedata->enveloplambda = nodedata->enveloptheta;
5967  nodedata->energylambda = nodedata->energytheta;
5968 
5969  nodedata->enveloptheta = -1;
5970  nodedata->energytheta = 0;
5971  nodedata->intheta = FALSE;
5972 
5973  /* update the energy and envelop values on trace */
5974  updateEnvelope(scip, node);
5975 
5976  return SCIP_OKAY;
5977 }
5978 
5979 /** inserts a node into the theta set and update the envelops */
5980 static
5982  SCIP* scip, /**< SCIP data structure */
5983  SCIP_BT* tree, /**< binary tree */
5984  SCIP_BTNODE* node, /**< node to insert */
5985  SCIP_NODEDATA* nodedatas, /**< array of node data */
5986  int* nodedataidx, /**< array of indices for node data */
5987  int* nnodedatas /**< pointer to number of node data */
5988  )
5989 {
5990  /* if the tree is empty the node will be the root node */
5991  if( SCIPbtIsEmpty(tree) )
5992  {
5993  SCIPbtSetRoot(tree, node);
5994  }
5995  else
5996  {
5997  SCIP_NODEDATA* newnodedata;
5998  SCIP_NODEDATA* leafdata;
5999  SCIP_NODEDATA* nodedata;
6000  SCIP_BTNODE* leaf;
6001  SCIP_BTNODE* newnode;
6002  SCIP_BTNODE* parent;
6003 
6004  leaf = SCIPbtGetRoot(tree);
6005  assert(leaf != NULL);
6006 
6007  leafdata = (SCIP_NODEDATA*)SCIPbtnodeGetData(leaf);
6008  assert(leafdata != NULL);
6009 
6010  nodedata = (SCIP_NODEDATA*)SCIPbtnodeGetData(node);
6011  assert(nodedata != NULL);
6012  assert(nodedata->intheta);
6013 
6014  /* find the position to insert the node */
6015  while( !SCIPbtnodeIsLeaf(leaf) )
6016  {
6017  if( nodedata->key < leafdata->key )
6018  leaf = SCIPbtnodeGetLeftchild(leaf);
6019  else
6020  leaf = SCIPbtnodeGetRightchild(leaf);
6021 
6022  leafdata = (SCIP_NODEDATA*)SCIPbtnodeGetData(leaf);
6023  assert(leafdata != NULL);
6024  }
6025 
6026  assert(leaf != NULL);
6027  assert(leaf != node);
6028 
6029  /* store node data to be able to delete them latter */
6030  newnodedata = &nodedatas[*nnodedatas];
6031  nodedataidx[*nnodedatas] = *nnodedatas;
6032  ++(*nnodedatas);
6033 
6034  /* init node data */
6035  newnodedata->var = NULL;
6036  newnodedata->key = SCIP_INVALID;
6037  newnodedata->est = INT_MIN;
6038  newnodedata->lct = INT_MAX;
6039  newnodedata->duration = 0;
6040  newnodedata->demand = 0;
6041  newnodedata->enveloptheta = -1;
6042  newnodedata->energytheta = 0;
6043  newnodedata->enveloplambda = -1;
6044  newnodedata->energylambda = -1;
6045  newnodedata->idx = -1;
6046  newnodedata->intheta = TRUE;
6047 
6048  /* create a new node */
6049  SCIP_CALL( SCIPbtnodeCreate(tree, &newnode, newnodedata) );
6050  assert(newnode != NULL);
6051 
6052  parent = SCIPbtnodeGetParent(leaf);
6053 
6054  if( parent != NULL )
6055  {
6056  SCIPbtnodeSetParent(newnode, parent);
6057 
6058  /* check if the node is the left child */
6059  if( SCIPbtnodeGetLeftchild(parent) == leaf )
6060  {
6061  SCIPbtnodeSetLeftchild(parent, newnode);
6062  }
6063  else
6064  {
6065  SCIPbtnodeSetRightchild(parent, newnode);
6066  }
6067  }
6068  else
6069  SCIPbtSetRoot(tree, newnode);
6070 
6071  if( nodedata->key < leafdata->key )
6072  {
6073  /* node is on the left */
6074  SCIPbtnodeSetLeftchild(newnode, node);
6075  SCIPbtnodeSetRightchild(newnode, leaf);
6076  newnodedata->key = nodedata->key;
6077  }
6078  else
6079  {
6080  /* leaf is on the left */
6081  SCIPbtnodeSetLeftchild(newnode, leaf);
6082  SCIPbtnodeSetRightchild(newnode, node);
6083  newnodedata->key = leafdata->key;
6084  }
6085 
6086  SCIPbtnodeSetParent(leaf, newnode);
6087  SCIPbtnodeSetParent(node, newnode);
6088  }
6089 
6090  /* update envelop */
6091  updateEnvelope(scip, node);
6092 
6093  return SCIP_OKAY;
6094 }
6095 
6096 /** returns the leaf responsible for the lambda energy */
6097 static
6099  SCIP_BTNODE* node /**< node which defines the subtree beases on the lambda energy */
6100  )
6101 {
6102  SCIP_BTNODE* left;
6103  SCIP_BTNODE* right;
6104  SCIP_NODEDATA* nodedata;
6105  SCIP_NODEDATA* leftdata;
6106  SCIP_NODEDATA* rightdata;
6107 
6108  assert(node != NULL);
6109