48 void print_final_values(
int n_u,
int n_v,
float* u,
float**
B,
float* v,
float* umin,
float* umax);
52 #define WLS_VERBOSE FALSE
67 for (
int j = 0; j < n; j++) {
68 for (
int i = 0; i < m; i++) {
101 int wls_alloc(
float* u,
float* v,
float* umin,
float* umax,
float**
B,
102 int n_u,
int n_v,
float* u_guess,
float* W_init,
float* Wv,
103 float* Wu,
float*
up,
float gamma_sq,
int imax) {
105 if(!gamma_sq) gamma_sq = 100000;
106 if(!imax) imax = 100;
110 float A_free[n_c][n_u];
114 float * A_free_ptr[n_c];
115 for(
int i = 0; i < n_c; i++)
116 A_free_ptr[i] = A_free[i];
122 int free_index_lookup[n_u];
130 int infeasible_index[n_u]
UNUSED;
131 int n_infeasible = 0;
137 for (
int i = 0; i < n_u; i++) {
138 u[i] = (umax[i] + umin[i]) * 0.5;
141 for (
int i = 0; i < n_u; i++) {
145 W_init ? memcpy(W, W_init, n_u *
sizeof(
float))
146 : memset(W, 0, n_u *
sizeof(
float));
148 memset(free_index_lookup, -1, n_u *
sizeof(
float));
152 for (
int i = 0; i < n_u; i++) {
154 free_index_lookup[i] = n_free;
155 free_index[n_free++] = i;
160 for (
int i = 0; i < n_v; i++) {
162 b[i] = Wv ? gamma_sq * Wv[i] * v[i] : gamma_sq * v[i];
164 for (
int j = 0; j < n_u; j++) {
166 A[i][j] = Wv ? gamma_sq * Wv[i] * B[i][j] : gamma_sq * B[i][j];
167 d[i] -= A[i][j] * u[j];
170 for (
int i = n_v; i < n_c; i++) {
171 memset(A[i], 0, n_u *
sizeof(
float));
172 A[i][i - n_v] = Wu ? Wu[i - n_v] : 1.0;
173 b[i] = up ? (Wu ? Wu[i] * up[i] : up[i]) : 0;
174 d[i] = b[i] - A[i][i - n_v] * u[i - n_v];
178 while (iter++ < imax) {
180 memset(p, 0, n_u *
sizeof(
float));
181 memcpy(u_opt, u, n_u *
sizeof(
float));
184 if (free_chk != n_free) {
185 for (
int i = 0; i < n_c; i++) {
186 for (
int j = 0; j < n_free; j++) {
187 A_free[i][j] = A[i][free_index[j]];
208 for (
int i = 0; i < n_free; i++) {
209 p[free_index[i]] = p_free[i];
210 u_opt[free_index[i]] += p_free[i];
214 for (
int i = 0; i < n_u; i++) {
215 if (u_opt[i] >= (umax[i] + 1.0) || u_opt[i] <= (umin[i] - 1.0)) {
216 infeasible_index[n_infeasible++] = i;
221 if (n_infeasible == 0) {
223 memcpy(u, u_opt, n_u *
sizeof(
float));
224 memset(lambda, 0, n_u *
sizeof(
float));
227 for (
int i = 0; i < n_c; i++) {
228 for (
int k = 0; k < n_free; k++) {
229 d[i] -= A_free[i][k] * p_free[k];
231 for (
int k = 0; k < n_u; k++) {
232 lambda[k] += A[i][k] * d[i];
235 bool break_flag =
true;
238 for (
int i = 0; i < n_u; i++) {
241 if (lambda[i] < -FLT_EPSILON) {
245 if (free_index_lookup[i] < 0) {
246 free_index_lookup[i] = n_free;
247 free_index[n_free++] = i;
261 float alpha = INFINITY;
266 for (
int i = 0; i < n_free; i++) {
267 int id = free_index[i];
268 if(fabs(p[
id]) > FLT_EPSILON) {
269 alpha_tmp = (p[id] < 0) ? (umin[
id] - u[
id]) / p[id]
270 : (umax[id] - u[id]) / p[
id];
272 alpha_tmp = INFINITY;
274 if (alpha_tmp < alpha) {
281 for (
int i = 0; i < n_u; i++) {
282 u[i] += alpha * p[i];
285 for (
int i = 0; i < n_c; i++) {
286 for (
int k = 0; k < n_free; k++) {
287 d[i] -= A_free[i][k] * alpha * p_free[k];
291 W[id_alpha] = (p[id_alpha] > 0) ? 1.0 : -1.0;
293 free_index[free_index_lookup[id_alpha]] = free_index[--n_free];
294 free_index_lookup[free_index[free_index_lookup[id_alpha]]] =
295 free_index_lookup[id_alpha];
296 free_index_lookup[id_alpha] = -1;
306 printf(
"n_c = %d n_free = %d\n", n_c, n_free);
308 printf(
"A_free =\n");
309 for(
int i = 0; i < n_c; i++) {
310 for (
int j = 0; j < n_free; j++) {
311 printf(
"%f ", A_free_ptr[i][j]);
317 for (
int j = 0; j < n_c; j++) {
321 printf(
"\noutput = ");
322 for (
int j = 0; j < n_free; j++) {
323 printf(
"%f ", p_free[j]);
328 void print_final_values(
int n_u,
int n_v,
float* u,
float**
B,
float* v,
float* umin,
float* umax) {
329 printf(
"n_u = %d n_v = %d\n", n_u, n_v);
332 for(
int i = 0; i < n_v; i++) {
333 for (
int j = 0; j < n_u; j++) {
334 printf(
"%f ", B[i][j]);
340 for (
int j = 0; j < n_v; j++) {
345 for (
int j = 0; j < n_u; j++) {
351 for (
int j = 0; j < n_u; j++) {
352 printf(
"%f ", umin[j]);
357 for (
int j = 0; j < n_u; j++) {
358 printf(
"%f ", umax[j]);
void print_final_values(int n_u, int n_v, float *u, float **B, float *v, float *umin, float *umax)
void qr_solve(int m, int n, float a[], float b[], float x[])
int wls_alloc(float *u, float *v, float *umin, float *umax, float **B, int n_u, int n_v, float *u_guess, float *W_init, float *Wv, float *Wu, float *up, float gamma_sq, int imax)
active set algorithm for control allocation
void print_in_and_outputs(int n_c, int n_free, float **A_free_ptr, float *d, float *p_free)
void qr_solve_wrapper(int m, int n, float **A, float *b, float *x)
Wrapper for qr solve.