Example #1
0
int test_stack_push(void)
{
    struct stack *s = stack_new();
    float *f1 = malloc_float(2.0);
    float *f2 = malloc_float(4.0);
    float *f3 = malloc_float(8.0);

    /* push float 1 */
    stack_push(s, f1);
    mu_check(fltcmp(s->end->value, f1) == 0);
    mu_check(s->size == 1);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev == NULL);

    /* push float 2 */
    stack_push(s, f2);
    mu_check(fltcmp(s->end->value, f2) == 0);
    mu_check(s->size == 2);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev->value == f1);
    mu_check(fltcmp(s->end->prev->value, f1) == 0);

    /* push float 3 */
    stack_push(s, f3);
    mu_check(fltcmp(s->end->value, f3) == 0);
    mu_check(s->size == 3);
    mu_check(s->root->value == f1);
    mu_check(s->end->prev->value == f2);
    mu_check(fltcmp(s->end->prev->value, f2) == 0);

    stack_destroy(s, free);
    return 0;
}
Example #2
0
void by_value(book *library, book temp, int i, int j)
{
	if (fltcmp(library[i].value, library[j].value) > 0) {
		temp = library[i];
		library[i] = library[j];
		library[j] = temp;
	}
}
Example #3
0
int test_crossover_config_new_and_destroy(void)
{
    float uninit = -1.0f;
    struct crossover_config *cc = crossover_config_new(0);

    mu_check(cc->method == 0);
    mu_check(fltcmp(&cc->probability, &uninit) == 0);

    crossover_config_destroy(cc);
    return 0;
}
Example #4
0
int regression_evaluate(
    struct tree *t,
    float **func_input,
    struct data *d,
    char *resp
)
{
    int i;
    int res;
    int hits = 0;
    int col = data_field_index(d, resp);
    float value;
    float zero = 0.0;
    float err = 0.0;
    float sse = 0.0;
    struct node *result;
    struct stack *s = stack_new();

    /* evaluate chromosome */
    res = regression_traverse(0, t->size - 1, t->chromosome, s, func_input, d);
    silent_check(res != -1);

    /* check results */
    result = stack_pop(s);
    for (i = 0; i < d->rows; i++) {
        value = ((float *) result->values)[i];
        err = (float) fabs(value - *d->data[col][i]);
        err = (float) pow(err, 2);
        sse += err;

        if (fltcmp(&err, &zero) == 0) {
            hits++;
        }
    }

    /* record evaluation */
    if (t->score == NULL) {
        t->score = malloc_float(sse + t->size);
    } else {
        *t->score = (sse + t->size);
    }
    t->hits = hits;

    /* clean up */
    node_destroy(result);
    regression_clear_stack(s);
    return 0;
error:
    free(t->score);
    t->score = NULL;
    regression_clear_stack(s);
    return -1;
}
Example #5
0
int test_crossover_config_new_and_destroy(void)
{
    float uninit = -1.0f;
    struct crossover_config *cc;

    /* setup */
    cc = crossover_config_new();

    /* assert */
    mu_check(cc->methods == NULL);
    mu_check(fltcmp(cc->probability, uninit) == 0);

    /* clean up */
    crossover_config_destroy(cc);

    return 0;
}
Example #6
0
int test_regression_evaluate(void)
{
    int i;
    int res;
    float f = 100.0;
    float **func_input;
    float solution_score = 5.0;
    struct tree *t;
    struct node **chromosome = malloc(sizeof(struct node *) * 5);
    struct data *d = csv_load_data(TEST_DATA, 1, ",");

    /* initialize func_input */
    func_input = malloc(sizeof(float *) * 2);
    for (i = 0; i < 2; i++) {
        func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows);
    }

    /* build chromosome */
    chromosome[0] = node_new_constant(FLOAT, &f);
    chromosome[1] = node_new_input((char *) "x");
    chromosome[2] = node_new_func(MUL, 2);
    chromosome[3] = node_new_func(RAD, 1);
    chromosome[4] = node_new_func(SIN, 1);

    /* build tree */
    t = malloc(sizeof(struct tree));
    t->root = NULL;
    t->size = 5;
    t->depth = -1;
    t->score = NULL;
    t->chromosome = chromosome;

    /* evaluate tree */
    res = regression_evaluate(t, func_input, d, (char *) "y");
    mu_check(res == 0);
    mu_check(fltcmp(t->score,  &solution_score) == 0);
    mu_check(t->hits == 361);

    /* clean up */
    free_chromosome(chromosome, 5);
    tree_destroy(t);
    data_destroy(d);
    free_mem_arr(func_input, 2, free);
    return 0;
}
Example #7
0
int test_evolve_new_and_destroy(void)
{
    struct config *c = config_new();
    float solution = -1.0f;

    mu_check(c->max_generations == -1);
    mu_check(c->population_size == -1);
    mu_check(c->population_generator == NULL);

    mu_check(c->stale_limit == -1);
    mu_check(fltcmp(&c->target_score, &solution) == 0);

    mu_check(c->data_struct == NULL);

    mu_check(c->selection == NULL);
    mu_check(c->crossover == NULL);
    mu_check(c->mutation == NULL);

    config_destroy(c);
    return 0;
}
Example #8
0
int regression_traverse(
    int index,
    int end,
    struct node **chromosome,
    struct stack *stack,
    float **func_input,
    struct data *d
)
{
    int i;
    float zero = 0.0;
    float *result = NULL;
    struct node *n = chromosome[index];
    struct node *in1 = NULL;
    struct node *in2 = NULL;
    int in1_type = -1;
    int in2_type = -1;

    errno = 0;
    if (n->type == TERM_NODE) {
        stack_push(stack, n);

    } else if (n->type == FUNC_NODE) {
        /* prep function input data */
        if (n->arity == 2) {
            in2 = stack_pop(stack);
            in1 = stack_pop(stack);
            in1_type = in1->terminal_type;
            in2_type = in2->terminal_type;

            silent_check(regression_func_input(in1, d, func_input, 0) == 0);
            silent_check(regression_func_input(in2, d, func_input, 1) == 0);

        } else {
            in1 = stack_pop(stack);
            in1_type = in1->terminal_type;

            silent_check(regression_func_input(in1, d, func_input, 0) == 0);

        }

        /* evaluate function */
        result = malloc(sizeof(float) * (unsigned long) d->rows);
        switch (n->function) {
        case ADD:
            for (i = 0; i < d->rows; i++) {
                result[i] = func_input[0][i] + func_input[1][i];
                silent_check(regression_check() == 0);
            }
            break;

        case SUB:
            for (i = 0; i < d->rows; i++) {
                result[i] = func_input[0][i] - func_input[1][i];
                silent_check(regression_check() == 0);
            }
            break;

        case MUL:
            for (i = 0; i < d->rows; i++) {
                result[i] = func_input[0][i] * func_input[1][i];
                silent_check(regression_check() == 0);
            }
            break;

        case DIV:
            for (i = 0; i < d->rows; i++) {
                /* check for zero */
                silent_check(fltcmp(&func_input[1][i], &zero) != 0);
                result[i] = func_input[0][i] / func_input[1][i];
                silent_check(regression_check() == 0);
            }
            break;

        case POW:
            for (i = 0; i < d->rows; i++) {
                func_input[0][i] = fabsf(func_input[0][i]);
                result[i] = powf(func_input[0][i], func_input[1][i]);
                silent_check(regression_check() == 0);
            }
            break;

        case LOG:
            for (i = 0; i < d->rows; i++) {
                /* check for zero and negative */
                silent_check(fltcmp(&func_input[0][i], &zero) == 1);
                result[i] = logf(func_input[0][i]);
                silent_check(regression_check() == 0);
            }
            break;

        case EXP:
            for (i = 0; i < d->rows; i++) {
                result[i] = expf(func_input[0][i]);
                silent_check(regression_check() == 0);
            }
            break;

        case RAD:
            for (i = 0; i < d->rows; i++) {
                result[i] = func_input[0][i] * (float) (PI / 180.0);
                silent_check(regression_check() == 0);
            }
            break;

        case SIN:
            for (i = 0; i < d->rows; i++) {
                result[i] = sinf(func_input[0][i]);
                silent_check(regression_check() == 0);
            }
            break;

        case COS:
            for (i = 0; i < d->rows; i++) {
                result[i] = cosf(func_input[0][i]);
                silent_check(regression_check() == 0);
            }
            break;

        default:
            /* UNRECOGNIZED FUNCTION */
            goto func_error;
        }

        stack_push(stack, node_new_eval(FLOAT, result, d->rows));
        regression_free_inputs(in1_type, in1, in2_type, in2);
    }

    /* terminate check */
    if (index == end) {
        return 0;
    } else {
        return regression_traverse(
            index + 1,
            end,
            chromosome,
            stack,
            func_input,
            d
        );
    }

error:
    regression_free_inputs(in1_type, in1, in2_type, in2);
    free(result);
    return -1;

func_error:
    regression_free_inputs(in1_type, in1, in2_type, in2);
    free(result);
    return -2;
}
Example #9
0
int test_regression_traverse(void)
{
    int i;
    int res;
    float f1 = 1.0;
    float f2 = 2.0;
    float f3;
    float *flt_ptr;
    float zero = 0.0;
    float **func_input;
    struct node *result_node;
    struct node **chromosome = malloc(sizeof(struct node *) * 3);
    struct stack *stack = stack_new();
    struct data *d = csv_load_data(TEST_DATA, 1, ",");

    /* initialize func_input */
    func_input = malloc(sizeof(float *) * 2);
    for (i = 0; i < 2; i++) {
        func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows);
    }

    /* ADD */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &f2);
    chromosome[2] = node_new_func(ADD, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = f1 + f2;
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 3);


    /* SUB */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &f2);
    chromosome[2] = node_new_func(SUB, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = f1 - f2;
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 3);


    /* MUL */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &f2);
    chromosome[2] = node_new_func(MUL, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = f1 * f2;
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 3);


    /* DIV */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &f2);
    chromosome[2] = node_new_func(DIV, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = f1 / f2;
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 3);


    /* DIV - FAIL TEST - DIVIDE BY ZERO */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &zero);
    chromosome[2] = node_new_func(DIV, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);
    mu_check(res == -1);
    free_chromosome(chromosome, 3);


    /* POW */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_constant(FLOAT, &f2);
    chromosome[2] = node_new_func(POW, 2);
    res = regression_traverse(0, 2, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) pow(f1, f2);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 3);


    /* LOG */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(LOG, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) log(f1);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 2);


    /* EXP */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(EXP, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) exp(f1);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 2);

    /* RAD */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(RAD, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) f1 * (float) (PI / 180.0);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 2);


    /* SIN */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(SIN, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) sin(f1);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 2);


    /* COS */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(COS, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);

    result_node = stack_pop(stack);
    for (i = 0; i < d->rows; i++) {
        f3 = (float) cos(f1);
        flt_ptr = &((float *) result_node->values)[i];
        mu_check(fltcmp(flt_ptr, &f3) == 0);
    }
    node_destroy(result_node);
    free_chromosome(chromosome, 2);


    /* FAIL TEST - UNKNOWN FUNCTION */
    chromosome[0] = node_new_constant(FLOAT, &f1);
    chromosome[1] = node_new_func(99, 1);
    res = regression_traverse(0, 1, chromosome, stack, func_input, d);
    mu_check(res == -2);
    free_chromosome(chromosome, 2);


    /* clean up */
    stack_destroy(stack, free);
    free(chromosome);
    data_destroy(d);
    free_mem_arr(func_input, 2, free);
    return 0;
}
Example #10
0
int test_regression_func_input(void)
{
    int i;
    int res;
    float f = 100.0;
    float *f_arr;
    float **func_input;
    struct data *d = csv_load_data(TEST_DATA, 1, ",");
    struct node **stack = malloc(sizeof(struct node *) * 4);

    /* malloc func_input */
    func_input = malloc(sizeof(float *) * 2);
    for (i = 0; i < 2; i++) {
        func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows);
    }

    /* initialize float array */
    f_arr = malloc(sizeof(float) * (unsigned long) d->rows);
    for (i = 0; i < d->rows; i++) {
        f_arr[i] = (float) i;
    }

    /* create stack */
    stack[0] = node_new_input((char *) "x");
    stack[1] = node_new_input((char *) "y");
    stack[2] = node_new_constant(FLOAT, &f);
    stack[3] = node_new_eval(FLOAT, (void *) f_arr, d->rows);

    /* input node - x */
    regression_func_input(stack[0], d, func_input, 0);
    for (i = 0; i < d->rows; i++) {
        res = fltcmp(&func_input[0][i], d->data[data_field_index(d, "x")][i]);
        mu_check(res == 0);
    }

    /* input node - y */
    regression_func_input(stack[1], d, func_input, 1);
    for (i = 0; i < d->rows; i++) {
        res = fltcmp(&func_input[1][i], d->data[data_field_index(d, "y")][i]);
        mu_check(res == 0);
    }

    /* constant node */
    regression_func_input(stack[2], d, func_input, 0);
    for (i = 0; i < d->rows; i++) {
        res = fltcmp(&func_input[0][i], &f);
        mu_check(res == 0);
    }

    /* eval node */
    regression_func_input(stack[3], d, func_input, 0);
    for (i = 0; i < d->rows; i++) {
        f = (float) i;
        res = fltcmp(&func_input[0][i], &f);
        mu_check(res == 0);
    }

    /* clean up */
    node_destroy(stack[0]);
    node_destroy(stack[1]);
    node_destroy(stack[2]);
    node_destroy(stack[3]);
    data_destroy(d);
    free_mem_arr(func_input, 2, free);
    free(stack);

    return 0;
}