Skip to content

Commit

Permalink
Merge branch 'reimplement_SCIPvarGetActiveRepresentatives' into 'master'
Browse files Browse the repository at this point in the history
reimplement SCIPvarGetActiveRepresentatives

See merge request integer/scip!3409
  • Loading branch information
pfetsch committed Nov 23, 2024
2 parents d6b88b8 + 0178843 commit 0edfe44
Show file tree
Hide file tree
Showing 25 changed files with 260 additions and 420 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Features
Performance improvements
------------------------

- Reimplemented SCIPvarGetActiveRepresentatives() by using dense arrays to avoid repeated resorting.

Examples and applications
-------------------------

Expand All @@ -30,6 +32,7 @@ Interface changes

### Deleted and changed API methods

- SCIPgetProbvarLinearSum: remove parameter mergemultiples, which is now automatically true with the new implementation of SCIPvarGetActiveRepresentatives.
- SCIPcreateRow*(), SCIPaddVarToRow(), SCIPaddVarsToRow(), SCIPaddVarsToRowSameCoef() can now only be called in the solving stage,
because otherwise the LP is not yet available and the row data is invalid.
- intvar removed from arguments for SCIPcreateConsPseudobooleanWithConss(), SCIPcreateConsPseudoboolean(), and SCIPcreateConsBasicPseudoboolean() due to dysfunctionality of non-linear objective reformulation with pseudoboolean constraint
Expand Down
2 changes: 1 addition & 1 deletion src/scip/cons_countsols.c
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ SCIP_RETCODE writeExpandedSolutions(
nvars = 1;
constant = 0.0;

SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, &nvars, nallvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, &nvars, nallvars, &constant, &requiredsize) );
assert(requiredsize <= nallvars);
assert(nvars <= nactivevars);

Expand Down
12 changes: 6 additions & 6 deletions src/scip/cons_linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -17938,15 +17938,15 @@ SCIP_RETCODE SCIPcreateConsLinear(
SCIP_CALL( SCIPduplicateBufferArray(scip, &consvals, vals, nconsvars) );

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize) );

/* if space was not enough we need to resize the buffers */
if( requiredsize > nconsvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize) );
assert(requiredsize <= nconsvars);
}

Expand Down Expand Up @@ -18157,14 +18157,14 @@ SCIP_RETCODE SCIPcopyConsLinear(
*/
if( !SCIPvarIsOriginal(vars[0]) )
{
SCIP_CALL( SCIPgetProbvarLinearSum(sourcescip, vars, coefs, &nvars, nvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(sourcescip, vars, coefs, &nvars, nvars, &constant, &requiredsize) );

if( requiredsize > nvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(sourcescip, vars, coefs, &nvars, requiredsize, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(sourcescip, vars, coefs, &nvars, requiredsize, &constant, &requiredsize) );
assert(requiredsize <= nvars);
}
}
Expand Down Expand Up @@ -18254,15 +18254,15 @@ SCIP_RETCODE SCIPaddCoefLinear(
consvals[0] = val;

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize) );

/* if space was not enough we need to resize the buffers */
if( requiredsize > nconsvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize) );
assert(requiredsize <= nconsvars);
}

Expand Down
22 changes: 13 additions & 9 deletions src/scip/cons_logicor.c
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ SCIP_RETCODE applyFixings(
SCIP_Bool easycase;
int nconsvars;
int requiredsize;
int size = 1;
int v2;

nconsvars = 1;
Expand All @@ -1004,15 +1005,17 @@ SCIP_RETCODE applyFixings(
consvals[0] = 1.0;

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );
/* if space was not enough we need to resize the buffers */
if( requiredsize > nconsvars )
if( requiredsize > size )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );
size = requiredsize;

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
assert(requiredsize <= nconsvars);
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );
assert(nconsvars <= size);
assert(requiredsize <= size);
}

easycase = FALSE;
Expand Down Expand Up @@ -1057,7 +1060,6 @@ SCIP_RETCODE applyFixings(
SCIP_CONS* newcons;
SCIP_Real lhs;
SCIP_Real rhs;
int size;
int k;

/* it might happen that there are more than one multi-aggregated variable, so we need to get the whole probvar sum over all variables */
Expand All @@ -1080,16 +1082,18 @@ SCIP_RETCODE applyFixings(
constant = 0.0;

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );

/* if space was not enough(we found another multi-aggregation), we need to resize the buffers */
if( requiredsize > nconsvars )
if( requiredsize > size )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );
size = requiredsize;

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
assert(requiredsize <= nconsvars);
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );
assert(nconsvars <= size);
assert(requiredsize <= size);
}

