See algebraic expressions of problem in debug mode or print
回答済みHello,
I have the following toy problem:
import gurobipy as gp
from gurobipy import GRB
# Create a new model
model = gp.Model("quadratic_constraint_problem")
# Add variables
x1 = model.addVar(name="x1", lb=0)
x2 = model.addVar(name="x2", lb=0)
# Set objective function
model.setObjective(x1 + 2 * x2, GRB.MINIMIZE)
# Define the quadratic constraint matrix Q
Q = [[2, 1],
[1, 2]]
# Add quadratic constraint: x^T Q x <= 1
quad_expr = gp.QuadExpr()
quad_expr += Q[0][0] * x1 * x1 + Q[0][1] * x1 * x2
quad_expr += Q[1][0] * x2 * x1 + Q[1][1] * x2 * x2
model.addQConstr(quad_expr <= 1, "quad_constr")
# Optimize model
model.optimize()
# Print the results
if model.status == GRB.OPTIMAL:
print("Optimal solution found:")
print(f"x1 = {x1.x}")
print(f"x2 = {x2.x}")
print(f"Objective value = {model.objVal}")
else:
print("No optimal solution found.")
How can I see the algebraic expression of this problem? This might be very useful, specifically when we have larger scale Q and x.
I mean by algebraic expression something like:
min x[0] + 2 * x[1]
s.t. Q[0][0] * x[0] * x[0] + Q[0][1] * x[0] * x[1] + Q[1][0] * x[1] * x[0] + Q[1][1] * x[1] * x[1] <= 1
x[0] >= 0, x[1] >= 0
Thank you and best regards
Paul
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Paul,
You can export the model in an LP file format using model.write("mymodel.lp") just before the optimize command. The LP file format provides a readable representation of the optimization model, including the objective function, constraints, and variable bounds. For your example model, the LP file will contain:
\ Model quadratic_constraint_problem
\ LP format - for model browsing. Use MPS format to capture full model detail.
Minimize
x1 + 2 x2
Subject To
quad_constr: [ 2 x1 ^2 + 2 x1 * x2 + 2 x2 ^2 ] <= 1
Bounds
EndBest regards,
Simran0 -
Dear Simran,
thanks for the message! This files solves my issue
Best,Paul
0
投稿コメントは受け付けていません。
コメント
3件のコメント