-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhessian.h
More file actions
218 lines (176 loc) · 6.02 KB
/
hessian.h
File metadata and controls
218 lines (176 loc) · 6.02 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef PROBLEM_HESSIAN_H
#define PROBLEM_HESSIAN_H
#include "common.h"
/*
* py_problem_hessian: Compute Lagrangian Hessian
*
* Args:
* prob_capsule: PyCapsule containing problem pointer
* obj_factor: Scaling factor for objective Hessian (double)
* lagrange: Array of Lagrange multipliers (numpy array, length =
* total_constraint_size)
*
* Returns:
* Tuple of (data, indices, indptr, (m, n)) for scipy.sparse.csr_matrix
*/
static PyObject *py_problem_hessian(PyObject *self, PyObject *args)
{
PyObject *prob_capsule;
double obj_factor;
PyObject *lagrange_obj;
if (!PyArg_ParseTuple(args, "OdO", &prob_capsule, &obj_factor, &lagrange_obj))
{
return NULL;
}
problem *prob =
(problem *) PyCapsule_GetPointer(prob_capsule, PROBLEM_CAPSULE_NAME);
if (!prob)
{
PyErr_SetString(PyExc_ValueError, "invalid problem capsule");
return NULL;
}
/* Convert lagrange to contiguous C array */
PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF(
lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!lagrange_arr)
{
return NULL;
}
double *lagrange = (double *) PyArray_DATA(lagrange_arr);
/* Compute Hessian */
problem_hessian(prob, obj_factor, lagrange);
Py_DECREF(lagrange_arr);
/* Extract CSR components and return as tuple */
CSR_Matrix *H = prob->lagrange_hessian;
npy_intp nnz = H->nnz;
npy_intp n_plus_1 = H->n + 1;
PyObject *data = PyArray_SimpleNew(1, &nnz, NPY_DOUBLE);
PyObject *indices = PyArray_SimpleNew(1, &nnz, NPY_INT32);
PyObject *indptr = PyArray_SimpleNew(1, &n_plus_1, NPY_INT32);
if (!data || !indices || !indptr)
{
Py_XDECREF(data);
Py_XDECREF(indices);
Py_XDECREF(indptr);
return NULL;
}
/* Copy CSR data using memcpy for efficiency */
memcpy(PyArray_DATA((PyArrayObject *) data), H->x, nnz * sizeof(double));
memcpy(PyArray_DATA((PyArrayObject *) indices), H->i, nnz * sizeof(int));
memcpy(PyArray_DATA((PyArrayObject *) indptr), H->p, n_plus_1 * sizeof(int));
return Py_BuildValue("(OOO(ii))", data, indices, indptr, H->m, H->n);
}
static PyObject *py_get_hessian(PyObject *self, PyObject *args)
{
PyObject *prob_capsule;
if (!PyArg_ParseTuple(args, "O", &prob_capsule))
{
return NULL;
}
problem *prob =
(problem *) PyCapsule_GetPointer(prob_capsule, PROBLEM_CAPSULE_NAME);
if (!prob)
{
PyErr_SetString(PyExc_ValueError, "invalid problem capsule");
return NULL;
}
if (!prob->lagrange_hessian)
{
PyErr_SetString(PyExc_RuntimeError,
"hessian not initialized - call problem_hessian first");
return NULL;
}
CSR_Matrix *H = prob->lagrange_hessian;
npy_intp nnz = H->nnz;
npy_intp n_plus_1 = H->n + 1;
PyObject *data = PyArray_SimpleNew(1, &nnz, NPY_DOUBLE);
PyObject *indices = PyArray_SimpleNew(1, &nnz, NPY_INT32);
PyObject *indptr = PyArray_SimpleNew(1, &n_plus_1, NPY_INT32);
if (!data || !indices || !indptr)
{
Py_XDECREF(data);
Py_XDECREF(indices);
Py_XDECREF(indptr);
return NULL;
}
memcpy(PyArray_DATA((PyArrayObject *) data), H->x, nnz * sizeof(double));
memcpy(PyArray_DATA((PyArrayObject *) indices), H->i, nnz * sizeof(int));
memcpy(PyArray_DATA((PyArrayObject *) indptr), H->p, n_plus_1 * sizeof(int));
return Py_BuildValue("(OOO(ii))", data, indices, indptr, H->m, H->n);
}
static PyObject *py_get_problem_hessian_sparsity_coo(PyObject *self, PyObject *args)
{
PyObject *prob_capsule;
if (!PyArg_ParseTuple(args, "O", &prob_capsule))
{
return NULL;
}
problem *prob =
(problem *) PyCapsule_GetPointer(prob_capsule, PROBLEM_CAPSULE_NAME);
if (!prob)
{
PyErr_SetString(PyExc_ValueError, "invalid problem capsule");
return NULL;
}
if (!prob->lagrange_hessian_coo)
{
PyErr_SetString(PyExc_RuntimeError,
"hessian COO not initialized - call "
"problem_init_hessian_coo_lower_triangular first");
return NULL;
}
COO_Matrix *coo = prob->lagrange_hessian_coo;
npy_intp nnz = coo->nnz;
PyObject *rows = PyArray_SimpleNew(1, &nnz, NPY_INT32);
PyObject *cols = PyArray_SimpleNew(1, &nnz, NPY_INT32);
if (!rows || !cols)
{
Py_XDECREF(rows);
Py_XDECREF(cols);
return NULL;
}
memcpy(PyArray_DATA((PyArrayObject *) rows), coo->rows, nnz * sizeof(int));
memcpy(PyArray_DATA((PyArrayObject *) cols), coo->cols, nnz * sizeof(int));
return Py_BuildValue("(OO(ii))", rows, cols, coo->m, coo->n);
}
static PyObject *py_problem_eval_hessian_vals_coo(PyObject *self, PyObject *args)
{
PyObject *prob_capsule;
double obj_factor;
PyObject *lagrange_obj;
if (!PyArg_ParseTuple(args, "OdO", &prob_capsule, &obj_factor, &lagrange_obj))
{
return NULL;
}
problem *prob =
(problem *) PyCapsule_GetPointer(prob_capsule, PROBLEM_CAPSULE_NAME);
if (!prob)
{
PyErr_SetString(PyExc_ValueError, "invalid problem capsule");
return NULL;
}
/* Convert lagrange to contiguous C array */
PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF(
lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!lagrange_arr)
{
return NULL;
}
double *lagrange = (double *) PyArray_DATA(lagrange_arr);
/* Compute Hessian */
problem_hessian(prob, obj_factor, lagrange);
Py_DECREF(lagrange_arr);
/* Refresh COO values from the CSR Hessian */
refresh_lower_triangular_coo(prob->lagrange_hessian_coo,
prob->lagrange_hessian->x);
COO_Matrix *coo = prob->lagrange_hessian_coo;
npy_intp nnz = coo->nnz;
PyObject *data = PyArray_SimpleNew(1, &nnz, NPY_DOUBLE);
if (!data)
{
return NULL;
}
memcpy(PyArray_DATA((PyArrayObject *) data), coo->x, nnz * sizeof(double));
return data;
}
#endif /* PROBLEM_HESSIAN_H */