lhs = 1.0 - constant;
Expand Down
4 changes: 2 additions & 2 deletions src/scip/cons_pseudoboolean.c
Original file line number Diff line number Diff line change
Expand Up @@ -5103,7 +5103,7 @@ SCIP_RETCODE correctConshdlrdata(
activeconstant = 0.0;
nactivevars = 1;
SCIP_CALL( SCIPgetProbvarLinearSum(scip, activevars, activescalars, &nactivevars, nvars,
&activeconstant, &requiredsize, TRUE) );
&activeconstant, &requiredsize) );
assert(requiredsize <= nvars);

for( i = 0; i < nactivevars && del; ++i )
Expand Down Expand Up @@ -5618,7 +5618,7 @@ SCIP_RETCODE tryUpgradingXor(
constant = 0;

/* get linear active representation */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, linvars, lincoefs, &nlinvars, MAXNVARS, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, linvars, lincoefs, &nlinvars, MAXNVARS, &constant, &requiredsize) );
SCIP_CALL( SCIPduplicateBufferArray(scip, &activelinvars, linvars, nlinvars) );

if( requiredsize > MAXNVARS )
Expand Down
33 changes: 19 additions & 14 deletions src/scip/cons_setppc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1831,25 +1831,28 @@ SCIP_RETCODE applyFixings(
SCIP_Bool easycase;
int nconsvars;
int requiredsize;
int consvarssize = 10;
int v2;

nconsvars = 1;
SCIP_CALL( SCIPallocBufferArray(scip, &consvars, 1) );
SCIP_CALL( SCIPallocBufferArray(scip, &consvals, 1) );
SCIP_CALL( SCIPallocBufferArray(scip, &consvars, consvarssize) );
SCIP_CALL( SCIPallocBufferArray(scip, &consvals, consvarssize) );
consvars[0] = repvar;
consvals[0] = 1.0;

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, nconsvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, consvarssize, &constant, &requiredsize) );
/* if space was not enough we need to resize the buffers */
if( requiredsize > nconsvars )
if( requiredsize > consvarssize )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );
consvarssize = requiredsize;
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, consvarssize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, consvarssize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
assert(requiredsize <= nconsvars);
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, consvarssize, &constant, &requiredsize) );
assert(requiredsize <= consvarssize);
}
assert(nconsvars <= consvarssize);

easycase = FALSE;

Expand Down Expand Up @@ -1975,17 +1978,19 @@ SCIP_RETCODE applyFixings(
constant = 0.0;

/* get active variables for new constraint */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );

/* if space was not enough (we found another multi-aggregation), we need to resize the buffers */
if( requiredsize > nconsvars )
if( requiredsize > size )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, requiredsize) );
size = requiredsize;
SCIP_CALL( SCIPreallocBufferArray(scip, &consvars, size) );
SCIP_CALL( SCIPreallocBufferArray(scip, &consvals, size) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, requiredsize, &constant, &requiredsize, TRUE) );
assert(requiredsize <= nconsvars);
SCIP_CALL( SCIPgetProbvarLinearSum(scip, consvars, consvals, &nconsvars, size, &constant, &requiredsize) );
assert(requiredsize == size);
}
assert(nconsvars <= size);

/* compute sides */
if( (SCIP_SETPPCTYPE)consdata->setppctype == SCIP_SETPPCTYPE_PACKING )
Expand Down
4 changes: 2 additions & 2 deletions src/scip/cons_sos1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2805,13 +2805,13 @@ SCIP_RETCODE tightenVarsBoundsSOS1(

/* transform linear constraint */
constant = 0.0;
SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, ntrafolinvars, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, ntrafolinvars, &constant, &requiredsize) );
if( requiredsize > ntrafolinvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &trafolinvars, requiredsize + 1) );
SCIP_CALL( SCIPreallocBufferArray(scip, &trafolinvals, requiredsize + 1) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, requiredsize, &constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, requiredsize, &constant, &requiredsize) );
assert( requiredsize <= ntrafolinvars );
}
if( !SCIPisInfinity(scip, -trafolhs) )
Expand Down
4 changes: 2 additions & 2 deletions src/scip/expr_var.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ SCIP_DECL_EXPRSIMPLIFY(simplifyVar)
{
int requsize;

SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize) );

if( requsize > varssize )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &vars, requsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, requsize) );
varssize = requsize;
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize) );
assert(requsize <= nvars);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scip/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ SCIP_RETCODE getActiveVariables(
assert(nvars != NULL);
assert(constant != NULL);

SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize) );

if( requiredsize > *nvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, scalars, requiredsize) );

/* call function a second time with enough memory */
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize) );
assert(requiredsize <= *nvars);
}

Expand Down
4 changes: 2 additions & 2 deletions src/scip/prop_genvbounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -2480,7 +2480,7 @@ SCIP_DECL_PROPEXITPRE(propExitpreGenvbounds)
/* replace non-active by active variables and update constant; note that this may result in coefficients where
* SCIPisZero() is true; this should not create any problems
*/
SCIP_CALL( SCIPgetProbvarLinearSum(scip, genvbound->vars, genvbound->coefs, &genvbound->ncoefs, genvbound->ncoefs, &genvbound->constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, genvbound->vars, genvbound->coefs, &genvbound->ncoefs, genvbound->ncoefs, &genvbound->constant, &requiredsize) );

/* if space was not enough we need to resize the buffers */
if( requiredsize > genvbound->ncoefs )
Expand All @@ -2493,7 +2493,7 @@ SCIP_DECL_PROPEXITPRE(propExitpreGenvbounds)
genvbound->coefssize = requiredsize;
}

SCIP_CALL( SCIPgetProbvarLinearSum(scip, genvbound->vars, genvbound->coefs, &genvbound->ncoefs, requiredsize, &genvbound->constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, genvbound->vars, genvbound->coefs, &genvbound->ncoefs, requiredsize, &genvbound->constant, &requiredsize) );
assert(requiredsize <= genvbound->ncoefs);
}

Expand Down
4 changes: 2 additions & 2 deletions src/scip/reader_ccg.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ SCIP_RETCODE getActiveVariables(

if( transformed )
{
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, *nvars, constant, &requiredsize) );

if( requiredsize > *nvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &scalars, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, requiredsize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, requiredsize, constant, &requiredsize) );
assert( requiredsize <= *nvars );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scip/reader_fzn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3883,14 +3883,14 @@ SCIP_RETCODE getActiveVariables(

if( transformed )
{
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize) );

if( requiredsize > *nvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, scalars, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize) );
assert( requiredsize <= *nvars );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scip/reader_gms.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ SCIP_RETCODE getActiveVariables(

if( transformed )
{
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *varssize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *varssize, constant, &requiredsize) );

if( requiredsize > *varssize )
{
*varssize = SCIPcalcMemGrowSize(scip, requiredsize);
SCIP_CALL( SCIPreallocBufferArray(scip, vars, *varssize) );
SCIP_CALL( SCIPreallocBufferArray(scip, scalars, *varssize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *varssize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *varssize, constant, &requiredsize) );
assert(requiredsize <= *varssize);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scip/reader_lp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2537,14 +2537,14 @@ SCIP_RETCODE getActiveVariables(

if( transformed )
{
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, *nvars, constant, &requiredsize) );

if( requiredsize > *nvars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, scalars, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, *vars, *scalars, nvars, requiredsize, constant, &requiredsize) );
assert( requiredsize <= *nvars );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scip/reader_mps.c
Original file line number Diff line number Diff line change
Expand Up @@ -2961,14 +2961,14 @@ SCIP_RETCODE getLinearCoeffs(
/* retransform given variables to active variables */
if( transformed )
{
SCIP_CALL( SCIPgetProbvarLinearSum(scip, activevars, activevals, &nactivevars, nactivevars, &activeconstant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, activevars, activevals, &nactivevars, nactivevars, &activeconstant, &requiredsize) );

if( requiredsize > nactivevars )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &activevars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &activevals, requiredsize) );

SCIP_CALL( SCIPgetProbvarLinearSum(scip, activevars, activevals, &nactivevars, requiredsize, &activeconstant, &requiredsize, TRUE) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, activevars, activevals, &nactivevars, requiredsize, &activeconstant, &requiredsize) );
assert( requiredsize <= nactivevars );
}
}
Expand Down
Loading

0 comments on commit 0edfe44

Please sign in to comment.