BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hello, i have to solve an optimization problem.

I attached two slides where you can see optimization problem(maximum revenue) with constraints and example.

What procedure can i use to solve problem, and how i write this optimization? I never used OR before.

Thank you

 

 


Slide1.JPGSlide2.JPG
11 REPLIES 11
RobPratt
SAS Super FREQ

You can use the OPTMODEL procedure in SAS/OR to formulate and solve optimization problems.  There is a whole book of examples to help you.

 

AlexeyS
Pyrite | Level 9

The problem thay i don't have sas SAS/OR, only SAS/IML. Can i use it to solve optimization?

Thank you

Ksharp
Super User
Here are several questions.
What does Px stand for , how do you define Px ?
How do you define x1=1 ? when you choose both cola and  peanuts ? or just one of them ?

Here is iML code for your  example by Genetic Algorithm.



proc iml;
start func(x) global(x1,x2,x3,m,c,p,items,idx); 
if any(idx=sum(x)) then do;
 temp=p[loc(x)];
 xx=all(element(x1,temp))||
    all(element(x2,temp))||
    all(element(x3,temp)) ;   
 obj= sum(m#xx)-sum(c#x); 
end;
else obj=-99999;
 return (obj);
finish;


items=3;

p={cola peanuts cheese beer};
m={10 20 30};
c={5 3 1 4};
n=ncol(p);
idx=1:items;
encoding=repeat({0,1},1,n);

x1={cola peanuts};
x2={peanuts cheese};
x3={peanuts beer};

id=gasetup(2,n,12345678);
call gasetobj(id,1,"func");
call gasetcro(id,0.95,2);
call gasetmut(id,0.95,3);
call gasetsel(id,100,1,1);
call gainit(id,10000,encoding); 

niter = 1000;
do i = 1 to niter;
 call garegen(id);
 call gagetval(value, id);
end;
call gagetmem(mem, value, id, 1);

print (p[loc(mem)])[l="members"],value[l = "Max Value:"] ;
call gaend(id);
quit;




OUTPUT:

members
PEANUTS
CHEESE
BEER


Max Value:
42

RobPratt
SAS Super FREQ

GA provides no measure of optimality for the solution it returns, and so you should probably use that only when a specialized solver is not available.  In particular, if you are going to use IML to solve a MILP, you might as well call the MILPSOLVE subroutine, which invokes the same MILP solver that is accessible from PROC OPTMODEL.  The PROC OPTMODEL syntax will more closely match your algebraic formulation.

AlexeyS
Pyrite | Level 9

Unfortunately i don't have SAS/OR, only SAS/IML. Can i solve this optimization with SAS/IML?

Please see attached file.  Thank you


Optimization.jpg
AlexeyS
Pyrite | Level 9

Unfortunately i don't have SAS/OR, only SAS/IML. I attached more normal explanation. please see it. I have a lot of itemsets(X), they can be different bu size, for example, in one you can gave only milk, in other you have milk and sugar.

i have two decision variables to be optimized P and Q.(they binary variables, and got two values : 1 or zero, 1 means that product was choosen). Thank you for your help

 

 


Optimization.jpg
Ksharp
Super User
Yeah. GA can't guarantee the optimal solution. But if the number of items is not too big, I think GA is ok .
And change number of iteration in my code to be bigger and make sure to get better solution.

I still am not understand Q1>=P1 .
Q1,P1  in (0 ,1) ?
How do you define P1=1 , when x1=both cola and peanuts ?

AlexeyS
Pyrite | Level 9
That means when i choose itemset X1, and this itemset adds profit i.e P1=1, then every product inside itemset X1 have to be choosen, i.e i itemset X1 has three products then all Q1, Q2 and Q3 have to be equal to 1. P and Q decision binary variables. The problem that i have to data sets, one that include all itemsets and the second all products and their cost. I really dont know if sas can solve this problem. Maybe try other language?
Ksharp
Super User
So My GA code is doing that. I also want see @rob post OR code, that might be your best choice.
AlexeyS
Pyrite | Level 9

Thank you for your help

RobPratt
SAS Super FREQ

Here it is in PROC OPTMODEL with the MILP solver:

proc optmodel;
   set ITEMS = /cola peanuts cheese beer/;
   num cost {ITEMS} = [5 3 1 4];
   set ITEM_SETS = 1..3;
   set <str> X {ITEM_SETS};
   X[1] = /cola peanuts/;
   X[2] = /peanuts cheese/;
   X[3] = /peanuts beer/;
   num m {ITEM_SETS} = [10 20 30];

   var P {ITEM_SETS} binary;
   var Q {ITEMS} binary;
   con SelectThreeItems:
      sum {i in ITEMS} Q[i] <= 3;
   con SetImpliesItem {s in ITEM_SETS, i in X[s]}:
      P[s] <= Q[i];
   max Z = sum {s in ITEM_SETS} m[s] * P[s] - sum {i in ITEMS} cost[i] * Q[i];

   solve;
   print P Q;
quit;

SAS Output

Solution Summary
Solver MILP
Algorithm Branch and Cut
Objective Function Z
Solution Status Optimal
Objective Value 42
   
Relative Gap 0
Absolute Gap 0
Primal Infeasibility 0
Bound Infeasibility 0
Integer Infeasibility 0
   
Best Bound 42
Nodes 1
Iterations 4
Presolve Time 0.00
Solution Time 0.00

[1] P
1 0
2 1
3 1

[1] Q
beer 1
cheese 1
cola 0
peanuts 1

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 2173 views
  • 1 like
  • 3 in conversation