Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame^] | 1 | import numpy as np |
| 2 | from scipy import sparse |
| 3 | import utils.codegen_utils as cu |
| 4 | from numpy.random import Generator, PCG64 |
| 5 | |
| 6 | # Set random seed for reproducibility |
| 7 | rg = Generator(PCG64(2)) |
| 8 | |
| 9 | # Define tests |
| 10 | n = 5 |
| 11 | m = 8 |
| 12 | test_form_KKT_n = n |
| 13 | test_form_KKT_m = m |
| 14 | p = 0.7 |
| 15 | |
| 16 | test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc', random_state=rg) |
| 17 | test_form_KKT_P = sparse.random(n, n, density=p, random_state=rg) |
| 18 | test_form_KKT_P = (test_form_KKT_P @ test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc') |
| 19 | test_form_KKT_Pu = sparse.triu(test_form_KKT_P, format='csc') |
| 20 | test_form_KKT_rho = 1.6 |
| 21 | test_form_KKT_sigma = 0.1 |
| 22 | test_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') |
| 26 | test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT, format='csc') |
| 27 | |
| 28 | |
| 29 | # Create new P, A and KKT |
| 30 | test_form_KKT_A_new = test_form_KKT_A.copy() |
| 31 | test_form_KKT_A_new.data += rg.standard_normal(test_form_KKT_A_new.nnz) |
| 32 | test_form_KKT_Pu_new = test_form_KKT_Pu.copy() |
| 33 | test_form_KKT_Pu_new.data += 0.1 * rg.standard_normal(test_form_KKT_Pu_new.nnz) |
| 34 | test_form_KKT_P_new = test_form_KKT_Pu_new + test_form_KKT_Pu_new.T - sparse.diags(test_form_KKT_Pu_new.diagonal()) |
| 35 | |
| 36 | test_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') |
| 40 | test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new, format='csc') |
| 41 | |
| 42 | |
| 43 | # Test solve problem with initial P and A |
| 44 | test_solve_P = test_form_KKT_P.copy() |
| 45 | test_solve_Pu = test_form_KKT_Pu.copy() |
| 46 | test_solve_q = rg.standard_normal(n) |
| 47 | test_solve_A = test_form_KKT_A.copy() |
| 48 | test_solve_l = -30 + rg.standard_normal(m) |
| 49 | test_solve_u = 30 + rg.standard_normal(m) |
| 50 | |
| 51 | |
| 52 | # Define new P |
| 53 | test_solve_P_new = test_form_KKT_P_new.copy() |
| 54 | test_solve_Pu_new = test_form_KKT_Pu_new.copy() |
| 55 | |
| 56 | |
| 57 | # Define new A |
| 58 | test_solve_A_new = test_form_KKT_A_new.copy() |
| 59 | |
| 60 | |
| 61 | # Generate test data and solutions |
| 62 | data = {'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 |
| 109 | cu.generate_data('update_matrices', data) |