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

Trying to understand the least squares optimization example from the repo... #562

Open
mpgussert opened this issue Jul 6, 2023 · 0 comments

Comments

@mpgussert
Copy link

mpgussert commented Jul 6, 2023

Hello all!

I am trying to make use of OSQP for a fairly straight forward least squares problem in python. This is the example given in the docs

import osqp
import numpy as np
import scipy as sp
from scipy import sparse

# Generate problem data
# sp.random.seed(1)
m = 30
n = 20
Ad = sparse.random(m, n, density=0.7, format='csc')
b = np.random.randn(m)

# OSQP data
P = sparse.block_diag([sparse.csc_matrix((n, n)), sparse.eye(m)], format='csc')
q = np.zeros(n+m)
A = sparse.bmat([[Ad,            -sparse.eye(m)],
                 [sparse.eye(n),  None]], format='csc')
l = np.hstack([b, np.zeros(n)])
u = np.hstack([b, np.ones(n)])

# Create an OSQP object
prob = osqp.OSQP()

# Setup workspace
prob.setup(P, q, A, l, u)

# Solve problem
res = prob.solve()

note that I had to comment out sp.random.seed(1) because scipy has no module named random. There are no additional comments or discussions for this examples, just a note at the top that we are minimizing ||Ax - b|| subject to 0 < x < 1. Needless to say, I am confused and have many questions... but generally, just a discussion about what is going on here would be an excellent addition to the documentation.

I think my biggest hang up is translating from the general problem described on the home page of the docs https://osqp.org/docs/solver/index.html to the least squares examples listed here https://osqp.org/docs/examples/least-squares.html. My biggest confusion is around how to handle A and b. It looks like b just becomes part of x and the limits are used to "set" what "b" should be, but it isn't super clear to me. So, for example, if you are controlling a dynamic system, the "b" term takes the from of the force / torque residuals in the least squares phrasing of the problem. You expand X to include these terms and the limits on that part of X becomes L = U = command residual. Am I at least thinking about this the right way?

Can anyone here walk me through what this example above is doing exactly? The transition between the two formulations is especially confusing for me.

thanks!
Gus

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

1 participant