Skip to content

Commit

Permalink
[examples/python] Add util.py file, move "generate_mixed_qp" function…
Browse files Browse the repository at this point in the history
… to it

give reg a meaning
  • Loading branch information
ManifoldFR committed Aug 13, 2024
1 parent 25827f6 commit 91d3dc9
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 399 deletions.
27 changes: 1 addition & 26 deletions examples/python/estimate_nonconvex_eigenvalue.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/init_dense_qp.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/init_dense_qp_with_box.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/init_dense_qp_with_other_options.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/init_dense_qp_with_timings.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/init_with_default_options.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 2 additions & 27 deletions examples/python/loading_sparse_qp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/overview-simple.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/solve_dense_qp.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 1 addition & 26 deletions examples/python/solve_dense_qp_with_setting.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 2 additions & 29 deletions examples/python/solve_without_api.py
Original file line number Diff line number Diff line change
@@ -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")
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
Expand All @@ -40,9 +15,7 @@ def generate_mixed_qp(n, seed=1):

# solve the problem using the dense backend

results2 = proxsuite.proxqp.dense.solve(
H.toarray(), g, A.toarray(), b, C.toarray(), l, u
)
results2 = proxsuite.proxqp.dense.solve(H, g, A.toarray(), b, C.toarray(), l, u)
# Note finally, that the matrices are in sparse format, when using the dense backend you
# should convert them in dense format

Expand Down
Loading

0 comments on commit 91d3dc9

Please sign in to comment.