Invalid argument to QuadExpr multiplication
AnsweredHi,
I am new to Gurobi so I do apologize for not being able to figure out the solution from other posts.
I need to write the following objective function:
but I get
Invalid argument to QuadExpr multiplication
I understand Gurobi does not allow multiplying of more than 2 variables, still I wasn't able to re-write it correctly; this is my current code:
opt_model = grb.Model()
opt_model.setParam("NonConvex", 2)
u = opt_model.addVars(d.n_users, vtype=grb.GRB.CONTINUOUS, name="users")
v = opt_model.addVars(d.n_movies, vtype=grb.GRB.CONTINUOUS, name="movies")
objective = grb.quicksum((u[i]*v[j] - d.X[i,j])**2
for i, j in zip(rows, cols))
or, attempting to (partially) multiply 2 elements at a time as in:
objective = 0
for i, j in zip(rows, cols):
z = u[i]*v[j]
objective += z * u[i] #- d.X[i, j]
still results in the same error.
Kind regards,
Nicolo'
-
Official comment
This post is more than three 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 why not try our AI Gurobot?. -
You are on the right track in creating a new variable, \(z\). However, you will need to
- Use the \(\texttt{.addVar()}\) method to create \(z_{ij}\) as a variable
- Use the \(\texttt{.addConstr()}\) method to define the following constraint \[z_{ij} = u_i\cdot v_j \quad \forall \ \ ij\]
- Your objective would then be \[\sum_{ij} \left(z_{ij} - X_{ij} \right)^2 \]
Let me know if that works for you.
You can read up further on How do I model multilinear terms in Gurobi?
1 -
Thanks a lot for the reply, I've re-written the objective following your suggestion:
u = opt_model.addVars(d.n_users, lb=-100.0, ub=100.0, vtype=grb.GRB.CONTINUOUS, name="users")
v = opt_model.addVars(d.n_movies,lb=-100.0, ub=100.0, vtype=grb.GRB.CONTINUOUS, name="movies")
objective = 0
for i, j in zip(rows, cols):
z = opt_model.addVar(name=f'z_{i}_{j}')
opt_model.addConstr(u[i]*v[j] == z)
objective += (z - d.X[i, j]) * (z - d.X[i, j])And it seems to work fine!
I'm still trying to get the optimization to converge fully in a decent amount of time, but I guess I'll try with an even smaller problem in size, unless I misswrote the objective again or you have other helpful tips.
Thanks again.
0 -
Here are a couple of thoughts that may help performance:
Tightening the ranges on your variables.
In nonconvex optimization, it good modeling practice to have tight bounds on all variables. As Gurobi traverses the branch-and-bound tree, it is making outer approximations of the nonconvex functions. Tight bounds help the relaxation be as close to the original function as possible. In general, the tighter the relaxation the faster the solve.
Try tuning Gurobi Parameters.
Gurobi has a number of parameters that affect the solution speed. The Parameter Tuning Tool helps improve the parameter selection.
0
Post is closed for comments.
Comments
4 comments