/** creates a relaxation handler */ SCIP_RETCODE SCIPrelaxCreate( SCIP_RELAX** relax, /**< pointer to relaxation handler data structure */ SCIP_SET* set, /**< global SCIP settings */ SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of relaxation handler */ const char* desc, /**< description of relaxation handler */ int priority, /**< priority of the relaxation handler (negative: after LP, non-negative: before LP) */ int freq, /**< frequency for calling relaxation handler */ SCIP_DECL_RELAXCOPY ((*relaxcopy)), /**< copy method of relaxation handler or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_RELAXFREE ((*relaxfree)), /**< destructor of relaxation handler */ SCIP_DECL_RELAXINIT ((*relaxinit)), /**< initialize relaxation handler */ SCIP_DECL_RELAXEXIT ((*relaxexit)), /**< deinitialize relaxation handler */ SCIP_DECL_RELAXINITSOL((*relaxinitsol)), /**< solving process initialization method of relaxation handler */ SCIP_DECL_RELAXEXITSOL((*relaxexitsol)), /**< solving process deinitialization method of relaxation handler */ SCIP_DECL_RELAXEXEC ((*relaxexec)), /**< execution method of relaxation handler */ SCIP_RELAXDATA* relaxdata /**< relaxation handler data */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(relax != NULL); assert(name != NULL); assert(desc != NULL); assert(freq >= -1); assert(relaxexec != NULL); SCIP_ALLOC( BMSallocMemory(relax) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*relax)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*relax)->desc, desc, strlen(desc)+1) ); (*relax)->priority = priority; (*relax)->freq = freq; (*relax)->relaxcopy = relaxcopy; (*relax)->relaxfree = relaxfree; (*relax)->relaxinit = relaxinit; (*relax)->relaxexit = relaxexit; (*relax)->relaxinitsol = relaxinitsol; (*relax)->relaxexitsol = relaxexitsol; (*relax)->relaxexec = relaxexec; (*relax)->relaxdata = relaxdata; SCIP_CALL( SCIPclockCreate(&(*relax)->setuptime, SCIP_CLOCKTYPE_DEFAULT) ); SCIP_CALL( SCIPclockCreate(&(*relax)->relaxclock, SCIP_CLOCKTYPE_DEFAULT) ); (*relax)->ncalls = 0; (*relax)->lastsolvednode = -1; (*relax)->initialized = FALSE; /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "relaxing/%s/priority", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of relaxation handler <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*relax)->priority, FALSE, priority, INT_MIN/4, INT_MAX/4, paramChgdRelaxPriority, (SCIP_PARAMDATA*)(*relax)) ); /*lint !e740*/ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "relaxing/%s/freq", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "frequency for calling relaxation handler <%s> (-1: never, 0: only in root node)", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*relax)->freq, FALSE, freq, -1, INT_MAX, NULL, NULL) ); return SCIP_OKAY; }
/** creates a variable pricer * To use the variable pricer for solving a problem, it first has to be activated with a call to SCIPactivatePricer(). */ SCIP_RETCODE SCIPpricerCreate( SCIP_PRICER** pricer, /**< pointer to variable pricer data structure */ SCIP_SET* set, /**< global SCIP settings */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of variable pricer */ const char* desc, /**< description of variable pricer */ int priority, /**< priority of the variable pricer */ SCIP_Bool delay, /**< should the pricer be delayed until no other pricers or already existing * problem variables with negative reduced costs are found */ SCIP_DECL_PRICERCOPY ((*pricercopy)), /**< copy method of pricer or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_PRICERFREE ((*pricerfree)), /**< destructor of variable pricer */ SCIP_DECL_PRICERINIT ((*pricerinit)), /**< initialize variable pricer */ SCIP_DECL_PRICEREXIT ((*pricerexit)), /**< deinitialize variable pricer */ SCIP_DECL_PRICERINITSOL((*pricerinitsol)),/**< solving process initialization method of variable pricer */ SCIP_DECL_PRICEREXITSOL((*pricerexitsol)),/**< solving process deinitialization method of variable pricer */ SCIP_DECL_PRICERREDCOST((*pricerredcost)),/**< reduced cost pricing method of variable pricer for feasible LPs */ SCIP_DECL_PRICERFARKAS((*pricerfarkas)), /**< Farkas pricing method of variable pricer for infeasible LPs */ SCIP_PRICERDATA* pricerdata /**< variable pricer data */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(pricer != NULL); assert(name != NULL); assert(desc != NULL); assert(pricerredcost != NULL); SCIP_ALLOC( BMSallocMemory(pricer) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*pricer)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*pricer)->desc, desc, strlen(desc)+1) ); (*pricer)->priority = priority; (*pricer)->pricercopy = pricercopy; (*pricer)->pricerfree = pricerfree; (*pricer)->pricerinit = pricerinit; (*pricer)->pricerexit = pricerexit; (*pricer)->pricerinitsol = pricerinitsol; (*pricer)->pricerexitsol = pricerexitsol; (*pricer)->pricerredcost = pricerredcost; (*pricer)->pricerfarkas = pricerfarkas; (*pricer)->pricerdata = pricerdata; SCIP_CALL( SCIPclockCreate(&(*pricer)->pricerclock, SCIP_CLOCKTYPE_DEFAULT) ); (*pricer)->ncalls = 0; (*pricer)->nvarsfound = 0; (*pricer)->delay = delay; (*pricer)->active = FALSE; (*pricer)->initialized = FALSE; /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "pricers/%s/priority", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of pricer <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, blkmem, paramname, paramdesc, &(*pricer)->priority, FALSE, priority, INT_MIN/4, INT_MAX/4, paramChgdPricerPriority, (SCIP_PARAMDATA*)(*pricer)) ); /*lint !e740*/ return SCIP_OKAY; }
/** issues an error message and marks the LP data to have errors */ static void syntaxError( SCIP* scip, /**< SCIP data structure */ LPINPUT* lpinput, /**< LP reading data */ const char* msg /**< error message */ ) { char formatstr[256]; assert(lpinput != NULL); SCIPerrorMessage("Syntax error in line %d ('%s'): %s \n", lpinput->linenumber, lpinput->token, msg); if( lpinput->linebuf[strlen(lpinput->linebuf)-1] == '\n' ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, " input: %s", lpinput->linebuf); } else { SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, " input: %s\n", lpinput->linebuf); } (void) SCIPsnprintf(formatstr, 256, " %%%ds\n", lpinput->linepos); SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, (const char*)formatstr, "^"); lpinput->section = LP_END; lpinput->haserror = TRUE; }
/** issues an error message and marks the BLK data to have errors */ static void syntaxError( SCIP* scip, /**< SCIP data structure */ BLKINPUT* blkinput, /**< BLK reading data */ const char* msg /**< error message */ ) { char formatstr[256]; assert(blkinput != NULL); SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "Syntax error in line %d: %s ('%s')\n", blkinput->linenumber, msg, blkinput->token); if( blkinput->linebuf[strlen(blkinput->linebuf)-1] == '\n' ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, " input: %s", blkinput->linebuf); } else { SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, " input: %s\n", blkinput->linebuf); } (void) SCIPsnprintf(formatstr, 256, " %%%ds\n", blkinput->linepos); SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, formatstr, "^"); blkinput->section = BLK_END; blkinput->haserror = TRUE; }
/** displays a time value fitting in a given width */ void SCIPdispTime( SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ FILE* file, /**< output stream */ SCIP_Real val, /**< value in seconds to display */ int width /**< width to fit into */ ) { assert(width >= 1); if( width == 1 ) { if( val < 0.0 ) SCIPmessageFPrintInfo(messagehdlr, file, "-"); else if( val < 10.0 ) SCIPmessageFPrintInfo(messagehdlr, file, "%.0f", val); else SCIPmessageFPrintInfo(messagehdlr, file, "+"); } else { char format[SCIP_MAXSTRLEN]; SCIP_Longint maxval; int timepower; int i; maxval = 1; for( i = 0; i < width-1; ++i ) maxval *= 10; if( val < 0.0 ) maxval /= 10; timepower = 0; while( REALABS(val) + 0.5 >= maxval && timepower < MAXTIMEPOWER ) { timepower++; val /= timepowerval[timepower]; } if( REALABS(val) + 0.05 < maxval/100 ) /*lint !e653*/ (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%d.1f%c", width-1, timepowerchar[timepower]); else (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%d.0f%c", width-1, timepowerchar[timepower]); if( width == 2 && val < 0.0 ) SCIPmessageFPrintInfo(messagehdlr, file, "-%c", timepowerchar[timepower]); else SCIPmessageFPrintInfo(messagehdlr, file, (const char*)format, val); } }
/** parese job informations */ static SCIP_RETCODE getJobs( SCIP* scip, /**< SCIP data structure */ int lineno, /**< current line number of input file */ char* linestr, /**< current line */ STATE* state, /**< pointer to current reading state */ SCIP_RCPSPDATA* rcpspdata /**< pointer to resources constrained project scheduling data */ ) { char jobname[SCIP_MAXSTRLEN]; int value; int jobid; int r; assert(linestr != NULL); assert(state != NULL); /* skip lines which are not of interest */ if ( (!strncmp(linestr, "REQUESTS", 4) ) || ( !strncmp(linestr, "jobnr", 3) ) || ( !strncmp(linestr, "-", 1) ) ) { *state = JOBS; return SCIP_OKAY; } /* parse job id */ SCIPstrToIntValue(linestr, &value, &linestr); jobid = value - 1; /* construct job name */ (void)SCIPsnprintf(jobname, SCIP_MAXSTRLEN, "%d" , jobid) ; /* copy job name */ SCIP_CALL( SCIPduplicateBufferArray(scip, &rcpspdata->jobnames[jobid], jobname, strlen(jobname) + 1) ); /* skip next value */ SCIPstrToIntValue(linestr, &value, &linestr); /* parse duration */ SCIPstrToIntValue(linestr, &value, &linestr); rcpspdata->durations[jobid] = value; SCIP_CALL( SCIPallocBufferArray(scip, &rcpspdata->demands[jobid], rcpspdata->nresources) ); /* parse demands */ for( r = 0; r < rcpspdata->nresources; ++r ) { SCIPstrToIntValue(linestr, &value, &linestr); rcpspdata->demands[jobid][r] = value; } /* check if we paresed the last job */ if(jobid == rcpspdata->njobs - 1) *state = NEXT; return SCIP_OKAY; }
/** write a DEC file for a given decomposition */ SCIP_RETCODE GCGwriteDecomp( SCIP* scip, /**< SCIP data structure */ FILE* file, /**< File pointer to write to */ DEC_DECOMP* decdecomp /**< Decomposition pointer */ ) { char outname[SCIP_MAXSTRLEN]; assert(scip != NULL); if( decdecomp == NULL ) { SCIPwarningMessage(scip, "Cannot write decomposed problem if decomposition structure is empty!\n"); (void) SCIPsnprintf(outname, SCIP_MAXSTRLEN, "%s", SCIPgetProbName(scip)); } else { (void) SCIPsnprintf(outname, SCIP_MAXSTRLEN, "%s_%d", SCIPgetProbName(scip), DECdecompGetNBlocks(decdecomp)); SCIP_CALL( writeData(scip, file, decdecomp) ); } return SCIP_OKAY; }
/** reads the next non-empty non-comment line of a cnf file */ static SCIP_RETCODE readCnfLine( SCIP* scip, /**< SCIP data structure */ SCIP_FILE* file, /**< input file */ char* buffer, /**< buffer for storing the input line */ int size, /**< size of the buffer */ int* linecount /**< pointer to the line number counter */ ) { char* line; int linelen; assert(file != NULL); assert(buffer != NULL); assert(size >= 2); assert(linecount != NULL); do { (*linecount)++; line = SCIPfgets(buffer, size, file); if( line != NULL ) { linelen = (int)strlen(line); if( linelen == size-1 ) { char s[SCIP_MAXSTRLEN]; (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "line too long (exceeds %d characters)", size-2); readError(scip, *linecount, s); return SCIP_READERROR; } } else linelen = 0; } while( line != NULL && (*line == 'c' || *line == '\n') ); if( line != NULL && linelen >= 2 && line[linelen-2] == '\n' ) line[linelen-2] = '\0'; else if( linelen == 0 ) *buffer = '\0'; assert((line == NULL) == (*buffer == '\0')); return SCIP_OKAY; }
/** displays a long integer in decimal form fitting in a given width */ void SCIPdispLongint( SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ FILE* file, /**< output stream */ SCIP_Longint val, /**< value to display */ int width /**< width to fit into */ ) { assert(width >= 1); if( width == 1 ) { if( val < 0 ) SCIPmessageFPrintInfo(messagehdlr, file, "-"); else if( val < 10 ) SCIPmessageFPrintInfo(messagehdlr, file, "%"SCIP_LONGINT_FORMAT, val); else SCIPmessageFPrintInfo(messagehdlr, file, "+"); } else { char format[SCIP_MAXSTRLEN]; SCIP_Longint maxval; int decpower; int i; maxval = 1; for( i = 0; i < width-1; ++i ) maxval *= 10; if( val < 0 ) maxval /= 10; decpower = 0; while( ABS(val) >= maxval && decpower < MAXDECPOWER ) { decpower++; val /= 1000; } (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%d"SCIP_LONGINT_FORMAT"%c", width-1, decpowerchar[decpower]); if( width == 2 && val < 0 ) SCIPmessageFPrintInfo(messagehdlr, file, "-%c", decpowerchar[decpower]); else SCIPmessageFPrintInfo(messagehdlr, file, (const char*)format, val); } }
/** create linear ordering problem model */ SCIP_RETCODE LOPgenerateModel( SCIP* scip /**< SCIP data structure */ ) { SCIP_PROBDATA* probdata; SCIP_CONS* cons; int i, j; /* get problem data */ probdata = SCIPgetProbData(scip); assert( probdata != NULL ); /* generate variables */ SCIP_CALL( SCIPallocMemoryArray(scip, &probdata->vars, probdata->n) ); for (i = 0; i < probdata->n; ++i) { SCIP_CALL( SCIPallocMemoryArray(scip, &(probdata->vars[i]), probdata->n) ); /*lint !e866*/ for (j = 0; j < probdata->n; ++j) { if (j != i) { char s[SCIP_MAXSTRLEN]; (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "x#%d#%d", i, j); SCIP_CALL( SCIPcreateVar(scip, &(probdata->vars[i][j]), s, 0.0, 1.0, probdata->W[i][j], SCIP_VARTYPE_BINARY, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL)); SCIP_CALL( SCIPaddVar(scip, probdata->vars[i][j]) ); } else probdata->vars[i][j] = NULL; } } /* generate linear ordering constraint */ SCIP_CALL( SCIPcreateConsLinearOrdering(scip, &cons, "LOP", probdata->n, probdata->vars, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)); SCIP_CALL( SCIPaddCons(scip, cons) ); SCIP_CALL( SCIPreleaseCons(scip, &cons) ); /* set maximization */ SCIP_CALL( SCIPsetObjsense(scip, SCIP_OBJSENSE_MAXIMIZE) ); return SCIP_OKAY; }
/** creates a tree compression */ SCIP_RETCODE SCIPcomprCreate( SCIP_COMPR** compr, /**< pointer to tree compression data structure */ SCIP_SET* set, /**< global SCIP settings */ SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of tree compression */ const char* desc, /**< description of tree compression */ int priority, /**< priority of the tree compression */ int minnnodes, /**< minimal number of nodes for calling compression */ SCIP_DECL_COMPRCOPY ((*comprcopy)), /**< copy method of tree compression or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_COMPRFREE ((*comprfree)), /**< destructor of tree compression */ SCIP_DECL_COMPRINIT ((*comprinit)), /**< initialize tree compression */ SCIP_DECL_COMPREXIT ((*comprexit)), /**< deinitialize tree compression */ SCIP_DECL_COMPRINITSOL ((*comprinitsol)), /**< solving process initialization method of tree compression */ SCIP_DECL_COMPREXITSOL ((*comprexitsol)), /**< solving process deinitialization method of tree compression */ SCIP_DECL_COMPREXEC ((*comprexec)), /**< execution method of tree compression */ SCIP_COMPRDATA* comprdata /**< tree compression data */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(compr != NULL); assert(name != NULL); assert(desc != NULL); assert(comprexec != NULL); SCIP_ALLOC( BMSallocMemory(compr) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*compr)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*compr)->desc, desc, strlen(desc)+1) ); (*compr)->priority = priority; (*compr)->minnnodes = minnnodes; (*compr)->comprcopy = comprcopy; (*compr)->comprfree = comprfree; (*compr)->comprinit = comprinit; (*compr)->comprexit = comprexit; (*compr)->comprinitsol = comprinitsol; (*compr)->comprexitsol = comprexitsol; (*compr)->comprexec = comprexec; (*compr)->comprdata = comprdata; SCIP_CALL( SCIPclockCreate(&(*compr)->setuptime, SCIP_CLOCKTYPE_DEFAULT) ); SCIP_CALL( SCIPclockCreate(&(*compr)->comprclock, SCIP_CLOCKTYPE_DEFAULT) ); (*compr)->ncalls = 0; (*compr)->nfound = 0; (*compr)->rate = 0.0; (*compr)->initialized = FALSE; (*compr)->nnodes = 0; (*compr)->loi = 0.0; /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "compression/%s/priority", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of compression <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*compr)->priority, TRUE, priority, INT_MIN/4, INT_MAX/4, paramChgdComprPriority, (SCIP_PARAMDATA*)(*compr)) ); /*lint !e740*/ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "compression/%s/minnleaves", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "minimal number of leave nodes for calling tree compression <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*compr)->minnnodes, FALSE, minnnodes, 1, INT_MAX, NULL, NULL) ); return SCIP_OKAY; }
/** sets up the problem data */ SCIP_RETCODE SCIPprobdataCreate( SCIP* scip, /**< SCIP data structure */ const char* probname, /**< problem name */ int* ids, /**< array of item ids */ SCIP_Longint* weights, /**< array containing the item weights */ int nitems, /**< number of items */ SCIP_Longint capacity /**< bin capacity */ ) { SCIP_PROBDATA* probdata; SCIP_CONS** conss; char name[SCIP_MAXSTRLEN]; int i; assert(scip != NULL); /* create event handler if it does not exist yet */ if( SCIPfindEventhdlr(scip, EVENTHDLR_NAME) == NULL ) { SCIP_CALL( SCIPincludeEventhdlrBasic(scip, NULL, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecAddedVar, NULL) ); } /* create problem in SCIP and add non-NULL callbacks via setter functions */ SCIP_CALL( SCIPcreateProbBasic(scip, probname) ); SCIP_CALL( SCIPsetProbDelorig(scip, probdelorigBinpacking) ); SCIP_CALL( SCIPsetProbTrans(scip, probtransBinpacking) ); SCIP_CALL( SCIPsetProbDeltrans(scip, probdeltransBinpacking) ); SCIP_CALL( SCIPsetProbInitsol(scip, probinitsolBinpacking) ); SCIP_CALL( SCIPsetProbExitsol(scip, probexitsolBinpacking) ); /* set objective sense */ SCIP_CALL( SCIPsetObjsense(scip, SCIP_OBJSENSE_MINIMIZE) ); /* tell SCIP that the objective will be always integral */ SCIP_CALL( SCIPsetObjIntegral(scip) ); SCIP_CALL( SCIPallocBufferArray(scip, &conss, nitems) ); /* create set covering constraints for each item */ for( i = 0; i < nitems; ++i ) { (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "item_%d", ids[i]); SCIP_CALL( SCIPcreateConsBasicSetcover(scip, &conss[i], name, 0, NULL) ); /* declare constraint modifiable for adding variables during pricing */ SCIP_CALL( SCIPsetConsModifiable(scip, conss[i], TRUE) ); SCIP_CALL( SCIPaddCons(scip, conss[i]) ); } /* create problem data */ SCIP_CALL( probdataCreate(scip, &probdata, NULL, conss, weights, ids, 0, nitems, capacity) ); SCIP_CALL( createInitialColumns(scip, probdata) ); /* set user problem data */ SCIP_CALL( SCIPsetProbData(scip, probdata) ); SCIP_CALL( SCIPpricerBinpackingActivate(scip, conss, weights, ids, nitems, capacity) ); /* free local buffer arrays */ SCIPfreeBufferArray(scip, &conss); return SCIP_OKAY; }
/** create initial columns */ static SCIP_RETCODE createInitialColumns( SCIP* scip, /**< SCIP data structure */ SCIP_PROBDATA* probdata /**< problem data */ ) { SCIP_CONS** conss; SCIP_VARDATA* vardata; SCIP_VAR* var; char name[SCIP_MAXSTRLEN]; int* ids; SCIP_Longint* weights; int nitems; int i; conss = probdata->conss; ids = probdata->ids; weights = probdata->weights; nitems = probdata->nitems; /* create start solution each item in exactly one bin */ for( i = 0; i < nitems; ++i ) { (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "item_%d", ids[i]); SCIPdebugMessage("create variable for item %d with weight = %"SCIP_LONGINT_FORMAT"\n", ids[i], weights[i]); /* create variable for the packing pattern which contains only this item */ SCIP_CALL( SCIPcreateVarBinpacking(scip, &var, name, 1.0, TRUE, TRUE, NULL) ); /* add variable to the problem */ SCIP_CALL( SCIPaddVar(scip, var) ); /* store variable in the problme data */ SCIP_CALL( SCIPprobdataAddVar(scip, probdata, var) ); /* add variable to corresponding set covering constraint */ SCIP_CALL( SCIPaddCoefSetppc(scip, conss[i], var) ); /* create the variable data for the variable; the variable data contains the information in which constraints the * variable appears */ SCIP_CALL( SCIPvardataCreateBinpacking(scip, &vardata, &i, 1) ); /* add the variable data to the variable */ SCIPvarSetData(var, vardata); /* change the upper bound of the binary variable to lazy since the upper bound is already enforced * due to the objective function the set covering constraint; * The reason for doing is that, is to avoid the bound of x <= 1 in the LP relaxation since this bound * constraint would produce a dual variable which might have a positive reduced cost */ SCIP_CALL( SCIPchgVarUbLazy(scip, var, 1.0) ); /* release variable */ SCIP_CALL( SCIPreleaseVar(scip, &var) ); } return SCIP_OKAY; }
/** method for either Farkas or Redcost pricing */ static SCIP_RETCODE pricing( SCIP* scip, /**< SCIP data structure */ SCIP_PRICER* pricer, /**< pricer */ SCIP_Real* lowerbound, /**< lowerbound pointer */ SCIP_Bool farkas /**< TRUE: Farkas pricing; FALSE: Redcost pricing */ ) { SCIP_PRICERDATA* pricerdata; /* the data of the pricer */ SCIP_PROBDATA* probdata; GRAPH* graph; SCIP_VAR* var; PATH* path; SCIP_Real* edgecosts; /* edgecosts of the current subproblem */ char varname[SCIP_MAXSTRLEN]; SCIP_Real newlowerbound = -SCIPinfinity(scip); SCIP_Real redcost; /* reduced cost */ int tail; int e; int t; int i; assert(scip != NULL); assert(pricer != NULL); /* get pricer data */ pricerdata = SCIPpricerGetData(pricer); assert(pricerdata != NULL); /* get problem data */ probdata = SCIPgetProbData(scip); assert(probdata != NULL); SCIPdebugMessage("solstat=%d\n", SCIPgetLPSolstat(scip)); if( !farkas && SCIPgetLPSolstat(scip) == SCIP_LPSOLSTAT_OPTIMAL ) newlowerbound = SCIPgetSolTransObj(scip, NULL); SCIPdebug( SCIP_CALL( SCIPprintSol(scip, NULL, NULL, FALSE) ) ); # if 0 if ( pricerdata->lowerbound <= 4 ) { char label[SCIP_MAXSTRLEN]; (void)SCIPsnprintf(label, SCIP_MAXSTRLEN, "X%g.gml", pricerdata->lowerbound); SCIP_CALL( SCIPprobdataPrintGraph(scip, label , NULL, TRUE) ); pricerdata->lowerbound++; } #endif /* get the graph*/ graph = SCIPprobdataGetGraph(probdata); /* get dual solutions and save them in mi and pi */ for( t = 0; t < pricerdata->realnterms; ++t ) { if( farkas ) { pricerdata->mi[t] = SCIPgetDualfarkasLinear(scip, pricerdata->pathcons[t]); } else { pricerdata->mi[t] = SCIPgetDualsolLinear(scip, pricerdata->pathcons[t]); assert(!SCIPisNegative(scip, pricerdata->mi[t])); } } for( e = 0; e < pricerdata->nedges; ++e ) { if( !pricerdata->bigt ) { for( t = 0; t < pricerdata->realnterms; ++t ) { if( farkas ) { pricerdata->pi[t * pricerdata->nedges + e] = SCIPgetDualfarkasLinear( scip, pricerdata->edgecons[t * pricerdata->nedges + e]); } else { pricerdata->pi[t * pricerdata->nedges + e] = SCIPgetDualsolLinear( scip, pricerdata->edgecons[t * pricerdata->nedges + e]); } } } else { if( farkas ) { pricerdata->pi[e] = SCIPgetDualfarkasLinear( scip, pricerdata->edgecons[e]); } else { pricerdata->pi[e] = SCIPgetDualsolLinear( scip, pricerdata->edgecons[e]); } } } SCIP_CALL( SCIPallocMemoryArray(scip, &path, graph->knots) ); SCIP_CALL( SCIPallocMemoryArray(scip, &edgecosts, pricerdata->nedges) ); if( pricerdata->bigt ) { for( e = 0; e < pricerdata->nedges; ++e ) { edgecosts[e] = (-pricerdata->pi[e]); } } /* find shortest r-t (r root, t terminal) paths and create corresponding variables iff reduced cost < 0 */ for( t = 0; t < pricerdata->realnterms; ++t ) { for( e = 0; e < pricerdata->nedges; ++e ) { if( !pricerdata->bigt ) { edgecosts[e] = (-pricerdata->pi[t * pricerdata->nedges + e]); } assert(!SCIPisNegative(scip, edgecosts[e])); } for( i = 0; i < graph->knots; i++ ) graph->mark[i] = 1; graph_path_exec(scip, graph, FSP_MODE, pricerdata->root, edgecosts, path); /* compute reduced cost of shortest path to terminal t */ redcost = 0.0; tail = pricerdata->realterms[t]; while( tail != pricerdata->root ) { redcost += edgecosts[path[tail].edge]; tail = graph->tail[path[tail].edge]; } redcost -= pricerdata->mi[t]; if( !farkas && SCIPgetLPSolstat(scip) == SCIP_LPSOLSTAT_OPTIMAL ) { newlowerbound += redcost; } /* check if reduced cost < 0 */ if( SCIPisNegative(scip, redcost) ) { /* create variable to the shortest path (having reduced cost < 0) */ var = NULL; sprintf(varname, "PathVar%d_%d", t, pricerdata->ncreatedvars[t]); ++(pricerdata->ncreatedvars[t]); SCIP_CALL( SCIPcreateVarBasic(scip, &var, varname, 0.0, SCIPinfinity(scip), 0.0, SCIP_VARTYPE_CONTINUOUS) ); SCIP_CALL( SCIPaddPricedVar(scip, var, -redcost) ); tail = pricerdata->realterms[t]; while( tail != pricerdata->root ) { /* add variable to constraints */ if( !pricerdata->bigt ) { SCIP_CALL( SCIPaddCoefLinear(scip, pricerdata->edgecons[t * pricerdata->nedges + path[tail].edge], var, 1.0) ); } else { SCIP_CALL( SCIPaddCoefLinear(scip, pricerdata->edgecons[path[tail].edge], var, 1.0) ); } tail = graph->tail[path[tail].edge]; } SCIP_CALL( SCIPaddCoefLinear(scip, pricerdata->pathcons[t], var, 1.0) ); } } if( !farkas && SCIPgetLPSolstat(scip) == SCIP_LPSOLSTAT_OPTIMAL ) *lowerbound = newlowerbound; SCIPfreeMemoryArray(scip, &edgecosts); SCIPfreeMemoryArray(scip, &path); return SCIP_OKAY; }
/** presolving execution method */ static SCIP_DECL_PRESOLEXEC(presolExecBoundshift) { /*lint --e{715}*/ SCIP_PRESOLDATA* presoldata; SCIP_VAR** scipvars; SCIP_VAR** vars; int nbinvars; int nvars; int v; assert(scip != NULL); assert(presol != NULL); assert(strcmp(SCIPpresolGetName(presol), PRESOL_NAME) == 0); assert(result != NULL); *result = SCIP_DIDNOTRUN; /* get presolver data */ presoldata = SCIPpresolGetData(presol); assert(presoldata != NULL); /* get the problem variables */ scipvars = SCIPgetVars(scip); nbinvars = SCIPgetNBinVars(scip); nvars = SCIPgetNVars(scip) - nbinvars; if( nvars == 0 ) return SCIP_OKAY; if( SCIPdoNotAggr(scip) ) return SCIP_OKAY; *result = SCIP_DIDNOTFIND; /* copy the integer variables into an own array, since adding new integer variables affects the left-most slots in * the array and thereby interferes with our search loop */ SCIP_CALL( SCIPduplicateBufferArray(scip, &vars, &scipvars[nbinvars], nvars) ); /* scan the integer, implicit, and continuous variables for possible conversion */ for( v = nvars - 1; v >= 0; --v ) { SCIP_VAR* var = vars[v]; SCIP_Real lb; SCIP_Real ub; assert(SCIPvarGetType(var) != SCIP_VARTYPE_BINARY); /* get current variable's bounds */ lb = SCIPvarGetLbGlobal(var); ub = SCIPvarGetUbGlobal(var); assert( SCIPisLE(scip, lb, ub) ); if( SCIPisEQ(scip, lb, ub) ) continue; if( presoldata->integer && !SCIPisIntegral(scip, ub - lb) ) continue; /* check if bounds are shiftable */ if( !SCIPisEQ(scip, lb, 0.0) && /* lower bound != 0.0 */ SCIPisLT(scip, ub, SCIPinfinity(scip)) && /* upper bound != infinity */ SCIPisGT(scip, lb, -SCIPinfinity(scip)) && /* lower bound != -infinity */ #if 0 SCIPisLT(scip, ub - lb, SCIPinfinity(scip)) && /* interval length less than SCIPinfinity(scip) */ #endif SCIPisLT(scip, ub - lb, (SCIP_Real) presoldata->maxshift) ) /* less than max shifting */ { SCIP_VAR* newvar; char newvarname[SCIP_MAXSTRLEN]; SCIP_Bool infeasible; SCIP_Bool redundant; SCIP_Bool aggregated; SCIPdebugMessage("convert range <%s>[%g,%g] to [%g,%g]\n", SCIPvarGetName(var), lb, ub, 0.0, (ub - lb) ); /* create new variable */ (void) SCIPsnprintf(newvarname, SCIP_MAXSTRLEN, "%s_shift", SCIPvarGetName(var)); SCIP_CALL( SCIPcreateVar(scip, &newvar, newvarname, 0.0, (ub - lb), 0.0, SCIPvarGetType(var), SCIPvarIsInitial(var), SCIPvarIsRemovable(var), NULL, NULL, NULL, NULL, NULL) ); SCIP_CALL( SCIPaddVar(scip, newvar) ); /* aggregate old variable with new variable */ if( presoldata->flipping ) { if( REALABS(ub) < REALABS(lb) ) { SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, 1.0, ub, &infeasible, &redundant, &aggregated) ); } else { SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) ); } } else { SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) ); } assert(!infeasible); assert(redundant); assert(aggregated); SCIPdebugMessage("var <%s> with bounds [%f,%f] has obj %f\n", SCIPvarGetName(newvar),SCIPvarGetLbGlobal(newvar),SCIPvarGetUbGlobal(newvar),SCIPvarGetObj(newvar)); /* release variable */ SCIP_CALL( SCIPreleaseVar(scip, &newvar) ); /* take care of statistic */ (*naggrvars)++; *result = SCIP_SUCCESS; } } /* free temporary memory */ SCIPfreeBufferArray(scip, &vars); return SCIP_OKAY; }
/** creates a separator */ SCIP_RETCODE SCIPsepaCreate( SCIP_SEPA** sepa, /**< pointer to separator data structure */ SCIP_SET* set, /**< global SCIP settings */ SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of separator */ const char* desc, /**< description of separator */ int priority, /**< priority of separator (>= 0: before, < 0: after constraint handlers) */ int freq, /**< frequency for calling separator */ SCIP_Real maxbounddist, /**< maximal relative distance from current node's dual bound to primal bound compared * to best node's dual bound for applying separation */ SCIP_Bool usessubscip, /**< does the separator use a secondary SCIP instance? */ SCIP_Bool delay, /**< should separator be delayed, if other separators found cuts? */ SCIP_DECL_SEPACOPY ((*sepacopy)), /**< copy method of separator or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_SEPAFREE ((*sepafree)), /**< destructor of separator */ SCIP_DECL_SEPAINIT ((*sepainit)), /**< initialize separator */ SCIP_DECL_SEPAEXIT ((*sepaexit)), /**< deinitialize separator */ SCIP_DECL_SEPAINITSOL ((*sepainitsol)), /**< solving process initialization method of separator */ SCIP_DECL_SEPAEXITSOL ((*sepaexitsol)), /**< solving process deinitialization method of separator */ SCIP_DECL_SEPAEXECLP ((*sepaexeclp)), /**< LP solution separation method of separator */ SCIP_DECL_SEPAEXECSOL ((*sepaexecsol)), /**< arbitrary primal solution separation method of separator */ SCIP_SEPADATA* sepadata /**< separator data */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(sepa != NULL); assert(name != NULL); assert(desc != NULL); assert(freq >= -1); assert(0.0 <= maxbounddist && maxbounddist <= 1.0); assert(sepaexeclp != NULL || sepaexecsol != NULL); SCIP_ALLOC( BMSallocMemory(sepa) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*sepa)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*sepa)->desc, desc, strlen(desc)+1) ); (*sepa)->priority = priority; (*sepa)->freq = freq; (*sepa)->maxbounddist = maxbounddist; (*sepa)->usessubscip = usessubscip; (*sepa)->sepacopy = sepacopy; (*sepa)->sepafree = sepafree; (*sepa)->sepainit = sepainit; (*sepa)->sepaexit = sepaexit; (*sepa)->sepainitsol = sepainitsol; (*sepa)->sepaexitsol = sepaexitsol; (*sepa)->sepaexeclp = sepaexeclp; (*sepa)->sepaexecsol = sepaexecsol; (*sepa)->sepadata = sepadata; SCIP_CALL( SCIPclockCreate(&(*sepa)->setuptime, SCIP_CLOCKTYPE_DEFAULT) ); SCIP_CALL( SCIPclockCreate(&(*sepa)->sepaclock, SCIP_CLOCKTYPE_DEFAULT) ); (*sepa)->lastsepanode = -1; (*sepa)->ncalls = 0; (*sepa)->ncutoffs = 0; (*sepa)->ncutsfound = 0; (*sepa)->ncutsapplied = 0; (*sepa)->nconssfound = 0; (*sepa)->ndomredsfound = 0; (*sepa)->ncallsatnode = 0; (*sepa)->ncutsfoundatnode = 0; (*sepa)->lpwasdelayed = FALSE; (*sepa)->solwasdelayed = FALSE; (*sepa)->initialized = FALSE; /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "separating/%s/priority", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of separator <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*sepa)->priority, TRUE, priority, INT_MIN/4, INT_MAX/4, paramChgdSepaPriority, (SCIP_PARAMDATA*)(*sepa)) ); /*lint !e740*/ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "separating/%s/freq", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "frequency for calling separator <%s> (-1: never, 0: only in root node)", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*sepa)->freq, FALSE, freq, -1, INT_MAX, NULL, NULL) ); (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "separating/%s/maxbounddist", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "maximal relative distance from current node's dual bound to primal bound compared to best node's dual bound for applying separator <%s> (0.0: only on current best node, 1.0: on all nodes)", name); SCIP_CALL( SCIPsetAddRealParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*sepa)->maxbounddist, TRUE, maxbounddist, 0.0, 1.0, NULL, NULL) ); (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "separating/%s/delay", name); SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname, "should separator be delayed, if other separators found cuts?", &(*sepa)->delay, TRUE, delay, NULL, NULL) ); /*lint !e740*/ return SCIP_OKAY; }
/* Read SAT formula in "CNF File Format". * * The specification is taken from the * * Satisfiability Suggested Format * * Online available at http://www.intellektik.informatik.tu-darmstadt.de/SATLIB/Benchmarks/SAT/satformat.ps * * The method reads all files of CNF format. Other formats (SAT, SATX, SATE) are not supported. */ static SCIP_RETCODE readCnf( SCIP* scip, /**< SCIP data structure */ SCIP_FILE* file /**< input file */ ) { SCIP_RETCODE retcode; SCIP_VAR** vars; SCIP_VAR** clausevars; SCIP_CONS* cons; int* varsign; char* tok; char* nexttok; char line[MAXLINELEN]; char format[SCIP_MAXSTRLEN]; char varname[SCIP_MAXSTRLEN]; char s[SCIP_MAXSTRLEN]; SCIP_Bool dynamicconss; SCIP_Bool dynamiccols; SCIP_Bool dynamicrows; SCIP_Bool useobj; int linecount; int clauselen; int clausenum; int nvars; int nclauses; int varnum; int v; assert(scip != NULL); assert(file != NULL); retcode = SCIP_OKAY; linecount = 0; /* read header */ SCIP_CALL( readCnfLine(scip, file, line, (int) sizeof(line), &linecount) ); if( *line != 'p' ) { readError(scip, linecount, "problem declaration line expected"); return SCIP_READERROR; } if( sscanf(line, "p %8s %d %d", format, &nvars, &nclauses) != 3 ) { readError(scip, linecount, "invalid problem declaration (must be 'p cnf <nvars> <nclauses>')"); return SCIP_READERROR; } if( strcmp(format, "cnf") != 0 ) { (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid format tag <%s> (must be 'cnf')", format); readError(scip, linecount, s); return SCIP_READERROR; } if( nvars <= 0 ) { (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid number of variables <%d> (must be positive)", nvars); readError(scip, linecount, s); return SCIP_READERROR; } if( nclauses <= 0 ) { (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid number of clauses <%d> (must be positive)", nclauses); readError(scip, linecount, s); return SCIP_READERROR; } /* get parameter values */ SCIP_CALL( SCIPgetBoolParam(scip, "reading/cnfreader/dynamicconss", &dynamicconss) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/cnfreader/dynamiccols", &dynamiccols) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/cnfreader/dynamicrows", &dynamicrows) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/cnfreader/useobj", &useobj) ); /* get temporary memory */ SCIP_CALL( SCIPallocBufferArray(scip, &vars, nvars) ); SCIP_CALL( SCIPallocBufferArray(scip, &clausevars, nvars) ); SCIP_CALL( SCIPallocBufferArray(scip, &varsign, nvars) ); /* create the variables */ for( v = 0; v < nvars; ++v ) { (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "x%d", v+1); SCIP_CALL( SCIPcreateVar(scip, &vars[v], varname, 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY, !dynamiccols, dynamiccols, NULL, NULL, NULL, NULL, NULL) ); SCIP_CALL( SCIPaddVar(scip, vars[v]) ); varsign[v] = 0; } /* read clauses */ clausenum = 0; clauselen = 0; do { retcode = readCnfLine(scip, file, line, (int) sizeof(line), &linecount); if( retcode != SCIP_OKAY ) goto TERMINATE; if( *line != '\0' && *line != '%' ) { tok = SCIPstrtok(line, " \f\n\r\t", &nexttok); while( tok != NULL ) { /* parse literal and check for errors */ if( sscanf(tok, "%d", &v) != 1 ) { (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid literal <%s>", tok); readError(scip, linecount, s); retcode = SCIP_READERROR; goto TERMINATE; } /* interpret literal number: v == 0: end of clause, v < 0: negated literal, v > 0: positive literal */ if( v == 0 ) { /* end of clause: construct clause and add it to SCIP */ if( clauselen == 0 ) readWarning(scip, linecount, "empty clause detected in line -- problem infeasible"); clausenum++; (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "c%d", clausenum); if( SCIPfindConshdlr(scip, "logicor") != NULL ) { /* if the constraint handler logicor exit create a logicor constraint */ SCIP_CALL( SCIPcreateConsLogicor(scip, &cons, s, clauselen, clausevars, !dynamicrows, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) ); } else if( SCIPfindConshdlr(scip, "setppc") != NULL ) { /* if the constraint handler logicor does not exit but constraint * handler setppc create a setppc constraint */ SCIP_CALL( SCIPcreateConsSetcover(scip, &cons, s, clauselen, clausevars, !dynamicrows, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) ); } else { /* if none of the previous constraint handler exits create a linear * constraint */ SCIP_Real* vals; int i; SCIP_CALL( SCIPallocBufferArray(scip, &vals, clauselen) ); for( i = 0; i < clauselen; ++i ) vals[i] = 1.0; SCIP_CALL( SCIPcreateConsLinear(scip, &cons, s, clauselen, clausevars, vals, 1.0, SCIPinfinity(scip), !dynamicrows, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, dynamicconss, dynamicrows, FALSE) ); SCIPfreeBufferArray(scip, &vals); } SCIP_CALL( SCIPaddCons(scip, cons) ); SCIP_CALL( SCIPreleaseCons(scip, &cons) ); clauselen = 0; } else if( v >= -nvars && v <= nvars ) { if( clauselen >= nvars ) { readError(scip, linecount, "too many literals in clause"); retcode = SCIP_READERROR; goto TERMINATE; } /* add literal to clause */ varnum = ABS(v)-1; if( v < 0 ) { SCIP_CALL( SCIPgetNegatedVar(scip, vars[varnum], &clausevars[clauselen]) ); varsign[varnum]--; } else { clausevars[clauselen] = vars[varnum]; varsign[varnum]++; } clauselen++; } else { (void) SCIPsnprintf(s, SCIP_MAXSTRLEN, "invalid variable number <%d>", ABS(v)); readError(scip, linecount, s); retcode = SCIP_READERROR; goto TERMINATE; } /* get next token */ tok = SCIPstrtok(NULL, " \f\n\r\t", &nexttok); } } } while( *line != '\0' && *line != '%' ); /* check for additional literals */ if( clauselen > 0 ) { SCIPwarningMessage(scip, "found %d additional literals after last clause\n", clauselen); } /* check number of clauses */ if( clausenum != nclauses ) { SCIPwarningMessage(scip, "expected %d clauses, but found %d\n", nclauses, clausenum); } TERMINATE: /* change objective values and release variables */ SCIP_CALL( SCIPsetObjsense(scip, SCIP_OBJSENSE_MAXIMIZE) ); if( useobj ) { for( v = 0; v < nvars; ++v ) { SCIP_CALL( SCIPchgVarObj(scip, vars[v], (SCIP_Real)varsign[v]) ); SCIP_CALL( SCIPreleaseVar(scip, &vars[v]) ); } } /* free temporary memory */ SCIPfreeBufferArray(scip, &varsign); SCIPfreeBufferArray(scip, &clausevars); SCIPfreeBufferArray(scip, &vars); return retcode; }
/** execution method of primal heuristic */ static SCIP_DECL_HEUREXEC(heurExecMutation) { /*lint --e{715}*/ SCIP_Longint maxnnodes; SCIP_Longint nsubnodes; /* node limit for the subproblem */ SCIP_HEURDATA* heurdata; /* heuristic's data */ SCIP* subscip; /* the subproblem created by mutation */ SCIP_VAR** vars; /* original problem's variables */ SCIP_VAR** subvars; /* subproblem's variables */ SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */ SCIP_Real cutoff; /* objective cutoff for the subproblem */ SCIP_Real maxnnodesr; SCIP_Real memorylimit; SCIP_Real timelimit; /* timelimit for the subproblem */ SCIP_Real upperbound; int nvars; /* number of original problem's variables */ int i; SCIP_Bool success; SCIP_RETCODE retcode; assert( heur != NULL ); assert( scip != NULL ); assert( result != NULL ); /* get heuristic's data */ heurdata = SCIPheurGetData(heur); assert( heurdata != NULL ); *result = SCIP_DELAYED; /* only call heuristic, if feasible solution is available */ if( SCIPgetNSols(scip) <= 0 ) return SCIP_OKAY; /* only call heuristic, if the best solution comes from transformed problem */ assert( SCIPgetBestSol(scip) != NULL ); if( SCIPsolIsOriginal(SCIPgetBestSol(scip)) ) return SCIP_OKAY; /* only call heuristic, if enough nodes were processed since last incumbent */ if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip,SCIPgetBestSol(scip)) < heurdata->nwaitingnodes) return SCIP_OKAY; *result = SCIP_DIDNOTRUN; /* only call heuristic, if discrete variables are present */ if( SCIPgetNBinVars(scip) == 0 && SCIPgetNIntVars(scip) == 0 ) return SCIP_OKAY; /* calculate the maximal number of branching nodes until heuristic is aborted */ maxnnodesr = heurdata->nodesquot * SCIPgetNNodes(scip); /* reward mutation if it succeeded often, count the setup costs for the sub-MIP as 100 nodes */ maxnnodesr *= 1.0 + 2.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0); maxnnodes = (SCIP_Longint) maxnnodesr - 100 * SCIPheurGetNCalls(heur); maxnnodes += heurdata->nodesofs; /* determine the node limit for the current process */ nsubnodes = maxnnodes - heurdata->usednodes; nsubnodes = MIN(nsubnodes, heurdata->maxnodes); /* check whether we have enough nodes left to call subproblem solving */ if( nsubnodes < heurdata->minnodes ) return SCIP_OKAY; if( SCIPisStopped(scip) ) return SCIP_OKAY; *result = SCIP_DIDNOTFIND; SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) ); /* initializing the subproblem */ SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) ); SCIP_CALL( SCIPcreate(&subscip) ); /* create the variable mapping hash map */ SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) ); if( heurdata->uselprows ) { char probname[SCIP_MAXSTRLEN]; /* copy all plugins */ SCIP_CALL( SCIPincludeDefaultPlugins(subscip) ); /* get name of the original problem and add the string "_mutationsub" */ (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_mutationsub", SCIPgetProbName(scip)); /* create the subproblem */ SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ); /* copy all variables */ SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) ); } else { SCIP_Bool valid; valid = FALSE; SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "rens", TRUE, FALSE, TRUE, &valid) ); if( heurdata->copycuts ) { /* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */ SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) ); } SCIPdebugMessage("Copying the SCIP instance was %s complete.\n", valid ? "" : "not "); } for( i = 0; i < nvars; i++ ) subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]); /* free hash map */ SCIPhashmapFree(&varmapfw); /* create a new problem, which fixes variables with same value in bestsol and LP relaxation */ SCIP_CALL( createSubproblem(scip, subscip, subvars, heurdata->minfixingrate, &heurdata->randseed, heurdata->uselprows) ); /* do not abort subproblem on CTRL-C */ SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) ); /* disable output to console */ SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) ); /* check whether there is enough time and memory left */ SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) ); if( !SCIPisInfinity(scip, timelimit) ) timelimit -= SCIPgetSolvingTime(scip); SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) ); /* substract the memory already used by the main SCIP and the estimated memory usage of external software */ if( !SCIPisInfinity(scip, memorylimit) ) { memorylimit -= SCIPgetMemUsed(scip)/1048576.0; memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0; } /* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */ if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 ) goto TERMINATE; /* set limits for the subproblem */ SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) ); SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) ); SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) ); /* forbid recursive call of heuristics and separators solving subMIPs */ SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) ); /* disable cutting plane separation */ SCIP_CALL( SCIPsetSeparating(subscip, SCIP_PARAMSETTING_OFF, TRUE) ); /* disable expensive presolving */ SCIP_CALL( SCIPsetPresolving(subscip, SCIP_PARAMSETTING_FAST, TRUE) ); /* use best estimate node selection */ if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") ) { SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) ); } /* use inference branching */ if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") ) { SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) ); } /* disable conflict analysis */ if( !SCIPisParamFixed(subscip, "conflict/useprop") ) { SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) ); } if( !SCIPisParamFixed(subscip, "conflict/useinflp") ) { SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) ); } if( !SCIPisParamFixed(subscip, "conflict/useboundlp") ) { SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) ); } if( !SCIPisParamFixed(subscip, "conflict/usesb") ) { SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) ); } if( !SCIPisParamFixed(subscip, "conflict/usepseudo") ) { SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) ); } /* employ a limit on the number of enforcement rounds in the quadratic constraint handlers; this fixes the issue that * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no decutions shall be * made for the original SCIP */ if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") ) { SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 10) ); } /* add an objective cutoff */ cutoff = SCIPinfinity(scip); assert( !SCIPisInfinity(scip, SCIPgetUpperbound(scip)) ); upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip); if( !SCIPisInfinity(scip, -1.0 * SCIPgetLowerbound(scip)) ) { cutoff = (1-heurdata->minimprove) * SCIPgetUpperbound(scip) + heurdata->minimprove * SCIPgetLowerbound(scip); } else { if( SCIPgetUpperbound ( scip ) >= 0 ) cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip ); else cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip ); } cutoff = MIN(upperbound, cutoff ); SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) ); /* solve the subproblem */ SCIPdebugMessage("Solve Mutation subMIP\n"); retcode = SCIPsolve(subscip); /* Errors in solving the subproblem should not kill the overall solving process * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop. */ if( retcode != SCIP_OKAY ) { #ifndef NDEBUG SCIP_CALL( retcode ); #endif SCIPwarningMessage(scip, "Error while solving subproblem in Mutation heuristic; sub-SCIP terminated with code <%d>\n",retcode); } heurdata->usednodes += SCIPgetNNodes(subscip); /* check, whether a solution was found */ if( SCIPgetNSols(subscip) > 0 ) { SCIP_SOL** subsols; int nsubsols; /* check, whether a solution was found; * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted */ nsubsols = SCIPgetNSols(subscip); subsols = SCIPgetSols(subscip); success = FALSE; for( i = 0; i < nsubsols && !success; ++i ) { SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) ); } if( success ) *result = SCIP_FOUNDSOL; } TERMINATE: /* free subproblem */ SCIPfreeBufferArray(scip, &subvars); SCIP_CALL( SCIPfree(&subscip) ); return SCIP_OKAY; }
/** LP solution separation method of separator */ static SCIP_DECL_SEPAEXECLP(sepaExeclpGomory) { /*lint --e{715}*/ SCIP_SEPADATA* sepadata; SCIP_VAR** vars; SCIP_COL** cols; SCIP_ROW** rows; SCIP_Real* binvrow; SCIP_Real* cutcoefs; SCIP_Real maxscale; SCIP_Real minfrac; SCIP_Real maxfrac; SCIP_Longint maxdnom; SCIP_Bool cutoff; int* basisind; int naddedcuts; int nvars; int ncols; int nrows; int ncalls; int depth; int maxdepth; int maxsepacuts; int c; int i; assert(sepa != NULL); assert(strcmp(SCIPsepaGetName(sepa), SEPA_NAME) == 0); assert(scip != NULL); assert(result != NULL); *result = SCIP_DIDNOTRUN; sepadata = SCIPsepaGetData(sepa); assert(sepadata != NULL); depth = SCIPgetDepth(scip); ncalls = SCIPsepaGetNCallsAtNode(sepa); minfrac = sepadata->away; maxfrac = 1.0 - sepadata->away; /* only call separator, if we are not close to terminating */ if( SCIPisStopped(scip) ) return SCIP_OKAY; /* only call the gomory cut separator a given number of times at each node */ if( (depth == 0 && sepadata->maxroundsroot >= 0 && ncalls >= sepadata->maxroundsroot) || (depth > 0 && sepadata->maxrounds >= 0 && ncalls >= sepadata->maxrounds) ) return SCIP_OKAY; /* only call separator, if an optimal LP solution is at hand */ if( SCIPgetLPSolstat(scip) != SCIP_LPSOLSTAT_OPTIMAL ) return SCIP_OKAY; /* only call separator, if the LP solution is basic */ if( !SCIPisLPSolBasic(scip) ) return SCIP_OKAY; /* only call separator, if there are fractional variables */ if( SCIPgetNLPBranchCands(scip) == 0 ) return SCIP_OKAY; /* get variables data */ SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) ); /* get LP data */ SCIP_CALL( SCIPgetLPColsData(scip, &cols, &ncols) ); SCIP_CALL( SCIPgetLPRowsData(scip, &rows, &nrows) ); if( ncols == 0 || nrows == 0 ) return SCIP_OKAY; #if 0 /* if too many columns, separator is usually very slow: delay it until no other cuts have been found */ if( ncols >= 50*nrows ) return SCIP_OKAY; if( ncols >= 5*nrows ) { int ncutsfound; ncutsfound = SCIPgetNCutsFound(scip); if( ncutsfound > sepadata->lastncutsfound || !SCIPsepaWasLPDelayed(sepa) ) { sepadata->lastncutsfound = ncutsfound; *result = SCIP_DELAYED; return SCIP_OKAY; } } #endif /* set the maximal denominator in rational representation of gomory cut and the maximal scale factor to * scale resulting cut to integral values to avoid numerical instabilities */ /**@todo find better but still stable gomory cut settings: look at dcmulti, gesa3, khb0525, misc06, p2756 */ maxdepth = SCIPgetMaxDepth(scip); if( depth == 0 ) { maxdnom = 1000; maxscale = 1000.0; } else if( depth <= maxdepth/4 ) { maxdnom = 1000; maxscale = 1000.0; } else if( depth <= maxdepth/2 ) { maxdnom = 100; maxscale = 100.0; } else { maxdnom = 10; maxscale = 10.0; } /* allocate temporary memory */ SCIP_CALL( SCIPallocBufferArray(scip, &cutcoefs, nvars) ); SCIP_CALL( SCIPallocBufferArray(scip, &basisind, nrows) ); SCIP_CALL( SCIPallocBufferArray(scip, &binvrow, nrows) ); /* get basis indices */ SCIP_CALL( SCIPgetLPBasisInd(scip, basisind) ); /* get the maximal number of cuts allowed in a separation round */ if( depth == 0 ) maxsepacuts = sepadata->maxsepacutsroot; else maxsepacuts = sepadata->maxsepacuts; SCIPdebugMessage("searching gomory cuts: %d cols, %d rows, maxdnom=%"SCIP_LONGINT_FORMAT", maxscale=%g, maxcuts=%d\n", ncols, nrows, maxdnom, maxscale, maxsepacuts); cutoff = FALSE; naddedcuts = 0; /* for all basic columns belonging to integer variables, try to generate a gomory cut */ for( i = 0; i < nrows && naddedcuts < maxsepacuts && !SCIPisStopped(scip) && !cutoff; ++i ) { SCIP_Bool tryrow; tryrow = FALSE; c = basisind[i]; if( c >= 0 ) { SCIP_VAR* var; assert(c < ncols); var = SCIPcolGetVar(cols[c]); if( SCIPvarGetType(var) != SCIP_VARTYPE_CONTINUOUS ) { SCIP_Real primsol; primsol = SCIPcolGetPrimsol(cols[c]); assert(SCIPgetVarSol(scip, var) == primsol); /*lint !e777*/ if( SCIPfeasFrac(scip, primsol) >= minfrac ) { SCIPdebugMessage("trying gomory cut for col <%s> [%g]\n", SCIPvarGetName(var), primsol); tryrow = TRUE; } } } else if( sepadata->separaterows ) { SCIP_ROW* row; assert(0 <= -c-1 && -c-1 < nrows); row = rows[-c-1]; if( SCIProwIsIntegral(row) && !SCIProwIsModifiable(row) ) { SCIP_Real primsol; primsol = SCIPgetRowActivity(scip, row); if( SCIPfeasFrac(scip, primsol) >= minfrac ) { SCIPdebugMessage("trying gomory cut for row <%s> [%g]\n", SCIProwGetName(row), primsol); tryrow = TRUE; } } } if( tryrow ) { SCIP_Real cutrhs; SCIP_Real cutact; SCIP_Bool success; SCIP_Bool cutislocal; /* get the row of B^-1 for this basic integer variable with fractional solution value */ SCIP_CALL( SCIPgetLPBInvRow(scip, i, binvrow) ); cutact = 0.0; cutrhs = SCIPinfinity(scip); /* create a MIR cut out of the weighted LP rows using the B^-1 row as weights */ SCIP_CALL( SCIPcalcMIR(scip, NULL, BOUNDSWITCH, USEVBDS, ALLOWLOCAL, FIXINTEGRALRHS, NULL, NULL, (int) MAXAGGRLEN(nvars), sepadata->maxweightrange, minfrac, maxfrac, binvrow, 1.0, NULL, NULL, cutcoefs, &cutrhs, &cutact, &success, &cutislocal) ); assert(ALLOWLOCAL || !cutislocal); /* @todo Currently we are using the SCIPcalcMIR() function to compute the coefficients of the Gomory * cut. Alternatively, we could use the direct version (see thesis of Achterberg formula (8.4)) which * leads to cut a of the form \sum a_i x_i \geq 1. Rumor has it that these cuts are better. */ SCIPdebugMessage(" -> success=%u: %g <= %g\n", success, cutact, cutrhs); /* if successful, convert dense cut into sparse row, and add the row as a cut */ if( success && SCIPisFeasGT(scip, cutact, cutrhs) ) { SCIP_ROW* cut; char cutname[SCIP_MAXSTRLEN]; int v; /* construct cut name */ if( c >= 0 ) (void) SCIPsnprintf(cutname, SCIP_MAXSTRLEN, "gom%d_x%d", SCIPgetNLPs(scip), c); else (void) SCIPsnprintf(cutname, SCIP_MAXSTRLEN, "gom%d_s%d", SCIPgetNLPs(scip), -c-1); /* create empty cut */ SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &cut, sepa, cutname, -SCIPinfinity(scip), cutrhs, cutislocal, FALSE, sepadata->dynamiccuts) ); /* cache the row extension and only flush them if the cut gets added */ SCIP_CALL( SCIPcacheRowExtensions(scip, cut) ); /* collect all non-zero coefficients */ for( v = 0; v < nvars; ++v ) { if( !SCIPisZero(scip, cutcoefs[v]) ) { SCIP_CALL( SCIPaddVarToRow(scip, cut, vars[v], cutcoefs[v]) ); } } if( SCIProwGetNNonz(cut) == 0 ) { assert(SCIPisFeasNegative(scip, cutrhs)); SCIPdebugMessage(" -> gomory cut detected infeasibility with cut 0 <= %f\n", cutrhs); cutoff = TRUE; } else if( SCIProwGetNNonz(cut) == 1 ) { /* add the bound change as cut to avoid that the LP gets modified. that would mean the LP is not flushed * and the method SCIPgetLPBInvRow() fails; SCIP internally will apply that bound change automatically */ SCIP_CALL( SCIPaddCut(scip, NULL, cut, TRUE) ); naddedcuts++; } else { /* Only take efficacious cuts, except for cuts with one non-zero coefficients (= bound * changes); the latter cuts will be handeled internally in sepastore. */ if( SCIPisCutEfficacious(scip, NULL, cut) ) { assert(success == TRUE); SCIPdebugMessage(" -> gomory cut for <%s>: act=%f, rhs=%f, eff=%f\n", c >= 0 ? SCIPvarGetName(SCIPcolGetVar(cols[c])) : SCIProwGetName(rows[-c-1]), cutact, cutrhs, SCIPgetCutEfficacy(scip, NULL, cut)); if( sepadata->makeintegral ) { /* try to scale the cut to integral values */ SCIP_CALL( SCIPmakeRowIntegral(scip, cut, -SCIPepsilon(scip), SCIPsumepsilon(scip), maxdnom, maxscale, MAKECONTINTEGRAL, &success) ); if( sepadata->forcecuts ) success = TRUE; /* in case the left hand side in minus infinity and the right hand side is plus infinity the cut is * useless so we are not taking it at all */ if( (SCIPisInfinity(scip, -SCIProwGetLhs(cut)) && SCIPisInfinity(scip, SCIProwGetRhs(cut))) ) success = FALSE; /* @todo Trying to make the Gomory cut integral might fail. Due to numerical reasons/arguments we * currently ignore such cuts. If the cut, however, has small support (let's say smaller or equal to * 5), we might want to add that cut (even it does not have integral coefficients). To be able to * do that we need to add a rank to the data structure of a row. The rank of original rows are * zero and for aggregated rows it is the maximum over all used rows plus one. */ } if( success ) { SCIPdebugMessage(" -> found gomory cut <%s>: act=%f, rhs=%f, norm=%f, eff=%f, min=%f, max=%f (range=%f)\n", cutname, SCIPgetRowLPActivity(scip, cut), SCIProwGetRhs(cut), SCIProwGetNorm(cut), SCIPgetCutEfficacy(scip, NULL, cut), SCIPgetRowMinCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut)/SCIPgetRowMinCoef(scip, cut)); /* flush all changes before adding the cut */ SCIP_CALL( SCIPflushRowExtensions(scip, cut) ); /* add global cuts which are not implicit bound changes to the cut pool */ if( !cutislocal ) { if( sepadata->delayedcuts ) { SCIP_CALL( SCIPaddDelayedPoolCut(scip, cut) ); } else { SCIP_CALL( SCIPaddPoolCut(scip, cut) ); } } else { /* local cuts we add to the sepastore */ SCIP_CALL( SCIPaddCut(scip, NULL, cut, FALSE) ); } naddedcuts++; } } } /* release the row */ SCIP_CALL( SCIPreleaseRow(scip, &cut) ); } } } /* free temporary memory */ SCIPfreeBufferArray(scip, &binvrow); SCIPfreeBufferArray(scip, &basisind); SCIPfreeBufferArray(scip, &cutcoefs); SCIPdebugMessage("end searching gomory cuts: found %d cuts\n", naddedcuts); sepadata->lastncutsfound = SCIPgetNCutsFound(scip); /* evalute the result of the separation */ if( cutoff ) *result = SCIP_CUTOFF; else if ( naddedcuts > 0 ) *result = SCIP_SEPARATED; else *result = SCIP_DIDNOTFIND; return SCIP_OKAY; }
/** main procedure of the RENS heuristic, creates and solves a subMIP */ SCIP_RETCODE SCIPapplyGcgrens( SCIP* scip, /**< original SCIP data structure */ SCIP_HEUR* heur, /**< heuristic data structure */ SCIP_RESULT* result, /**< result data structure */ SCIP_Real minfixingrate, /**< minimum percentage of integer variables that have to be fixed */ SCIP_Real minimprove, /**< factor by which RENS should at least improve the incumbent */ SCIP_Longint maxnodes, /**< maximum number of nodes for the subproblem */ SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */ SCIP_Bool binarybounds, /**< should general integers get binary bounds [floor(.),ceil(.)]? */ SCIP_Bool uselprows /**< should subproblem be created out of the rows in the LP rows? */ ) { SCIP* subscip; /* the subproblem created by RENS */ SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */ SCIP_VAR** vars; /* original problem's variables */ SCIP_VAR** subvars; /* subproblem's variables */ SCIP_Real cutoff; /* objective cutoff for the subproblem */ SCIP_Real timelimit; SCIP_Real memorylimit; int nvars; int i; SCIP_Bool success; SCIP_RETCODE retcode; assert(scip != NULL); assert(heur != NULL); assert(result != NULL); assert(maxnodes >= 0); assert(nstallnodes >= 0); assert(0.0 <= minfixingrate && minfixingrate <= 1.0); assert(0.0 <= minimprove && minimprove <= 1.0); SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) ); /* initialize the subproblem */ SCIP_CALL( SCIPcreate(&subscip) ); /* create the variable mapping hash map */ SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) ); SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) ); if( uselprows ) { char probname[SCIP_MAXSTRLEN]; /* copy all plugins */ SCIP_CALL( SCIPincludeDefaultPlugins(subscip) ); /* get name of the original problem and add the string "_gcgrenssub" */ (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_gcgrenssub", SCIPgetProbName(scip)); /* create the subproblem */ SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ); /* copy all variables */ SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) ); } else { SCIP_Bool valid; SCIP_HEURDATA* heurdata; valid = FALSE; SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "gcgrens", TRUE, FALSE, TRUE, &valid) ); /** @todo check for thread safeness */ /* get heuristic's data */ heurdata = SCIPheurGetData(heur); assert( heurdata != NULL ); if( heurdata->copycuts ) { /** copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */ SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) ); } SCIPdebugMessage("Copying the SCIP instance was %s complete.\n", valid ? "" : "not "); } for( i = 0; i < nvars; i++ ) subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]); /* free hash map */ SCIPhashmapFree(&varmapfw); /* create a new problem, which fixes variables with same value in bestsol and LP relaxation */ SCIP_CALL( createSubproblem(scip, subscip, subvars, minfixingrate, binarybounds, uselprows, &success) ); SCIPdebugMessage("RENS subproblem: %d vars, %d cons, success=%u\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip), success); /* do not abort subproblem on CTRL-C */ SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) ); /* disable output to console */ SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) ); /* check whether there is enough time and memory left */ timelimit = 0.0; memorylimit = 0.0; SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) ); if( !SCIPisInfinity(scip, timelimit) ) timelimit -= SCIPgetSolvingTime(scip); SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) ); if( !SCIPisInfinity(scip, memorylimit) ) memorylimit -= SCIPgetMemUsed(scip)/1048576.0; if( timelimit <= 0.0 || memorylimit <= 0.0 ) goto TERMINATE; /* set limits for the subproblem */ SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", nstallnodes) ); SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", maxnodes) ); SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) ); SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) ); /* forbid recursive call of heuristics and separators solving sub-SCIPs */ SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) ); /* disable cutting plane separation */ SCIP_CALL( SCIPsetSeparating(subscip, SCIP_PARAMSETTING_OFF, TRUE) ); /* disable expensive presolving */ SCIP_CALL( SCIPsetPresolving(subscip, SCIP_PARAMSETTING_FAST, TRUE) ); /* use best estimate node selection */ if( SCIPfindNodesel(scip, "estimate") != NULL ) { SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) ); } /* use inference branching */ if( SCIPfindBranchrule(scip, "inference") != NULL ) { SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) ); } /* disable conflict analysis */ SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) ); SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) ); SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) ); SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) ); SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) ); #ifdef SCIP_DEBUG /* for debugging RENS, enable MIP output */ SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) ); SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) ); #endif /* if the subproblem could not be created, free memory and return */ if( !success ) { *result = SCIP_DIDNOTRUN; SCIPfreeBufferArray(scip, &subvars); SCIP_CALL( SCIPfree(&subscip) ); return SCIP_OKAY; } /* if there is already a solution, add an objective cutoff */ if( SCIPgetNSols(scip) > 0 ) { SCIP_Real upperbound; assert( !SCIPisInfinity(scip,SCIPgetUpperbound(scip)) ); upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip); if( !SCIPisInfinity(scip,-1.0*SCIPgetLowerbound(scip)) ) { cutoff = (1-minimprove)*SCIPgetUpperbound(scip) + minimprove*SCIPgetLowerbound(scip); } else { if( SCIPgetUpperbound ( scip ) >= 0 ) cutoff = ( 1 - minimprove ) * SCIPgetUpperbound ( scip ); else cutoff = ( 1 + minimprove ) * SCIPgetUpperbound ( scip ); } cutoff = MIN(upperbound, cutoff); SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) ); } /* presolve the subproblem */ retcode = SCIPpresolve(subscip); /* Errors in solving the subproblem should not kill the overall solving process * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop. */ if( retcode != SCIP_OKAY ) { #ifndef NDEBUG SCIP_CALL( retcode ); #endif SCIPwarningMessage(scip, "Error while presolving subproblem in GCG RENS heuristic; sub-SCIP terminated with code <%d>\n",retcode); } SCIPdebugMessage("GCG RENS presolved subproblem: %d vars, %d cons, success=%u\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip), success); /* after presolving, we should have at least reached a certain fixing rate over ALL variables (including continuous) * to ensure that not only the MIP but also the LP relaxation is easy enough */ if( ( nvars - SCIPgetNVars(subscip) ) / (SCIP_Real)nvars >= minfixingrate / 2.0 ) { SCIP_SOL** subsols; int nsubsols; /* solve the subproblem */ SCIPdebugMessage("solving subproblem: nstallnodes=%"SCIP_LONGINT_FORMAT", maxnodes=%"SCIP_LONGINT_FORMAT"\n", nstallnodes, maxnodes); retcode = SCIPsolve(subscip); /* Errors in solving the subproblem should not kill the overall solving process * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop. */ if( retcode != SCIP_OKAY ) { #ifndef NDEBUG SCIP_CALL( retcode ); #endif SCIPwarningMessage(scip, "Error while solving subproblem in GCG RENS heuristic; sub-SCIP terminated with code <%d>\n",retcode); } /* check, whether a solution was found; * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted */ nsubsols = SCIPgetNSols(subscip); subsols = SCIPgetSols(subscip); success = FALSE; for( i = 0; i < nsubsols && !success; ++i ) { SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) ); } if( success ) *result = SCIP_FOUNDSOL; } TERMINATE: /* free subproblem */ SCIPfreeBufferArray(scip, &subvars); SCIP_CALL( SCIPfree(&subscip) ); return SCIP_OKAY; }
int GamsScip::callSolver() { assert(gmo != NULL); assert(gev != NULL); assert(scip != NULL); /* set interface type so we see =B= and =X= equations */ gmoInterfaceSet(gmo, gmoIFace_Raw); if( gmoGetEquTypeCnt(gmo, gmoequ_C) || gmoGetEquTypeCnt(gmo, gmoequ_B) || gmoGetEquTypeCnt(gmo, gmoequ_X) ) { gevLogStat(gev, "ERROR: Conic and logic constraints and external functions not supported by SCIP interface.\n"); gmoSolveStatSet(gmo, gmoSolveStat_Capability); gmoModelStatSet(gmo, gmoModelStat_NoSolutionReturned); return 1; } // set number of threads for linear algebra routines used in Ipopt setNumThreads(gev, gevThreads(gev)); // update error printing callback in SCIP to use current gev SCIPmessageSetErrorPrinting(printErrorGev, (void*)gev); SCIP_RETCODE scipret; // let GMO reader setup SCIP parameters and read options file // do this here already so we know how to assemble dialog scipret = SCIPreadParamsReaderGmo(scip); if( scipret != SCIP_OKAY ) { char buffer[256]; sprintf(buffer, "Error %d in call of SCIP function\n", scipret); gevLogStatPChar(gev, buffer); gmoSolveStatSet(gmo, gmoSolveStat_SystemErr); gmoModelStatSet(gmo, gmoModelStat_ErrorNoSolution); return 1; } SCIPinfoMessage(scip, NULL, "non-default parameter settings:\n"); SCIPwriteParams(scip, NULL, FALSE, TRUE); char* interactive = NULL; SCIP_CALL_ABORT( SCIPgetStringParam(scip, "gams/interactive", &interactive) ); assert(interactive != NULL); #ifdef GAMS_BUILD if( interactive[0] != '\0' && !palLicenseIsAcademic(pal) ) { gevLogStat(gev, "SCIP interactive shell not available in demo mode.\n"); interactive[0] = '\0'; } #endif SCIP_Bool printstat; SCIP_CALL_ABORT( SCIPgetBoolParam(scip, "display/statistics", &printstat) ); char* attrfile = NULL; #if 0 SCIP_CALL( SCIPgetStringParam(scip, "constraints/attrfile", &attrfile) ); #endif // setup commands to be executed by SCIP SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, "readgams") ); // setup model if( attrfile != NULL && *attrfile != '\0' ) { char buffer[SCIP_MAXSTRLEN + 10]; size_t len; len = strlen(attrfile); if( len >= 3 && strcmp(&attrfile[len-3], ".ca") == 0 ) (void) SCIPsnprintf(buffer, sizeof(buffer), "read %g", attrfile); else (void) SCIPsnprintf(buffer, sizeof(buffer), "read %g ca", attrfile); SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, buffer) ); // process constraints attribute file } if( interactive[0] == '\0' ) { SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, "optimize") ); // solve model if( printstat ) { SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, "disp statistics") ); // display solution statistics } SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, "write gamssol") ); // pass solution to GMO SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, "quit") ); // quit shell } else { // pass user commands to shell SCIP_CALL_ABORT( SCIPaddDialogInputLine(scip, interactive) ); } // run SCIP scipret = SCIPstartInteraction(scip); // evaluate SCIP return code switch( scipret ) { case SCIP_OKAY: break; case SCIP_READERROR: /* if it's readerror, then we guess that it comes from encountering an unsupported gams instruction in the gmo readers makeExprtree method * we still return with zero then */ gmoModelStatSet(gmo, gmoModelStat_NoSolutionReturned); gmoSolveStatSet(gmo, gmoSolveStat_Capability); break; case SCIP_LPERROR: case SCIP_MAXDEPTHLEVEL: /* if SCIP failed due to internal error (forced LP solve failed, max depth level reached), also return zero */ gmoModelStatSet(gmo, gmoModelStat_ErrorNoSolution); gmoSolveStatSet(gmo, gmoSolveStat_SolverErr); break; case SCIP_NOMEMORY: /* there is no extra solver status for running out of memory, but memory is a resource, so return this */ gmoModelStatSet(gmo, gmoModelStat_ErrorNoSolution); gmoSolveStatSet(gmo, gmoSolveStat_Resource); break; default: { char buffer[256]; sprintf(buffer, "Error %d in call of SCIP function\n", scipret); gevLogStatPChar(gev, buffer); gmoModelStatSet(gmo, gmoModelStat_ErrorNoSolution); gmoSolveStatSet(gmo, gmoSolveStat_SystemErr); return 1; } } return 0; }
/** * @brief translates a SCIP_RETCODE into an error string * * @param[in] retcode SCIP_RETCODE you want to translate * @param[out] buffer_str buffer to character array to store translated message, this must be at least of size \ref SCIP_MSG_MAX * @return buffer_str or NULL, if retcode could not be translated */ inline char* SCIPgetErrorString(SCIP_RETCODE retcode, char* buffer_str, int buffersize) { // the following was copied from SCIPprintError switch(retcode) { case SCIP_OKAY: SCIPsnprintf(buffer_str, buffersize, "normal termination"); return buffer_str; case SCIP_ERROR: SCIPsnprintf(buffer_str, buffersize, "unspecified error"); return buffer_str; case SCIP_NOMEMORY: SCIPsnprintf(buffer_str, buffersize, "insufficient memory error"); return buffer_str; case SCIP_READERROR: SCIPsnprintf(buffer_str, buffersize, "file read error"); return buffer_str; case SCIP_WRITEERROR: SCIPsnprintf(buffer_str, buffersize, "file write error"); return buffer_str; case SCIP_NOFILE: SCIPsnprintf(buffer_str, buffersize, "file not found error"); return buffer_str; case SCIP_FILECREATEERROR: SCIPsnprintf(buffer_str, buffersize, "cannot create file"); return buffer_str; case SCIP_LPERROR: SCIPsnprintf(buffer_str, buffersize, "error in LP solver"); return buffer_str; case SCIP_NOPROBLEM: SCIPsnprintf(buffer_str, buffersize, "no problem exists"); return buffer_str; case SCIP_INVALIDCALL: SCIPsnprintf(buffer_str, buffersize, "method cannot be called at this time in solution process"); return buffer_str; case SCIP_INVALIDDATA: SCIPsnprintf(buffer_str, buffersize, "method cannot be called with this type of data"); return buffer_str; case SCIP_INVALIDRESULT: SCIPsnprintf(buffer_str, buffersize, "method returned an invalid result code"); return buffer_str; case SCIP_PLUGINNOTFOUND: SCIPsnprintf(buffer_str, buffersize, "a required plugin was not found"); return buffer_str; case SCIP_PARAMETERUNKNOWN: SCIPsnprintf(buffer_str, buffersize, "the parameter with the given name was not found"); return buffer_str; case SCIP_PARAMETERWRONGTYPE: SCIPsnprintf(buffer_str, buffersize, "the parameter is not of the expected type"); return buffer_str; case SCIP_PARAMETERWRONGVAL: SCIPsnprintf(buffer_str, buffersize, "the value is invalid for the given parameter"); return buffer_str; case SCIP_KEYALREADYEXISTING: SCIPsnprintf(buffer_str, buffersize, "the given key is already existing in table"); return buffer_str; case SCIP_MAXDEPTHLEVEL: SCIPsnprintf(buffer_str, buffersize, "maximal branching depth level exceeded"); return buffer_str; default: return NULL; } }
/** @brief constructs a SCIPEexception from an error code * * this constructs a new SCIPException from given error code * @param[in] retcode SCIP error code */ SCIPException(SCIP_RETCODE retcode) { if(SCIPgetErrorString(retcode, _msg, SCIP_MSG_MAX)==NULL) SCIPsnprintf(_msg, SCIP_MSG_MAX, "unknown SCIP retcode %d",retcode); }
/** read fixed variable */ static SCIP_RETCODE getFixedVariable( SCIP* scip, /**< SCIP data structure */ CIPINPUT* cipinput /**< CIP parsing data */ ) { SCIP_Bool success; SCIP_VAR* var; char* buf; char* endptr; char name[SCIP_MAXSTRLEN]; buf = cipinput->strbuf; if( strncmp(buf, "CONSTRAINTS", 11) == 0 ) cipinput->section = CIP_CONSTRAINTS; else if( strncmp(buf, "END", 3) == 0 ) cipinput->section = CIP_END; if( cipinput->section != CIP_FIXEDVARS ) return SCIP_OKAY; SCIPdebugMessage("parse fixed variable\n"); /* parse the variable */ SCIP_CALL( SCIPparseVar(scip, &var, buf, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL, &endptr, &success) ); if( !success ) { SCIPerrorMessage("syntax error in variable information (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf); cipinput->haserror = TRUE; return SCIP_OKAY; } /* skip intermediate stuff */ buf = endptr; while ( *buf != '\0' && (*buf == ' ' || *buf == ',') ) ++buf; /* check whether variable is fixed */ if ( strncmp(buf, "fixed:", 6) == 0 ) { SCIP_CALL( SCIPaddVar(scip, var) ); SCIPdebug( SCIP_CALL( SCIPprintVar(scip, var, NULL) ) ); } else if ( strncmp(buf, "negated:", 8) == 0 ) { SCIP_CONS* lincons; SCIP_VAR* negvar; SCIP_Real vals[2]; SCIP_VAR* vars[2]; buf += 8; /* we can just parse the next variable (ignoring all other information in between) */ SCIP_CALL( SCIPparseVarName(scip, buf, &negvar, &endptr) ); if ( negvar == NULL ) { SCIPerrorMessage("could not parse negated variable (line: %d):\n%s\n", cipinput->linenumber, cipinput->strbuf); cipinput->haserror = TRUE; return SCIP_OKAY; } assert(SCIPvarIsBinary(var)); assert(SCIPvarIsBinary(negvar)); SCIP_CALL( SCIPaddVar(scip, var) ); SCIPdebugMessage("creating negated variable <%s> (of <%s>) ...\n", SCIPvarGetName(var), SCIPvarGetName(negvar) ); SCIPdebug( SCIP_CALL( SCIPprintVar(scip, var, NULL) ) ); /* add linear constraint for negation */ (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "neg_%s", SCIPvarGetName(var) ); vars[0] = var; vars[1] = negvar; vals[0] = 1.0; vals[1] = 1.0; SCIPdebugMessage("coupling constraint:\n"); SCIP_CALL( SCIPcreateConsLinear(scip, &lincons, name, 2, vars, vals, 1.0, 1.0, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) ); SCIPdebugPrintCons(scip, lincons, NULL); SCIP_CALL( SCIPaddCons(scip, lincons) ); SCIP_CALL( SCIPreleaseCons(scip, &lincons) ); } else if ( strncmp(buf, "aggregated:", 11) == 0 ) { /* handle (multi-)aggregated variables */ SCIP_CONS* lincons; SCIP_Real* vals; SCIP_VAR** vars; SCIP_Real rhs = 0.0; const char* str; int nvarssize = 20; int requsize; int nvars; buf += 11; SCIPdebugMessage("parsing aggregated variable <%s> ...\n", SCIPvarGetName(var)); /* first parse constant */ if ( ! SCIPstrToRealValue(buf, &rhs, &endptr) ) { SCIPerrorMessage("expected constant when aggregated variable information (line: %d):\n%s\n", cipinput->linenumber, buf); cipinput->haserror = TRUE; return SCIP_OKAY; } /* check whether constant is 0.0 */ str = endptr; while ( *str != '\0' && isspace(*str) ) ++str; /* if next char is '<' we found a variable -> constant is 0 */ if ( *str != '<' ) { SCIPdebugMessage("constant: %f\n", rhs); buf = endptr; } else { /* otherwise keep buf */ rhs = 0.0; } /* initialize buffers for storing the variables and values */ SCIP_CALL( SCIPallocBufferArray(scip, &vars, nvarssize) ); SCIP_CALL( SCIPallocBufferArray(scip, &vals, nvarssize) ); vars[0] = var; vals[0] = -1.0; --nvarssize; /* parse linear sum to get variables and coefficients */ SCIP_CALL( SCIPparseVarsLinearsum(scip, buf, &(vars[1]), &(vals[1]), &nvars, nvarssize, &requsize, &endptr, &success) ); if ( success && requsize > nvarssize ) { /* realloc buffers and try again */ nvarssize = requsize; SCIP_CALL( SCIPreallocBufferArray(scip, &vars, nvarssize + 1) ); SCIP_CALL( SCIPreallocBufferArray(scip, &vals, nvarssize + 1) ); SCIP_CALL( SCIPparseVarsLinearsum(scip, buf, &(vars[1]), &(vals[1]), &nvars, nvarssize, &requsize, &endptr, &success) ); assert( ! success || requsize <= nvarssize); /* if successful, then should have had enough space now */ } if( success ) { /* add aggregated variable */ SCIP_CALL( SCIPaddVar(scip, var) ); /* special handling of variables that seem to be slack variables of indicator constraints */ str = SCIPvarGetName(var); if ( strncmp(str, "indslack", 8) == 0 ) { (void) strcpy(name, "indlin"); (void) strncat(name, str+8, SCIP_MAXSTRLEN-7); } else if ( strncmp(str, "t_indslack", 10) == 0 ) { (void) strcpy(name, "indlin"); (void) strncat(name, str+10, SCIP_MAXSTRLEN-7); } else (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s", SCIPvarGetName(var) ); /* add linear constraint for (multi-)aggregation */ SCIPdebugMessage("coupling constraint:\n"); SCIP_CALL( SCIPcreateConsLinear(scip, &lincons, name, nvars + 1, vars, vals, -rhs, -rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) ); SCIPdebugPrintCons(scip, lincons, NULL); SCIP_CALL( SCIPaddCons(scip, lincons) ); SCIP_CALL( SCIPreleaseCons(scip, &lincons) ); } else { SCIPwarningMessage(scip, "Could not read (multi-)aggregated variable <%s>: dependent variables unkown - consider changing the order (line: %d):\n%s\n", SCIPvarGetName(var), cipinput->linenumber, buf); } SCIPfreeBufferArray(scip, &vals); SCIPfreeBufferArray(scip, &vars); } else { SCIPerrorMessage("unknown section when parsing variables (line: %d):\n%s\n", cipinput->linenumber, buf); cipinput->haserror = TRUE; return SCIP_OKAY; } SCIP_CALL( SCIPreleaseVar(scip, &var) ); return SCIP_OKAY; }
/** creates a display column */ SCIP_RETCODE SCIPdispCreate( SCIP_DISP** disp, /**< pointer to store display column */ SCIP_SET* set, /**< global SCIP settings */ SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of display column */ const char* desc, /**< description of display column */ const char* header, /**< head line of display column */ SCIP_DISPSTATUS dispstatus, /**< display activation status of display column */ SCIP_DECL_DISPCOPY ((*dispcopy)), /**< copy method of display column or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_DISPFREE ((*dispfree)), /**< destructor of display column */ SCIP_DECL_DISPINIT ((*dispinit)), /**< initialize display column */ SCIP_DECL_DISPEXIT ((*dispexit)), /**< deinitialize display column */ SCIP_DECL_DISPINITSOL ((*dispinitsol)), /**< solving process initialization method of display column */ SCIP_DECL_DISPEXITSOL ((*dispexitsol)), /**< solving process deinitialization method of display column */ SCIP_DECL_DISPOUTPUT ((*dispoutput)), /**< output method */ SCIP_DISPDATA* dispdata, /**< display column data */ int width, /**< width of display column (no. of chars used) */ int priority, /**< priority of display column */ int position, /**< relative position of display column */ SCIP_Bool stripline /**< should the column be separated with a line from its right neighbor? */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(disp != NULL); assert(name != NULL); assert(desc != NULL); assert(header != NULL); assert(dispoutput != NULL); assert(width >= 0); SCIP_ALLOC( BMSallocMemory(disp) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*disp)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*disp)->desc, desc, strlen(desc)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*disp)->header, header, strlen(header)+1) ); (*disp)->dispstatus = dispstatus; (*disp)->dispcopy = dispcopy; (*disp)->dispfree = dispfree; (*disp)->dispinit = dispinit; (*disp)->dispexit = dispexit; (*disp)->dispinitsol = dispinitsol; (*disp)->dispexitsol = dispexitsol; (*disp)->dispoutput = dispoutput; (*disp)->dispdata = dispdata; (*disp)->width = width; (*disp)->priority = priority; (*disp)->position = position; (*disp)->stripline = stripline; (*disp)->initialized = FALSE; (*disp)->active = (dispstatus == SCIP_DISPSTATUS_ON); /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "display/%s/active", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "display activation status of display column <%s> (0: off, 1: auto, 2:on)", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, (int*)(&(*disp)->dispstatus), FALSE, (int)dispstatus, 0, 2, SCIPparamChgdDispActive, NULL) ); return SCIP_OKAY; }
/** creates a presolver */ SCIP_RETCODE SCIPpresolCreate( SCIP_PRESOL** presol, /**< pointer to store presolver */ SCIP_SET* set, /**< global SCIP settings */ SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */ BMS_BLKMEM* blkmem, /**< block memory for parameter settings */ const char* name, /**< name of presolver */ const char* desc, /**< description of presolver */ int priority, /**< priority of the presolver (>= 0: before, < 0: after constraint handlers) */ int maxrounds, /**< maximal number of presolving rounds the presolver participates in (-1: no limit) */ SCIP_Bool delay, /**< should presolver be delayed, if other presolvers found reductions? */ SCIP_DECL_PRESOLCOPY ((*presolcopy)), /**< copy method of presolver or NULL if you don't want to copy your plugin into sub-SCIPs */ SCIP_DECL_PRESOLFREE ((*presolfree)), /**< destructor of presolver to free user data (called when SCIP is exiting) */ SCIP_DECL_PRESOLINIT ((*presolinit)), /**< initialization method of presolver (called after problem was transformed) */ SCIP_DECL_PRESOLEXIT ((*presolexit)), /**< deinitialization method of presolver (called before transformed problem is freed) */ SCIP_DECL_PRESOLINITPRE((*presolinitpre)),/**< presolving initialization method of presolver (called when presolving is about to begin) */ SCIP_DECL_PRESOLEXITPRE((*presolexitpre)),/**< presolving deinitialization method of presolver (called after presolving has been finished) */ SCIP_DECL_PRESOLEXEC ((*presolexec)), /**< execution method of presolver */ SCIP_PRESOLDATA* presoldata /**< presolver data */ ) { char paramname[SCIP_MAXSTRLEN]; char paramdesc[SCIP_MAXSTRLEN]; assert(presol != NULL); assert(name != NULL); assert(desc != NULL); SCIP_ALLOC( BMSallocMemory(presol) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*presol)->name, name, strlen(name)+1) ); SCIP_ALLOC( BMSduplicateMemoryArray(&(*presol)->desc, desc, strlen(desc)+1) ); (*presol)->presolcopy = presolcopy; (*presol)->presolfree = presolfree; (*presol)->presolinit = presolinit; (*presol)->presolexit = presolexit; (*presol)->presolinitpre = presolinitpre; (*presol)->presolexitpre = presolexitpre; (*presol)->presolexec = presolexec; (*presol)->presoldata = presoldata; SCIP_CALL( SCIPclockCreate(&(*presol)->setuptime, SCIP_CLOCKTYPE_DEFAULT) ); SCIP_CALL( SCIPclockCreate(&(*presol)->presolclock, SCIP_CLOCKTYPE_DEFAULT) ); (*presol)->wasdelayed = FALSE; (*presol)->initialized = FALSE; /* add parameters */ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "presolving/%s/priority", name); (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of presolver <%s>", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc, &(*presol)->priority, TRUE, priority, INT_MIN/4, INT_MAX/4, paramChgdPresolPriority, (SCIP_PARAMDATA*)(*presol)) ); /*lint !e740*/ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "presolving/%s/maxrounds", name); SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, "maximal number of presolving rounds the presolver participates in (-1: no limit)", &(*presol)->maxrounds, FALSE, maxrounds, -1, INT_MAX, NULL, NULL) ); /*lint !e740*/ (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "presolving/%s/delay", name); SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname, "should presolver be delayed, if other presolvers found reductions?", &(*presol)->delay, TRUE, delay, NULL, NULL) ); /*lint !e740*/ return SCIP_OKAY; }
/** presolving execution method */ static SCIP_DECL_PRESOLEXEC(presolExecInttobinary) { /*lint --e{715}*/ SCIP_VAR** scipvars; SCIP_VAR** vars; int nbinvars; int nintvars; int v; assert(result != NULL); *result = SCIP_DIDNOTRUN; if( SCIPdoNotAggr(scip) ) return SCIP_OKAY; /* get the problem variables */ scipvars = SCIPgetVars(scip); nbinvars = SCIPgetNBinVars(scip); nintvars = SCIPgetNIntVars(scip); if( nintvars == 0 ) return SCIP_OKAY; *result = SCIP_DIDNOTFIND; /* copy the integer variables into an own array, since adding binary variables affects the left-most slots in the * array and thereby interferes with our search loop */ SCIP_CALL( SCIPduplicateBufferArray(scip, &vars, &scipvars[nbinvars], nintvars) ); /* scan the integer variables for possible conversion into binaries; * we have to collect the variables first in an own */ for( v = 0; v < nintvars; ++v ) { SCIP_Real lb; SCIP_Real ub; assert(SCIPvarGetType(vars[v]) == SCIP_VARTYPE_INTEGER); /* get variable's bounds */ lb = SCIPvarGetLbGlobal(vars[v]); ub = SCIPvarGetUbGlobal(vars[v]); /* check if bounds are exactly one apart */ if( SCIPisEQ(scip, lb, ub - 1.0) ) { SCIP_VAR* binvar; char binvarname[SCIP_MAXSTRLEN]; SCIP_Bool infeasible; SCIP_Bool redundant; SCIP_Bool aggregated; SCIPdebugMessage("converting <%s>[%g,%g] into binary variable\n", SCIPvarGetName(vars[v]), lb, ub); /* create binary variable */ (void) SCIPsnprintf(binvarname, SCIP_MAXSTRLEN, "%s_bin", SCIPvarGetName(vars[v])); SCIP_CALL( SCIPcreateVar(scip, &binvar, binvarname, 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY, SCIPvarIsInitial(vars[v]), SCIPvarIsRemovable(vars[v]), NULL, NULL, NULL, NULL, NULL) ); SCIP_CALL( SCIPaddVar(scip, binvar) ); /* aggregate integer and binary variable */ SCIP_CALL( SCIPaggregateVars(scip, vars[v], binvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) ); /* release binary variable */ SCIP_CALL( SCIPreleaseVar(scip, &binvar) ); /* it can be the case that this aggregation detects an infeasibility; for example, during the copy of the * variable bounds from the integer variable to the binary variable, infeasibility can be detected; this can * happen because an upper bound or a lower bound of such a variable bound variable was "just" changed and the * varbound constraint handler, who would detect that infeasibility (since it was creating it from a varbound * constraint), was called before that bound change was detected due to the presolving priorities; */ if( infeasible ) { *result = SCIP_CUTOFF; break; } assert(redundant); assert(aggregated); (*nchgvartypes)++; *result = SCIP_SUCCESS; } } /* free temporary memory */ SCIPfreeBufferArray(scip, &vars); return SCIP_OKAY; }
/* standard "main" method for mex interface */ void mexFunction( int nlhs, /* number of expected outputs */ mxArray* plhs[], /* array of pointers to output arguments */ int nrhs, /* number of inputs */ const mxArray* prhs[] /* array of pointers to input arguments */ ) { SCIP* scip; SCIP_VAR** vars; SCIP_Real* objs; SCIP_Real* lhss; SCIP_Real* rhss; SCIP_Real* lbs; SCIP_Real* ubs; SCIP_Real* matrix; SCIP_Real* bestsol; SCIP_Real* objval; char* vartypes; char objsense[SCIP_MAXSTRLEN]; int nvars; int nconss; int stringsize; int i; if( SCIPmajorVersion() < 2 ) { mexErrMsgTxt("SCIP versions less than 2.0 are not supported\n"); return; } /* initialize SCIP */ SCIP_CALL_ABORT( SCIPcreate(&scip) ); /* output SCIP information */ SCIPprintVersion(scip, NULL); /* include default SCIP plugins */ SCIP_CALL_ABORT( SCIPincludeDefaultPlugins(scip) ); if( nlhs != 2 || nrhs != 8 ) mexErrMsgTxt("invalid number of parameters. Call as [bestsol, objval] = matscip(matrix, lhs, rhs, obj, lb, ub, vartype, objsense)\n"); if( mxIsSparse(prhs[0]) ) mexErrMsgTxt("sparse matrices are not supported yet"); /* ???????? of course this has to change */ /* get linear constraint coefficient matrix */ matrix = mxGetPr(prhs[0]); if( matrix == NULL ) mexErrMsgTxt("matrix must not be NULL"); if( mxGetNumberOfDimensions(prhs[0]) != 2 ) mexErrMsgTxt("matrix must have exactly two dimensions"); /* get dimensions of matrix */ nconss = mxGetM(prhs[0]); nvars = mxGetN(prhs[0]); assert(nconss > 0); assert(nvars > 0); /* get left hand sides of linear constraints */ lhss = mxGetPr(prhs[1]); if( mxGetM(prhs[1]) != nconss ) mexErrMsgTxt("dimension of left hand side vector does not match matrix dimension"); assert(lhss != NULL); /* get right hand sides of linear constraints */ rhss = mxGetPr(prhs[2]); if( mxGetM(prhs[2]) != nconss ) mexErrMsgTxt("dimension of right hand side vector does not match matrix dimension"); assert(rhss != NULL); /* get objective coefficients */ objs = mxGetPr(prhs[3]); if( mxGetM(prhs[3]) != nvars ) mexErrMsgTxt("dimension of objective coefficient vector does not match matrix dimension"); /* get lower bounds of variables */ lbs = mxGetPr(prhs[4]); if( mxGetM(prhs[4]) != nvars ) mexErrMsgTxt("dimension of lower bound vector does not match matrix dimension"); /* get upper bounds of variables */ ubs = mxGetPr(prhs[5]); if( mxGetM(prhs[5]) != nvars ) mexErrMsgTxt("dimension of upper bound vector does not match matrix dimension"); /* allocate memory for variable type characters */ SCIP_CALL_ABORT( SCIPallocMemoryArray(scip, &vartypes, nvars+1) ); /* get variable types */ if( mxGetString(prhs[6], vartypes, nvars+1) != 0 ) mexErrMsgTxt("Error when parsing variable types, maybe a wrong vector dimension?"); /* get objective sense */ stringsize = mxGetNumberOfElements(prhs[7]); if( stringsize != 3 ) mexErrMsgTxt("objective sense must be a three character word: \"max\" or \"min\""); if( mxGetString(prhs[7], objsense, stringsize+1) != 0) mexErrMsgTxt("Error when parsing objective sense string"); if( strcmp(objsense,"max") != 0 && strcmp(objsense,"min") != 0 ) mexErrMsgTxt("objective sense must be either \"max\" or \"min\""); /* get output parameters */ plhs[0] = mxCreateDoubleMatrix(nvars, 1, mxREAL); bestsol = mxGetPr(plhs[0]); plhs[1] = mxCreateDoubleScalar(mxREAL); objval = mxGetPr(plhs[1]); /* create SCIP problem */ SCIP_CALL_ABORT( SCIPcreateProb(scip, "mex_prob", NULL, NULL, NULL, NULL, NULL, NULL, NULL) ); /* allocate memory for variable array */ SCIP_CALL_ABORT( SCIPallocMemoryArray(scip, &vars, nvars) ); /* create variables */ for( i = 0; i < nvars; ++i) { SCIP_VARTYPE vartype; char varname[SCIP_MAXSTRLEN]; /* convert vartype character to SCIP vartype */ if( vartypes[i] == 'i' ) vartype = SCIP_VARTYPE_INTEGER; else if( vartypes[i] == 'b' ) vartype = SCIP_VARTYPE_BINARY; else if( vartypes[i] == 'c' ) vartype = SCIP_VARTYPE_CONTINUOUS; else mexErrMsgTxt("unkown variable type"); /* variables get canonic names x_i */ (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "x_%d", i); /* create variable object and add it to SCIP */ SCIP_CALL_ABORT( SCIPcreateVar(scip, &vars[i], varname, lbs[i], ubs[i], objs[i], vartype, TRUE, FALSE, NULL, NULL, NULL, NULL, NULL) ); assert(vars[i] != NULL); SCIP_CALL_ABORT( SCIPaddVar(scip, vars[i]) ); } /* create linear constraints */ for( i = 0; i < nconss; ++i ) { SCIP_CONS* cons; char consname[SCIP_MAXSTRLEN]; int j; /* constraints get canonic names cons_i */ (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "cons_%d", i); /* create empty linear constraint */ SCIP_CALL_ABORT( SCIPcreateConsLinear(scip, &cons, consname, 0, NULL, NULL, lhss[i], rhss[i], TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) ); /* add non-zero coefficients to linear constraint */ for( j = 0; j < nvars; ++j ) { if( !SCIPisFeasZero(scip, matrix[i+j*nconss]) ) { SCIP_CALL_ABORT( SCIPaddCoefLinear(scip, cons, vars[j], matrix[i+j*nconss]) ); } } /* add constraint to SCIP and release it */ SCIP_CALL_ABORT( SCIPaddCons(scip, cons) ); SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) ); } /* set objective sense in SCIP */ if( strcmp(objsense,"max") == 0) { SCIP_CALL_ABORT( SCIPsetObjsense(scip, SCIP_OBJSENSE_MAXIMIZE) ); } else if( strcmp(objsense,"min") == 0) { SCIP_CALL_ABORT( SCIPsetObjsense(scip, SCIP_OBJSENSE_MINIMIZE) ); } else /* this should have been caught earlier when parsing objsense */ mexErrMsgTxt("unkown objective sense"); /* solve SCIP problem */ SCIP_CALL_ABORT( SCIPsolve(scip) ); /* if SCIP found a solution, pass it back into MATLAB output parameters */ if( SCIPgetNSols > 0 ) { SCIP_SOL* scipbestsol; /* get incumbent solution vector */ scipbestsol = SCIPgetBestSol(scip); assert(scipbestsol != NULL); /* get objective value of incumbent solution */ *objval = SCIPgetSolOrigObj(scip, scipbestsol); assert(!SCIPisInfinity(scip, REALABS(*objval))); /* copy solution values into output vector */ for( i = 0; i < nvars; ++i ) bestsol[i] = SCIPgetSolVal(scip,scipbestsol,vars[i]); } /* release variables */ for( i = 0; i < nvars; ++i ) { SCIP_CALL_ABORT( SCIPreleaseVar(scip, &vars[i]) ); } /* free memory for variable arrays */ SCIPfreeMemoryArray(scip, &vartypes); SCIPfreeMemoryArray(scip, &vars); /* deinitialize SCIP */ SCIP_CALL_ABORT( SCIPfree(&scip) ); /* check for memory leaks */ BMScheckEmptyMemory(); return; }
/** writes problem data to file with given reader or returns SCIP_DIDNOTRUN */ SCIP_RETCODE SCIPreaderWrite( SCIP_READER* reader, /**< reader */ SCIP_PROB* prob, /**< problem data */ SCIP_SET* set, /**< global SCIP settings */ FILE* file, /**< output file (or NULL for standard output) */ const char* extension, /**< file format */ SCIP_Bool genericnames, /**< using generic variable and constraint names? */ SCIP_RESULT* result /**< pointer to store the result of the callback method */ ) { SCIP_RETCODE retcode; assert(reader != NULL); assert(set != NULL); assert(extension != NULL); assert(result != NULL); /* check, if reader is applicable on the given file */ if( readerIsApplicable(reader, extension) && reader->readerwrite != NULL ) { SCIP_VAR** vars; int nvars; SCIP_VAR** fixedvars; int nfixedvars; SCIP_CONS** conss; int nconss; int i; SCIP_CONS* cons; char* name; const char* consname; const char** varnames; const char** fixedvarnames; const char** consnames; varnames = NULL; fixedvarnames = NULL; consnames = NULL; vars = prob->vars; nvars = prob->nvars; fixedvars = prob->fixedvars; nfixedvars = prob->nfixedvars; /* case of the transformed problem, we want to write currently valid problem */ if( prob->transformed ) { SCIP_CONSHDLR** conshdlrs; int nconshdlrs; conshdlrs = set->conshdlrs; nconshdlrs = set->nconshdlrs; /* collect number of constraints which have to be enforced; these are the constraints which currency (locally) * enabled; these also includes the local constraints */ nconss = 0; for( i = 0; i < nconshdlrs; ++i ) { /* check if all constraints of the constraint handler should be written */ if( set->write_allconss ) nconss += SCIPconshdlrGetNConss(conshdlrs[i]); else nconss += SCIPconshdlrGetNEnfoConss(conshdlrs[i]); } SCIPdebugMessage("Writing %d constraints.\n", nconss); SCIP_ALLOC( BMSallocMemoryArray(&conss, nconss) ); /* copy the constraints */ nconss = 0; for( i = 0; i < nconshdlrs; ++i ) { SCIP_CONS** conshdlrconss; int nconshdlrconss; int c; /* check if all constraints of the constraint handler should be written */ if( set->write_allconss ) { conshdlrconss = SCIPconshdlrGetConss(conshdlrs[i]); nconshdlrconss = SCIPconshdlrGetNConss(conshdlrs[i]); } else { conshdlrconss = SCIPconshdlrGetEnfoConss(conshdlrs[i]); nconshdlrconss = SCIPconshdlrGetNEnfoConss(conshdlrs[i]); } SCIPdebugMessage("Conshdlr <%s> has %d constraints to write from all in all %d constraints.\n", SCIPconshdlrGetName(conshdlrs[i]), nconshdlrconss, SCIPconshdlrGetNConss(conshdlrs[i])); for( c = 0; c < nconshdlrconss; ++c ) { conss[nconss] = conshdlrconss[c]; nconss++; } } } else { conss = prob->conss; nconss = prob->nconss; } if( genericnames ) { SCIP_VAR* var; int size; /* save variable and constraint names and replace these names by generic names */ /* allocate memory for saving the original variable and constraint names */ SCIP_ALLOC( BMSallocMemoryArray(&varnames, nvars) ); SCIP_ALLOC( BMSallocMemoryArray(&fixedvarnames, nfixedvars) ); SCIP_ALLOC( BMSallocMemoryArray(&consnames, nconss) ); /* compute length of the generic variable names: * - nvars + 1 to avoid log of zero * - +3 (zero at end + 'x' + 1 because we round down) * Example: 10 -> need 4 chars ("x10\0") */ size = (int) log10(nvars+1.0) + 3; for( i = 0; i < nvars; ++i ) { var = vars[i]; varnames[i] = SCIPvarGetName(var); SCIP_ALLOC( BMSallocMemoryArray(&name, size) ); (void) SCIPsnprintf(name, size, "x%d", i + set->write_genoffset); SCIPvarSetNamePointer(var, name); } /* compute length of the generic variable names */ size = (int) log10(nfixedvars+1.0) + 3; for( i = 0; i < nfixedvars; ++i ) { var = fixedvars[i]; fixedvarnames[i] = SCIPvarGetName(var); SCIP_ALLOC( BMSallocMemoryArray(&name, size) ); (void) SCIPsnprintf(name, size, "y%d", i); SCIPvarSetNamePointer(var, name); } /* compute length of the generic constraint names */ size = (int) log10(nconss+1.0) + 3; for( i = 0; i < nconss; ++i ) { cons = conss[i]; consnames[i] = SCIPconsGetName(cons); SCIP_ALLOC( BMSallocMemoryArray(&name, size) ); (void) SCIPsnprintf(name, size, "c%d", i); SCIPconsSetNamePointer(cons, name); } } /* call reader to write problem */ retcode = reader->readerwrite(set->scip, reader, file, prob->name, prob->probdata, prob->transformed, prob->transformed ? SCIP_OBJSENSE_MINIMIZE : prob->objsense, prob->objscale, prob->objoffset, vars, nvars, prob->nbinvars, prob->nintvars, prob->nimplvars, prob->ncontvars, fixedvars, nfixedvars, prob->startnvars, conss, nconss, prob->maxnconss, prob->startnconss, genericnames, result); /* reset variable and constraint names to original names */ if( genericnames ) { assert(varnames != NULL); assert(fixedvarnames != NULL); assert(consnames != NULL); for( i = 0; i < nvars; ++i ) resetVarname(vars[i], varnames[i]); for( i = 0; i < nfixedvars; ++i ) resetVarname(fixedvars[i], fixedvarnames[i]); for( i = 0; i < nconss; ++i ) { cons = conss[i]; /* get pointer to temporary generic name and free the memory */ consname = SCIPconsGetName(cons); BMSfreeMemory(&consname); /* reset name */ SCIPconsSetNamePointer(cons, consnames[i]); } /* free memory */ BMSfreeMemoryArray(&varnames); BMSfreeMemoryArray(&fixedvarnames); BMSfreeMemoryArray(&consnames); } if( prob->transformed ) { /* free memory */ BMSfreeMemoryArray(&conss); } } else { *result = SCIP_DIDNOTRUN; retcode = SCIP_OKAY; } /* check for reader errors */ if( retcode == SCIP_WRITEERROR ) return retcode; SCIP_CALL( retcode ); return SCIP_OKAY; }
/** reduced cost pricing method of variable pricer for feasible LPs */ static SCIP_DECL_PRICERREDCOST(pricerRedcostBinpacking) { /*lint --e{715}*/ SCIP* subscip; SCIP_PRICERDATA* pricerdata; SCIP_CONS** conss; SCIP_VAR** vars; int* ids; SCIP_Bool addvar; SCIP_SOL** sols; int nsols; int s; int nitems; SCIP_Longint capacity; SCIP_Real timelimit; SCIP_Real memorylimit; assert(scip != NULL); assert(pricer != NULL); (*result) = SCIP_DIDNOTRUN; /* get the pricer data */ pricerdata = SCIPpricerGetData(pricer); assert(pricerdata != NULL); capacity = pricerdata->capacity; conss = pricerdata->conss; ids = pricerdata->ids; nitems = pricerdata->nitems; /* get the remaining time and memory limit */ SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) ); if( !SCIPisInfinity(scip, timelimit) ) timelimit -= SCIPgetSolvingTime(scip); SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) ); if( !SCIPisInfinity(scip, memorylimit) ) memorylimit -= SCIPgetMemUsed(scip)/1048576.0; /* initialize SCIP */ SCIP_CALL( SCIPcreate(&subscip) ); SCIP_CALL( SCIPincludeDefaultPlugins(subscip) ); /* create problem in sub SCIP */ SCIP_CALL( SCIPcreateProbBasic(subscip, "pricing") ); SCIP_CALL( SCIPsetObjsense(subscip, SCIP_OBJSENSE_MAXIMIZE) ); /* do not abort subproblem on CTRL-C */ SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) ); /* disable output to console */ SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) ); /* set time and memory limit */ SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) ); SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) ); SCIP_CALL( SCIPallocMemoryArray(subscip, &vars, nitems) ); /* initialization local pricing problem */ SCIP_CALL( initPricing(scip, pricerdata, subscip, vars) ); SCIPdebugMessage("solve pricer problem\n"); /* solve sub SCIP */ SCIP_CALL( SCIPsolve(subscip) ); sols = SCIPgetSols(subscip); nsols = SCIPgetNSols(subscip); addvar = FALSE; /* loop over all solutions and create the corresponding column to master if the reduced cost are negative for master, * that is the objective value i greater than 1.0 */ for( s = 0; s < nsols; ++s ) { SCIP_Bool feasible; SCIP_SOL* sol; /* the soultion should be sorted w.r.t. the objective function value */ assert(s == 0 || SCIPisFeasGE(subscip, SCIPgetSolOrigObj(subscip, sols[s-1]), SCIPgetSolOrigObj(subscip, sols[s]))); sol = sols[s]; assert(sol != NULL); /* check if solution is feasible in original sub SCIP */ SCIP_CALL( SCIPcheckSolOrig(subscip, sol, &feasible, FALSE, FALSE ) ); if( !feasible ) { SCIPwarningMessage(scip, "solution in pricing problem (capacity <%d>) is infeasible\n", capacity); continue; } /* check if the solution has a value greater than 1.0 */ if( SCIPisFeasGT(subscip, SCIPgetSolOrigObj(subscip, sol), 1.0) ) { SCIP_VAR* var; SCIP_VARDATA* vardata; int* consids; char strtmp[SCIP_MAXSTRLEN]; char name[SCIP_MAXSTRLEN]; int nconss; int o; int v; SCIPdebug( SCIP_CALL( SCIPprintSol(subscip, sol, NULL, FALSE) ) ); nconss = 0; (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "items"); SCIP_CALL( SCIPallocBufferArray(scip, &consids, nitems) ); /* check which variables are fixed -> which item belongs to this packing */ for( o = 0, v = 0; o < nitems; ++o ) { if( !SCIPconsIsEnabled(conss[o]) ) continue; assert(SCIPgetNFixedonesSetppc(scip, conss[o]) == 0); if( SCIPgetSolVal(subscip, sol, vars[v]) > 0.5 ) { (void) SCIPsnprintf(strtmp, SCIP_MAXSTRLEN, "_%d", ids[o]); strcat(name, strtmp); consids[nconss] = o; nconss++; } else assert( SCIPisFeasEQ(subscip, SCIPgetSolVal(subscip, sol, vars[v]), 0.0) ); v++; } SCIP_CALL( SCIPvardataCreateBinpacking(scip, &vardata, consids, nconss) ); /* create variable for a new column with objective function coefficient 0.0 */ SCIP_CALL( SCIPcreateVarBinpacking(scip, &var, name, 1.0, FALSE, TRUE, vardata) ); /* add the new variable to the pricer store */ SCIP_CALL( SCIPaddPricedVar(scip, var, 1.0) ); addvar = TRUE; /* change the upper bound of the binary variable to lazy since the upper bound is already enforced due to * the objective function the set covering constraint; The reason for doing is that, is to avoid the bound * of x <= 1 in the LP relaxation since this bound constraint would produce a dual variable which might have * a positive reduced cost */ SCIP_CALL( SCIPchgVarUbLazy(scip, var, 1.0) ); /* check which variable are fixed -> which orders belong to this packing */ for( v = 0; v < nconss; ++v ) { assert(SCIPconsIsEnabled(conss[consids[v]])); SCIP_CALL( SCIPaddCoefSetppc(scip, conss[consids[v]], var) ); } SCIPdebug(SCIPprintVar(scip, var, NULL) ); SCIP_CALL( SCIPreleaseVar(scip, &var) ); SCIPfreeBufferArray(scip, &consids); } else break; } /* free pricer MIP */ SCIPfreeMemoryArray(subscip, &vars); if( addvar || SCIPgetStatus(subscip) == SCIP_STATUS_OPTIMAL ) (*result) = SCIP_SUCCESS; /* free sub SCIP */ SCIP_CALL( SCIPfree(&subscip) ); return SCIP_OKAY; }