Category: Solution to Exercises

Exercise 2.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

*Program 2.1_exercise: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’Example 2.1_exercise: Transportation problem for three suppliers and four customers.’;
%let _data=’c:\sasor\data2_1_exercise.txt’;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set SUPPLIERS;
set CUSTOMERS={1..4};

* Define parameters;
number demand{CUSTOMERS};
number supply{SUPPLIERS};
number cost{SUPPLIERS, CUSTOMERS};

* Define variables;
var X{SUPPLIERS, CUSTOMERS} >= 0;

* Load the supplier set and their amount of supply;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier] supply[Supplier]=col(“supply”);

* Load the customer set and their demand;
read data dcost (where= (supply eq .)) into
{c in CUSTOMERS} ;

* Load the cost of shipment from each supplier to each customer;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier]
{c in CUSTOMERS} ;

* Define objective function;
min obj = sum{s in SUPPLIERS, c in CUSTOMERS} cost[s,c]*x[s,c];

* Define constraints;
con req_supply{s in SUPPLIERS}:
sum{c in CUSTOMERS} x[s,c] <= supply[s]; con req_demand{c in CUSTOMERS}: sum{s in SUPPLIERS} x[s,c] >= demand[c];

con zero{c in CUSTOMERS, s in SUPPLIERS : cost[s,c]=1E10}: x[s,c]=0;

* Solve the model;
solve with lp/solver=primal;

* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [SUPPLIERS CUSTOMERS]
={s in SUPPLIERS, c in CUSTOMERS: x[s,c]^=0}
amount=x;

* End of OPTMODEL Procedure;
quit;
%mend model;

* The report writing macro;
%macro report;
* report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class SUPPLIERS CUSTOMERS ;
var amount;
table SUPPLIERS =” Suppliers”,
CUSTOMERS*amount*sum
/ BOX=’Amount of suppliers to customers’ ;
run;
%mend report;

* The ortrans macro for transportation problem;
%macro ortrans;
%data;
%model;
%report;
%mend ortrans;

%ortrans;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-2-1

Exercise 2.2

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* Using PROC OPTMODEL for Assignment problem;
option nodate ;
%let _title=’Assignment network: solution to exercise 2.2′;
%let _data=’c:\sasor\data2_2_exercise.txt’;


* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out= dataassign
dbms=tab
replace;
getnames=yes;
run;
%mend data;


%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set WORKERS;
set TASKS={1..4};


* Define parameters;
number costprofit{WORKERS, TASKS};


* Define variables;
var X{WORKERS, TASKS} integer >= 0;


* Load the cost/profit matrix ;
read data dataassign
into WORKERS=[worker]
{t in TASKS} <costprofit[worker,t]=col(“task”||t)>;


* Define objective function;
min obj = sum{w in WORKERS, t in TASKS} costprofit[w,t]*x[w,t];


* Define constraints;
con req_supply{w in WORKERS}:
sum{t in TASKS} x[w,t] = 1;


con req_demand{t in TASKS}:
sum{w in WORKERS} x[w,t] = 1;


* Solve the model;
solve with milp;


* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [WORKERS TASKS]
={w in WORKERS, t in TASKS: x[w,t]^=0}
amount=x cp= costprofit[w,t];


* End of OPTMODEL Procedure;
quit;
%mend model;


%macro report;
* Report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class WORKERS TASKS ;
var amount;
table WORKERS =” Workers”,
TASKS*amount*sum
/ BOX=’Assigning workers to each tasks’ ;
run;


* Report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class WORKERS TASKS ;
var cp;
table WORKERS =” Workers”,
TASKS*cp*sum
/ BOX=’Cost-profit of workers to each tasks’ ;
run;
%mend report;


* The orassign macro for assignment problem;
%macro orassign;
%data;
%model;
%report;
%mend orassign;


%orassign;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-2-2

Exercise 2.3

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

*Program 2.3: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’solution to exercise 2.3′;
%let _data=’c:\sasor\data2_3_exercise.txt’;


* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;


%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set SUPPLIERS;
set CUSTOMERS={1..5};


* Define parameters;
number demand{CUSTOMERS};
number supply{SUPPLIERS};
number cost{SUPPLIERS, CUSTOMERS};


* Define variables;
var X{SUPPLIERS, CUSTOMERS} >= 0;


