generate_problem.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import numpy as np
  2. from scipy import sparse
  3. import utils.codegen_utils as cu
  4. # Set numpy seed for reproducibility
  5. np.random.seed(2)
  6. # Define tests
  7. n = 5
  8. m = 8
  9. test_form_KKT_n = n
  10. test_form_KKT_m = m
  11. p = 0.7
  12. test_form_KKT_A = sparse.random(test_form_KKT_m, test_form_KKT_n, density=p, format='csc')
  13. test_form_KKT_P = sparse.random(n, n, density=p)
  14. test_form_KKT_P = test_form_KKT_P.dot(test_form_KKT_P.T).tocsc() + sparse.eye(n, format='csc')
  15. test_form_KKT_Pu = sparse.triu(test_form_KKT_P, format='csc')
  16. test_form_KKT_rho = 1.6
  17. test_form_KKT_sigma = 0.1
  18. test_form_KKT_KKT = sparse.vstack([
  19. sparse.hstack([test_form_KKT_P + test_form_KKT_sigma *
  20. sparse.eye(test_form_KKT_n), test_form_KKT_A.T]),
  21. sparse.hstack([test_form_KKT_A,
  22. -1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
  23. ], format='csc')
  24. test_form_KKT_KKTu = sparse.triu(test_form_KKT_KKT, format='csc')
  25. # Create new P, A and KKT
  26. test_form_KKT_A_new = test_form_KKT_A.copy()
  27. test_form_KKT_A_new.data += np.random.randn(test_form_KKT_A_new.nnz)
  28. test_form_KKT_Pu_new = test_form_KKT_Pu.copy()
  29. test_form_KKT_Pu_new.data += 0.1 * np.random.randn(test_form_KKT_Pu_new.nnz)
  30. test_form_KKT_P_new = test_form_KKT_Pu_new + test_form_KKT_Pu_new.T - sparse.diags(test_form_KKT_Pu_new.diagonal())
  31. test_form_KKT_KKT_new = sparse.vstack([
  32. sparse.hstack([test_form_KKT_P_new + test_form_KKT_sigma *
  33. sparse.eye(test_form_KKT_n), test_form_KKT_A_new.T]),
  34. sparse.hstack([test_form_KKT_A_new,
  35. -1./test_form_KKT_rho * sparse.eye(test_form_KKT_m)])
  36. ], format='csc')
  37. test_form_KKT_KKTu_new = sparse.triu(test_form_KKT_KKT_new, format='csc')
  38. # Test solve problem with initial P and A
  39. test_solve_P = test_form_KKT_P.copy()
  40. test_solve_Pu = test_form_KKT_Pu.copy()
  41. test_solve_q = np.random.randn(n)
  42. test_solve_A = test_form_KKT_A.copy()
  43. test_solve_l = -30 + np.random.randn(m)
  44. test_solve_u = 30 + np.random.randn(m)
  45. # Define new P
  46. test_solve_P_new = test_form_KKT_P_new.copy()
  47. test_solve_Pu_new = test_form_KKT_Pu_new.copy()
  48. # Define new A
  49. test_solve_A_new = test_form_KKT_A_new.copy()
  50. # Generate test data and solutions
  51. data = {'test_form_KKT_n': test_form_KKT_n,
  52. 'test_form_KKT_m': test_form_KKT_m,
  53. 'test_form_KKT_A': test_form_KKT_A,
  54. 'test_form_KKT_Pu': test_form_KKT_Pu,
  55. 'test_form_KKT_rho': test_form_KKT_rho,
  56. 'test_form_KKT_sigma': test_form_KKT_sigma,
  57. 'test_form_KKT_KKT': test_form_KKT_KKT,
  58. 'test_form_KKT_KKTu': test_form_KKT_KKTu,
  59. 'test_form_KKT_A_new': test_form_KKT_A_new,
  60. 'test_form_KKT_Pu_new': test_form_KKT_Pu_new,
  61. 'test_form_KKT_KKT_new': test_form_KKT_KKT_new,
  62. 'test_form_KKT_KKTu_new': test_form_KKT_KKTu_new,
  63. 'test_solve_Pu': test_solve_Pu,
  64. 'test_solve_q': test_solve_q,
  65. 'test_solve_A': test_solve_A,
  66. 'test_solve_l': test_solve_l,
  67. 'test_solve_u': test_solve_u,
  68. 'n': n,
  69. 'm': m,
  70. 'test_solve_x': np.array([-0.34967513, 1.20460722, -0.46259805,
  71. 0.59083905, -0.87685541]),
  72. 'test_solve_y': np.zeros(m),
  73. 'test_solve_obj_value': -1.7665127080483103,
  74. 'test_solve_status': 'optimal',
  75. 'test_solve_Pu_new': test_solve_Pu_new,
  76. 'test_solve_P_new_x': np.array([-0.28228879, 1.3527703, -0.69277181,
  77. 0.82445911, -1.11688134]),
  78. 'test_solve_P_new_y': np.zeros(m),
  79. 'test_solve_P_new_obj_value': -2.1490899311728526,
  80. 'test_solve_P_new_status': 'optimal',
  81. 'test_solve_A_new': test_solve_A_new,
  82. 'test_solve_A_new_x': np.array([-0.34967513, 1.20460722, -0.46259805,
  83. 0.59083905, -0.87685541]),
  84. 'test_solve_A_new_y': np.zeros(m),
  85. 'test_solve_A_new_obj_value': -1.7665127080484808,
  86. 'test_solve_A_new_status': 'optimal',
  87. 'test_solve_P_A_new_x': np.array([-0.28228879, 1.3527703, -0.69277181,
  88. 0.82445911, -1.11688134]),
  89. 'test_solve_P_A_new_y': np.zeros(m),
  90. 'test_solve_P_A_new_obj_value': -2.1490899311726253,
  91. 'test_solve_P_A_new_status': 'optimal'
  92. }
  93. # Generate test data
  94. cu.generate_data('update_matrices', data)