Advertisement
Dido09

Untitled

Nov 24th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. from gurobipy import Model, GRB
  2.  
  3. # Създаване на модела
  4. model = Model("Nonlinear Constraints Example")
  5.  
  6. # Променливи
  7. x1 = model.addVar(name="x1")
  8. x2 = model.addVar(name="x2")
  9.  
  10. # Целева функция
  11. model.setObjective(x1 + x2, GRB.MAXIMIZE)
  12.  
  13. # Ограничения
  14. model.addConstr(x1**2 + x2**2 <= 1, "CircleConstraint")  # Квадратично ограничение
  15. model.addConstr(x1 + x2 <= 1, "LinearConstraint")  # Линейно ограничение
  16.  
  17. # Оптимизация
  18. model.optimize()
  19.  
  20. # Резултати
  21. for v in model.getVars():
  22.     print(f"{v.varName}: {v.x}")
  23. print(f"Objective Value: {model.ObjVal}")
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement