1

I'm attempting to use CVXPY in python to solve a linear programming problem that has a handful of constraints. I created a single list with all of the constraints listed but when it came to running the problem.solve(), it returned the following error:

"AttributeError: 'list' object has no attribute 'parameters'"

I believe it's referencing the constraints list I created, but I'm at a loss for how to rectify the issue.

import cvxpy as cvx
import numpy as np

p = cvx.Variable(shape=(3,1), name="p")
f = cvx.Variable(shape=(6,1), name="f")
theta = cvx.Variable(shape=(6,1), name="theta")

r = np.array([16, 20, 8])
objective = cvx.Minimize(cvx.matmul(r,p))

A = np.array([[1,0,0,0,0,1],[0,1,1,0,0,0],[0,0,0,1,1,0]])
B = np.array([[1,0,0],[0,1,0],[0,0,1]])
constraint_genflow = [cvx.matmul(A,f)==cvx.matmul(B,p)]

C = np.array([[1,1,0,0,0,0],[0,0,1,1,0,0],[0,0,0,0,1,1]])
D = np.array([[-110],[-65],[-205]])
constraint_demandflow= [cvx.matmul(C,f)==D]

E = np.array([[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[0,0,0,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]])
F = np.array([[-100],[-110],[-50],[-80],[-60],[-40]])
G = np.array([[100],[110],[50],[80],[60],[40]])
constraint_flowlimit1 = [cvx.matmul(E,f)<=G]
constraint_flowlimit2 = [cvx.matmul(E,f)>=E]

H = np.array([[1,0,0],[0,1,0],[0,0,1]])
I = np.array([[20],[20],[10]])
J = np.array([[200],[150],[150]])
constraint_genpower1 = [cvx.matmul(H,p)<=J]
constraint_genpower2 = [cvx.matmul(H,p)>=I]

K = np.array([[11.6,-11.6,0,0,0,0],[0,5.9,-5.9,0,0,0],[0,0,13.7,-13.7,0,0],[0,0,0,9.8,-9.8,0],[0,0,0,0,5.6,-5.6],[-10.5,0,0,0,0,10.5]])
L = np.array([[100],[110],[50],[80],[60],[40]])
constraint_nodalpot = [cvx.matmul(K,theta)==L]

constraints = [constraint_nodalpot, constraint_genpower1, constraint_genpower2, constraint_flowlimit2, constraint_flowlimit1, constraint_genflow, constraint_demandflow] 

problem = cvx.Problem(objective, constraints)
solution = problem.solve()
print(solution)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-120-05b45a639232> in <module>
      1 #Define the Problem and Solve
      2 problem = cvx.Problem(objective, constraints)
----> 3 solution = problem.solve()
      4 print(solution)

~\Anaconda3\lib\site-packages\cvxpy\problems\problem.py in solve(self, *args, **kwargs)
    460         else:
    461             solve_func = Problem._solve
--> 462         return solve_func(self, *args, **kwargs)
    463 
    464     @classmethod

~\Anaconda3\lib\site-packages\cvxpy\problems\problem.py in _solve(self, solver, warm_start, verbose, gp, qcp, requires_grad, enforce_dpp, **kwargs)
    876             print(_HEADER)
    877 
--> 878         for parameter in self.parameters():
    879             if parameter.value is None:
    880                 raise error.ParameterError(

~\Anaconda3\lib\site-packages\cvxpy\utilities\performance_utils.py in _compute_once(self, *args, **kwargs)
     67         if key in cache:
     68             return cache[key]
---> 69         result = func(self, *args, **kwargs)
     70         cache[key] = result
     71         return result

~\Anaconda3\lib\site-packages\cvxpy\problems\problem.py in parameters(self)
    334         params = self.objective.parameters()
    335         for constr in self.constraints:
--> 336             params += constr.parameters()
    337         return unique_list(params)
    338 

AttributeError: 'list' object has no attribute 'parameters'

Any help would be greatly appreciated

ewolf
  • 39
  • 4

1 Answers1

0

CVXPY expects the get list of constraints. Notice you defined each constraint inside a list, and then put all those lists inside another list. if you print constraints you will see:

[[Equality(Expression(AFFINE, UNKNOWN, (6, 1)), Constant(CONSTANT, NONNEGATIVE, (6, 1)))], [Inequality(Expression(AFFINE, UNKNOWN, (3, 1)))], [Inequality(Constant(CONSTANT, NONNEGATIVE, (3, 1)))], [Inequality(Constant(CONSTANT, NONNEGATIVE, (6, 6)))], [Inequality(Expression(AFFINE, UNKNOWN, (6, 1)))], [Equality(Expression(AFFINE, UNKNOWN, (3, 1)), Expression(AFFINE, UNKNOWN, (3, 1)))], [Equality(Expression(AFFINE, UNKNOWN, (3, 1)), Constant(CONSTANT, NONPOSITIVE, (3, 1)))]]

As you can see, there are nested lists here.

CVXPY expect somethings like constraints = [a<b, b<=c] etc.

simply change:

constraints = [constraint_nodalpot, constraint_genpower1, constraint_genpower2, constraint_flowlimit2, constraint_flowlimit1, constraint_genflow, constraint_demandflow]

to:

constraints = constraint_nodalpot + constraint_genpower1 + constraint_genpower2, + constraint_flowlimit2 + constraint_flowlimit1 + constraint_genflow + constraint_demandflow

creating one big list of constraints, inside of putting all the lists inside another list.

and that's it.

Roim
  • 2,986
  • 2
  • 10
  • 25
  • Thank you! I'm a beginner and trying to learn this on my own. You were a tremendous help! – ewolf Sep 24 '21 at 06:11