This example demonstrates how to rewrite certain models for more efficient processing. Sometimes optimization models that run out of memory during problem generation can be rewritten to take advantage of sparsity to use memory more efficiently. This often occurs when a large array is modeled in a dense format but most of its entries are zeros. Usually, the array provides problem coefficients or it contains optimization variables.
The model for this example solves the facility location problem that is described in Example 7.3. This example is concerned with the resources that are required for PROC OPTMODEL problem generation and solver initialization. So the size of the problem has been increased, but the model has also been modified to make it easier to solve. In order to handle the larger problem size, the model eliminates a large number of the potential assignments of customers to facilities based on distance, making the problem sparse.
The following code generates a random instance of the facility location problem:
%let NumCustomers = 1500; %let NumSites = 250; %let SiteCapacity = 50; %let MaxDemand = 10; %let xmax = 200; %let ymax = 100; %let seed = 938; /* generate random customer locations */ data cdata(drop=i); length name $8; do i = 1 to &NumCustomers; name = compress('C'||put(i,best.)); x = ranuni(&seed) * &xmax; y = ranuni(&seed) * &ymax; demand = 1; output; end; run; /* generate random site locations and fixed charge */ data sdata(drop=i); length name $8; do i = 1 to &NumSites; name = compress('SITE'||put(i,best.)); x = ranuni(&seed) * &xmax; y = ranuni(&seed) * &ymax; fixed_charge = 300 * (abs(&xmax/2-x)/&xmax + abs(&ymax/2-y)/&ymax); output; end; run;
The following code uses a dense version of the facility location model. This model is equivalent to the model from Example 7.3 except for the added constraint distance_at_most_30
. This constraint eliminates from consideration the assignment of customers to facilities over long distances by forcing the
corresponding Assign
variables to 0. The RELOBJGAP= option prevents the solver from stopping close to the optimal solution because of the relatively
large fixed costs.
proc optmodel; performance details; set <str> CUSTOMERS; set <str> SITES init {}; /* x and y coordinates of CUSTOMERS and SITES */ num x {CUSTOMERS union SITES}; num y {CUSTOMERS union SITES}; num demand {CUSTOMERS}; num fixed_charge {SITES}; /* distance from customer i to site j */ num dist {i in CUSTOMERS, j in SITES} = sqrt((x[i] - x[j])^2 + (y[i] - y[j])^2); read data cdata into CUSTOMERS=[name] x y demand; read data sdata into SITES=[name] x y fixed_charge; var Assign {CUSTOMERS, SITES} binary; var Build {SITES} binary; min CostNoFixedCharge = sum {i in CUSTOMERS, j in SITES} dist[i,j] * Assign[i,j]; min CostFixedCharge = CostNoFixedCharge + sum {j in SITES} fixed_charge[j] * Build[j]; /* each customer assigned to exactly one site */ con assign_def {i in CUSTOMERS}: sum {j in SITES} Assign[i,j] = 1; /* if customer i assigned to site j, then facility must be built at j */ con link {i in CUSTOMERS, j in SITES}: Assign[i,j] <= Build[j]; /* each site can handle at most &SiteCapacity demand */ con capacity {j in SITES}: sum {i in CUSTOMERS} demand[i] * Assign[i,j] <= &SiteCapacity * Build[j]; /* do not assign customer to site more than 30 units away */ con distance_at_most_30 {i in CUSTOMERS, j in SITES: dist[i,j] > 30}: Assign[i,j] = 0; /* solve the MILP */ solve with milp/timetype=real relobjgap=1e-8; quit;
If you inspect the log after running the preceding code, then you will see that the OPTMILP presolver has pruned down the problem size considerably. If also you run the code with the SAS option FULLSTIMER enabled on a 64-bit system, then you will notice that about 1.3GB of memory is required for the OPTMODEL step.
The solution and timing results for the dense model are shown in Output 5.7.1. The PERFORMANCE DETAILS statement from the model requests display of the task timing table.
Output 5.7.1: Dense Model Results
Solution Summary | |
---|---|
Solver | MILP |
Algorithm | Branch and Cut |
Objective Function | CostFixedCharge |
Solution Status | Optimal |
Objective Value | 17994.210085 |
Relative Gap | 0 |
Absolute Gap | 0 |
Primal Infeasibility | 6.3360056E-7 |
Bound Infeasibility | 7.0958916E-7 |
Integer Infeasibility | 1.0737316E-6 |
Best Bound | 17994.210085 |
Nodes | 3 |
Iterations | 13491 |
Presolve Time | 14.52 |
Solution Time | 211.44 |
Procedure Task Timing | ||
---|---|---|
Task | Time (sec.) |
% Time |
Problem Generation | 9.58 | 4.23% |
Solver Initialization | 3.25 | 1.44% |
Code Generation | 0.21 | 0.09% |
Solver | 212.32 | 93.74% |
Solver Postprocessing | 1.15 | 0.51% |
The best approach for reducing the memory requirements is to eliminate the Assign
variables that are always going to be 0. This is accomplished in the following sparse version of the code. Instead of indexing
Assign
over the crossproduct of CUSTOMERS
and SITES
, now the code defines a new set of pairs that satisfy the distance requirement, CUSTOMERS_SITES
. This set replaces the constraint distance_at_most_30
in the dense model. The objective and constraints have been modified to use the new indexing scheme, with implicit set slicing
(as described in the section More on Index Sets) for constraints assign_def
and capacity
.
proc optmodel; performance details; set <str> CUSTOMERS; set <str> SITES init {}; /* x and y coordinates of CUSTOMERS and SITES */ num x {CUSTOMERS union SITES}; num y {CUSTOMERS union SITES}; num demand {CUSTOMERS}; num fixed_charge {SITES}; /* distance from customer i to site j */ num dist {i in CUSTOMERS, j in SITES} = sqrt((x[i] - x[j])^2 + (y[i] - y[j])^2); read data cdata into CUSTOMERS=[name] x y demand; read data sdata into SITES=[name] x y fixed_charge; set CUSTOMERS_SITES = {i in CUSTOMERS, j in SITES: dist[i,j] <= 30}; var Assign {CUSTOMERS_SITES} binary; var Build {SITES} binary; min CostNoFixedCharge = sum {<i,j> in CUSTOMERS_SITES} dist[i,j] * Assign[i,j]; min CostFixedCharge = CostNoFixedCharge + sum {j in SITES} fixed_charge[j] * Build[j]; /* each customer assigned to exactly one site */ con assign_def {i in CUSTOMERS}: sum {<(i),j> in CUSTOMERS_SITES} Assign[i,j] = 1; /* if customer i assigned to site j, then facility must be built at j */ con link {<i,j> in CUSTOMERS_SITES}: Assign[i,j] <= Build[j]; /* each site can handle at most &SiteCapacity demand */ con capacity {j in SITES}: sum {<i,(j)> in CUSTOMERS_SITES} demand[i] * Assign[i,j] <= &SiteCapacity * Build[j]; /* solve the MILP */ solve with milp/timetype=real relobjgap=1e-8; quit;
The log from running the preceding code shows that the OPTMILP presolver does not find anything to improve with this version of the model. On a 64-bit system, the FULLSTIMER option shows that memory requirements have been reduced to about 580MB, less than half the requirements of the previous model.
The solution and timing results for the dense model are shown in Output 5.7.2. Note that the dense model (Output 5.7.1) and the sparse model (Output 5.7.2) are equivalent after presolver processing and generate the same result using similar amounts of solver time. On the other hand, problem generation time is significantly reduced as are other times including presolve time. Both models used the solver option TIMETYPE=REAL so that all times are reported in seconds of real time.
Output 5.7.2: Sparse Model Results
Solution Summary | |
---|---|
Solver | MILP |
Algorithm | Branch and Cut |
Objective Function | CostFixedCharge |
Solution Status | Optimal |
Objective Value | 17994.210085 |
Relative Gap | 0 |
Absolute Gap | 0 |
Primal Infeasibility | 6.3360056E-7 |
Bound Infeasibility | 7.0958916E-7 |
Integer Infeasibility | 1.0737316E-6 |
Best Bound | 17994.210085 |
Nodes | 3 |
Iterations | 13491 |
Presolve Time | 10.39 |
Solution Time | 215.41 |
Procedure Task Timing | ||
---|---|---|
Task | Time (sec.) |
% Time |
Problem Generation | 2.43 | 1.11% |
Solver Initialization | 0.39 | 0.18% |
Code Generation | 0.03 | 0.01% |
Solver | 215.73 | 98.65% |
Solver Postprocessing | 0.09 | 0.04% |