/** constraint display method of constraint handler */ static SCIP_DECL_CONSPRINT(consPrintConjunction) { /*lint --e{715}*/ SCIP_CONSDATA* consdata; int i; assert( scip != NULL ); assert( conshdlr != NULL ); assert( cons != NULL ); consdata = SCIPconsGetData(cons); assert(consdata != NULL); SCIPinfoMessage(scip, file, "conjunction("); for( i = 0; i < consdata->nconss; ++i ) { if( i > 0 ) SCIPinfoMessage(scip, file, ", "); SCIP_CALL( SCIPprintCons(scip, consdata->conss[i], file) ); } SCIPinfoMessage(scip, file, ")"); return SCIP_OKAY; }
/** problem writing method of reader */ static SCIP_DECL_READERWRITE(readerWriteCip) { /*lint --e{715}*/ int i; SCIPinfoMessage(scip, file, "STATISTICS\n"); SCIPinfoMessage(scip, file, " Problem name : %s\n", name); SCIPinfoMessage(scip, file, " Variables : %d (%d binary, %d integer, %d implicit integer, %d continuous)\n", nvars, nbinvars, nintvars, nimplvars, ncontvars); SCIPinfoMessage(scip, file, " Constraints : %d initial, %d maximal\n", startnconss, maxnconss); SCIPinfoMessage(scip, file, "OBJECTIVE\n"); SCIPinfoMessage(scip, file, " Sense : %s\n", objsense == SCIP_OBJSENSE_MINIMIZE ? "minimize" : "maximize"); if( !SCIPisZero(scip, objoffset) ) SCIPinfoMessage(scip, file, " Offset : %+.15g\n", objoffset); if( !SCIPisEQ(scip, objscale, 1.0) ) SCIPinfoMessage(scip, file, " Scale : %.15g\n", objscale); if( nvars > 0 ) { SCIPinfoMessage(scip, file, "VARIABLES\n"); for( i = 0; i < nvars; ++i ) { SCIP_CALL( SCIPprintVar(scip, vars[i], file) ); } } if( nfixedvars > 0 ) { SCIPinfoMessage(scip, file, "FIXED\n"); for( i = 0; i < nfixedvars; ++i ) { SCIP_CALL( SCIPprintVar(scip, fixedvars[i], file) ); } } if( nconss > 0 ) { SCIPinfoMessage(scip, file, "CONSTRAINTS\n"); for( i = 0; i < nconss; ++i ) { /* in case the transformed is written only constraint are posted which are enabled in the current node */ assert(!transformed || SCIPconsIsEnabled(conss[i])); SCIP_CALL( SCIPprintCons(scip, conss[i], file) ); SCIPinfoMessage(scip, file, ";\n"); } } *result = SCIP_SUCCESS; SCIPinfoMessage(scip, file, "END\n"); return SCIP_OKAY; }
/** checks all constraints in conjunction constraints for feasibility */ static SCIP_RETCODE checkAllConss( SCIP* scip, /**< SCIP data structure */ SCIP_CONS** conss, /**< active conjunction constraints */ int nconss, /**< number of active conjunction constraints */ SCIP_SOL* sol, /**< solution to check */ SCIP_Bool checkintegrality, /**< has integrality to be checked? */ SCIP_Bool checklprows, /**< have current LP rows to be checked? */ SCIP_Bool printreason, /**< should the reason for the violation be printed? */ SCIP_RESULT* result /**< pointer to store the result */ ) { SCIP_CONSDATA* consdata; int c; int i; assert(result != NULL); for( c = 0; c < nconss && *result == SCIP_FEASIBLE; ++c ) { consdata = SCIPconsGetData(conss[c]); assert(consdata != NULL); /* check all constraints */ for( i = 0; i < consdata->nconss && *result == SCIP_FEASIBLE; ++i ) { SCIP_CALL( SCIPcheckCons(scip, consdata->conss[i], sol, checkintegrality, checklprows, printreason, result) ); assert(*result == SCIP_FEASIBLE || *result == SCIP_INFEASIBLE); } if( printreason && *result == SCIP_INFEASIBLE ) { SCIPinfoMessage(scip, NULL, "conjunction constraint %s is violated, at least the sub-constraint %s is violated by this given solution\n", SCIPconsGetName(conss[c]), SCIPconsGetName(consdata->conss[i-1])); SCIPdebug( SCIP_CALL( SCIPprintCons(scip, conss[c], NULL) ) ); } } return SCIP_OKAY; }
/** problem writing method of reader */ static SCIP_DECL_READERWRITE(readerWriteCip) { /*lint --e{715}*/ SCIP_HASHTABLE* varhash = NULL; SCIP_READERDATA* readerdata; int i; assert(reader != NULL); assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0); SCIPinfoMessage(scip, file, "STATISTICS\n"); SCIPinfoMessage(scip, file, " Problem name : %s\n", name); SCIPinfoMessage(scip, file, " Variables : %d (%d binary, %d integer, %d implicit integer, %d continuous)\n", nvars, nbinvars, nintvars, nimplvars, ncontvars); SCIPinfoMessage(scip, file, " Constraints : %d initial, %d maximal\n", startnconss, maxnconss); SCIPinfoMessage(scip, file, "OBJECTIVE\n"); SCIPinfoMessage(scip, file, " Sense : %s\n", objsense == SCIP_OBJSENSE_MINIMIZE ? "minimize" : "maximize"); if( !SCIPisZero(scip, objoffset) ) SCIPinfoMessage(scip, file, " Offset : %+.15g\n", objoffset); if( !SCIPisEQ(scip, objscale, 1.0) ) SCIPinfoMessage(scip, file, " Scale : %.15g\n", objscale); if ( nfixedvars > 0 ) { /* set up hash table for variables that have been written property (used for writing out fixed vars in the right order) */ SCIP_CALL( SCIPhashtableCreate(&varhash, SCIPblkmem(scip), SCIPcalcHashtableSize(10 * (nvars + nfixedvars)), hashGetKeyVar, hashKeyEqVar, hashKeyValVar, NULL) ); } if ( nvars + nfixedvars > 0 ) { SCIPinfoMessage(scip, file, "VARIABLES\n"); } if( nvars > 0 ) { for( i = 0; i < nvars; ++i ) { SCIP_VAR* var; var = vars[i]; assert( var != NULL ); SCIP_CALL( SCIPprintVar(scip, var, file) ); if ( varhash != NULL ) { /* add free variable to hashtable */ if ( ! SCIPhashtableExists(varhash, (void*) var) ) { SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) ); } } } } readerdata = SCIPreaderGetData(reader); assert(readerdata != NULL); if( readerdata->writefixedvars && nfixedvars > 0 ) { int nwritten = 0; SCIPinfoMessage(scip, file, "FIXED\n"); /* loop through variables until each has been written after the variables that it depends on have been written; this * requires several runs over the variables, but the depth (= number of loops) is usually small. */ while ( nwritten < nfixedvars ) { SCIPdebugMessage("written %d of %d fixed variables.\n", nwritten, nfixedvars); for (i = 0; i < nfixedvars; ++i) { SCIP_VAR* var; SCIP_VAR* tmpvar; var = fixedvars[i]; assert( var != NULL ); /* skip variables already written */ if ( SCIPhashtableExists(varhash, (void*) var) ) continue; switch ( SCIPvarGetStatus(var) ) { case SCIP_VARSTATUS_FIXED: /* fixed variables can simply be output and added to the hashtable */ SCIP_CALL( SCIPprintVar(scip, var, file) ); assert( ! SCIPhashtableExists(varhash, (void*) var) ); SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) ); ++nwritten; break; case SCIP_VARSTATUS_NEGATED: tmpvar = SCIPvarGetNegationVar(var); assert( tmpvar != NULL ); assert( var == SCIPvarGetNegatedVar(tmpvar) ); /* if the negated variable has been written, we can write the current variable */ if ( SCIPhashtableExists(varhash, (void*) tmpvar) ) { SCIP_CALL( SCIPprintVar(scip, var, file) ); assert( ! SCIPhashtableExists(varhash, (void*) var) ); SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) ); ++nwritten; } break; case SCIP_VARSTATUS_AGGREGATED: tmpvar = SCIPvarGetAggrVar(var); assert( tmpvar != NULL ); /* if the aggregating variable has been written, we can write the current variable */ if ( SCIPhashtableExists(varhash, (void*) tmpvar) ) { SCIP_CALL( SCIPprintVar(scip, var, file) ); assert( ! SCIPhashtableExists(varhash, (void*) var) ); SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) ); ++nwritten; } break; case SCIP_VARSTATUS_MULTAGGR: { SCIP_VAR** aggrvars; int naggrvars; int j; /* get the active representation */ SCIP_CALL( SCIPflattenVarAggregationGraph(scip, var) ); naggrvars = SCIPvarGetMultaggrNVars(var); aggrvars = SCIPvarGetMultaggrVars(var); assert(aggrvars != NULL || naggrvars == 0); for (j = 0; j < naggrvars; ++j) { if( !SCIPhashtableExists(varhash, (void*) aggrvars[j]) ) /*lint !e613*/ break; } /* if all multi-aggregating variables have been written, we can write the current variable */ if ( j >= naggrvars ) { SCIP_CALL( SCIPprintVar(scip, var, file) ); assert( ! SCIPhashtableExists(varhash, (void*) var) ); SCIP_CALL( SCIPhashtableInsert(varhash, (void*) var) ); ++nwritten; } break; } case SCIP_VARSTATUS_ORIGINAL: case SCIP_VARSTATUS_LOOSE: case SCIP_VARSTATUS_COLUMN: SCIPerrorMessage("Only fixed variables are allowed to be present in fixedvars list.\n"); SCIPABORT(); return SCIP_ERROR; /*lint !e527*/ } } } } if( nconss > 0 ) { SCIPinfoMessage(scip, file, "CONSTRAINTS\n"); for( i = 0; i < nconss; ++i ) { SCIP_CALL( SCIPprintCons(scip, conss[i], file) ); SCIPinfoMessage(scip, file, ";\n"); } } SCIPinfoMessage(scip, file, "END\n"); *result = SCIP_SUCCESS; if( nfixedvars > 0 ) SCIPhashtableFree(&varhash); else assert(varhash == NULL); return SCIP_OKAY; }
/** add branching decisions constraints to the sub SCIP */ static SCIP_RETCODE addBranchingDecisionConss( SCIP* scip, /**< SCIP data structure */ SCIP* subscip, /**< pricing SCIP data structure */ SCIP_VAR** vars, /**< variable array of the subscuip oder variables */ SCIP_CONSHDLR* conshdlr /**< constraint handler for branching data */ ) { SCIP_CONS** conss; SCIP_CONS* cons; int nconss; int id1; int id2; CONSTYPE type; SCIP_Real vbdcoef; SCIP_Real lhs; SCIP_Real rhs; int c; assert( scip != NULL ); assert( subscip != NULL ); assert( conshdlr != NULL ); /* collect all branching decision constraints */ conss = SCIPconshdlrGetConss(conshdlr); nconss = SCIPconshdlrGetNConss(conshdlr); /* loop over all branching decision constraints and apply the branching decision if the corresponding constraint is * active */ for( c = 0; c < nconss; ++c ) { cons = conss[c]; /* ignore constraints which are not active since these are not laying on the current active path of the search * tree */ if( !SCIPconsIsActive(cons) ) continue; /* collect the two item ids and the branching type (SAME or DIFFER) on which the constraint branched */ id1 = SCIPgetItemid1Samediff(scip, cons); id2 = SCIPgetItemid2Samediff(scip, cons); type = SCIPgetTypeSamediff(scip, cons); SCIPdebugMessage("create varbound for %s(%d,%d)\n", type == SAME ? "same" : "diff", SCIPprobdataGetIds(SCIPgetProbData(scip))[id1], SCIPprobdataGetIds(SCIPgetProbData(scip))[id2]); /* depending on the branching type select the correct left and right hand side for the linear constraint which * enforces this branching decision in the pricing problem MIP */ if( type == SAME ) { lhs = 0.0; rhs = 0.0; vbdcoef = -1.0; } else if( type == DIFFER ) { lhs = -SCIPinfinity(scip); rhs = 1.0; vbdcoef = 1.0; } else { SCIPerrorMessage("unknow constraint type <%d>\n, type"); return SCIP_INVALIDDATA; } /* add linear (in that case a variable bound) constraint to pricing MIP depending on the branching type: * * - branching type SAME: x1 = x2 <=> x1 - x2 = 0 <=> 0 <= x1 - x2 <= 0 * * - branching type DIFFER: x1 - x2 <= 1 <=> -inf <= x1 - x2 <= 1 * */ SCIP_CALL( SCIPcreateConsBasicVarbound(subscip, &cons, SCIPconsGetName(conss[c]), vars[id1], vars[id2], vbdcoef, lhs, rhs) ); SCIPdebug( SCIPprintCons(subscip, cons, NULL) ); SCIP_CALL( SCIPaddCons(subscip, cons) ); SCIP_CALL( SCIPreleaseCons(subscip, &cons) ); } return SCIP_OKAY; }
/** writes problem to file */ SCIP_RETCODE SCIPwriteCcg( SCIP* scip, /**< SCIP data structure */ FILE* file, /**< output file, or NULL if standard output should be used */ const char* name, /**< problem name */ SCIP_Bool transformed, /**< TRUE iff problem is the transformed problem */ SCIP_VAR** vars, /**< array with active variables ordered binary, integer, implicit, continuous */ int nvars, /**< number of active variables in the problem */ SCIP_CONS** conss, /**< array with constraints of the problem */ int nconss, /**< number of constraints in the problem */ SCIP_RESULT* result /**< pointer to store the result of the file writing call */ ) { /*lint --e{715}*/ int c; int v; int i; SCIP_CONSHDLR* conshdlr; const char* conshdlrname; SCIP_CONS* cons; SCIP_VAR** consvars; SCIP_Real* consvals; int nconsvars; SparseGraph G; assert( scip != NULL ); assert( nvars >= 0 ); /* initialize graph */ SCIP_CALL( initGraph(scip, &G, (unsigned int) nvars, 10) ); /* check all constraints */ for( c = 0; c < nconss; ++c) { cons = conss[c]; assert( cons != NULL); /* in case the transformed is written only constraint are posted which are enabled in the current node */ assert(!transformed || SCIPconsIsEnabled(cons)); conshdlr = SCIPconsGetHdlr(cons); assert( conshdlr != NULL ); conshdlrname = SCIPconshdlrGetName(conshdlr); assert( transformed == SCIPconsIsTransformed(cons) ); if( strcmp(conshdlrname, "linear") == 0 ) { consvars = SCIPgetVarsLinear(scip, cons); nconsvars = SCIPgetNVarsLinear(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, SCIPgetVarsLinear(scip, cons), SCIPgetValsLinear(scip, cons), SCIPgetNVarsLinear(scip, cons), transformed, &G) ); } } else if( strcmp(conshdlrname, "setppc") == 0 ) { consvars = SCIPgetVarsSetppc(scip, cons); nconsvars = SCIPgetNVarsSetppc(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, consvars, NULL, nconsvars, transformed, &G) ); } } else if( strcmp(conshdlrname, "logicor") == 0 ) { consvars = SCIPgetVarsLogicor(scip, cons); nconsvars = SCIPgetNVarsLogicor(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, SCIPgetVarsLogicor(scip, cons), NULL, SCIPgetNVarsLogicor(scip, cons), transformed, &G) ); } } else if( strcmp(conshdlrname, "knapsack") == 0 ) { SCIP_Longint* w; consvars = SCIPgetVarsKnapsack(scip, cons); nconsvars = SCIPgetNVarsKnapsack(scip, cons); assert( consvars != NULL || nconsvars == 0 ); /* copy Longint array to SCIP_Real array */ w = SCIPgetWeightsKnapsack(scip, cons); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nconsvars) ); for( v = 0; v < nconsvars; ++v ) consvals[v] = (SCIP_Real)w[v]; if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, consvars, consvals, nconsvars, transformed, &G) ); } SCIPfreeBufferArray(scip, &consvals); } else if( strcmp(conshdlrname, "varbound") == 0 ) { SCIP_CALL( SCIPallocBufferArray(scip, &consvars, 2) ); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, 2) ); consvars[0] = SCIPgetVarVarbound(scip, cons); consvars[1] = SCIPgetVbdvarVarbound(scip, cons); consvals[0] = 1.0; consvals[1] = SCIPgetVbdcoefVarbound(scip, cons); SCIP_CALL( handleLinearCons(scip, consvars, consvals, 2, transformed, &G) ); SCIPfreeBufferArray(scip, &consvars); SCIPfreeBufferArray(scip, &consvals); } else { SCIPwarningMessage(scip, "constraint handler <%s> cannot print requested format\n", conshdlrname ); SCIPinfoMessage(scip, file, "\\ "); SCIP_CALL( SCIPprintCons(scip, cons, file) ); SCIPinfoMessage(scip, file, ";\n"); } } /* output graph */ SCIPinfoMessage(scip, file, "c graph generated from %s\n", name); SCIPinfoMessage(scip, file, "p edge %d %d\n", nvars, G.m); for( i = 0; i < nvars; ++i ) { unsigned int k; int a; k = 0; a = G.A[i][k]; while( a >= 0 ) { /* only output edges from lower to higher number */ if( i < a ) { /* note: node numbers start with 1 in the DIMACS format */ SCIPinfoMessage(scip, file, "e %d %d %f\n", i+1, a+1, G.W[i][k]); } a = G.A[i][++k]; assert( k <= G.size[i] ); } assert( k == G.deg[i] ); } freeGraph(scip, &G); *result = SCIP_SUCCESS; return SCIP_OKAY; }
/** writes problem to file */ SCIP_RETCODE SCIPwritePpm( SCIP* scip, /**< SCIP data structure */ FILE* file, /**< output file, or NULL if standard output should be used */ const char* name, /**< problem name */ SCIP_READERDATA* readerdata, /**< information for reader */ SCIP_Bool transformed, /**< TRUE iff problem is the transformed problem */ SCIP_VAR** vars, /**< array with active variables ordered binary, integer, implicit, continuous */ int nvars, /**< number of active variables in the problem */ SCIP_CONS** conss, /**< array with constraints of the problem */ int nconss, /**< number of constraints in the problem */ SCIP_RESULT* result /**< pointer to store the result of the file writing call */ ) { /*lint --e{715}*/ int c; int v; int i; int linecnt; char linebuffer[PPM_MAX_LINELEN]; SCIP_CONSHDLR* conshdlr; const char* conshdlrname; SCIP_CONS* cons; SCIP_VAR** consvars; SCIP_Real* consvals; int nconsvars; int i_max = 1; SCIP_Real maxcoef = 0; SCIP_Bool printbool = FALSE; assert( scip != NULL ); assert(readerdata != NULL); /* print statistics as comment to file */ if(readerdata->rgb_ascii) SCIPinfoMessage(scip, file, "P6\n"); else SCIPinfoMessage(scip, file, "P3\n"); SCIPinfoMessage(scip, file, "# %s\n", name); SCIPinfoMessage(scip, file, "%d %d\n", nvars, nconss); SCIPinfoMessage(scip, file, "255\n"); clearLine(linebuffer, &linecnt); if(!(readerdata->rgb_relativ)) { i_max = 2; } for(i = 0; i < i_max; ++i) { if(i) { printbool = TRUE; SCIPdebugPrintf("Maximal coefficient = %g\n", maxcoef); } for(c = 0; c < nconss; ++c) { cons = conss[c]; assert( cons != NULL); /* in case the transformed is written only constraint are posted which are enabled in the current node */ assert(!transformed || SCIPconsIsEnabled(cons)); conshdlr = SCIPconsGetHdlr(cons); assert( conshdlr != NULL ); conshdlrname = SCIPconshdlrGetName(conshdlr); assert( transformed == SCIPconsIsTransformed(cons) ); if( strcmp(conshdlrname, "linear") == 0 ) { consvars = SCIPgetVarsLinear(scip, cons); nconsvars = SCIPgetNVarsLinear(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( printLinearCons(scip, file, readerdata, consvars, SCIPgetValsLinear(scip, cons), nconsvars, nvars, transformed, &maxcoef, printbool) ); } } else if( strcmp(conshdlrname, "setppc") == 0 ) { consvars = SCIPgetVarsSetppc(scip, cons); nconsvars = SCIPgetNVarsSetppc(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( printLinearCons(scip, file, readerdata, consvars, NULL, nconsvars, nvars, transformed, &maxcoef, printbool) ); } } else if( strcmp(conshdlrname, "logicor") == 0 ) { consvars = SCIPgetVarsLogicor(scip, cons); nconsvars = SCIPgetNVarsLogicor(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( printLinearCons(scip, file, readerdata, consvars, NULL, nconsvars, nvars, transformed, &maxcoef, printbool) ); } } else if( strcmp(conshdlrname, "knapsack") == 0 ) { SCIP_Longint* weights; consvars = SCIPgetVarsKnapsack(scip, cons); nconsvars = SCIPgetNVarsKnapsack(scip, cons); assert( consvars != NULL || nconsvars == 0 ); /* copy Longint array to SCIP_Real array */ weights = SCIPgetWeightsKnapsack(scip, cons); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nconsvars) ); for( v = 0; v < nconsvars; ++v ) consvals[v] = (SCIP_Real)weights[v]; if( nconsvars > 0 ) { SCIP_CALL( printLinearCons(scip, file, readerdata, consvars, consvals, nconsvars, nvars, transformed, &maxcoef, printbool) ); } SCIPfreeBufferArray(scip, &consvals); } else if( strcmp(conshdlrname, "varbound") == 0 ) { SCIP_CALL( SCIPallocBufferArray(scip, &consvars, 2) ); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, 2) ); consvars[0] = SCIPgetVarVarbound(scip, cons); consvars[1] = SCIPgetVbdvarVarbound(scip, cons); consvals[0] = 1.0; consvals[1] = SCIPgetVbdcoefVarbound(scip, cons); SCIP_CALL( printLinearCons(scip, file, readerdata, consvars, consvals, 2, nvars, transformed, &maxcoef, printbool) ); SCIPfreeBufferArray(scip, &consvars); SCIPfreeBufferArray(scip, &consvals); } else { SCIPwarningMessage(scip, "constraint handler <%s> cannot print requested format\n", conshdlrname ); SCIPinfoMessage(scip, file, "\\ "); SCIP_CALL( SCIPprintCons(scip, cons, file) ); SCIPinfoMessage(scip, file, ";\n"); } } } *result = SCIP_SUCCESS; return SCIP_OKAY; }