/** evaluates command line parameters and runs SCIP appropriately in the given SCIP instance */ SCIP_RETCODE SCIPprocessShellArguments( SCIP* scip, /**< SCIP data structure */ int argc, /**< number of shell parameters */ char** argv, /**< array with shell parameters */ const char* defaultsetname /**< name of default settings file */ ) { /*lint --e{850}*/ char* probname = NULL; char* settingsname = NULL; char* logname = NULL; SCIP_Bool quiet; SCIP_Bool paramerror; SCIP_Bool interactive; int i; /******************** * Parse parameters * ********************/ quiet = FALSE; paramerror = FALSE; interactive = FALSE; for( i = 1; i < argc; ++i ) { if( strcmp(argv[i], "-l") == 0 ) { i++; if( i < argc ) logname = argv[i]; else { printf("missing log filename after parameter '-l'\n"); paramerror = TRUE; } } else if( strcmp(argv[i], "-q") == 0 ) quiet = TRUE; else if( strcmp(argv[i], "-s") == 0 ) { i++; if( i < argc ) settingsname = argv[i]; else { printf("missing settings filename after parameter '-s'\n"); paramerror = TRUE; } } else if( strcmp(argv[i], "-f") == 0 ) { i++; if( i < argc ) probname = argv[i]; else { printf("missing problem filename after parameter '-f'\n"); paramerror = TRUE; } } else if( strcmp(argv[i], "-c") == 0 ) { i++; if( i < argc ) { SCIP_CALL( SCIPaddDialogInputLine(scip, argv[i]) ); interactive = TRUE; } else { printf("missing command line after parameter '-c'\n"); paramerror = TRUE; } } else if( strcmp(argv[i], "-b") == 0 ) { i++; if( i < argc ) { SCIP_FILE* file; file = SCIPfopen(argv[i], "r"); if( file == NULL ) { printf("cannot read command batch file <%s>\n", argv[i]); SCIPprintSysError(argv[i]); paramerror = TRUE; } else { while( !SCIPfeof(file) ) { char buffer[SCIP_MAXSTRLEN]; (void)SCIPfgets(buffer, (int) sizeof(buffer), file); if( buffer[0] != '\0' ) { SCIP_CALL( SCIPaddDialogInputLine(scip, buffer) ); } } SCIPfclose(file); interactive = TRUE; } } else { printf("missing command batch filename after parameter '-b'\n"); paramerror = TRUE; } } else { printf("invalid parameter <%s>\n", argv[i]); paramerror = TRUE; } } if( interactive && probname != NULL ) { printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n"); paramerror = TRUE; } if( !paramerror ) { /*********************************** * create log file message handler * ***********************************/ if( quiet ) { SCIPsetMessagehdlrQuiet(scip, quiet); } if( logname != NULL ) { SCIPsetMessagehdlrLogfile(scip, logname); } /*********************************** * Version and library information * ***********************************/ SCIPprintVersion(scip, NULL); SCIPinfoMessage(scip, NULL, "\n"); SCIPprintExternalCodes(scip, NULL); SCIPinfoMessage(scip, NULL, "\n"); /***************** * Load settings * *****************/ if( settingsname != NULL ) { SCIP_CALL( readParams(scip, settingsname) ); } else if( defaultsetname != NULL ) { SCIP_CALL( readParams(scip, defaultsetname) ); } /************** * Start SCIP * **************/ if( probname != NULL ) { SCIP_CALL( fromCommandLine(scip, probname) ); } else { SCIPinfoMessage(scip, NULL, "\n"); SCIP_CALL( SCIPstartInteraction(scip) ); } } else { printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-f <problem>] [-b <batchfile>] [-c \"command\"]\n" " -l <logfile> : copy output into log file\n" " -q : suppress screen messages\n" " -s <settings> : load parameter settings (.set) file\n" " -f <problem> : load and solve problem file\n" " -b <batchfile>: load and execute dialog command batch file (can be used multiple times)\n" " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n\n", argv[0]); } return SCIP_OKAY; }
/** problem reading method of reader */ static SCIP_DECL_READERREAD(readerReadBpa) { /*lint --e{715}*/ SCIP_FILE* file; SCIP_Longint* weights; int* ids; SCIP_Bool error; char name[SCIP_MAXSTRLEN]; char format[16]; char buffer[SCIP_MAXSTRLEN]; int capacity; int nitems; int bestsolvalue; int nread; int weight; int nweights; int lineno; *result = SCIP_DIDNOTRUN; /* open file */ file = SCIPfopen(filename, "r"); if( file == NULL ) { SCIPerrorMessage("cannot open file <%s> for reading\n", filename); SCIPprintSysError(filename); return SCIP_NOFILE; } lineno = 0; sprintf(name, "++ uninitialized ++"); /* read problem name */ if( !SCIPfeof(file) ) { /* get next line */ if( SCIPfgets(buffer, (int)sizeof(buffer), file) == NULL ) return SCIP_READERROR; lineno++; /* parse dimension line */ sprintf(format, "%%%ds\n", SCIP_MAXSTRLEN); nread = sscanf(buffer, format, name); if( nread == 0 ) { SCIPwarningMessage(scip, "invalid input line %d in file <%s>: <%s>\n", lineno, filename, buffer); return SCIP_READERROR; } SCIPdebugMessage("problem name <%s>\n", name); } capacity = 0; nitems = 0; /* read problem dimension */ if( !SCIPfeof(file) ) { /* get next line */ if( SCIPfgets(buffer, (int)sizeof(buffer), file) == NULL ) return SCIP_READERROR; lineno++; /* parse dimension line */ nread = sscanf(buffer, "%d %d %d\n", &capacity, &nitems, &bestsolvalue); if( nread < 2 ) { SCIPwarningMessage(scip, "invalid input line %d in file <%s>: <%s>\n", lineno, filename, buffer); return SCIP_READERROR; } SCIPdebugMessage("capacity = <%d>, number of items = <%d>, best known solution = <%d>\n", capacity, nitems, bestsolvalue); } /* allocate buffer memory for storing the weights and ids temporary */ SCIP_CALL( SCIPallocBufferArray(scip, &weights, nitems) ); SCIP_CALL( SCIPallocBufferArray(scip, &ids, nitems) ); /* pasre weights */ nweights = 0; error = FALSE; while( !SCIPfeof(file) && !error ) { /* get next line */ if( SCIPfgets(buffer, (int)sizeof(buffer), file) == NULL ) break; lineno++; /* parse the line */ nread = sscanf(buffer, "%d\n", &weight); if( nread == 0 ) { SCIPwarningMessage(scip, "invalid input line %d in file <%s>: <%s>\n", lineno, filename, buffer); error = TRUE; break; } SCIPdebugMessage("found weight %d <%d>\n", nweights, weight); weights[nweights] = weight; ids[nweights] = nweights; nweights++; if( nweights == nitems ) break; } if( nweights < nitems ) { SCIPwarningMessage(scip, "set nitems from <%d> to <%d> since the file <%s> only contains <%d> weights\n", nitems, weights, filename, weights); nitems = nweights; } if( !error ) { /* create a new problem in SCIP */ SCIP_CALL( SCIPprobdataCreate(scip, name, ids, weights, nitems, (SCIP_Longint)capacity) ); } (void)SCIPfclose(file); SCIPfreeBufferArray(scip, &ids); SCIPfreeBufferArray(scip, &weights); if( error ) return SCIP_READERROR; *result = SCIP_SUCCESS; return SCIP_OKAY; }
/** problem reading method of reader */ static SCIP_DECL_READERREAD(readerReadCip) { /*lint --e{715}*/ CIPINPUT cipinput; SCIP_Real objscale; SCIP_Real objoffset; SCIP_Bool initialconss; SCIP_Bool dynamicconss; SCIP_Bool dynamiccols; SCIP_Bool dynamicrows; SCIP_Bool initialvar; SCIP_Bool removablevar; SCIP_RETCODE retcode; if( NULL == (cipinput.file = SCIPfopen(filename, "r")) ) { SCIPerrorMessage("cannot open file <%s> for reading\n", filename); SCIPprintSysError(filename); return SCIP_NOFILE; } cipinput.len = 131071; SCIP_CALL( SCIPallocBufferArray(scip, &(cipinput.strbuf), cipinput.len) ); cipinput.linenumber = 0; cipinput.section = CIP_START; cipinput.haserror = FALSE; cipinput.endfile = FALSE; cipinput.readingsize = 65535; SCIP_CALL( SCIPcreateProb(scip, filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/initialconss", &initialconss) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamiccols", &dynamiccols) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicconss", &dynamicconss) ); SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicrows", &dynamicrows) ); initialvar = !dynamiccols; removablevar = dynamiccols; objscale = 1.0; objoffset = 0.0; while( cipinput.section != CIP_END && !cipinput.haserror ) { /* get next input string */ SCIP_CALL( getInputString(scip, &cipinput) ); if( cipinput.endfile ) break; switch( cipinput.section ) { case CIP_START: getStart(scip, &cipinput); break; case CIP_STATISTIC: SCIP_CALL( getStatistics(scip, &cipinput) ); break; case CIP_OBJECTIVE: SCIP_CALL( getObjective(scip, &cipinput, &objscale, &objoffset) ); break; case CIP_VARS: retcode = getVariable(scip, &cipinput, initialvar, removablevar, objscale); if( retcode == SCIP_READERROR ) { cipinput.haserror = TRUE; goto TERMINATE; } SCIP_CALL(retcode); break; case CIP_FIXEDVARS: retcode = getFixedVariable(scip, &cipinput); if( retcode == SCIP_READERROR ) { cipinput.haserror = TRUE; goto TERMINATE; } SCIP_CALL(retcode); break; case CIP_CONSTRAINTS: retcode = getConstraint(scip, &cipinput, initialconss, dynamicconss, dynamicrows); if( retcode == SCIP_READERROR ) { cipinput.haserror = TRUE; goto TERMINATE; } SCIP_CALL(retcode); break; default: SCIPerrorMessage("invalid CIP state\n"); SCIPABORT(); return SCIP_INVALIDDATA; /*lint !e527*/ } /*lint !e788*/ } if( !SCIPisZero(scip, objoffset) && !cipinput.haserror ) { SCIP_VAR* objoffsetvar; objoffset *= objscale; SCIP_CALL( SCIPcreateVar(scip, &objoffsetvar, "objoffset", objoffset, objoffset, 1.0, SCIP_VARTYPE_CONTINUOUS, TRUE, TRUE, NULL, NULL, NULL, NULL, NULL) ); SCIP_CALL( SCIPaddVar(scip, objoffsetvar) ); SCIP_CALL( SCIPreleaseVar(scip, &objoffsetvar) ); SCIPdebugMessage("added variables <objoffset> for objective offset of <%g>\n", objoffset); } if( cipinput.section != CIP_END && !cipinput.haserror ) { SCIPerrorMessage("unexpected EOF\n"); } TERMINATE: /* close file stream */ SCIPfclose(cipinput.file); SCIPfreeBufferArray(scip, &cipinput.strbuf); if( cipinput.haserror ) return SCIP_READERROR; /* successfully parsed cip format */ *result = SCIP_SUCCESS; return SCIP_OKAY; }
/** reads the given solution file */ static SCIP_RETCODE readSol( SCIP* scip, /**< SCIP data structure */ const char* filename /**< name of the input file */ ) { SCIP_FILE* file; SCIP_Bool error; SCIP_Bool unknownvariablemessage; int lineno; int nfixed; assert(scip != NULL); assert(filename != NULL); /* open input file */ file = SCIPfopen(filename, "r"); if( file == NULL ) { SCIPerrorMessage("cannot open file <%s> for reading\n", filename); SCIPprintSysError(filename); return SCIP_NOFILE; } /* read the file */ error = FALSE; unknownvariablemessage = FALSE; lineno = 0; nfixed = 0; while( !SCIPfeof(file) && !error ) { char buffer[SCIP_MAXSTRLEN]; char varname[SCIP_MAXSTRLEN]; char valuestring[SCIP_MAXSTRLEN]; char objstring[SCIP_MAXSTRLEN]; SCIP_VAR* var; SCIP_Real value; SCIP_Bool infeasible; SCIP_Bool fixed; int nread; /* get next line */ if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL ) break; lineno++; /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */ if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 ) continue; /* parse the line */ nread = sscanf(buffer, "%s %s %s\n", varname, valuestring, objstring); if( nread < 2 ) { SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer); error = TRUE; break; } /* find the variable */ var = SCIPfindVar(scip, varname); if( var == NULL ) { if( !unknownvariablemessage ) { SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename); SCIPwarningMessage(scip, " (further unknown variables are ignored)\n"); unknownvariablemessage = TRUE; } continue; } /* cast the value */ if( strncasecmp(valuestring, "inv", 3) == 0 ) continue; else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 ) value = SCIPinfinity(scip); else if( strncasecmp(valuestring, "-inf", 4) == 0 ) value = -SCIPinfinity(scip); else { nread = sscanf(valuestring, "%lf", &value); if( nread != 1 ) { SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n", valuestring, varname, lineno, filename); error = TRUE; break; } } /* fix the variable */ SCIP_CALL( SCIPfixVar(scip, var, value, &infeasible, &fixed) ); if( infeasible ) { SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n", varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename); error = TRUE; break; } if( fixed ) nfixed++; } /* close input file */ SCIPfclose(file); /* display result */ SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename); if( error ) return SCIP_READERROR; else return SCIP_OKAY; }
/** reads a given SCIP solution file, problem has to be transformed in advance */ static SCIP_RETCODE readSol( SCIP* scip, /**< SCIP data structure */ const char* fname /**< name of the input file */ ) { SCIP_SOL* sol; SCIP_FILE* file; SCIP_Bool error; SCIP_Bool unknownvariablemessage; SCIP_Bool stored; SCIP_Bool usevartable; int lineno; assert(scip != NULL); assert(fname != NULL); SCIP_CALL( SCIPgetBoolParam(scip, "misc/usevartable", &usevartable) ); if( !usevartable ) { SCIPerrorMessage("Cannot read solution file if vartable is disabled. Make sure parameter 'misc/usevartable' is set to TRUE.\n"); return SCIP_READERROR; } /* open input file */ file = SCIPfopen(fname, "r"); if( file == NULL ) { SCIPerrorMessage("cannot open file <%s> for reading\n", fname); SCIPprintSysError(fname); return SCIP_NOFILE; } /* create zero solution */ SCIP_CALL( SCIPcreateSol(scip, &sol, NULL) ); /* read the file */ error = FALSE; unknownvariablemessage = FALSE; lineno = 0; while( !SCIPfeof(file) && !error ) { char buffer[SCIP_MAXSTRLEN]; char varname[SCIP_MAXSTRLEN]; char valuestring[SCIP_MAXSTRLEN]; char objstring[SCIP_MAXSTRLEN]; SCIP_VAR* var; SCIP_Real value; int nread; /* get next line */ if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL ) break; lineno++; /* there are some lines which may preceed the solution information */ if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 || strncasecmp(buffer, "Log started", 11) == 0 || strncasecmp(buffer, "Variable Name", 13) == 0 || strncasecmp(buffer, "All other variables", 19) == 0 || strncasecmp(buffer, "\n", 1) == 0 || strncasecmp(buffer, "NAME", 4) == 0 || strncasecmp(buffer, "ENDATA", 6) == 0 ) /* allow parsing of SOL-format on the MIPLIB 2003 pages */ continue; /* parse the line */ nread = sscanf(buffer, "%s %s %s\n", varname, valuestring, objstring); if( nread < 2 ) { SCIPerrorMessage("Invalid input line %d in solution file <%s>: <%s>.\n", lineno, fname, buffer); error = TRUE; break; } /* find the variable */ var = SCIPfindVar(scip, varname); if( var == NULL ) { if( !unknownvariablemessage ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, fname); SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, " (further unknown variables are ignored)\n"); unknownvariablemessage = TRUE; } continue; } /* cast the value */ if( strncasecmp(valuestring, "inv", 3) == 0 ) continue; else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 ) value = SCIPinfinity(scip); else if( strncasecmp(valuestring, "-inf", 4) == 0 ) value = -SCIPinfinity(scip); else { nread = sscanf(valuestring, "%lf", &value); if( nread != 1 ) { SCIPerrorMessage("Invalid solution value <%s> for variable <%s> in line %d of solution file <%s>.\n", valuestring, varname, lineno, fname); error = TRUE; break; } } /* set the solution value of the variable, if not multiaggregated */ if( SCIPisTransformed(scip) && SCIPvarGetStatus(SCIPvarGetProbvar(var)) == SCIP_VARSTATUS_MULTAGGR ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "ignored solution value for multiaggregated variable <%s>\n", SCIPvarGetName(var)); } else { SCIP_RETCODE retcode; retcode = SCIPsetSolVal(scip, sol, var, value); if( retcode == SCIP_INVALIDDATA ) { if( SCIPvarGetStatus(SCIPvarGetProbvar(var)) == SCIP_VARSTATUS_FIXED ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "ignored conflicting solution value for fixed variable <%s>\n", SCIPvarGetName(var)); } else { SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "ignored solution value for multiaggregated variable <%s>\n", SCIPvarGetName(var)); } } else { SCIP_CALL( retcode ); } } } /* close input file */ SCIPfclose(file); if( !error ) { /* add and free the solution */ if( SCIPisTransformed(scip) ) { SCIP_CALL( SCIPtrySolFree(scip, &sol, TRUE, TRUE, TRUE, TRUE, &stored) ); /* display result */ SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "primal solution from solution file <%s> was %s\n", fname, stored ? "accepted" : "rejected - solution is infeasible or objective too poor"); } else { /* add primal solution to solution candidate storage, frees the solution afterwards */ SCIP_CALL( SCIPaddSolFree(scip, &sol, &stored) ); /* display result */ SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "primal solution from solution file <%s> was %s\n", fname, stored ? "accepted as candidate, will be checked when solving starts" : "rejected - solution objective too poor"); } return SCIP_OKAY; } else { /* free solution */ SCIP_CALL( SCIPfreeSol(scip, &sol) ); return SCIP_READERROR; } }
/** problem reading method of reader * * In order to determine the type of the file, we have to open it. Thus, it has to be opened * twice. This might be removed, but is likely to not hurt the performance too much. */ static SCIP_DECL_READERREAD(readerReadSol) { /*lint --e{715}*/ SCIP_FILE* file; char buffer[SCIP_MAXSTRLEN]; char *s; assert(reader != NULL); assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0); assert(result != NULL); *result = SCIP_DIDNOTRUN; if( SCIPgetStage(scip) < SCIP_STAGE_PROBLEM ) { SCIPerrorMessage("reading of solution file is only possible after a problem was created\n"); return SCIP_READERROR; } if( SCIPgetStage(scip) == SCIP_STAGE_SOLVED ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "primal solution from solution file <%s> was ignored - problem is already solved to optimality\n", filename); *result = SCIP_SUCCESS; return SCIP_OKAY; } /* open input file in order to determine type */ file = SCIPfopen(filename, "r"); if( file == NULL ) { SCIPerrorMessage("cannot open file <%s> for reading\n", filename); SCIPprintSysError(filename); return SCIP_NOFILE; } /* get next line */ if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL ) { SCIPerrorMessage("cannot parse file.\n"); return SCIP_READERROR; } /* close file */ SCIPfclose(file); /* decide whether it is xml */ s = buffer; /* skip spaces */ while( isspace((unsigned char)*s) ) ++s; if( s[0] == '<' && s[1] == '?' && s[2] == 'x' && s[3] == 'm' && s[4] == 'l' ) { /* read XML solution and add it to the solution pool */ SCIP_CALL( readXMLSol(scip, filename) ); } else { /* read the solution and add it to the solution pool */ SCIP_CALL( readSol(scip, filename) ); } *result = SCIP_SUCCESS; return SCIP_OKAY; }
/** reads solution from given file into given arrays */ static SCIP_RETCODE readSolfile( SCIP_SET* set, /**< global SCIP settings */ const char* solfilename, /**< solution filename to read */ char*** names, /**< pointer to store the array of variable names */ SCIP_Real** vals, /**< pointer to store the array of solution values */ int* nvals, /**< pointer to store the number of non-zero elements */ int* valssize /**< pointer to store the length of the variable names and solution values arrays */ ) { FILE* file; int nonvalues; int i; assert(set != NULL); assert(solfilename != NULL); assert(names != NULL); assert(*names == NULL); assert(vals != NULL); assert(*vals == NULL); assert(nvals != NULL); assert(valssize != NULL); printf("***** debug: reading solution file <%s>\n", solfilename); /* open solution file */ file = fopen(solfilename, "r"); if( file == NULL ) { SCIPerrorMessage("cannot open solution file <%s> specified in scip/debug.h\n", solfilename); SCIPprintSysError(solfilename); return SCIP_NOFILE; } /* read data */ nonvalues = 0; *valssize = 0; while( !feof(file) ) { char buf[SCIP_MAXSTRLEN]; char name[SCIP_MAXSTRLEN]; char objstring[SCIP_MAXSTRLEN]; SCIP_Real val; int nread; if( fgets(buf, SCIP_MAXSTRLEN, file) == NULL ) { if( feof(file) ) break; else return SCIP_READERROR; } /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */ if( strncmp(buf, "solution", 8) == 0 || strncmp(buf, "objective", 9) == 0 ) { nonvalues++; continue; } /* skip empty lines */ if( strlen(buf) == 1 ) { nonvalues++; continue; } nread = sscanf(buf, "%s %lf %s\n", name, &val, objstring); if( nread < 2 ) { printf("invalid input line %d in solution file <%s>: <%s>\n", *nvals + nonvalues, SCIP_DEBUG_SOLUTION, name); fclose(file); return SCIP_READERROR; } /* allocate memory */ if( *nvals >= *valssize ) { *valssize = MAX(2 * *valssize, (*nvals)+1); SCIP_ALLOC( BMSreallocMemoryArray(names, *valssize) ); SCIP_ALLOC( BMSreallocMemoryArray(vals, *valssize) ); } assert(*nvals < *valssize); /* store solution value in sorted list */ for( i = *nvals; i > 0 && strcmp(name, (*names)[i-1]) < 0; --i ) { (*names)[i] = (*names)[i-1]; (*vals)[i] = (*vals)[i-1]; } SCIP_ALLOC( BMSduplicateMemoryArray(&(*names)[i], name, strlen(name)+1) ); SCIPdebugMessage("found variable <%s>: value <%g>\n", (*names)[i], val); (*vals)[i] = val; (*nvals)++; } debugsolval = 0.0; /* get solution value */ for( i = *nvals - 1; i >= 0; --i) { SCIP_VAR* var; var = SCIPfindVar(set->scip, (*names)[i]); if( var != NULL ) debugsolval += (*vals)[i] * SCIPvarGetObj(var); } SCIPdebugMessage("Debug Solution value is %g.\n", debugsolval); /* close file */ fclose(file); /* remember the set pointer to identify sub-MIP calls */ mainscipset = set; printf("***** debug: read %d non-zero entries\n", *nvals); return SCIP_OKAY; }
/** reads a DEC file */ static SCIP_RETCODE readDECFile( SCIP* scip, /**< SCIP data structure */ SCIP_READER* reader, /**< Reader data structure */ DECINPUT* decinput, /**< DEC reading data */ const char* filename /**< name of the input file */ ) { SCIP_READERDATA* readerdata; SCIP_CONS** conss; DEC_DECOMP* decdecomp; int nconss; int nblocksread; int nvars; int i; assert(decinput != NULL); assert(scip != NULL); assert(reader != NULL); nblocksread = FALSE; if( SCIPgetStage(scip) == SCIP_STAGE_INIT || SCIPgetNVars(scip) == 0 || SCIPgetNConss(scip) == 0 ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_DIALOG, NULL, "No problem exists, will not read structure!\n"); return SCIP_OKAY; } /* open file */ decinput->file = SCIPfopen(filename, "r"); if( decinput->file == NULL ) { SCIPerrorMessage("cannot open file <%s> for reading\n", filename); SCIPprintSysError(filename); return SCIP_NOFILE; } readerdata = SCIPreaderGetData(reader); assert(readerdata != NULL); readerdata->nlinkingconss = SCIPgetNConss(scip); readerdata->nlinkingvars = 0; nvars = SCIPgetNVars(scip); conss = SCIPgetConss(scip); nconss = SCIPgetNConss(scip); /* alloc: var -> block mapping */ SCIP_CALL( SCIPallocMemoryArray(scip, &readerdata->varstoblock, nvars) ); for( i = 0; i < nvars; i ++ ) { readerdata->varstoblock[i] = NOVALUE; } /* cons -> block mapping */ SCIP_CALL( SCIPhashmapCreate(&readerdata->constoblock, SCIPblkmem(scip), nconss) ); for( i = 0; i < nconss; i ++ ) { SCIP_CALL( SCIPhashmapInsert(readerdata->constoblock, conss[i], (void*) (size_t) LINKINGVALUE) ); } /* parse the file */ decinput->section = DEC_START; while( decinput->section != DEC_END && ! hasError(decinput) ) { switch( decinput->section ) { case DEC_START: SCIP_CALL( readStart(scip, decinput) ); break; /**@bug the reader should presolve the problem */ case DEC_PRESOLVED: SCIP_CALL( readPresolved(scip, decinput) ); if( decinput->presolved && SCIPgetStage(scip) < SCIP_STAGE_PRESOLVED ) { assert(decinput->haspresolvesection); SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "decomposition belongs to the presolved problem, please presolve the problem first.\n"); goto TERMINATE; } break; case DEC_NBLOCKS: SCIP_CALL( readNBlocks(scip, decinput) ); if( decinput->haspresolvesection && !decinput->presolved && SCIPgetStage(scip) >= SCIP_STAGE_PRESOLVED ) { SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "decomposition belongs to the unpresolved problem, please re-read the problem and read the decomposition without presolving.\n"); goto TERMINATE; } if( !decinput->haspresolvesection ) { SCIPwarningMessage(scip, "decomposition has no presolve section at beginning. The behaviour is undefined. See the FAQ for further information.\n"); } break; case DEC_BLOCK: if( nblocksread == FALSE ) { /* alloc n vars per block */ SCIP_CALL( SCIPallocMemoryArray(scip, &readerdata->nblockvars, decinput->nblocks) ); SCIP_CALL( SCIPallocMemoryArray(scip, &readerdata->nblockconss, decinput->nblocks) ); SCIP_CALL( SCIPallocMemoryArray(scip, &readerdata->blockconss, decinput->nblocks) ); for( i = 0; i < decinput->nblocks; i ++ ) { readerdata->nblockvars[i] = 0; readerdata->nblockconss[i] = 0; SCIP_CALL( SCIPallocMemoryArray(scip, &(readerdata->blockconss[i]), nconss) ); /*lint !e866*/ } nblocksread = TRUE; } SCIP_CALL( readBlock(scip, decinput, readerdata) ); break; case DEC_MASTERCONSS: SCIP_CALL( readMasterconss(scip, decinput, readerdata) ); break; case DEC_END: /* this is already handled in the while() loop */ default: SCIPerrorMessage("invalid DEC file section <%d>\n", decinput->section); return SCIP_INVALIDDATA; } } SCIP_CALL( DECdecompCreate(scip, &decdecomp) ); /* fill decomp */ SCIP_CALL( fillDecompStruct(scip, decinput, decdecomp, readerdata) ); /* add decomp to cons_decomp */ SCIP_CALL( SCIPconshdlrDecompAddDecdecomp(scip, decdecomp) ); TERMINATE: if( nblocksread ) { for( i = decinput->nblocks - 1; i >= 0; --i ) { SCIPfreeMemoryArray(scip, &readerdata->blockconss[i]); } SCIPfreeMemoryArray(scip, &readerdata->blockconss); SCIPfreeMemoryArray(scip, &readerdata->nblockconss); SCIPfreeMemoryArray(scip, &readerdata->nblockvars); } SCIPfreeMemoryArray(scip, &readerdata->varstoblock); SCIPhashmapFree(&readerdata->constoblock); /* close file */ SCIPfclose(decinput->file); return SCIP_OKAY; }