在Python中,做一个简单的Gurobi编码
时间: 2024-05-03 10:20:45 浏览: 42
基于pyomo的python+gurobi实例
5星 · 资源好评率100%
Sure, Gurobi is a popular optimization solver and it can be easily integrated with Python. Here is an example of a simple Gurobi optimization problem:
```python
import gurobipy as gp
# Create a new model
model = gp.Model("simple")
# Add decision variables
x = model.addVar(name="x")
y = model.addVar(name="y")
# Set objective function
model.setObjective(x + y, gp.GRB.MINIMIZE)
# Add constraints
model.addConstr(x + 2 * y >= 1, name="c1")
model.addConstr(2 * x + y >= 1, name="c2")
# Optimize the model
model.optimize()
# Print the solution
print(f"Optimal solution: x={x.x}, y={y.x}")
```
In this example, we are creating a new Gurobi model and adding two decision variables `x` and `y`. Then, we are setting the objective function to be the sum of `x` and `y` and specifying that it should be minimized. Finally, we are adding two constraints and optimizing the model to get the optimal values of `x` and `y`.
阅读全文