Skip to content

Commit

Permalink
Minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
gbanjac committed Mar 4, 2017
1 parent 60de966 commit 4e807da
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 78 deletions.
4 changes: 0 additions & 4 deletions tests/generate_tests_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Code to generate the unittests for OSQP C code
import numpy as np

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

import basic_qp.generate_problem
import basic_qp2.generate_problem
Expand Down
3 changes: 3 additions & 0 deletions tests/infeasibility/generate_problem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import scipy.sparse as spa
import scipy as sp
import utils.codegen_utils as cu

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

n = 50
m = 150
Expand Down
2 changes: 2 additions & 0 deletions tests/lin_alg/generate_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import scipy.sparse as spa
import utils.codegen_utils as cu

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

# Test sparse matrix construction vs dense
test_sp_matrix_Adns = np.around(.6*np.random.rand(5, 6)) + np.random.randn(5,6)
Expand Down
4 changes: 0 additions & 4 deletions tests/osqp_tester_direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <stdio.h>


#include "minunit.h"
#include "osqp.h"

Expand All @@ -16,9 +15,6 @@
#include "update_matrices/test_update_matrices.h"





int tests_run = 0;


Expand Down
5 changes: 4 additions & 1 deletion tests/solve_linsys/generate_problem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import scipy.sparse as spa
import scipy.sparse.linalg as spla
import utils.codegen_utils as cu

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

# Simple case
test_solve_KKT_n = 2
Expand All @@ -18,7 +21,7 @@
spa.hstack([test_solve_KKT_A,
-1./test_solve_KKT_rho * spa.eye(test_solve_KKT_m)])]).tocsc()
test_solve_KKT_rhs = np.random.randn(test_solve_KKT_m + test_solve_KKT_n)
test_solve_KKT_x = spa.linalg.splu(test_solve_KKT_KKT.tocsc()).solve(test_solve_KKT_rhs)
test_solve_KKT_x = spla.splu(test_solve_KKT_KKT.tocsc()).solve(test_solve_KKT_rhs)

# import ipdb; ipdb.set_trace()

Expand Down
96 changes: 27 additions & 69 deletions tests/update_matrices/generate_problem.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
import scipy.sparse as spa
import utils.codegen_utils as cu
import cvxpy

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


