Find the coefficient of a variable in a complex (very large) objective function
AnsweredAssume the model uses a very complex objective function with lots of variables. Some of those variables may appear more than once in the linear expression.
Given the name of a variable, how can one easily determine the coefficient for it in the expression of the objective function?
Using the LinExpr.getCoeff() does not look promising as it is based on "index i in the expression", and if a variable shows up more than once one would have to process all the variables, and group/sum by variable (names).
In general terms, after an optimization is complete, I simply want to have an easy way to look up the contribution to the objective function from individual variables - to be able to say "ok, this variable contributed a lot or not...", and the problem is challenging because of the shear number of variables.
-
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?. -
Hi Chris,
Given only the name of a variable, you can get the variable object using the getVarByName function. You can then look up the objective coefficient of a given variable directly via variable attributes.
Best regards,
Jaromił0 -
Thank you.
Indeed, I overlooked the obj attribute and ended up with a more complex solution using the functionality of the linear expression object representing the objective.
Thank you,
Chris
0
Post is closed for comments.
Comments
3 comments