51 pred->
n = 1 + 2 *
xcsf->x_dim +
xcsf->x_dim * (
xcsf->x_dim - 1) / 2;
53 pred->
n =
xcsf->x_dim + 1;
60 pred->
mu = malloc(
sizeof(
double) *
N_MU);
61 if (
xcsf->pred->evolve_eta) {
65 memset(pred->
mu, 0,
sizeof(
double) *
N_MU);
69 pred->
tmp_input = malloc(
sizeof(
double) * pred->
n);
86 memcpy(dest_pred->
mu, src_pred->
mu,
sizeof(
double) *
N_MU);
87 dest_pred->
eta = src_pred->
eta;
120 const int n = pred->
n;
121 const double X0 =
xcsf->pred->x0;
122 const double norm = X0 * X0 +
blas_dot(
xcsf->x_dim, x, 1, x, 1);
124 for (
int i = 0; i <
xcsf->y_dim; ++i) {
126 const double correction = (pred->
eta * error) / norm;
141 const int n = pred->
n;
143 for (
int i = 0; i <
xcsf->y_dim; ++i) {
158 printf(
"%s\n", json_str);
188 if (
xcsf->pred->evolve_eta) {
191 const double orig = pred->
eta;
194 if (orig != pred->
eta) {
228 s += fwrite(&pred->
n,
sizeof(
int), 1, fp);
229 s += fwrite(&pred->
n_weights,
sizeof(
int), 1, fp);
231 s += fwrite(pred->
mu,
sizeof(
double),
N_MU, fp);
232 s += fwrite(&pred->
eta,
sizeof(
double), 1, fp);
249 s += fread(&pred->
n,
sizeof(
int), 1, fp);
250 s += fread(&pred->
n_weights,
sizeof(
int), 1, fp);
252 s += fread(pred->
mu,
sizeof(
double),
N_MU, fp);
253 s += fread(&pred->
eta,
sizeof(
double), 1, fp);
267 cJSON *json = cJSON_CreateObject();
269 cJSON_AddStringToObject(json,
"type",
"nlms_quadratic");
271 cJSON_AddStringToObject(json,
"type",
"nlms_linear");
274 cJSON_AddItemToObject(json,
"weights",
weights);
275 cJSON_AddNumberToObject(json,
"eta", pred->
eta);
276 cJSON *mutation = cJSON_CreateDoubleArray(pred->
mu,
N_MU);
277 cJSON_AddItemToObject(json,
"mutation", mutation);
278 char *
string = cJSON_Print(json);
294 const cJSON *item = cJSON_GetObjectItem(json,
"weights");
295 if (item != NULL && cJSON_IsArray(item)) {
296 if (cJSON_GetArraySize(item) == pred->
n_weights) {
297 for (
int i = 0; i < pred->
n_weights; ++i) {
298 const cJSON *item_i = cJSON_GetArrayItem(item, i);
299 pred->
weights[i] = item_i->valuedouble;
302 printf(
"Import error: weight length mismatch\n");
306 item = cJSON_GetObjectItem(json,
"eta");
307 if (item != NULL && cJSON_IsNumber(item)) {
308 pred->
eta = item->valuedouble;
322 cJSON *json = cJSON_CreateObject();
323 cJSON_AddNumberToObject(json,
"x0", pred->
x0);
324 cJSON_AddNumberToObject(json,
"eta", pred->
eta);
325 cJSON_AddBoolToObject(json,
"evolve_eta", pred->
evolve_eta);
327 cJSON_AddNumberToObject(json,
"eta_min", pred->
eta_min);
329 char *
string = cJSON_Print(json);
343 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
344 if (strncmp(iter->string,
"x0\0", 3) == 0 && cJSON_IsNumber(iter)) {
346 }
else if (strncmp(iter->string,
"eta\0", 4) == 0 &&
347 cJSON_IsNumber(iter)) {
349 }
else if (strncmp(iter->string,
"evolve_eta\0", 11) == 0 &&
350 cJSON_IsBool(iter)) {
351 const bool evolve =
true ? iter->type == cJSON_True :
false;
353 }
else if (strncmp(iter->string,
"eta_min\0", 8) == 0 &&
354 cJSON_IsNumber(iter)) {
void blas_fill(const int N, const double ALPHA, double *X, const int INCX)
Fills the vector X with the value ALPHA.
double blas_dot(const int N, const double *X, const int INCX, const double *Y, const int INCY)
Computes the dot product of two vectors.
void blas_axpy(const int N, const double ALPHA, const double *X, const int INCX, double *Y, const int INCY)
Multiplies vector X by the scalar ALPHA and adds it to the vector Y.
Basic linear algebra functions.
static const int MU_TYPE[(1)]
Self-adaptation method for mutating NLMS predictions.
void pred_nlms_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates an NLMS prediction from a cJSON object.
char * pred_nlms_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the NLMS parameters.
void pred_nlms_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current NLMS prediction for a provided input.
char * pred_nlms_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of an NLMS prediction.
size_t pred_nlms_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes an NLMS prediction to a file.
bool pred_nlms_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since NLMS predictions do not perform crossover.
#define N_MU
Number of self-adaptive mutation rates.
void pred_nlms_print(const struct XCSF *xcsf, const struct Cl *c)
Prints an NLMS prediction.
void pred_nlms_init(const struct XCSF *xcsf, struct Cl *c)
Initialises an NLMS prediction.
void pred_nlms_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by an NLMS prediction.
bool pred_nlms_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates the gradient descent rate used to update an NLMS prediction.
double pred_nlms_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of an NLMS prediction.
void pred_nlms_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates an NLMS prediction for a given input and truth sample.
char * pred_nlms_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the NLMS parameters from a cJSON object.
void pred_nlms_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies an NLMS prediction from one classifier to another.
size_t pred_nlms_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads an NLMS prediction from a file.
Normalised least mean squares prediction functions.
void pred_param_set_x0(struct XCSF *xcsf, const double a)
void pred_param_set_eta_min(struct XCSF *xcsf, const double a)
void pred_param_set_evolve_eta(struct XCSF *xcsf, const bool a)
void pred_param_set_eta(struct XCSF *xcsf, const double a)
void pred_transform_input(const struct XCSF *xcsf, const double *x, const double X0, double *tmp_input)
Prepares the input state for least squares computation.
#define PRED_TYPE_NLMS_QUADRATIC
Prediction type quadratic nlms.
void sam_json_import(double *mu, const int N, const cJSON *json)
Initialises a mutation vector from a cJSON object.
void sam_init(double *mu, const int N, const int *type)
Initialises self-adaptive mutation rates.
void sam_adapt(double *mu, const int N, const int *type)
Self-adapts mutation rates.
Self-adaptive mutation functions.
#define SAM_LOG_NORMAL
Log normal self-adaptation.
Parameters for initialising and operating predictions.
double eta_min
Minimum gradient descent rate.
bool evolve_eta
Whether to evolve the gradient descent rate.
double x0
Prediction weight vector offset value.
double eta
Gradient descent rate.
Classifier data structure.
void * pred
Prediction structure.
double * prediction
Current classifier prediction.
Normalised least mean squares prediction data structure.
double * mu
Mutation rates.
int n_weights
Total number of weights.
double * tmp_input
Temporary storage for updating weights.
int n
Number of weights for each predicted variable.
double eta
Gradient descent rate.
double * weights
Weights used to compute prediction.
double rand_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Utility functions for random number handling, etc.
static double clamp(const double a, const double min, const double max)
Returns a float clamped within the specified range.