Код: Выделить всё
import gurobipy as gp
from gurobipy import GRB
# Create the model
model = gp.Model("quadratic_optimization_with_slack")
# Variables
x = model.addVar(name="x", lb=0) # x >= 0
y = model.addVar(name="y", lb=0) # y >= 0
slack = model.addVar(name="slack", lb=0) # Slack variable for relaxation. If slack = 0 --> Model is infeasible
# Objective: Minimize 2x^2 + y
model.setObjective(2 * x**2 + y, GRB.MINIMIZE)
# Constraints
model.addConstr(x - 5 + slack == 0, name="constraint_1") # (slack allows relaxation)
Подробнее здесь: [url]https://stackoverflow.com/questions/79206398/how-to-get-the-result-of-the-objective-function-while-using-a-slack-variable[/url]
Мобильная версия