Skip to content

Instantly share code, notes, and snippets.

@daynebatten
Created February 29, 2016 16:34
Show Gist options
  • Save daynebatten/2f694519d274639de717 to your computer and use it in GitHub Desktop.
Save daynebatten/2f694519d274639de717 to your computer and use it in GitHub Desktop.
proc optmodel;
num num_patterns init 0;
set PATTERNS = 1..num_patterns;
set cuts;
number choices {patterns, cuts};
number prices {patterns};
number lumber {patterns};
number targets {cuts};
number length {cuts};
read data cuts into cuts=[_n_] targets=number length;
/* use CLP solver to generate all possible patterns */
set RAWS;
num capacity {RAWS};
num price {RAWS};
read data lumber into RAWS=[_N_] capacity=length price;
num r;
var Y {cuts} >= 0 integer;
con CapacityCon:
sum {j in cuts} length[j] * Y[j] <= capacity[r];
problem CLP_Problem include Y CapacityCon;
use problem CLP_Problem;
do r = RAWS;
put r=;
solve with CLP / findallsolns;
for {s in 1.._NSOL_} do;
num_patterns = num_patterns + 1;
for {j in cuts} choices[num_patterns,j] = Y[j].sol[s];
prices[num_patterns] = price[r];
lumber[num_patterns] = capacity[r];
end;
end;
put num_patterns=;
/* Selections will contain the patterns in the optimal solution */
var selections {patterns} >= 0 integer;
/* Minimizing total cost... */
minimize total_cost = sum{p in patterns} prices[p] * selections[p];
/* Must cut enough of each length! */
con target_con {c in cuts}:
sum {p in patterns} selections[p] * choices[p, c] = targets[c];
problem MILP_Problem include selections total_cost target_con;
use problem MILP_Problem;
solve;
create data outdata(drop=i) from [i] = {i in patterns: selections[i].sol > 0.5}
chosen = selections[i] {j in cuts} <col('choice'||j)=choices[i,j]> lumber;
quit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment