Hello all,
This may be an easy question dealing with the syntax or structure of PROC OPTMODEL.
I have simplified my problem here, just to focus what I want to know.
Assume I have pairs of cities, Origin and Destination, my Objective Function Cost is defined as-
Origin Destination Type Cost Distance
A1 B1 One 5 1
A1 B2 One 5 2
A1 B3 One 5 3
B1 C1 Two 8 4
B1 C2 Two 8 5
B2 C1 Two 8 6
B2 C2 Two 8 7
B3 C1 Two 8 8
B3 C2 Two 8 9
min Total_Cost =
(sum{<a, b> in ARCS, p in PRODUCTS} ((Flow[a,b,p] * Distance[a,b] * Cost[a,b]) ;
Data-
This is basic structure- where I just read cost from the data table.
But now, we want to pass Cost as a Macro variable. As you can see, Cost is 5 for Type One and 8 for Type Two Arc. So, I would have %MACRO Name (cost_one, cost_two) at the top of PRO OPTMODEL. Obviously, now with conditional usage of variable Cost- the above Objective Function is not the same. It needs be broken down for type One and Two Arcs, as below.
min Total_Cost =
(sum{<o, d> in ARCS: o in SET_A, p in PRODUCTS} ((Flow[o,d,p] * Distance[o,d] * &cost_one]) + (sum{< o,d > in ARCS: o in SET_B, p in PRODUCTS } ((Flow[o,d,p] * Distance[o,d] * &cost_two]);
o in SET_A represent ARCS in Type One; o in SET_B represent ARCS in Type Two
We know, “{<o, d> in ARCS: o in SET_A “ --- such conditional statement to pick only selected ARCS works in a constraint declaration, but Does NOT in Objective function.
My question is, how can we break down this Objective Function- to use Cost as Macro variable for different types of Arcs ? How would the above function (which is written intuitively) be programmed to fit in syntax of SAS/OR?
Am open to suggestion, alternate or easier way to accomplish the same.
-Thanks,
The logical condition has to appear last, after all index-set-items:
SAS/OR(R) 13.2 User's Guide: Mathematical Programming
Here are two ways to accomplish what you want:
sum{<o, d> in ARCS, p in PRODUCTS: o in SET_A}
sum{<o, d> in ARCS: o in SET_A} sum{p in PRODUCTS}
The logical condition has to appear last, after all index-set-items:
SAS/OR(R) 13.2 User's Guide: Mathematical Programming
Here are two ways to accomplish what you want:
sum{<o, d> in ARCS, p in PRODUCTS: o in SET_A}
sum{<o, d> in ARCS: o in SET_A} sum{p in PRODUCTS}
Can you clarify it more . What is constraint ? What is your object ? Maybe I could get job done by Data Step or IML .
Xia Keshan
I think I misunderstood something about the data earlier. So here is a better answer. It is still best to define the macro variables early IMHO.
It might be simpler to continue to incorporate the macro variable earlier, and leave the origin-based cost as is. That way the macro references are in one place:
%let cost_one = 5;
%let cost_two = 8;
proc optmodel;
set NODES;
set ARCS init NODES cross NODES;
set SET_A, SET_B;
num cost{<o,d> in ARCS} = if o in SET_A then &cost_one
else if o in SET_B then &cost_two
else .;
quit;
Then if there are any errors they get detected early, and you can still use cost[o,d] to create reports and diagnostics.
@ All- Thank you so much, this helps and gives me a direction at least to develop on this further.
@Rob- Thanks Rob, I should have thought about it, but this helps. Thanks again.
@Leo- Great approach, sure I will think on this, once the basic one starts working. Thanks for suggestion.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.