* Load the supplier set and their amount of supply;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier] supply[Supplier]=col(“supply”);


* Load the customer set and their demand;
read data dcost (where= (supply eq .)) into
{c in CUSTOMERS} ;


* Load the cost of shipment from each supplier to each customer;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier]
{c in CUSTOMERS} ;


* Define objective function;
min obj = sum{s in SUPPLIERS, c in CUSTOMERS} cost[s,c]*x[s,c];


* Define constraints;
con req_supply{s in SUPPLIERS}:
sum{c in CUSTOMERS} x[s,c] <= supply[s];


con req_demand{c in CUSTOMERS}:
sum{s in SUPPLIERS} x[s,c] >= demand[c];


con zero{c in CUSTOMERS, s in SUPPLIERS : cost[s,c]=1E10}: x[s,c]=0;

* Solve the model;
solve with lp/solver=primal;

* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [SUPPLIERS CUSTOMERS]
={s in SUPPLIERS, c in CUSTOMERS: x[s,c]^=0}
amount=x;

* End of OPTMODEL Procedure;
quit;
%mend model;

* The report writing macro;
%macro report;
* report the results in a tabulated form;


proc tabulate data=optimout;
title &_title;
class SUPPLIERS CUSTOMERS ;
var amount;
table SUPPLIERS =” Suppliers”,
CUSTOMERS*amount*sum
/ BOX=’Amount of suppliers to customers’ ;
run;
%mend report;

* The ortrans macro for transportation problem;
%macro ortrans;
%data;
%model;
%report;
%mend ortrans;

%ortrans;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-2-3

Exercise 3.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

* SAS macro for minimum cost flow problem: solution to exercise 3.4;
%let _title=’Minimum cost flow problem, solution to exercise 3.1′;
option nodate ;
%let _data=’c:/sasor/Data3_1_exercise.txt’;
%let _tail=origin;
%let _head=destination;
%let _cost=cost;
%let _capac=capacity;
%let _demand=demand;
%let _supply=supply;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;
proc netflow
arcdata=dcost
arcout=arcout1
nodeout=nodeout1;
tail &_tail;
head &_head;
cost &_cost;
capac &_capac;
demand &_demand;
supply &_supply;
run;

%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title;

proc print
data=arcout1;
sum _fcost_;

proc print
data=nodeout1;
run;
%mend report;

* A SAS macro for minimum cost capacitated flow problem;
%macro ormcflow;
%data;
%model;
%report;
%mend ormcflow;

%ormcflow;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-3-1

Exercise 3.2

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

* A SAS procedure for maximum flow problem: solution to exercise 3.2;
%let _title=’Maximum flow problem: solution to exercise 3.2′;
option nodate;
%let _data=’c:/sasor/Data3_2_exercise.txt’;
%let _sourcenode=’a0′;
%let _sinknode=’a8′;
%let _tail=origin;
%let _head=dest;
%let _lowerb=lowerb;
%let _capac=upperb;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;
proc netflow
maxflow
arcdata=dcost
source=&_sourcenode
sink=&_sinknode
arcout=arcout1
nodeout=nodeout1;
minflow &_lowerb;
capac &_capac;
tail &_tail;
head &_head;
run;

%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title ‘, how to get from ‘ &_sourcenode ‘ to ‘ &_sinknode ;

proc print
data=arcout1;
sum _fcost_;

proc print
data=nodeout1;
run;
%mend report;

* A SAS macro for maximum flow problem;
%macro ormaxflow;
%data;
%model;
%report;
%mend ormaxflow;

%ormaxflow;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-3-2

Exercise 3.3

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

* SAS macro for shortest path problem: solution to exercise 3.6.;
%let _title=’An example of a shortest path network, solution to exercise 3.3′;
%let _data=’c:\sasor\Data3_3_exercise.txt’;
%let _sourcenode=’City1′;
%let _sinknode=’City7′;
%let _cost=cost;
%let _tail=origin;
%let _head=dest;
%option nodate;

* The data macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dpath
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;
proc netflow
shortpath
sourcenode=&_sourcenode
sinknode=&_sinknode
arcdata=dpath
arcout=arcout1;
cost &_cost;
tail &_tail;
head &_head;
run;
%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title ‘, how to get from ‘ &_sourcenode ‘ to ‘ &_sinknode ;

