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.

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
Please sign in to leave a comment.
Comments
2 comments