$title Ten-fold cross validation example $eolcom ! $setglobal num_folds 10 set a set for category 1 /1*1505/ b set for category 2 /1*957/ o observations /1*14/ p folds to perform /1*%num_folds%/ f maximum features to select /1*10/ * Read in the data from the data files parameter a_data(a, o) / $offlisting $include "a_data.inc" $onlisting /; parameter b_data(b, o) / $offlisting $include "b_data.inc" $onlisting /; set a_test(p,a), b_test(p,b) testing sets a_trai(a), b_trai(b) training sets; * Define problem scalar w_tol /1/ features /6/; positive variables a_err(a), sla(a) b_err(b), slb(b); variables c, weight(o), gamma; binary variable y(o); equations w_def1(o), w_def2(o), y_def, c_def, a_def(a), b_def(b); w_def1(o).. weight(o) =l= w_tol*y(o); w_def2(o).. weight(o) =g= -w_tol*y(o); y_def.. sum(o, y(o)) =e= features; c_def.. c =e= sum(a, a_err(a)) + sum(b, b_err(b)); a_def(a).. -sum(o, a_data(a, o)*weight(o)) + gamma + 1 =l= a_err(a) + sla(a); b_def(b).. sum(o, b_data(b, o)*weight(o)) - gamma + 1 =l= b_err(b) + slb(b); model train /all/; train.optfile = 1; $batinclude gentestset.inc "p,a" "p,b" parameter wval(p,o), gval(p); $ontext UpdateType=2 lets GUSS start each scenario from the initial model and then applies the changes of the variable bounds (and other scenario changes). The default GUSS behavior builds up the changes from the previous scenario. Optfile tells the solver to use an option file starting with the second scenario. $offtext set headers report / modelstat, solvestat, objval /; parameter scenrep(p,headers) scopt(*) / SkipBaseCase 1, Optfile 1, LogOption 2 /; set dict / p. scenario.'' scopt. opt. scenrep a_err. upper. aupper b_err. upper. bupper sla. upper. afree slb. upper. bfree weight.level. wval gamma. level. gval / $echo loadmipsol=1 > xpress.opt Parameter aupper(p,a), bupper(p,b), afree(p,a), bfree(p,b); aupper(p,a)$(not a_test(p,a)) = inf; bupper(p,b)$(not b_test(p,b)) = inf; afree(p,a)$a_test(p,a) = inf; bfree(p,b)$b_test(p,b) = inf; option mip=xpress, optcr=0, optca=0, solvelink=%Solvelink.LoadLibrary%; solve train using mip minimizing c scenario dict; display scenrep, gval; $if not set runtraditional $exit * Traditional Solve parameter rep(p,headers); train.optfile = 0; option limrow=0, limcol=0, solprint=silent; loop(p, a_err.up(a) = inf; a_err.up(a)$a_test(p,a) = 0; b_err.up(b) = inf; b_err.fx(b)$b_test(p,b) = 0; sla.fx(a) = 0; sla.up(a)$a_test(p,a) = inf; slb.fx(b) = 0; slb.up(b)$b_test(p,b) = inf; solve train using mip minimizing c; train.optfile = 1; ! use mipstart for the second run rep(p,'modelstat') = train.modelstat; rep(p,'solvestat') = train.solvestat; rep(p,'objval') = train.objval; ); display rep;