# Define tests
Expand All @@ -21,7 +22,7 @@
test_form_KKT_KKT = spa.vstack([
spa.hstack([test_form_KKT_P + test_form_KKT_sigma *
spa.eye(test_form_KKT_n), test_form_KKT_A.T]),
spa.hstack([test_form_KKT_A,
spa.hstack([test_form_KKT_A,
-1./test_form_KKT_rho * spa.eye(test_form_KKT_m)])]).tocsc()
test_form_KKT_KKTu = spa.triu(test_form_KKT_KKT).tocsc()

Expand All @@ -41,8 +42,6 @@
test_form_KKT_KKTu_new = spa.triu(test_form_KKT_KKT_new).tocsc()




# Test solve problem with initial P and A
test_solve_P = test_form_KKT_P.copy()
test_solve_Pu = test_form_KKT_Pu.copy()
Expand All @@ -52,62 +51,18 @@
test_solve_u = 30 + np.random.randn(m)


# Solve problem with cvxpy
x = cvxpy.Variable(n)
objective = cvxpy.Minimize(0.5 * cvxpy.quad_form(x, test_solve_P.todense()) + test_solve_q * x )
constraints = [test_solve_A.todense() * x <= test_solve_u, test_solve_l <= test_solve_A.todense() * x]
prob = cvxpy.Problem(objective, constraints)
prob.solve(abstol=1e-7, reltol=1e-7)
test_solve_x = np.asarray(x.value).flatten()
test_solve_y = (constraints[0].dual_value - constraints[1].dual_value).A1
test_solve_obj_value = objective.value
test_solve_status = prob.status

# Solve with new P
# Define new P
test_solve_P_new = test_form_KKT_P_new.copy()
test_solve_Pu_new = test_form_KKT_Pu_new.copy()
x = cvxpy.Variable(n)
objective = cvxpy.Minimize(0.5 * cvxpy.quad_form(x, test_solve_P_new) + test_solve_q * x )
constraints = [test_solve_A * x <= test_solve_u, test_solve_l <= test_solve_A * x]
prob = cvxpy.Problem(objective, constraints)
prob.solve(abstol=1e-7, reltol=1e-7)
test_solve_P_new_x = np.asarray(x.value).flatten()
test_solve_P_new_y = (constraints[0].dual_value - constraints[1].dual_value).A1
test_solve_P_new_obj_value = objective.value
test_solve_P_new_status = prob.status



# Solve with new A
# Define new A
test_solve_A_new = test_form_KKT_A_new.copy()
x = cvxpy.Variable(n)
objective = cvxpy.Minimize(0.5 * cvxpy.quad_form(x, test_solve_P) + test_solve_q * x )
constraints = [test_solve_A_new * x <= test_solve_u, test_solve_l <= test_solve_A_new * x]
prob = cvxpy.Problem(objective, constraints)
prob.solve(abstol=1e-7, reltol=1e-7)
test_solve_A_new_x = np.asarray(x.value).flatten()
test_solve_A_new_y = (constraints[0].dual_value - constraints[1].dual_value).A1
test_solve_A_new_obj_value = objective.value
test_solve_A_new_status = prob.status


# Solve with new P and A
x = cvxpy.Variable(n)
objective = cvxpy.Minimize(0.5 * cvxpy.quad_form(x, test_solve_P_new) + test_solve_q * x )
constraints = [test_solve_A_new * x <= test_solve_u, test_solve_l <= test_solve_A_new * x]
prob = cvxpy.Problem(objective, constraints)
prob.solve(abstol=1e-7, reltol=1e-7)
test_solve_P_A_new_x = np.asarray(x.value).flatten()
test_solve_P_A_new_y = (constraints[0].dual_value - constraints[1].dual_value).A1
test_solve_P_A_new_obj_value = objective.value
test_solve_P_A_new_status = prob.status




# Generate test data and solutions
data = {'test_form_KKT_n':test_form_KKT_n,
'test_form_KKT_m':test_form_KKT_m,
data = {'test_form_KKT_n': test_form_KKT_n,
'test_form_KKT_m': test_form_KKT_m,
'test_form_KKT_A': test_form_KKT_A,
'test_form_KKT_P': test_form_KKT_P,
'test_form_KKT_Pu': test_form_KKT_Pu,
Expand All @@ -128,28 +83,31 @@
'test_solve_u': test_solve_u,
'n': n,
'm': m,
'test_solve_x': test_solve_x,
'test_solve_y': test_solve_y,
'test_solve_obj_value': test_solve_obj_value,
'test_solve_status': test_solve_status,
'test_solve_x': np.array([-0.34967513, 1.20460722, -0.46259805,
0.59083905, -0.87685541]),
'test_solve_y': np.zeros(m),
'test_solve_obj_value': -1.7665127080483103,
'test_solve_status': 'optimal',
'test_solve_P_new': test_solve_P_new,
'test_solve_Pu_new': test_solve_Pu_new,
'test_solve_P_new_x': test_solve_P_new_x,
'test_solve_P_new_y': test_solve_P_new_y,
'test_solve_P_new_obj_value': test_solve_P_new_obj_value,
'test_solve_P_new_status': test_solve_P_new_status,
'test_solve_P_new_x': np.array([-0.28228879, 1.3527703, -0.69277181,
0.82445911, -1.11688134]),
'test_solve_P_new_y': np.zeros(m),
'test_solve_P_new_obj_value': -2.1490899311728526,
'test_solve_P_new_status': 'optimal',
'test_solve_A_new': test_solve_A_new,
'test_solve_A_new_x': test_solve_A_new_x,
'test_solve_A_new_y': test_solve_A_new_y,
'test_solve_A_new_obj_value': test_solve_A_new_obj_value,
'test_solve_A_new_status': test_solve_A_new_status,
'test_solve_P_A_new_x': test_solve_P_A_new_x,
'test_solve_P_A_new_y': test_solve_P_A_new_y,
'test_solve_P_A_new_obj_value': test_solve_P_A_new_obj_value,
'test_solve_P_A_new_status': test_solve_P_A_new_status
'test_solve_A_new_x': np.array([-0.34967513, 1.20460722, -0.46259805,
0.59083905, -0.87685541]),
'test_solve_A_new_y': np.zeros(m),
'test_solve_A_new_obj_value': -1.7665127080484808,
'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_y': np.zeros(m),
'test_solve_P_A_new_obj_value': -2.1490899311726253,
'test_solve_P_A_new_status': 'optimal'
}

# import ipdb; ipdb.set_trace()

# Generate test data
cu.generate_data('update_matrices', data)

0 comments on commit 4e807da

Please sign in to comment.