Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable values return 'None' after solving the problem #924

Closed
BrianN92 opened this issue Jan 17, 2020 · 1 comment
Closed

Variable values return 'None' after solving the problem #924

BrianN92 opened this issue Jan 17, 2020 · 1 comment

Comments

@BrianN92
Copy link

Describe the bug
After solving the problem, variables u & y returned "None"

To Reproduce

import cvxpy as cp
import numpy as np
from numpy.linalg import pinv

# Define the state space matrices
A = np.array([[ 1.96745861e-05,  8.13485217e-05,  5.77344499e-04,
         2.50156980e-03, -4.20390862e-04],
       [ 1.83218791e-05, -6.64167786e-05, -2.59408881e-03,
         4.48058026e-04,  3.18793055e-03],
       [-1.89169954e-05,  5.12847259e-05, -5.48305222e-04,
        -3.06533107e-04,  8.44923166e-03],
       [-1.20247997e-05, -6.27915617e-07, -9.10863713e-05,
        -8.94478349e-04, -5.40108892e-04],
       [ 8.84586089e-06,  1.63172443e-05,  2.90230873e-04,
         8.18201672e-05, -4.44945551e-03]])
B = np.array([[-3.76965442e-04,  9.39303000e-06, -3.85775209e-04],
       [-6.18910044e-05, -2.51123269e-05,  2.66895587e-03],
       [-2.55411165e-05, -1.04126946e-05,  3.07196176e-03],
       [-9.61991011e-05, -2.35462956e-06, -1.32939899e-04],
       [ 2.69436292e-07,  5.11188160e-06, -1.64164895e-03]])
C = np.array([[-0.49404405, -0.08989964,  0.0388238 ,  0.73830449,  0.03432347],
       [-0.09379532,  0.52031726,  0.69049302, -0.01848544, -0.48960013]])
P = np.array([[-2.06760151e-05, -3.21313691e-05, -1.30601012e-04,
         -1.94565573e-03, -3.02356442e-04],
        [-9.48289848e-06, -1.47534791e-05, -1.92291562e-03,
         -2.36686530e-04,  9.72073962e-03],
        [ 2.21965970e-08, -9.95765465e-09,  2.32493205e-07,
          1.68952239e-06,  1.19896873e-06],
        [ 1.24753321e-07,  6.03565609e-08,  3.92995927e-06,
          1.56616827e-06, -5.94143681e-05],
        [-1.38540647e-11,  3.28933541e-11,  1.05254161e-10,
         -1.43334351e-09, -4.32397068e-09],
        [-6.15186801e-10, -7.62775488e-10, -1.96259023e-08,
         -7.12773739e-09,  2.96860790e-07],
        [-2.26745719e-14, -6.75690148e-14, -1.27542998e-12,
          8.76124070e-13,  2.10134794e-11],
        [ 3.05688294e-12,  3.84253319e-12,  9.91919233e-11,
          3.48000947e-11, -1.48501598e-09]]) 
Qc =  np.array([[-8.45801844e-05,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00],
        [ 4.35226485e-03,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00],
        [ 2.76036183e-07, -8.45801844e-05,  0.00000000e+00,
          0.00000000e+00],
        [-2.18694183e-05,  4.35226485e-03,  0.00000000e+00,
          0.00000000e+00],
        [-1.51381989e-09,  2.76036183e-07, -8.45801844e-05,
          0.00000000e+00],
        [ 1.09514976e-07, -2.18694183e-05,  4.35226485e-03,
          0.00000000e+00],
        [ 7.70546269e-12, -1.51381989e-09,  2.76036183e-07,
         -8.45801844e-05],
        [-5.48482155e-10,  1.09514976e-07, -2.18694183e-05,
          4.35226485e-03]])
