Prerequisites
First you need to install and configure the rosepy Python library.
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.