Skip to content Skip to sidebar Skip to footer

Error Passing A Linear Expression To A Quadratic Form In Docplex

I have a cplex/docplex model with an 'active risk' term. I believe I'm messing up the mix of Pandas and DocPlex, but I'm worried I'm trying to do something impossible. The term sh

Solution 1:

The problem comes from the conjonction of two events:

  1. Model.quad_sum expects variables, not expressions, as indicated in the documentation
  2. For performance reasons, class AdvModel disables type-checking of arguments. But this can be re-enabled.

Re-enabling type-checking for AdvModel (e.g. calling AdvModel(checker='on') yields the right error message:

docplex.mp.utils.DOcplexException: Expecting an iterable returning variables, docplex.mp.LinearExpr(Target_AAA-0.250) was passed at position 0

To compute a quadratic form over expressions, use Model.sum() as in:

#active_risk = model.quad_matrix_sum(covariances, target - optimal) / 2
size = len(assets)
active_risk = model.sum(covariances.iloc[i,j] * (target[i] - optimal[i]) * (target[j] - optimal[j])
                        for i inrange(size) for j inrange(size))

print(active_risk)

which yields

0.100Target_AAA^2+0.100Target_BBB^2+0.100Target_CCC^2+0.100Target_DDD^2-0.050Target_AAA-0.050Target_BBB-0.050Target_CCC-0.050Target_DDD+0.025

Post a Comment for "Error Passing A Linear Expression To A Quadratic Form In Docplex"