blob: d801fc7f5de7f95dfe511678fc8a81b8e2ccafdb [file] [log] [blame]
Austin Schuh9049e202022-02-20 17:34:16 -08001import numpy as np
2from scipy import sparse
3import utils.codegen_utils as cu
4from numpy.random import Generator, PCG64
5
6# Set random seed for reproducibility
7rg = Generator(PCG64(2))
8
9# Define tests
10n = 5
11m = 8
12test_form_KKT_n = n
13test_form_KKT_m = m
14p = 0.7
15
16test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc', random_state=rg)
17test_form_KKT_P = sparse.random(n, n, density=p, random_state=rg)
18test_form_KKT_P = (test_form_KKT_P @ test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc')
19test_form_KKT_Pu = sparse.triu(test_form_KKT_P, format='csc')
20test_form_KKT_rho = 1.6
21test_form_KKT_sigma = 0.1
22test_form_KKT_KKT = sparse.bmat([[test_form_KKT_P + test_form_KKT_sigma *
23 sparse.eye(test_form_KKT_n), test_form_KKT_A.T],
24 [test_form_KKT_A, -1./test_form_KKT_rho *
25 sparse.eye(test_form_KKT_m)]], format='csc')
26test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT, format='csc')
27
28
29# Create new P, A and KKT
30test_form_KKT_A_new = test_form_KKT_A.copy()
31test_form_KKT_A_new.data += rg.standard_normal(test_form_KKT_A_new.nnz)
32test_form_KKT_Pu_new = test_form_KKT_Pu.copy()
33test_form_KKT_Pu_new.data += 0.1 * rg.standard_normal(test_form_KKT_Pu_new.nnz)
34test_form_KKT_P_new = test_form_KKT_Pu_new + test_form_KKT_Pu_new.T - sparse.diags(test_form_KKT_Pu_new.diagonal())
35
36test_form_KKT_KKT_new = sparse.bmat([[test_form_KKT_P_new + test_form_KKT_sigma *
37 sparse.eye(test_form_KKT_n), test_form_KKT_A_new.T],
38 [test_form_KKT_A_new, -1./test_form_KKT_rho *
39 sparse.eye(test_form_KKT_m)]], format='csc')
40test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new, format='csc')
41
42
43# Test solve problem with initial P and A
44test_solve_P = test_form_KKT_P.copy()
45test_solve_Pu = test_form_KKT_Pu.copy()
46test_solve_q = rg.standard_normal(n)
47test_solve_A = test_form_KKT_A.copy()
48test_solve_l = -30 + rg.standard_normal(m)
49test_solve_u = 30 + rg.standard_normal(m)
50
51
52# Define new P
53test_solve_P_new = test_form_KKT_P_new.copy()
54test_solve_Pu_new = test_form_KKT_Pu_new.copy()
55
56
57# Define new A
58test_solve_A_new = test_form_KKT_A_new.copy()
59
60
61# Generate test data and solutions
62data = {'test_form_KKT_n': test_form_KKT_n,
63 'test_form_KKT_m': test_form_KKT_m,
64 'test_form_KKT_A': test_form_KKT_A,
65 'test_form_KKT_Pu': test_form_KKT_Pu,
66 'test_form_KKT_rho': test_form_KKT_rho,
67 'test_form_KKT_sigma': test_form_KKT_sigma,
68 'test_form_KKT_KKT': test_form_KKT_KKT,
69 'test_form_KKT_KKTu': test_form_KKT_KKTu,
70 'test_form_KKT_A_new': test_form_KKT_A_new,
71 'test_form_KKT_Pu_new': test_form_KKT_Pu_new,
72 'test_form_KKT_KKT_new': test_form_KKT_KKT_new,
73 'test_form_KKT_KKTu_new': test_form_KKT_KKTu_new,
74 'test_solve_Pu': test_solve_Pu,
75 'test_solve_q': test_solve_q,
76 'test_solve_A': test_solve_A,
77 'test_solve_l': test_solve_l,
78 'test_solve_u': test_solve_u,
79 'n': n,
80 'm': m,
81 'test_solve_x': np.array([-4.61725223e-01, 7.97298788e-01,
82 5.55470173e-04, 3.37603740e-01,
83 -1.14060693e+00]),
84 'test_solve_y': np.zeros(m),
85 'test_solve_obj_value': -1.885431747787806,
86 'test_solve_status': 'optimal',
87 'test_solve_Pu_new': test_solve_Pu_new,
88 'test_solve_P_new_x': np.array([-0.48845963, 0.70997599, -0.09017696,
89 0.33176037, -1.01867464]),
90 'test_solve_P_new_y': np.zeros(m),
91 'test_solve_P_new_obj_value': -1.7649689689774013,
92 'test_solve_P_new_status': 'optimal',
93 'test_solve_A_new': test_solve_A_new,
94 'test_solve_A_new_x': np.array([-4.61725223e-01, 7.97298788e-01,
95 5.55470173e-04, 3.37603740e-01,
96 -1.14060693e+00]),
97 'test_solve_A_new_y': np.zeros(m),
98 'test_solve_A_new_obj_value': -1.8854317477878062,
99 'test_solve_A_new_status': 'optimal',
100 'test_solve_P_A_new_x': np.array([-0.48845963, 0.70997599, -0.09017696,
101 0.33176037, -1.01867464]),
102 'test_solve_P_A_new_y': np.zeros(m),
103 'test_solve_P_A_new_obj_value': -1.764968968977401,
104 'test_solve_P_A_new_status': 'optimal'
105 }
106
107
108# Generate test data
109cu.generate_data('update_matrices', data)