Category: Chapter 2

Program 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: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’Example 2.1: Transportation problem for four suppliers and five customers.’;
%let _data=’c:\sasor\data2_1.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/program-2-1

Program 2.2


/****************************************************************************
* This program is taken from the following book:                            *
* Emrouznejad, A. and Ho, W. (2010) Applied Operational Research with SAS,  *
* Chapman and Hall/CRC – 284 Pages.                                         *
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


* Using PROC OPTMODEL for Assignment problem;
option nodate ;
%let _title=’Assignment network for five workers and five tasks.’;
%let _data=’c:\sasor\data2_2.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..5};

* 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} ;

* 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/program-2-2

Program 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=’Example 2.3: Transshipment problem for two suppliers, two warehouses and two customers.’;
%let _data=’c:\sasor\data2_3.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} <demand[c]=col(“Cstmer”||c)>;

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

* 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/program-2-3

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

Chapter 2: Transportation Models

  • Transportation Problem
  • Assignment Problem
  • Transshipment Problem

Chapter 2 - Programs

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