* Sort results by origin;
proc sort
data=arcout1
out=result1;
by _fcost_;
run;

* Print results sorted by origin;
proc print
data=result1;
sum _fcost_;
run;

* Sort results by total destination;
proc sort
data=arcout1
out=result2;
by _anumb_;
run;

* Print results sorted by total destination;
proc print
data=result2 (where=(_fcost_ ne 0));
sum _fcost_;
run;
%mend report;

%macro orshortpath;
%data;
%model;
%report;
%mend orshortpath;

%orshortpath;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-3-3

Exercise 4.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/

* A SAS procedure for project network of type Activity-On-Node: solution to exercise 4.1.;
%let _title=’A project network: Activity-On-Node Format: Critical Path ,Analysis, solution to exercise 4.1.’;
%let dcpm=’c:/sasor/Data4_1_exercise.txt’;
%let _activity=task;
%let _duration=days;
option nodate;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&dcpm
out=dcpm
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;
proc cpm
out=cpmout1;
activity &_activity;
duration &_duration;
successor succ1 succ2 succ3;
run;
%put &_ORCPM_;
%mend model;
%macro report;
data cpmout (where=(T_float=0));
set cpmout1;
run;

title &_title;
proc print
data =cpmout1 ;
run;

title &_title;
proc print
data =cpmout ;
sum &_duration;
run;
%mend report;

* Invoke PROC CPM to schedule the project specifying
* the ACTIVITY, DURATION and SUCCESSOR variables;
%macro orcpm;
%data;
%model;
%report;
%mend orcpm;

%orcpm;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-4-1

Exercise 4.2

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* A SAS procedure for project network of type Activity-On-Node: solution to exercise 3.8.;
%let _title=’A project network: Activity-On-Node Format: solution to exercise 4.2.’;
option nodate;
%let _data=’c:/sasor/Data4_2_exercise.txt’;
%let _activity=task;
%let _duration=days;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dpert
dbms=tab
replace;
getnames=yes;
run;

data dpert;
set dpert;
days=(Opti+4*most+pessi)/6;
variance=((Opti-pessi)/6)**2;
run;
%mend data;

* The model building macro;
%macro model;
proc cpm
out=pertout;
activity &_activity;
duration &_duration;
successor succ1 succ2 succ3;
run;
%put &_ORCPM_;
%mend model;

* The report writing macro;
%macro report;

data pertout1 ;
merge dpert pertout;
by task;
run;

data pertout2 (where=(T_float=0));
merge dpert pertout;
by task;
run;

title &_title;
proc print
data =pertout1 ;
run;

proc print
data =pertout2 ;
sum T_float;
sum days;
sum variance;
run;
%mend report;

%macro orpert;
%data;
%model;
%report;
%mend orpert;

%orpert;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-4-2

Exercise 5.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* SAS macro for assembly line balancing problem;
%let _title=’ A project network using PROC OPTMODEL, Assembly line balancing problem: solution to exercise 5.1′;
%let dataalb=’c:/sasor/Data5_1_exercise.txt’;
%let _activity=task;
%let _duration=time;
%let _earliest =Ei;
%let _latest =Li;
%let _nTask=8;
%let _nWork=4;
%let _CycleT=1.8;
%let _outdual=outdual ;
%let _outprimal=outprimal ;
%let _outtable=outtable;
option nodate;


* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&dataalb
out=dataalb
dbms=tab
replace;
getnames=yes;
run;


data dataalb2 (drop= i);
set dataalb;
array W(&_nWork);
do i=1 to &_nWork;
if Ei<=i<=Li then w(i)=1; else w(i)=0;
end;
run;
%mend data;


* The model building macro;
%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set TASKS=1..&_nTask;
set WORKS=1..&_nWork;


* Define variables;
var A{WORKS} binary >=0;
var x{TASKS,WORKS} binary >=0 ;
var z;


* Define parameters;
number Time{TASKS};
number EL{TASKS,WORKS};
number Succ1{TASKS};
number Succ2{TASKS};
number Succ3{TASKS};


* Load time (duration of each task);
read data dataalb2
into [_N_]
Time[_N_]=col(‘time’);


