Optimization using SAS OR package

There are multiple OR packages to solve the optimization problems at academic as well as industry levels. One of the best OR packages that I am using is the SAS OR package. It has a capability to model highly complex problems and program it if required. Most of the optimization algorithms have been implemented on SAS.  In SAS we call Procedure to any function. There are many procedures which are SAS implementation of the OR algorithms. But there is a procedure called Proc Opt Model using which we can call different algorithms to solve any optimization problems. We can write  programs also inside it implement some constraints which can not be directly coded. I think this is very powerful tool for an operational research analyst. I use this procedure to model the business problems. I learnt it myself by going through user documentation. The SAS document explains clearly how can we use SAS OR packages with examples. In fact all the examples given in the HP Williams Model Building in Mathematical Programming have been fully solved using SAS  OR package.

Like any other Optimization package we have some standard steps to use it to solve an OR problem.

1. Index Declaration Indexing  for parameters and decision variables.
   For character indices
                              set  <str>Item= {'a','b','c'};
                              set  <str>Country_code   = {'HK','UK','UAE'};
     For numeric indices
                             set  machine_code ={101,102,103} ;

If number of indices is large then we can read it from an external file or SAS data also  which is explained later

2. Parameter declaration:  using this we can declare the parameters like cost of the production at using a machine for an item type. We can also declare the demand s at each location. This is  a declaration only. We are not reading the data here.


                     num  Cost{Item, Machine_code};
                     num   Demand{Item,Country_code  };

One can notice that indices declared earlier are being used here in the parameter declaration. Once parameters are declared along with the necessary indices we can read the data. Reading the data differs depends upon the structure of the raw data (SAS data)

3. Data Read statements.

Read data  ProductionCost into [Item, Machine_code] Cost;
        Read data  DemandData into [Item, Country_code   ] Demand;

Cost data look like the table below

item   machine_code   Cost
a       101                   45.00
a       102                   40.00
a       103                    43.00
b       101                   41.00
b       102                   40.00
b      103                    46.00
c       101                   35.00
c       102                   44.00
c       103                    46.00


DemandData  look like the table below

item   Country_code   Demand
a       HK                   4005.00
a       UK                   4010.00
a       UAE                  4300.00
b       HK                   4100.00
b       UK                   4120.00
b      UAE                    2046.00
c       HK                     3500.00
c       UK                     4440.00
c       UAE                    4600.00


Here we could also read the indices using similar statement. But in that case any other read data statement which used this index can be declared after the declaration of the index otherwise Proc Optmodel will give an error.
4. Decision Variable Declaration:  Now we should declare the decision variables using the var statements

Var Production  {item,machine_code} >= 0;  /* non negative decision variable*/



5. Objective Function Declaration: We can use either max  or min  to declare the objective function depending upon the optimization problem. The example, taken here is the cost minimization problem so I will use  min .

min  TotalCost= /**Cost**/ sum {i in item, m in machine_code}  Cost[i,m]*Production[i,m];

i in the sum min statement varies by values in the item and m by machine.  It is similar to stating objective function using the mathematical form.

6.  Declaration of the constraints:  Constraint in the optimization problem can be declared using the  Constraint  keyword. We also have features to assign a name to constraints.


 CONSTRAINT  Max_demand {i in item, c in Country_code }:
  sum {m in machine_code}Production[i,m]  <= Demand[i,c];

Verbally   Supply of an item from all the Machines to a country should not exceed the demand at that country.

7. Invoking the solver: We can invoke any algorithm solve the problem. Here I am invoking Linear programming (LP)

Solve with lp /*** Invokes LP solver***/

Here we have a flexibility to invoke algorithm depending on the size and type of the problem, Heuristics can also be invoked.

8. Capturing the Output (Decision variables): Once Optimization problem is solved by default Proc optmodel produces the objective value only. To get the values of the decision variables one has to write simple SAS code.

create data solution /***user defined data set name****/
      from [Item, Machine_code ] = {i in Item, m in Machine_codeProduction[i,m].sol ne 0}

      Prod_Quantity=Production;
Quit;

 Prod_Quantity will be name of the field which has optimize decision variable values. 

The table Solution will look like

Item   Machine_code    Prod_Quantity 
a       101                  
a       102                  
a       103                  
b       101                  
b       102                  
b      103                  
c       101                  
c       102                
c       103                    



The following lines reproduces the Proc Opt model syntax from the SAS OR documentation. There are many functionality which have not be used in the above example but  depending upon the problem can be used.

PROC OPTMODEL options ;
Declaration Statements:
CONSTRAINT constraints ;
IMPVAR optimization expression declarations ;
MAX objective ;
MIN objective ;
NUMBER parameter declarations ;
PROBLEM problem declaration ;
SET Œ < types >  parameter declarations ;
STRING parameter declarations ;
VAR variable declarations ;
Programming Statements:
Assignment parameter = expression ;
CALL name Œ ( expressions )  ;
CLOSEFILE files ;
CONTINUE ;
CREATE DATA SAS-data-set FROM columns ;
DO ; statements ; END ;
DO variable = specifications ; statements ; END ;
DO UNTIL ( logic ) ; statements ; END ;
DO WHILE ( logic ) ; statements ; END ;
DROP constraint ;
EXPAND name Œ / options  ;
FILE file ;
FIX variable Œ = expression  ;
FOR { index-set } statement ;
IF logic THEN statement ; Œ ELSE statement  ;
LEAVE ;
.null statement/ ;
PRINT print items ;
PUT put items ;
QUIT ;
READ DATA SAS-data-set INTO columns ;
RESET OPTIONS options ;
RESTORE constraint ;
SAVE MPS SAS-data-set Œ ( OBJECTIVE | OBJ ) name  ;
SAVE QPS SAS-data-set Œ ( OBJECTIVE | OBJ ) name  ;
SOLVE Œ WITH solver  Œ OBJECTIVE name  Œ RELAXINT  Œ / options  ;
STOP ;
UNFIX variable Œ = expression  ;
USE PROBLEM problem ;

Comments

Popular posts from this blog

Solution for ERROR: Some character data was lost during transcoding in the dataset

How to check whether a SAS dataset exist or not and throw an error in the log ?

2018 plan for getting expertise in Machine Learning and Deep Learning