Rosepy is the SimpleRose Python SDK for integrating Python scripts with the remote, distributed Rose solver to solve optimization problems.
Installation
To install the latest version of the rosepy package, use the following command.
pip install https://simplerose-packages-public.s3.us-east-2.amazonaws.com/rosepy/release/rosepy-0.7.0-py3-none-any.whl
Refer to the Rosepy Release Notes for details and links for historical versions of rosepy.
Environment Variables
In order to authenticate you need to create an API key and secret. Follow the steps in the Creating an API key and secret article if you have not yet created an API key and secret.
- ROSEPY_API_KEY_ID - This is the ID of your API key
- ROSEPY_API_KEY_SECRET - This is the secret of your API key
You will also need to specify this environment variable that links your API key to your organization
- ROSEPY_ACCOUNT - this is your organization name that your account is tied to, this can be found at the top of your API keys settings page.
For other tips and information on using API keys, see our API key best practices.
Using the Rose solver in Pyomo
To solve optimization models in Pyomo with the Rose solver, specify "rose" in the Pyomo solver factory, as shown in the example below.
import pyomo.environ as pyo
from rosepy.pyomo.pyomo_interface import PyomoInterface
# Create the solver instance for the Rose solver
solver = pyo.SolverFactory('rose')
# Solve the model
result = solver.solve(model)
-
pyo.SolverFactory('rose')
initializes the Rose solver. -
solver.solve(model)
solves the defined model using the Rose solver.
For a complete example, see the full model demonstrated below.
Example rosepy script
The simplest way to define and submit an optimization problem with rosepy is through its Pyomo integration. Below is an example script showcasing the brewery optimization problem. The script demonstrates how to use Python's Pyomo package in combination with rosepy to solve the problem efficiently.
import pyomo.environ as pyo
from rosepy.pyomo.pyomo_interface import PyomoInterface
# Create the model
model = pyo.ConcreteModel(name='Bubbles Brewery')
# Decision variables
model.light_beer = pyo.Var(within=pyo.NonNegativeReals)
model.dark_beer = pyo.Var(within=pyo.NonNegativeReals)
# Objective function
model.objective = pyo.Objective(expr=13*model.light_beer + 23*model.dark_beer,
sense=pyo.maximize)
# Constraints
model.corn = pyo.Constraint(expr=5*model.light_beer + 15*model.dark_beer <= 480)
model.malt = pyo.Constraint(expr=4*model.light_beer + 4*model.dark_beer <= 160)
model.hops = pyo.Constraint(expr=35*model.light_beer + 20*model.dark_beer <= 1190)
# Create the Rose Solver Pyomo Interface
solver = pyo.SolverFactory('rose')
# Solve the problem
result = solver.solve(model)
# Print the results
print(f"Profit: {model.objective()}")
print(f"Light beer: {model.light_beer.value}")
print(f"Dark beer: {model.dark_beer.value}")
Comments
0 comments
Please sign in to leave a comment.