cvpp
cvpp

Reputation: 45

DCP Error when using CVXPY instead of scipy

How can I solve the following using cvxpy instead of scipy fsolve?

delta_E = {
    1: 13.552999999999999,
    2: 6.087000000000002,
    3: 35.187000000000005,
    4: 27.287000000000003,
    5: 16.687000000000005,
    6: 16.687000000000005
}

def e(lm, delta_E):
    return (1-lm)*delta_E[1] + (1-lm)*lm*delta_E[2] + (1-lm)*lm**2*delta_E[3] + (1-lm)*lm**3*delta_E[4] + (1-lm)*lm**4*delta_E[5] +lm**5*delta_E[6] - delta_E[6]
    
from scipy.optimize import fsolve
fsolve(e, x0=0.5, args=delta_E)

which gives me array([0.6227695])

Instead of fsolve, when I use cvxpy, I get the following error for the following code -

import cvxpy as cp

lm = cp.Variable()

objective = (1-lm)*delta_E[1] + (1-lm)*lm*delta_E[2] + (1-lm)*lm**2*delta_E[3] + (1-lm)*lm**3*delta_E[4] + (1-lm)*lm**4*delta_E[5] + lm**5*delta_E[6] - delta_E[6]


problem = cp.Problem(cp.Minimize(cp.abs(objective)))

problem.solve()

optimal_lm = lm.value
print(optimal_lm)

Error -

DCPError                                  Traceback (most recent call last)
<ipython-input-1-e0118156575e> in <cell line: 23>()
     21 
     22 # Solve the problem
---> 23 problem.solve()
     24 
     25 # Get the optimal value of lm

5 frames
/usr/local/lib/python3.10/dist-packages/cvxpy/reductions/solvers/solving_chain.py in _reductions_for_problem_class(problem, candidates, gp, solver_opts)
    113             append += ("\nHowever, the problem does follow DQCP rules. "
    114                        "Consider calling solve() with `qcp=True`.")
--> 115         raise DCPError(
    116             "Problem does not follow DCP rules. Specifically:\n" + append)
    117     elif gp and not problem.is_dgp():

DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
(1.0 + -var1) @ var1
(1.0 + -var1) @ power(var1, 2.0)
(1.0 + -var1) @ power(var1, 3.0)
(1.0 + -var1) @ power(var1, 4.0)

Upvotes: 0

Views: 40

Answers (0)

Related Questions