Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
boundary_transformation.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stddef.h>
19#include <stdlib.h>
20#include <math.h>
21#include <float.h> /* DBL_MAX */
22#include <stdio.h>
24
25static int do_assertions = 1;
26static unsigned long _index(cmaes_boundary_transformation_t *t, unsigned long i);
27static void _FatalError(const char *s);
28static double default_lower[1];
29static double default_upper[1];
30
32 double const *lower_bounds, double const *upper_bounds, unsigned long len_of_bounds)
33{
34 unsigned i;
35 double const *ub, *lb;
36 default_lower[0] = DBL_MAX / -1e2;
37 default_upper[0] = DBL_MAX / 1e2;
38 t->lower_bounds = lower_bounds;
39 t->upper_bounds = upper_bounds;
40 t->len_of_bounds = len_of_bounds;
41
42 if (lower_bounds == NULL && len_of_bounds <= 1) /* convenience default */
44 if (upper_bounds == NULL && len_of_bounds <= 1)
46 if (len_of_bounds == 0) {
49 t->len_of_bounds = 1;
50 }
51
52 if (t->lower_bounds == NULL || t->upper_bounds == NULL)
53 _FatalError("init: input upper_bounds or lower_bounds was NULL and len_of_bounds > 1");
54
55 /* compute boundaries in pre-image space, al and au */
56 t->al = (double *) calloc(t->len_of_bounds, sizeof(double));
57 t->au = (double *) calloc(t->len_of_bounds, sizeof(double));
58 if (!t->al || !t->au)
59 _FatalError(" in _init(): could not allocate memory");
60
61 lb = t->lower_bounds;
62 ub = t->upper_bounds;
63 for(i = 0; i < t->len_of_bounds; ++i) {
64 if (lb[i] == ub[i])
65 _FatalError("in _init: lower and upper bounds must be different in all variables");
66 /* between lb+al and ub-au transformation is the identity */
67 t->al[i] = fmin((ub[i] - lb[i]) / 2., (1. + fabs(lb[i])) / 20.);
68 t->au[i] = fmin((ub[i] - lb[i]) / 2., (1. + fabs(ub[i])) / 20.);
69 }
70}
72{
73 if(t->al)
74 free(t->al);
75 if(t->au)
76 free(t->au);
77}
79 double const *x, double *y, unsigned long len)
80{
81 double lb, ub, al, au;
82 unsigned long i;
84 for(i = 0; i < len; ++i) {
85 lb = t->lower_bounds[_index(t, i)];
86 ub = t->upper_bounds[_index(t, i)];
87 al = t->al[_index(t, i)];
88 au = t->au[_index(t, i)];
89 if (y[i] < lb + al)
90 y[i] = lb + (y[i] - (lb - al)) * (y[i] - (lb - al)) / 4. / al;
91 else if (y[i] > ub - au)
92 y[i] = ub - (y[i] - (ub + au)) * (y[i] - (ub + au)) / 4. / au;
93 }
94}
96 cmaes_boundary_transformation_t *t, double const *x, double *y, unsigned long len)
97{
98 double lb, ub, al, au, r, xlow, xup;
99 unsigned long i;
100
101 for(i = 0; i < len; ++i) {
102 lb = t->lower_bounds[_index(t, i)];
103 ub = t->upper_bounds[_index(t, i)];
104 al = t->al[_index(t, i)];
105 au = t->au[_index(t, i)];
106 xlow = lb - 2 * al - (ub - lb) / 2.0;
107 xup = ub + 2 * au + (ub - lb) / 2.0;
108 r = 2 * (ub - lb + al + au); /* == xup - xlow == period of the transformation */
109
110 y[i] = x[i];
111
112 if (y[i] < xlow) { /* shift up */
113 y[i] += r * (1 + (int)((xlow - y[i]) / r));
114 }
115 if (y[i] > xup) { /* shift down */
116 y[i] -= r * (1 + (int)((y[i] - xup) / r));
117 /* printf(" \n%f\n", fmod(y[i] - ub - au, r)); */
118 }
119 if (y[i] < lb - al) /* mirror */
120 y[i] += 2 * (lb - al - y[i]);
121 if (y[i] > ub + au)
122 y[i] -= 2 * (y[i] - ub - au);
123
124 if ((y[i] < lb - al - 1e-15) || (y[i] > ub + au + 1e-15)) {
125 printf("BUG in cmaes_boundary_transformation_shift_into_feasible_preimage: lb=%f, ub=%f, al=%f au=%f, y=%f\n",
126 lb, ub, al, au, y[i]);
127 _FatalError("BUG");
128 }
129 }
130}
132 double const *x, double *y, unsigned long len)
133{
134 double lb, ub, al, au;
135 unsigned long i;
136
137 for (i = 0; i < len; ++i) {
138 lb = t->lower_bounds[_index(t, i)];
139 ub = t->upper_bounds[_index(t, i)];
140 al = t->al[_index(t, i)];
141 au = t->au[_index(t, i)];
142 y[i] = x[i];
143 if (y[i] < lb + al)
144 y[i] = (lb - al) + 2 * sqrt(al * (y[i] - lb));
145 else if (y[i] > ub - au)
146 y[i] = (ub + au) - 2 * sqrt(au * (ub - y[i]));
147 }
148 if (11 < 3 || do_assertions) {
149 double *z = (double *) calloc(len, sizeof(double));
150 for (i = 0; i < len; ++i)
151 z[i] = y[i];
152 cmaes_boundary_transformation(t, z, y, len);
153 for (i = 0; i < len; ++i)
154 if (fabs(y[i] - x[i]) > 1e-14)
155 printf(" difference for index %ld should be zero, is %f ", i, y[i] - x[i]);
156 for (i = 0; i < len; ++i)
157 y[i] = z[i];
158 free(z);
159 }
160}
161/*
162static void _manage_len(cmaes_boundary_transformation_t *t, unsigned int len)
163{
164 if (t->len_of_return_value < len) {
165 if (t->return_value)
166 free(t->return_value);
167 t->return_value = calloc(len, sizeof(double));
168 t->len_of_return_value = len;
169 }
170 if (!t->return_value)
171 _FatalError("could not allocate memory");
172}
173*/
174static unsigned long _index(cmaes_boundary_transformation_t *t, unsigned long i)
175{
176 return i < t->len_of_bounds ? i : t->len_of_bounds - 1;
177}
178static void _FatalError(const char *s)
179{
180 printf("Fatal error in cmaes_boundary_transformation: %s\n", s);
181 exit(1);
182}
void cmaes_boundary_transformation_exit(cmaes_boundary_transformation_t *t)
static double default_lower[1]
void cmaes_boundary_transformation_init(cmaes_boundary_transformation_t *t, double const *lower_bounds, double const *upper_bounds, unsigned long len_of_bounds)
static unsigned long _index(cmaes_boundary_transformation_t *t, unsigned long i)
static int do_assertions
static double default_upper[1]
void cmaes_boundary_transformation_shift_into_feasible_preimage(cmaes_boundary_transformation_t *t, double const *x, double *y, unsigned long len)
static void _FatalError(const char *s)
void cmaes_boundary_transformation_inverse(cmaes_boundary_transformation_t *t, double const *x, double *y, unsigned long len)
void cmaes_boundary_transformation(cmaes_boundary_transformation_t *t, double const *x, double *y, unsigned long len)