41 pred->
n = 1 + 2 *
xcsf->x_dim +
xcsf->x_dim * (
xcsf->x_dim - 1) / 2;
43 pred->
n =
xcsf->x_dim + 1;
50 const int n_sqrd = pred->
n * pred->
n;
51 pred->
matrix = calloc(n_sqrd,
sizeof(
double));
52 for (
int i = 0; i < pred->
n; ++i) {
53 pred->
matrix[i * pred->
n + i] =
xcsf->pred->scale_factor;
56 pred->
tmp_input = malloc(
sizeof(
double) * pred->
n);
57 pred->
tmp_vec = calloc(pred->
n,
sizeof(
double));
111 const int n = pred->
n;
113 const double *A = pred->
matrix;
116 blas_gemm(0, 0,
n, 1,
n, 1, A,
n, B, 1, 0, C, 1);
119 divisor = 1 / (divisor +
xcsf->pred->lambda);
122 for (
int i = 0; i <
xcsf->y_dim; ++i) {
127 for (
int i = 0; i <
n; ++i) {
128 for (
int j = 0; j <
n; ++j) {
141 blas_gemm(0, 0,
n,
n,
n, 1, A,
n, B,
n, 0, C,
n);
143 const double lambda =
xcsf->pred->lambda;
144 for (
int i = 0; i <
n; ++i) {
145 for (
int j = 0; j <
n; ++j) {
161 const int n = pred->
n;
163 for (
int i = 0; i <
xcsf->y_dim; ++i) {
178 printf(
"%s\n", json_str);
240 s += fwrite(&pred->
n,
sizeof(
int), 1, fp);
241 s += fwrite(&pred->
n_weights,
sizeof(
int), 1, fp);
243 const int n_sqrd = pred->
n * pred->
n;
244 s += fwrite(pred->
matrix,
sizeof(
double), n_sqrd, fp);
261 s += fread(&pred->
n,
sizeof(
int), 1, fp);
262 s += fread(&pred->
n_weights,
sizeof(
int), 1, fp);
264 const int n_sqrd = pred->
n * pred->
n;
265 s += fread(pred->
matrix,
sizeof(
double), n_sqrd, fp);
279 cJSON *json = cJSON_CreateObject();
281 cJSON_AddStringToObject(json,
"type",
"rls_quadratic");
283 cJSON_AddStringToObject(json,
"type",
"rls_linear");
286 cJSON_AddItemToObject(json,
"weights",
weights);
287 char *
string = cJSON_Print(json);
303 const cJSON *item = cJSON_GetObjectItem(json,
"weights");
304 if (item != NULL && cJSON_IsArray(item)) {
305 if (cJSON_GetArraySize(item) == pred->
n_weights) {
306 for (
int i = 0; i < pred->
n_weights; ++i) {
307 const cJSON *item_i = cJSON_GetArrayItem(item, i);
308 pred->
weights[i] = item_i->valuedouble;
311 printf(
"Import error: weight length mismatch\n");
326 cJSON *json = cJSON_CreateObject();
327 cJSON_AddNumberToObject(json,
"x0", pred->
x0);
328 cJSON_AddNumberToObject(json,
"lambda", pred->
lambda);
329 cJSON_AddNumberToObject(json,
"scale_factor", pred->
scale_factor);
330 char *
string = cJSON_Print(json);
344 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
345 if (strncmp(iter->string,
"x0\0", 3) == 0 && cJSON_IsNumber(iter)) {
347 }
else if (strncmp(iter->string,
"lambda\0", 7) == 0 &&
348 cJSON_IsNumber(iter)) {
350 }
else if (strncmp(iter->string,
"scale_factor\0", 13) == 0 &&
351 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.
void blas_scal(const int N, const double ALPHA, double *X, const int INCX)
Scales vector X by the scalar ALPHA and overwrites it with the result.
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.
void blas_gemm(const int TA, const int TB, const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, const double BETA, double *C, const int ldc)
Performs the matrix-matrix multiplication: .
Basic linear algebra functions.
void pred_rls_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by an RLS prediction.
void pred_rls_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies an RLS prediction from one classifier to another.
void pred_rls_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates an RLS prediction for a given input and truth sample.
double pred_rls_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of an RLS prediction.
void pred_rls_init(const struct XCSF *xcsf, struct Cl *c)
Initialises an RLS prediction.
char * pred_rls_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of an RLS prediction.
size_t pred_rls_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads an RLS prediction from a file.
void pred_rls_print(const struct XCSF *xcsf, const struct Cl *c)
Prints an RLS prediction.
char * pred_rls_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the RLS parameters.
void pred_rls_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates an RLS prediction from a cJSON object.
bool pred_rls_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since RLS predictions do not perform crossover.
char * pred_rls_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the RLS parameters from a cJSON object.
void pred_rls_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current RLS prediction for a provided input.
bool pred_rls_mutate(const struct XCSF *xcsf, const struct Cl *c)
Dummy function since RLS predictions do not perform mutation.
size_t pred_rls_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes an RLS prediction to a file.
Recursive least mean squares prediction functions.
void pred_param_set_x0(struct XCSF *xcsf, const double a)
void pred_param_set_lambda(struct XCSF *xcsf, const double a)
void pred_param_set_scale_factor(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_RLS_QUADRATIC
Prediction type quadratic rls.
Parameters for initialising and operating predictions.
double scale_factor
Initial values for the RLS gain-matrix.
double x0
Prediction weight vector offset value.
double lambda
RLS forget rate.
Classifier data structure.
void * pred
Prediction structure.
double * prediction
Current classifier prediction.
Recursive least mean squares prediction data structure.
double * tmp_vec
Temporary storage for updating weights.
double * tmp_input
Temporary storage for updating weights.
double * matrix
Gain matrix used to update weights.
int n
Number of weights for each predicted variable.
double * weights
Weights used to compute prediction.
int n_weights
Total number of weights.
double * tmp_matrix2
Temporary storage for updating gain matrix.
double * tmp_matrix1
Temporary storage for updating gain matrix.
Utility functions for random number handling, etc.