Qd = np.array([[ 1.19794929e-04, -4.35021797e-06,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00],
        [-1.28349584e-05, -2.35965438e-05,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00],
        [ 2.00207348e-07,  5.00827964e-09,  1.19794929e-04,
         -4.35021797e-06,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00],
        [ 7.89893960e-08,  7.05527333e-08, -1.28349584e-05,
         -2.35965438e-05,  0.00000000e+00,  0.00000000e+00,
          0.00000000e+00,  0.00000000e+00],
        [-1.75896686e-10,  1.88459231e-13,  2.00207348e-07,
          5.00827964e-09,  1.19794929e-04, -4.35021797e-06,
          0.00000000e+00,  0.00000000e+00],
        [-3.17811134e-10, -3.48672309e-10,  7.89893960e-08,
          7.05527333e-08, -1.28349584e-05, -2.35965438e-05,
          0.00000000e+00,  0.00000000e+00],
        [ 1.37219715e-13, -2.07807729e-14, -1.75896686e-10,
          1.88459231e-13,  2.00207348e-07,  5.00827964e-09,
          1.19794929e-04, -4.35021797e-06],
        [ 1.54604756e-12,  1.75203552e-12, -3.17811134e-10,
         -3.48672309e-10,  7.89893960e-08,  7.05527333e-08,
         -1.28349584e-05, -2.35965438e-05]])
Ud = np.array([[0,0,0,0],[87.78,87.85,87.88,87.94]])
Y = np.array([[1.549219073455385898e+00],[3.605849153580493294e+00]])

# Prediction/control horizen
Nc = 4
Np = 4

n1,n_in = B.shape
B_d = B[:, 0:n_in-1]
B_c = B[:, n_in-1:n_in]
n_d = B_d.shape[1]
n_c = B_c.shape[1]

# Define weight matrices
weights_y = np.array(([0.8,0.2])) # Arbitrary weights
W_y = weights_y * np.identity(Y.shape[0])
weights_u = np.array(([0.05,0.05,0.9]))
W_u = weights_u * np.identity(B.shape[1])

# Initialize the problem
u = cp.Variable((n_c,Nc))  # Decision variable
umin = np.zeros(n_c) # lower bound
umax = np.ones(n_c)
y = cp.Variable((Y.shape[0],Nc+1))
ymin = np.zeros(Y.shape[0])
ymax = np.array([3.0,5.0])
y_init = cp.Parameter(Y.shape[0])
x = cp.Variable((n1,Nc+1))
x_init = cp.Parameter(n1)
U = cp.atoms.affine.vstack.vstack((Ud,u))
objective = 0
x_init.value =  np.dot(pinv(C),Y).reshape((-1,))
y_init.value = Y.reshape((-1,))
constraints = [x[:,0]==x_init,y[:,0]==y_init]
for k in range(Nc):
    objective += cp.quad_form(y[:,k], W_y) + cp.quad_form(U[:,k],W_u) # Objective function J=Y'QY + U'RU
    constraints += [x[:,k+1] == A@x[:,k] + B_d@Ud[:,k] + B_c@u[:,k]]
    constraints += [y[:,k+1] == C@x[:,k+1]]
    constraints += [ymin <= y[:,k], y[:,k] <= ymax]
    constraints += [umin <= u[:,k], u[:,k] <= umax]
prob = cp.Problem(cp.Minimize(objective), constraints)
prob.solve(solver=cp.OSQP, warm_start=True)

Version

  • OS: Windows 10
  • CVXPY Version: '1.0.25'

Additional context
Did anyone run into this issue? I am new to cvxpy, not sure how to address it... Thank you!

@rileyjmurray
Copy link
Collaborator

Before trying to access variable values, you should always check if prob.value is finite. In your case, you have a minimization problem, and prob.value == np.inf after calling prob.solve(). This means your problem is infeasible.

Diagnosing infeasibility issues is a common task when using optimization models in practice. Usually you will find either a bug in your code, or you will see that the abstract mathematical model can be infeasible (even if coded up perfectly). Helping users identify which situation they are in is beyond the scope of GitHub issues, so I'll be closing this issue. However, you can ask for help on StackOverflow with a cvxpy tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants