Debian Codegraph @

src/TonemappingOperators/mantiuk08/cqp/initial_point.c

raw
1 /**
2 * @brief Convex Quadratic Programming library
3 *
4 * From:
5 * http://ra.uni-trier.de/~huebner/software.html
6 *
7 * ----------------------------------------------------------------------
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ----------------------------------------------------------------------
22 *
23 * @author Ewgenij Hübner
24 *
25 * $$
26 */
27
28 #include <gsl/gsl_blas.h>
29 #include <gsl/gsl_linalg.h>
30 #include <gsl/gsl_math.h>
31
32 #include "initial_point.h"
33
34 int pdip_initial_point_feasible_x(const gsl_matrix *A, const gsl_vector *b,
35 gsl_vector *x) {
36 int signum;
37
38 /* x = A^t*(AA^t)^{-1}*b */
39
40 gsl_matrix *AA_t;
41 gsl_vector *tmp;
42 gsl_permutation *p;
43
44 AA_t = gsl_matrix_calloc(A->size1, A->size1);
45 if (AA_t == 0) {
46 GSL_ERROR_VAL("failed to initialize space for finding initial point",
47 GSL_ENOMEM, 0);
48 }
49
50 tmp = gsl_vector_alloc(A->size1);
51 if (tmp == 0) {
52 gsl_matrix_free(AA_t);
53 GSL_ERROR_VAL("failed to initialize space for finding initial point",
54 GSL_ENOMEM, 0);
55 }
56
57 p = gsl_permutation_alloc(A->size1);
58 if (p == 0) {
59 gsl_vector_free(tmp);
60 gsl_matrix_free(AA_t);
61 GSL_ERROR_VAL("failed to initialize space for finding initial point",
62 GSL_ENOMEM, 0);
63 }
64
65 /* status = gsl_blas_dsyrk(CblasUpper, CblasNoTrans, 1.0, cqp->A, 0.0,
66 * AA_t); */
67 gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, A, A, 0.0, AA_t);
68
69 /* (AA^t)tmp=b */
70 gsl_linalg_LU_decomp(AA_t, p, &signum);
71 gsl_linalg_LU_solve(AA_t, p, b, tmp);
72
73 /*
74 gsl_blas_dtrsv(CblasUpper, CblasNoTrans, CblasNonUnit, AA_t, tmp);
75 tmp=(AA^t)^{-1}tmp */
76
77 gsl_blas_dgemv(CblasTrans, 1.0, A, tmp, 0.0, x);
78
79 gsl_matrix_free(AA_t);
80 gsl_vector_free(tmp);
81 gsl_permutation_free(p);
82
83 return GSL_SUCCESS;
84 }
85
86 int pdip_initial_point_feasible_s(const gsl_matrix *C, const gsl_vector *d,
87 const gsl_vector *x, gsl_vector *s) {
88 /* s=Cx-d */
89
90 gsl_blas_dcopy(d, s);
91
92 gsl_blas_dgemv(CblasNoTrans, 1.0, C, x, -1.0, s);
93
94 return GSL_SUCCESS;
95 }
96
97 int pdip_initial_point_y(const gsl_matrix *Q, const gsl_vector *q,
98 const gsl_matrix *A, const gsl_vector *x,
99 gsl_vector *y) {
100 int signum;
101
102 /* y=(AA^t)^{-1}(A(Qx+q)) */
103
104 gsl_matrix *AA_t;
105 gsl_vector *tmp;
106 gsl_permutation *p;
107
108 AA_t = gsl_matrix_calloc(A->size1, A->size1);
109 if (AA_t == 0) {
110 GSL_ERROR_VAL("failed to initialize space for finding initial point",
111 GSL_ENOMEM, 0);
112 }
113
114 tmp = gsl_vector_alloc(q->size);
115 if (tmp == 0) {
116 gsl_matrix_free(AA_t);
117 GSL_ERROR_VAL("failed to initialize space for finding initial point",
118 GSL_ENOMEM, 0);
119 }
120
121 p = gsl_permutation_alloc(AA_t->size1);
122 if (p == 0) {
123 gsl_vector_free(tmp);
124 gsl_matrix_free(AA_t);
125 GSL_ERROR_VAL("failed to initialize space for finding initial point",
126 GSL_ENOMEM, 0);
127 }
128
129 gsl_blas_dcopy(q, tmp);
130 gsl_blas_dsymv(CblasUpper, 1.0, Q, x, 1.0, tmp);
131 gsl_blas_dgemv(CblasNoTrans, 1.0, A, tmp, 0.0, y);
132
133 /* gsl_blas_dsyrk(CblasUpper, CblasNoTrans, 1.0, cqp->A, 0.0, AA_t); */
134
135 gsl_vector_free(tmp);
136 tmp = gsl_vector_alloc(y->size);
137 if (tmp == 0) {
138 gsl_permutation_free(p);
139 gsl_matrix_free(AA_t);
140 GSL_ERROR_VAL("failed to initialize space for finding initial point",
141 GSL_ENOMEM, 0);
142 }
143
144 gsl_blas_dcopy(y, tmp);
145
146 gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, A, A, 0.0, AA_t);
147
148 gsl_linalg_LU_decomp(AA_t, p, &signum);
149 gsl_linalg_LU_solve(AA_t, p, tmp, y);
150
151 /* gsl_blas_dtrsv(CblasUpper, CblasNoTrans, CblasNonUnit, AA_t, y); */
152
153 gsl_matrix_free(AA_t);
154 gsl_vector_free(tmp);
155 gsl_permutation_free(p);
156
157 return GSL_SUCCESS;
158 }
159
160 int pdip_initial_point_z(gsl_vector *z) {
161 double delta_z = 0.1;
162
163 gsl_vector_set_all(z, delta_z);
164
165 return GSL_SUCCESS;
166 }
167
168 int pdip_initial_point_strict_feasible(gsl_vector *x, gsl_vector *s) {
169 double delta_x, delta_s, xs = 0.0, sum_x = 0.0, sum_s = 0.0, tmp,
170 rg = 1e-10;
171
172 size_t i;
173
174 // int status;
175
176 delta_x = GSL_MAX_DBL(-1.5 * gsl_vector_min(x), 0.0);
177 delta_s = GSL_MAX_DBL(-1.5 * gsl_vector_min(s), 0.0);
178
179 if (delta_x < rg && delta_s < rg) {
180 // status = gsl_blas_ddot(x, s, &tmp);
181 gsl_blas_ddot(x, s, &tmp);
182
183 if (tmp < rg) /* the initial point is optimal */
184 return GSL_SUCCESS;
185 }
186
187 for (i = 0; i < x->size; i++) {
188 xs +=
189 (gsl_vector_get(x, i) + delta_x) * (gsl_vector_get(s, i) + delta_s);
190
191 sum_x += gsl_vector_get(x, i);
192 sum_s += gsl_vector_get(s, i);
193 }
194
195 sum_x += delta_x * (x->size);
196 sum_s += delta_s * (s->size);
197
198 delta_x += 0.5 * xs / sum_s;
199 delta_s += 0.5 * xs / sum_x;
200
201 // status = gsl_vector_add_constant(x, delta_x);
202 // status = gsl_vector_add_constant(s, delta_s);
203 gsl_vector_add_constant(x, delta_x);
204 gsl_vector_add_constant(s, delta_s);
205
206 return GSL_SUCCESS;
207 }