-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
100 lines (89 loc) · 2.04 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# https://github.com/chcomin/ctypes-numpy-example/tree/master/simplest
import numpy as np
import ctypes as ct
funcs = ct.CDLL("./bin/libas.so")
AS_N_V = 6
AS_N_U = 20
AS_N_C = AS_N_U + AS_N_V
n_v = 4
n_u = 4
theta = ct.c_double(0.002)
cond_bound = ct.c_double(1e8)
B = np.random.random((AS_N_V, AS_N_U))
Wv = np.random.random(AS_N_V)
Wu = np.random.random(AS_N_U)
up = np.random.random(AS_N_U)
dv = np.random.random(AS_N_V)
A = np.zeros((AS_N_C, AS_N_U))
b = np.zeros(AS_N_C)
gamma = ct.c_double()
c_doublep = ct.POINTER(ct.c_double)
c_intp = ct.POINTER(ct.c_int)
c_int8p = ct.POINTER(ct.c_int8)
funcs.setupWLS_A.restype = None
funcs.setupWLS_A(
B.ctypes.data_as(c_doublep),
Wv.ctypes.data_as(c_doublep),
Wu.ctypes.data_as(c_doublep),
n_v, n_u,
theta,
cond_bound,
A.ctypes.data_as(c_doublep),
ct.byref(gamma),
)
funcs.setupWLS_b.restype = None
funcs.setupWLS_b(
dv.ctypes.data_as(c_doublep),
up.ctypes.data_as(c_doublep),
Wv.ctypes.data_as(c_doublep),
Wu.ctypes.data_as(c_doublep),
n_v, n_u,
gamma,
b.ctypes.data_as(c_doublep),
)
umin = np.zeros(AS_N_U)
umax = np.ones(AS_N_U)
us = np.zeros(AS_N_U)
Ws = np.zeros(AS_N_U, dtype=np.int8)
imax = 10
iter = ct.c_int()
n_free = ct.c_int()
costs = np.zeros((15), )
activeSetAlgo = ct.CFUNCTYPE(
ct.c_int,
c_doublep,
c_doublep,
c_doublep,
c_doublep,
c_doublep,
c_int8p,
ct.c_int,
ct.c_int,
ct.c_int,
c_intp,
c_intp,
c_doublep,
)
AS_QR_NAIVE = 0
AS_QR = 1
AS_CHOL = 2
AS_CG = 3
funcs.solveActiveSet.restype = ct.c_void_p # to get memory address correct
solveActiveSet = activeSetAlgo(funcs.solveActiveSet(AS_QR))
res = solveActiveSet(
A.ctypes.data_as(c_doublep),
b.ctypes.data_as(c_doublep),
umin.ctypes.data_as(c_doublep),
umax.ctypes.data_as(c_doublep),
us.ctypes.data_as(c_doublep),
Ws.ctypes.data_as(c_int8p),
imax,
n_u, n_v,
ct.byref(iter), ct.byref(n_free),
costs.ctypes.data_as(c_doublep),
)
#
# print("hey")
#
# print(f'Return: {res}')
# print(f'us : {us[:n_u]}')