Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-15-2018 01:01 PM
(634 views)

Hi,

I'm not sure if it's the right forum to ask any question related to sasoptpy here.Please delete it if it's not the right forum

I'm doing optimization in python using sasoptpy.My objective function looks like z=(a*p+q*b)*c. where all of these variables are columns of a data frame.

a,p,q,b are my variables & c is constant.

I'm defining a,b,p,q in following way va=m.add_variable(a, name='va',lb=LB2,ub=UB2). But not sure how to define constant c. Can you please suggest.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi,

Constants are defined as regular Python values (floats or integers), such as

`c = 5`

and can be used in expressions.

If you would like to use the value in dataframe, you can access the actual value as:

`c = df.at[0, 'c']`

Here's a full example:

```
import pandas as pd
# Define dataframe
df = pd.DataFrame([[4, 5, 6]], columns=['a', 'b', 'c'])
# Define model
m = so.Model(name='model1', session=None)
# Get columns
a = df['a']
b = df['b']
c = df.at[0, 'c']
# Define variables
va = m.add_variables(a.index.tolist(), name='va')
vb = m.add_variables(b.index.tolist(), name='vb')
# Set objective
obj = m.set_objective((va.mult(a) + vb.mult(b)) * c, name='obj')
# Print the objective function
print(m.get_objective())
```

This will print the objective function as

`24 * va[0] + 30 * vb[0]`

If this doesn't solve your problem, could you provide a minimal working example?

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi,

Constants are defined as regular Python values (floats or integers), such as

`c = 5`

and can be used in expressions.

If you would like to use the value in dataframe, you can access the actual value as:

`c = df.at[0, 'c']`

Here's a full example:

```
import pandas as pd
# Define dataframe
df = pd.DataFrame([[4, 5, 6]], columns=['a', 'b', 'c'])
# Define model
m = so.Model(name='model1', session=None)
# Get columns
a = df['a']
b = df['b']
c = df.at[0, 'c']
# Define variables
va = m.add_variables(a.index.tolist(), name='va')
vb = m.add_variables(b.index.tolist(), name='vb')
# Set objective
obj = m.set_objective((va.mult(a) + vb.mult(b)) * c, name='obj')
# Print the objective function
print(m.get_objective())
```

This will print the objective function as

`24 * va[0] + 30 * vb[0]`

If this doesn't solve your problem, could you provide a minimal working example?

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.