* Load EL (Earliest/Latest workstations to which the tasks can be assigned);
read data dataalb2
into [_N_]
{i in WORKS} <EL[_N_,i]=col(‘W’||i)>;


* Load Succ1 (First Immediate Successor);
read data dataalb2
into [_N_]
Succ1[_N_]=col(‘Succ1’);


* Load Succ2 (Second Immediate Successor);
read data dataalb2
into [_N_]
Succ2[_N_]=col(‘Succ2’);


* Load Succ3 (Third Immediate Successor);
read data dataalb2
into [_N_]
Succ3[_N_]=col(‘Succ3’);


* Define objective function ;
min obj = z;


* Define constrains;
con Objective: z= sum{i in WORKS} A[i];


con assignment_one_workstation {j in TASKS}: sum{i in WORKS} EL[j,i]*x[j,i]=1;


con cycle_time {i in WORKS}: sum{j in TASKS} EL[j,i]*Time[j]*x[j,i]<=&_CycleT;


con precedence1 {j in TASKS: succ1[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ1[j],i]*x[succ1[j],i]<=0; con precedence2 {j in TASKS: succ2[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ2[j],i]*x[succ2[j],i]<=0; con precedence3 {j in TASKS: succ3[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ3[j],i]*x[succ3[j],i]<=0;


con workstation {i in WORKS}: sum{j in TASKS} EL[j,i]*x[j,i]-sum{j in TASKS} EL[j,i]*A[i]<=0;


con utilize_earlier {i1 in WORKS, i2 in WORKS: i2>i1}: A[i2]-A[i1]<=0;


expand;
* Solve the model;
solve with MILP;
%put &_OROPTMODEL_;
* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [TASKS WORKS]
={j in TASKS, i in WORKS}
amount=x;


*End of PROC OPTMODE;
quit;
%mend model;


* The report writing macro;
%macro report;
* report the results in a tabulated form;


proc tabulate data=optimout;
title &_title;
class TASKS WORKS ;
var amount;
table TASKS =” TASKS”,
WORKS*amount*sum
/ BOX=’Assigning Task to Workstation’ ;
run;
%mend report;


* Assembly line problem;
%macro oralbp;
%data;
%model;
%report;
%mend oralbp;


%oralbp;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-5-1

Exercise 6.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* SAS macro for traveling salesman problem: solution to exercise 3.10.;
%let _title=’Traveling salesman problem: solution to exercise 3.10.’;
%let _data=’c:/sasor/Data3_10_exercise.txt’;
%let _travelP=city;
option nodate;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dtsp1
dbms=tab
replace;
getnames=yes;
run;


data dtsp(drop=&_travelP);
set dtsp1;
run;
%mend data;

* The model building macro;
%macro model;
proc ga Matrix1 = dtsp;
call SetEncoding (‘S6’);
call SetObj(‘TSP’,0,’distances’,Matrix1);
call SetCross(‘Order’);
call Initialize(‘DEFAULT’, 6);
call ContinueFor(85);
run;
%mend model;

* The report writing macro;
%macro report;
title &_title;
proc print;
run;
%mend report;

* A SAS macro for traveling salesman problem;
%macro ortsp;
%data;
%model;
%report;
%mend ortsp;
* SAS procedure for traveling salesman problem;

%ortsp;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-6-1

Exercise 7.1

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* SAS macro for line assignment problem: solution to exercise 3.11.;
%let _title=’A line assignment problem using PROC OPTMODEL: solution to exercise 7.1.’;
option nodate ;
%let _dataC=’c:/sasor/Data7_1_C_exercise.txt’;
%let _dataT=’c:/sasor/Data7_1_T_exercise.txt’;
%let _nLine=3;
%let _nBoard=5;
%let _optimout=mysolution;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_dataC
out=dataC
dbms=tab
replace;
getnames=yes;
run;
proc import
datafile=&_dataT
out=dataT
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set LINES=1..&_nLine;
set BOARDS=1..&_nBoard;

* Define variables;
var X{LINES, BOARDS} integer >=0;

* Define parameters;
number cost{LINES, BOARDS};
number time{LINES, BOARDS};
number s{LINES};
number d{BOARDS};

* Load the time matrix;
read data dataT
into [_N_]
{j in BOARDS} <time[_N_,j]=col(‘board’||j)>;

* Load the cost matrix;
read data dataC (where =(line ne “demand”))
into [_N_]
{j in BOARDS} <cost[_N_,j]=col(‘board’||j)>;

* Load the demand array;
read data dataC (where =(line eq “demand”))
into
{j in BOARDS} <d[j]=col(‘board’||j)>;

* Load the supply array;
read data dataC (where= (line ne “demand”))
into [_N_] s[_N_]=col(“supply”);

* Define objective function;
min obj = sum{i in LINES, j in BOARDS} cost[i,j]*X[i,j];

* Define constrains;
con supply_line{i in LINES}:
sum{j in BOARDS} time[i,j]*X[i,j] <=s[i];

con demand_board{j in BOARDS}:
sum{i in LINES} X[i,j] =d[j];

* Solve the model;
solve with milp;
%put &_OROPTMODEL_;

* Exdpand the model;
expand;

* Create optimum values in a SAS dataset ‘optimout’;
create data &_optimout
from [LINES BOARDS]
={i in LINES, j in BOARDS: x[i,j]^=0}
amount=x ;

* Report the results in a tabulated form;
proc tabulate data=&_optimout;
title &_title;
class LINES BOARDS ;
var amount;
table LINES =” from City”,
BOARDS*amount*sum
/ BOX=’Assigning of Board to Line’;
run;

* End of OPTMODEL Procedure;
quit;

%mend model;

%macro report;
* Report the results in a tabulated form;
proc tabulate data=&_optimout;
title &_title;
class LINES BOARDS ;
var amount;
table LINES =” Line”,
BOARDS*amount*sum
/ BOX=’Assigning of Boards to Lines’ ;
run;
%mend report;

%macro orala;
%data;
%model;
%report;
%mend orala;

%orala;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-7-1

Exercise 7.2

/***********************************************************************************
* This program is taken from the following book. If using please cite it as:           *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.                                               *
* For details please visit: http://www.sas-or.com.                                                                *
************************************************************************************/


* Using PROC OPTMODEL for Printed Circuit Board (PCB) Assembly Line Assignment Problem;
option nodate ;
%let _title=’Example 7.2: SMT placement machines or the component allocation problem using PROC OPTMODEL’;
%let _data=’c:/sasor/Data7_2_exercise.txt’;
%let _nMachine=4;
%let _nCompon=7;
%let _optimout=mysolution;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=data12
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set MACHINES=1..&_nMachine;
set COMPONENTS=1..&_nCompon;

* Define variables;
var X{MACHINES, COMPONENTS} integer >=0;
var T;

* Define parameters;
number time{MACHINES, COMPONENTS};
number s{MACHINES};
number c{COMPONENTS};

* Load the time matrix;
read data data12 (where =(machine ne “demand”))
into [_N_]
{j in COMPONENTS} <time[_N_,j]=col(‘compon’||j)>;

* Load the component array;
read data data12 (where =(machine eq “demand”))
into
{j in COMPONENTS} <c[j]=col(‘compon’||j)>;

print c;
* Load the setup-time array;
read data data12 (where= (machine ne “demand”))
into [_N_] s[_N_]=col(“setup”);

* Define objective function;
min obj = T;

* Define constrains;
con Total{i in MACHINES}:
T- s[i] – sum{j in COMPONENTS: time[i,j] ne 1E10} time[i,j]*X[i,j] >=0;
con compon{j in COMPONENTS: j ne 0}: sum{i in MACHINES} X[i,j] =c[j];

con zero{i in MACHINES, j in COMPONENTS : time[i,j]=1E10}: x[i,j]=0;

* Solve the model;
solve with milp;
%put &_OROPTMODEL_;
vexpand;
* Create optimum values in a SAS dataset ‘optimout’;
create data &_optimout
from [MACHINES COMPONENTS]
={i in MACHINES, j in COMPONENTS: x[i,j]^=0}
amount=x ;

* End of OPTMODEL Procedure;
quit;

%mend model;

%macro report;
* Report the results in a tabulated form;
proc tabulate data=&_optimout;
title &_title;
class MACHINES COMPONENTS ;
var amount;
table MACHINES =” Machine”,
COMPONENTS*amount*sum
/ BOX=’Machines or the Component Allocation Problem’;
run;
%mend report;

%macro orcap;
%data;
%model;
%report;
%mend orcap;

%orcap;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/exercise-7-2