diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f1395f0b..c95665016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Fix cereal include directory in cmake ([#340](https://github.com/Simple-Robotics/proxsuite/pull/340)) * Extend doc with hint for conda installation from source ([#340](https://github.com/Simple-Robotics/proxsuite/pull/340)) +### Changed + +* Refactor Python examples with a new "util.py" file ([#347](https://github.com/Simple-Robotics/proxsuite/pull/347)) + ## [0.6.6] - 2024-06-15 ### Fixed diff --git a/examples/python/estimate_nonconvex_eigenvalue.py b/examples/python/estimate_nonconvex_eigenvalue.py index 21e2e54ef..7b6ecac59 100644 --- a/examples/python/estimate_nonconvex_eigenvalue.py +++ b/examples/python/estimate_nonconvex_eigenvalue.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1, reg=-2.0): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/init_dense_qp.py b/examples/python/init_dense_qp.py index d76736360..4408870f4 100644 --- a/examples/python/init_dense_qp.py +++ b/examples/python/init_dense_qp.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/init_dense_qp_with_box.py b/examples/python/init_dense_qp_with_box.py index 268e96e8b..15aa61c69 100644 --- a/examples/python/init_dense_qp_with_box.py +++ b/examples/python/init_dense_qp_with_box.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/init_dense_qp_with_other_options.py b/examples/python/init_dense_qp_with_other_options.py index 8b3faa97a..dbc9b4ab4 100644 --- a/examples/python/init_dense_qp_with_other_options.py +++ b/examples/python/init_dense_qp_with_other_options.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/init_dense_qp_with_timings.py b/examples/python/init_dense_qp_with_timings.py index 09cb66e2c..b156bd4c5 100644 --- a/examples/python/init_dense_qp_with_timings.py +++ b/examples/python/init_dense_qp_with_timings.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/init_with_default_options.py b/examples/python/init_with_default_options.py index caf2929c5..133cbb6cf 100644 --- a/examples/python/init_with_default_options.py +++ b/examples/python/init_with_default_options.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/loading_sparse_qp.py b/examples/python/loading_sparse_qp.py index f762fb096..7b8b3f880 100644 --- a/examples/python/loading_sparse_qp.py +++ b/examples/python/loading_sparse_qp.py @@ -8,36 +8,11 @@ import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc") - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P, q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp2 object using matrix masks -H, g, A, b, C, u, l = generate_mixed_qp(n) +H, g, A, b, C, u, l = generate_mixed_qp(n, True) H_ = H != 0.0 A_ = A != 0.0 diff --git a/examples/python/overview-simple.py b/examples/python/overview-simple.py index cc0fbb438..e220efb8b 100644 --- a/examples/python/overview-simple.py +++ b/examples/python/overview-simple.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # generate a qp problem diff --git a/examples/python/solve_dense_qp.py b/examples/python/solve_dense_qp.py index 4eb04547c..d3cd330bc 100644 --- a/examples/python/solve_dense_qp.py +++ b/examples/python/solve_dense_qp.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/solve_dense_qp_with_setting.py b/examples/python/solve_dense_qp_with_setting.py index 5999037e1..0673a0f99 100644 --- a/examples/python/solve_dense_qp_with_setting.py +++ b/examples/python/solve_dense_qp_with_setting.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/solve_without_api.py b/examples/python/solve_without_api.py index 9e84676fc..3a489203a 100644 --- a/examples/python/solve_without_api.py +++ b/examples/python/solve_without_api.py @@ -1,39 +1,14 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc") - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P, q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions n = 10 n_eq = 2 n_in = 2 -H, g, A, b, C, u, l = generate_mixed_qp(n) +H, g, A, b, C, u, l = generate_mixed_qp(n, True) # solve the problem using the sparse backend results = proxsuite.proxqp.sparse.solve(H, g, A, b, C, l, u) diff --git a/examples/python/solve_without_api_and_option.py b/examples/python/solve_without_api_and_option.py index 9e1cea272..930715dc8 100644 --- a/examples/python/solve_without_api_and_option.py +++ b/examples/python/solve_without_api_and_option.py @@ -1,32 +1,7 @@ import proxsuite import numpy as np import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/update_dense_qp.py b/examples/python/update_dense_qp.py index 106d8f544..5ceab125f 100644 --- a/examples/python/update_dense_qp.py +++ b/examples/python/update_dense_qp.py @@ -1,32 +1,6 @@ import proxsuite import numpy as np -import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/update_dense_qp_ws_previous_result.py b/examples/python/update_dense_qp_ws_previous_result.py index e8289490b..3e9b995a8 100644 --- a/examples/python/update_dense_qp_ws_previous_result.py +++ b/examples/python/update_dense_qp_ws_previous_result.py @@ -1,32 +1,6 @@ import proxsuite import numpy as np -import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating sparse random convex qps in dense format - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc").toarray() - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P.toarray(), q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions diff --git a/examples/python/update_sparse_qp.py b/examples/python/update_sparse_qp.py index 12bfe7886..f52319139 100644 --- a/examples/python/update_sparse_qp.py +++ b/examples/python/update_sparse_qp.py @@ -1,32 +1,6 @@ import proxsuite import numpy as np -import scipy.sparse as spa - - -def generate_mixed_qp(n, seed=1): - # A function for generating random convex qps - - np.random.seed(seed) - n_eq = int(n / 4) - n_in = int(n / 4) - m = n_eq + n_in - - P = spa.random( - n, n, density=0.075, data_rvs=np.random.randn, format="csc" - ).toarray() - P = (P + P.T) / 2.0 - - s = max(np.absolute(np.linalg.eigvals(P))) - P += (abs(s) + 1e-02) * spa.eye(n) - P = spa.coo_matrix(P) - q = np.random.randn(n) - A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc") - v = np.random.randn(n) # Fictitious solution - delta = np.random.rand(m) # To get inequality - u = A @ v - l = -1.0e20 * np.ones(m) - - return P, q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:] +from util import generate_mixed_qp # load a qp object using qp problem dimensions @@ -35,7 +9,7 @@ def generate_mixed_qp(n, seed=1): n_in = 2 qp = proxsuite.proxqp.sparse.QP(n, n_eq, n_in) # generate a random QP -H, g, A, b, C, u, l = generate_mixed_qp(n) +H, g, A, b, C, u, l = generate_mixed_qp(n, True) # initialize the model of the problem to solve qp.init(H, g, A, b, C, l, u) qp.solve() @@ -44,7 +18,7 @@ def generate_mixed_qp(n, seed=1): qp.solve() # generate a QP with another sparsity structure # create a new problem and update qp -H2, g_new, A_new, b_new, C_new, u_new, l_new = generate_mixed_qp(n) +H2, g_new, A_new, b_new, C_new, u_new, l_new = generate_mixed_qp(n, True) qp.update(H=H2) # nothing will happen qp.update(g=g_new) # if only a vector changes, then the update takes effect qp.solve() # it solves the problem with the QP H,g_new,A,b,C,u,l diff --git a/examples/python/util.py b/examples/python/util.py new file mode 100644 index 000000000..53c0c53da --- /dev/null +++ b/examples/python/util.py @@ -0,0 +1,31 @@ +import numpy as np +import scipy.sparse as spa + + +def generate_mixed_qp(n, sparse=False, seed=1, reg=1e-2, dens1=0.075): + # A function for generating sparse random convex qps + + np.random.seed(seed) + n_eq = int(n / 4) + n_in = int(n / 4) + m = n_eq + n_in + + P = spa.random( + n, n, density=dens1, data_rvs=np.random.randn, format="csc" + ).toarray() + P = (P + P.T) / 2.0 + + s = max(np.absolute(np.linalg.eigvals(P))) + P += (abs(s) + reg) * spa.eye(n) + P = spa.coo_matrix(P) + q = np.random.randn(n) + A = spa.random(m, n, density=0.15, data_rvs=np.random.randn, format="csc") + if not sparse: + A = A.toarray() + P = P.toarray() + v = np.random.randn(n) # Fictitious solution + delta = np.random.rand(m) # To get inequality + u = A @ v + l = -1.0e20 * np.ones(m) + + return P, q, A[:n_eq, :], u[:n_eq], A[n_in:, :], u[n_in:], l[n_in:]