Appending variables to the optimizing variable
AnsweredHi, I was working on developing an algorithm and wanted to know if I could do the following:
Say, my model has 5 optimizing variables, u1, u2, .. u5.
But, I can define the last variable as, u5 = X - (u1 + u2 + u3 + u4). In other words, I can replace the last variable in terms of the sum of the other variables.
Thus, if I define the optimization vector as:
u = model.addMVar((4)),
I will also have u5 = X - u.sum()
Is there any way that I can append the above u vector and have u5 as its last element? In other words, I want to directly represent the last element of the optimizing vector as a function of the other elements of the optimizing vector.
My objective function is a quadratic function of the form au^2 + bu + c:
model.setObjective(((a@u) + (u@sp.sparse.diags(b)@u) + (c.sum())), GRB.MINIMIZE)
Here, a, b and c are matrices, so that I can have a (au^2 + bu + c) for each u1, u2 .. u5.
-
Hi Dipanjan,
The issue with simply "appending" u5 to your variables is that u5 is not a variable but a LinExpr object. Thus, you cannot just apply the @ operator to it later on.
You could define 5 variables and add an equality constraint stating
\[\begin{align*}
u_5 = X - \sum_{i=1}^{4} u_i
\end{align*}\]Like this, you wouldn't have to modify your code and you are not making the model noticeably more complex.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment