Skip to content

Commit

Permalink
Change the way random seeds are set in unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbanjac committed Jun 30, 2020
1 parent 6ed801e commit 41277c5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 60 deletions.
31 changes: 16 additions & 15 deletions tests/lin_alg/generate_problem.py
Expand Up @@ -2,20 +2,21 @@
from scipy import sparse
import scipy.sparse.linalg as sla
import utils.codegen_utils as cu
from numpy.random import Generator, PCG64

# Set numpy seed for reproducibility
np.random.seed(2)
# Set random seed for reproducibility
rg = Generator(PCG64(2))

# Test sparse matrix construction vs dense
test_sp_matrix_Adns = np.around(.6*np.random.rand(5, 6)) + np.random.randn(5,6)
test_sp_matrix_Adns = np.around(.6*rg.random((5, 6))) + rg.standard_normal((5,6))
test_sp_matrix_A = sparse.csc_matrix(test_sp_matrix_Adns)


# Test vector operations
test_vec_ops_n = 10
test_vec_ops_v1 = np.random.randn(test_vec_ops_n)
test_vec_ops_v2 = np.random.randn(test_vec_ops_n)
test_vec_ops_sc = np.random.randn()
test_vec_ops_v1 = rg.standard_normal(test_vec_ops_n)
test_vec_ops_v2 = rg.standard_normal(test_vec_ops_n)
test_vec_ops_sc = rg.standard_normal()
test_vec_ops_norm_inf = np.linalg.norm(test_vec_ops_v1, np.inf)
test_vec_ops_norm_inf_diff = np.linalg.norm(test_vec_ops_v1 - test_vec_ops_v2,
np.inf)
Expand All @@ -28,8 +29,8 @@

# Test matrix operations
test_mat_ops_n = 2
test_mat_ops_A = sparse.random(test_mat_ops_n, test_mat_ops_n, density=0.8, format='csc')
test_mat_ops_d = np.random.randn(test_mat_ops_n)
test_mat_ops_A = sparse.random(test_mat_ops_n, test_mat_ops_n, density=0.8, format='csc', random_state=rg)
test_mat_ops_d = rg.standard_normal(test_mat_ops_n)
D = sparse.diags(test_mat_ops_d, format='csc')
test_mat_ops_prem_diag = D.dot(test_mat_ops_A).tocoo().tocsc() # Force matrix reordering
test_mat_ops_postm_diag = test_mat_ops_A.dot(D).tocoo().tocsc() # Force matrix reordering
Expand All @@ -45,12 +46,12 @@

test_mat_vec_n = n
test_mat_vec_m = m
test_mat_vec_A = sparse.random(m, n, density=1.0, format='csc')
test_mat_vec_P = sparse.random(n, n, density=0.8, format='csc')
test_mat_vec_A = sparse.random(m, n, density=1.0, format='csc', random_state=rg)
test_mat_vec_P = sparse.random(n, n, density=0.8, format='csc', random_state=rg)
test_mat_vec_P = test_mat_vec_P + test_mat_vec_P.T
test_mat_vec_Pu = sparse.triu(test_mat_vec_P, format='csc')
test_mat_vec_x = np.random.randn(n)
test_mat_vec_y = np.random.randn(m)
test_mat_vec_x = rg.standard_normal(n)
test_mat_vec_y = rg.standard_normal(m)
test_mat_vec_Ax = test_mat_vec_A.dot(test_mat_vec_x)
test_mat_vec_Ax_cum = test_mat_vec_A.dot(test_mat_vec_x) + test_mat_vec_y
test_mat_vec_ATy = test_mat_vec_A.T.dot(test_mat_vec_y)
Expand All @@ -61,7 +62,7 @@

# Test extract upper triangular
test_mat_extr_triu_n = 5
test_mat_extr_triu_P = sparse.random(test_mat_extr_triu_n, test_mat_extr_triu_n, density=0.8, format='csc')
test_mat_extr_triu_P = sparse.random(test_mat_extr_triu_n, test_mat_extr_triu_n, density=0.8, format='csc', random_state=rg)
test_mat_extr_triu_P = test_mat_extr_triu_P + test_mat_extr_triu_P.T
test_mat_extr_triu_Pu = sparse.triu(test_mat_extr_triu_P, format='csc')
test_mat_extr_triu_P_inf_norm_cols = np.amax(np.abs(
Expand All @@ -70,10 +71,10 @@

# Test compute quad form
test_qpform_n = 4
test_qpform_P = sparse.random(test_qpform_n, test_qpform_n, density=0.8, format='csc')
test_qpform_P = sparse.random(test_qpform_n, test_qpform_n, density=0.8, format='csc', random_state=rg)
test_qpform_P = test_qpform_P + test_qpform_P.T
test_qpform_Pu = sparse.triu(test_qpform_P, format='csc')
test_qpform_x = np.random.randn(test_qpform_n)
test_qpform_x = rg.standard_normal(test_qpform_n)
test_qpform_value = .5 * test_qpform_x.T.dot(test_qpform_P.dot(test_qpform_x))


Expand Down
11 changes: 6 additions & 5 deletions tests/primal_infeasibility/generate_problem.py
Expand Up @@ -2,25 +2,26 @@
from scipy import sparse
import scipy as sp
import utils.codegen_utils as cu
from numpy.random import Generator, PCG64

# Set numpy seed for reproducibility
np.random.seed(2)
# Set random seed for reproducibility
rg = Generator(PCG64(2))

n = 50
m = 150

# Generate random Matrices
Pt = sparse.random(n, n)
Pt = sparse.random(n, n, random_state=rg)
P = Pt.T.dot(Pt) + sparse.eye(n)
P = sparse.triu(P, format='csc')
q = sp.randn(n)
A = sparse.random(m, n).tolil() # Lil for efficiency
A = sparse.random(m, n, random_state=rg).tolil() # Lil for efficiency
u = 3 + sp.randn(m)
l = -3 + sp.randn(m)

# Make random problem primal infeasible
A[int(n/2), :] = A[int(n/2)+1, :]
l[int(n/2)] = u[int(n/2)+1] + 10 * sp.rand()
l[int(n/2)] = u[int(n/2)+1] + 10 * rg.random()
u[int(n/2)] = l[int(n/2)] + 0.5

# Convert A to csc
Expand Down
11 changes: 6 additions & 5 deletions tests/solve_linsys/generate_problem.py
Expand Up @@ -2,19 +2,20 @@
from scipy import sparse
import scipy.sparse.linalg as spla
import utils.codegen_utils as cu
from numpy.random import Generator, PCG64

# Set numpy seed for reproducibility
np.random.seed(2)
# Set random seed for reproducibility
rg = Generator(PCG64(2))

# Simple case
test_solve_KKT_n = 3
test_solve_KKT_m = 4

test_solve_KKT_P = sparse.random(test_solve_KKT_n, test_solve_KKT_n,
density=0.4, format='csc')
density=0.4, format='csc', random_state=rg)
test_solve_KKT_P = test_solve_KKT_P.dot(test_solve_KKT_P.T).tocsc()
test_solve_KKT_A = sparse.random(test_solve_KKT_m, test_solve_KKT_n,
density=0.4, format='csc')
density=0.4, format='csc', random_state=rg)
test_solve_KKT_Pu = sparse.triu(test_solve_KKT_P, format='csc')

test_solve_KKT_rho = 4.0
Expand All @@ -25,7 +26,7 @@
sparse.hstack([test_solve_KKT_A,
-1./test_solve_KKT_rho * sparse.eye(test_solve_KKT_m)])
], format='csc')
test_solve_KKT_rhs = np.random.randn(test_solve_KKT_m + test_solve_KKT_n)
test_solve_KKT_rhs = rg.standard_normal(test_solve_KKT_m + test_solve_KKT_n)
test_solve_KKT_x = spla.splu(test_solve_KKT_KKT).solve(test_solve_KKT_rhs)

test_solve_KKT_x[test_solve_KKT_n:] = test_solve_KKT_rhs[test_solve_KKT_n:] + \
Expand Down
68 changes: 33 additions & 35 deletions tests/update_matrices/generate_problem.py
@@ -1,10 +1,10 @@
import numpy as np
from scipy import sparse
import utils.codegen_utils as cu
from numpy.random import Generator, PCG64

# Set numpy seed for reproducibility
np.random.seed(2)

# Set random seed for reproducibility
rg = Generator(PCG64(2))

# Define tests
n = 5
Expand All @@ -13,44 +13,40 @@
test_form_KKT_m = m
p = 0.7

test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc')
test_form_KKT_P = sparse.random(n, n, density=p)
test_form_KKT_P = test_form_KKT_P.dot(test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc')
test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc', random_state=rg)
test_form_KKT_P = sparse.random(n, n, density=p, random_state=rg)
test_form_KKT_P = (test_form_KKT_P @ test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc')
test_form_KKT_Pu = sparse.triu(test_form_KKT_P, format='csc')
test_form_KKT_rho = 1.6
test_form_KKT_sigma = 0.1
test_form_KKT_KKT = sparse.vstack([
sparse.hstack([test_form_KKT_P + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A.T]),
sparse.hstack([test_form_KKT_A,
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
], format='csc')
test_form_KKT_KKT = sparse.bmat([[test_form_KKT_P + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A.T],
[test_form_KKT_A, -1./test_form_KKT_rho *
sparse.eye(test_form_KKT_m)]], format='csc')
test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT, format='csc')


# Create new P, A and KKT
test_form_KKT_A_new = test_form_KKT_A.copy()
test_form_KKT_A_new.data += np.random.randn(test_form_KKT_A_new.nnz)
test_form_KKT_A_new.data += rg.standard_normal(test_form_KKT_A_new.nnz)
test_form_KKT_Pu_new = test_form_KKT_Pu.copy()
test_form_KKT_Pu_new.data += 0.1 * np.random.randn(test_form_KKT_Pu_new.nnz)
test_form_KKT_Pu_new.data += 0.1 * rg.standard_normal(test_form_KKT_Pu_new.nnz)
test_form_KKT_P_new = test_form_KKT_Pu_new + test_form_KKT_Pu_new.T - sparse.diags(test_form_KKT_Pu_new.diagonal())

test_form_KKT_KKT_new = sparse.vstack([
sparse.hstack([test_form_KKT_P_new + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A_new.T]),
sparse.hstack([test_form_KKT_A_new,
-1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
], format='csc')
test_form_KKT_KKT_new = sparse.bmat([[test_form_KKT_P_new + test_form_KKT_sigma *
sparse.eye(test_form_KKT_n), test_form_KKT_A_new.T],
[test_form_KKT_A_new, -1./test_form_KKT_rho *
sparse.eye(test_form_KKT_m)]], format='csc')
test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new, format='csc')


# Test solve problem with initial P and A
test_solve_P = test_form_KKT_P.copy()
test_solve_Pu = test_form_KKT_Pu.copy()
test_solve_q = np.random.randn(n)
test_solve_q = rg.standard_normal(n)
test_solve_A = test_form_KKT_A.copy()
test_solve_l = -30 + np.random.randn(m)
test_solve_u = 30 + np.random.randn(m)
test_solve_l = -30 + rg.standard_normal(m)
test_solve_u = 30 + rg.standard_normal(m)


# Define new P
Expand Down Expand Up @@ -82,27 +78,29 @@
'test_solve_u': test_solve_u,
'n': n,
'm': m,
'test_solve_x': np.array([-0.34967513, 1.20460722, -0.46259805,
0.59083905, -0.87685541]),
'test_solve_x': np.array([-4.61725223e-01, 7.97298788e-01,
5.55470173e-04, 3.37603740e-01,
-1.14060693e+00]),
'test_solve_y': np.zeros(m),
'test_solve_obj_value': -1.7665127080483103,
'test_solve_obj_value': -1.885431747787806,
'test_solve_status': 'optimal',
'test_solve_Pu_new': test_solve_Pu_new,
'test_solve_P_new_x': np.array([-0.28228879, 1.3527703, -0.69277181,
0.82445911, -1.11688134]),
'test_solve_P_new_x': np.array([-0.48845963, 0.70997599, -0.09017696,
0.33176037, -1.01867464]),
'test_solve_P_new_y': np.zeros(m),
'test_solve_P_new_obj_value': -2.1490899311728526,
'test_solve_P_new_obj_value': -1.7649689689774013,
'test_solve_P_new_status': 'optimal',
'test_solve_A_new': test_solve_A_new,
'test_solve_A_new_x': np.array([-0.34967513, 1.20460722, -0.46259805,
0.59083905, -0.87685541]),
'test_solve_A_new_x': np.array([-4.61725223e-01, 7.97298788e-01,
5.55470173e-04, 3.37603740e-01,
-1.14060693e+00]),
'test_solve_A_new_y': np.zeros(m),
'test_solve_A_new_obj_value': -1.7665127080484808,
'test_solve_A_new_obj_value': -1.8854317477878062,
'test_solve_A_new_status': 'optimal',
'test_solve_P_A_new_x': np.array([-0.28228879, 1.3527703, -0.69277181,
0.82445911, -1.11688134]),
'test_solve_P_A_new_x': np.array([-0.48845963, 0.70997599, -0.09017696,
0.33176037, -1.01867464]),
'test_solve_P_A_new_y': np.zeros(m),
'test_solve_P_A_new_obj_value': -2.1490899311726253,
'test_solve_P_A_new_obj_value': -1.764968968977401,
'test_solve_P_A_new_status': 'optimal'
}

Expand Down

0 comments on commit 41277c5

Please sign in to comment.