$ontext =============================================================== FILENAME: crbatch.gms LAST UPDATED: March 6, 2007 PURPOSE: Creates a GAMS batch file of particular models to run, with specific solvers and options. REMARKS: 1. If several input options are specified, seperated by a comma (,), these must be input in quotes ("). For example --solver="bdmlp,cplex". OPTIONS: Users can specify multiple parameter per option. The commands differ for Windows and for UNIX. For Windows: --option="option1, option2, ..." For UNIX: --option=\"option1, option2, ...\" --batfile: root of output filename (default: batch --> batch.gms) --gmsopts: GAMS command line options (default: none) --iterlim: solver iteration limit (same as GAMS option iterlim) --modelfile: modelfile containing model names (a single model per line). REQUIRED. --optca: final solution abs error. Only for discrete models. (same as GAMS option optca --optcr: final solution rel error. Only for discrete models. (same as GAMS option optr --optfile: solver options file (same as GAMS option optfile. Default none) --outdir: output directory for .LOG and .LST files (default none) --reslim: solver resource limit (same as GAMS option reslim) --solver: batch solvers (default = bdmlp) --trace: output trace filename (default=trace.trc) --type: mode type (default= all. Can be LP, MIP, etc.) $offtext =============================================================== $if not set modelfile $goto err_modfile *=== Check if modelfile has a single column of values (models) $call =rm -f "err_%modelfile%.txt" $onecho > %gams.scrdir%chktrace.awk { i = i+1; if ($1 !~ /^*/){ if (NF > 1){ print "ERROR: line " i " of your model file appears to have "\ NF " column(s), i.e. too many!" > "err_%modelfile%.txt" } } } $offecho $call =awk -F, -f %gams.scrdir%chktrace.awk %modelfile% $if exist "err_%modelfile%.txt" $goto err_modfile_format $if errorlevel 1 $abort "Error checking input modelfile (sed script)" *=== put models in quotes so that file extensions can be used in model names $onecho > %gams.scrdir%tracemod.awk { i = 1 while(i <= NF) { { if ($1 !~ /^*/) { if($i != " ") printf( "%s %s %s", "'", $i, "'") > "%modelfile%" if(i "%modelfile%" if(i==NF) printf("\n") > "%modelfile%" } i = i+1 } { if ($1 ~ /^*/) printf("%s", $0) > "%modelfile%" } } } $offecho $onecho > %gams.scrdir%sedscript # remove any whitespace s/[ ]*//g # remove any single quotes s/'//g # remove any tabs s/[ ]*// # remove any commas s/,//g # remove any double quotes s/"//g $offecho $call =cp %modelfile% %gams.scrdir%old_%modelfile% $call sed -f "%gams.scrdir%sedscript" "%gams.scrdir%old_%modelfile%" > "%gams.scrdir%old_%modelfile%"_sed $call =awk -f %gams.scrdir%tracemod.awk "%gams.scrdir%old_%modelfile%"_sed $if errorlevel 1 $abort "Error putting models in modelfile in quotes (sed script)" $if not set batfile $set batfile batch $if not set gmsopts $set def_gmsopts no $if set gmsopts $set def_gmsopts yes $if not set iterlim $set iterlim no $if set optca $set dooptca yes $if not set optca $set dooptca no $if set optcr $set dooptcr yes $if not set optcr $set dooptcr no $if not set optfile $set optfile no $if not set origin $set origin all $if not set outdir $set def_outdir no $if set outdir $set def_outdir yes $if not set reslim $set reslim no $if not set size $set size all $if not set solver $set solver bdmlp $if not set trace $set trace trace.trc $if not set type $set type lp $ set is_unix no $if %system.filesys% == UNIX $set is_unix yes *======================================================================== *=== Create batch file from models in --modelfile *======================================================================== $label crmodfile $if %system.filesys% == UNIX $call =dos2unix %modelfile% *=== Create batch file creation script (crb.gms) $onecho > crb.gms *=== Specify models Set runmodels(*) / $onempty $include %cr_modelfile% $offempty /; *=== Specify solver Set solvers / %cr_solver% /; *=== Output models and options to batch file file fbat / %cr_batfile%.gms /; put fbat '* GAMS batch file created by crbatch.gms.' /; *=== If output directory specified, create it if(%def_outdir%, put fbat "$call =mkdir %outdir%" /; ); *=== Create tracefile header info put fbat '* ' / '* %system.gstring% ' / '* %system.date% %system.time%' / '* ' / '* %solver% ' / '* ' /; $set gsystem %system. $set gstring gstring% $set gdate date% $set gtime time% $set gfile filesys% if (%optfile%, put fbat '$onecho > opts.awk'/; put fbat ' { printf("%s %s\n", "* ", $0) >> "%cr_trace%" }'/; put fbat '$offecho'//; ); put fbat / '$if not %gsystem%%gfile% == UNIX $goto is_windows'/; put fbat '$call echo "* GAMS trace file " > %cr_trace%' / '$call echo "*" >> %cr_trace%' / '$call echo "* "%gsystem%%gstring% >> %cr_trace%' / '$call echo "* "%gsystem%%gdate% %gsystem%%gtime% >> %cr_trace%' / '$call echo "* " >> %cr_trace%' /; if(%optfile%, put '$call echo "* %solver% options: " >> %cr_trace%' /; put '$if exist %solver%.opt $call =awk -f opts.awk %solver%.opt'/; put '$if not exist %solver%.opt $echo "* (option file does not exist)" >> %cr_trace% '/; else put '$call echo "* %solver% " >> %cr_trace%' /; ); put '$call echo "* " >> %cr_trace%' /; put '$goto done_header' //; put fbat '$label is_windows' /; put fbat '$call echo * GAMS trace file > %cr_trace%' / '$call echo * >> %cr_trace%' / '$call echo * %gsystem%%gstring% >> %cr_trace%' / '$call echo * %gsystem%%gdate% %gsystem%%gtime% >> %cr_trace%' / '$call echo * >> %cr_trace%' /; if(%optfile%, put '$call echo * %solver% options: >> %cr_trace%' /; put '$if exist %solver%.opt $call =awk -f opts.awk %solver%.opt'/; put '$if not exist %solver%.opt $echo * (option file does not exist) >> %cr_trace%'/; else put '$call echo * %solver% >> %cr_trace%' /; ); put '$call echo * >> %cr_trace%' /; put '$label done_header' //; loop(runmodels, loop(solvers, put fbat "$call =gams " runmodels.tl:0 " %cr_type%=" solvers.tl:0 ; *=== Iterlim if(%iterlim%, put fbat " iterlim=%cr_iterlim%"; ); *=== Optca if(%dooptca%, put fbat " optca=%cr_optca%"; ); *=== Optcr if(%dooptcr%, put fbat " optcr=%cr_optcr%"; ); *=== Optfile if(%optfile%, put fbat " optfile=%cr_optfile%"; ); *=== Reslim if(%reslim%, put fbat " reslim=%cr_reslim%"; ); *=== Trace put fbat " trace=%cr_trace% traceopt=3"; *=== GAMS options if(%def_gmsopts%, put fbat " %gmsopts%"; ); *=== Output directory (.LST and .LOG) if(%def_outdir%, if(%is_unix%, put fbat " o=%cr_outdir%/" runmodels.tl:0 ".lst lo=3 | tee %cr_outdir%/" runmodels.tl:0 ".log"; else put fbat " o=%cr_outdir%\" runmodels.tl:0 ".lst lo=3 | tee %cr_outdir%\" runmodels.tl:0 ".log"; ); ); put /; ); ); putclose fbat; $offecho *=== Create batch file $if %system.filesys% == UNIX $goto p_unixcall *=== If Windows $onecho > %gams.scrdir%parmfile.txt --cr_batfile="%batfile%" --cr_gmsopts="%gmsopts%" --cr_iterlim="%iterlim%" --cr_modelfile="%modelfile%" --cr_optca="%optca%" --cr_optcr="%optcr%" --cr_optfile="%optfile%" --cr_outdir="%outdir%" --cr_reslim="%reslim%" --cr_solver="%solver%" --cr_trace="%trace%" --cr_type=%type% $offecho $call =gams crb pf=%gams.scrdir%parmfile.txt $if errorlevel 1 $abort "Error running batch file creation script (crb.gms)" $goto done *=== If UNIX $label p_unixcall $onecho > %gams.scrdir%parmfile.txt --cr_batfile="%batfile%" --cr_gmsopts="%gmsopts%" --cr_iterlim="%iterlim%" --cr_modelfile="%modelfile%" --cr_optca="%optca%" --cr_optcr="%optcr%" --cr_optfile="%optfile%" --cr_outdir="%outdir%" --cr_reslim="%reslim%" --cr_solver="%solver%" --cr_trace="%trace%" --cr_type="%type%" $offecho $call =gams crb.gms pf=%gams.scrdir%parmfile.txt $if errorlevel 1 $abort "Error running batch file creation script (crb.gms)" $goto done *======================================================================== *=== Error messages: --modelfile *======================================================================== $label err_modfile execute "echo ---" execute "echo --- ERROR: no --modelfile specified. Aborting..." execute "echo ---" $goto done $label err_modfile_format execute "echo ---" execute "echo --- ERROR: Incorrect format for --modelfile=%modelfile%. Aborting..." execute "echo ---" $goto done $label done