BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MikeCarter
Calcite | Level 5

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-

  • ARCS is set of [Origin, Destination].
  • PRODUCTS is a set containing Product Types.
  • SET_A is set {A1,A2,A3} ; SET_B is {B1,B2,B3}

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,

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

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}

View solution in original post

4 REPLIES 4
RobPratt
SAS Super FREQ

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}

Ksharp
Super User

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

LeoLopes
SAS Employee

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.

MikeCarter
Calcite | Level 5

@ 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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1057 views
  • 6 likes
  • 4 in conversation