int
main(int argc, char *argv[])
{
    int error = 0;
    Context ctx;

    if (argc < 4) {
        fprintf(stderr, "Correct usage %s <config_file> <data_file> <engine>\n",
            argv[0]);
        return -1;
    }

    if (!_read_config(&ctx, argv[1]))
        return -1;

    ctx.f = fopen(argv[2], "r");
    if (!ctx.f) {
        fprintf(stderr, "Failed to open %s\n", argv[1]);
        return -1;
    }

    ctx.sml_engine = atoi(argv[3]);
    ctx.sml = _sml_new(ctx.sml_engine);
    ctx.reads = ctx.rights = ctx.predictions = 0;
    if (!ctx.sml || !_initialize_sml(&ctx)) {
        fprintf(stderr, "Failed to initialize sml\n");
        error = -1;
        goto end;
    }

    while (_read_data(&ctx)) {
        _do_prediction(&ctx);
        ctx.first_train = true;
        if ((error = sml_process(ctx.sml))) {
            fprintf(stderr, "sml_process error number %d\n", error);
            break;
        }

        ctx.first_train = false;
        if ((error = sml_process(ctx.sml))) {
            fprintf(stderr, "sml_process error number %d\n", error);
            break;
        }
    }

    sml_print_debug(ctx.sml, false);
    printf("Right guesses: %d of %d (%d games) \n", ctx.rights,
        ctx.predictions, ctx.reads);
    printf("Right predictions percentage:%f%%\n",
        ctx.rights * 100.0 / ctx.reads);
    printf("Total right predictions percentage:%f%%\n",
        ctx.rights * 100.0 / ctx.predictions);
    sml_free(ctx.sml);
    _free_players(ctx.players, ctx.num_players);

end:
    fclose(ctx.f);
    return error;
}
int
main(int argc, char *argv[])
{
    Context ctx;
    int i, tic, toc, total_toc, total_tic;
    double duration;
    int error;

    if (argc < 6) {
        fprintf(stderr, "%s TEST.conf TEST.data SEED_VAL DEBUG_VAL "
            "ENGINE_TYPE(0 fuzzy, 1 ann, 2 naive, "
            "3 fuzzy_no_simplification) [MAX_MEMORY_FOR_OBSERVATION] "
            "[ANN_CACHE_SIZE] [ANN_PSEUDO_REHEARSAL_STRATETY (1 for true, 0 for false)]\n",
            argv[0]);
        fprintf(stderr,
            "Eg: %s simple_office.conf simple_office.forget_lights_on.data "
            "30 1 1\n",
            argv[0]);
        return 1;
    }

    ctx.rand = g_rand_new_with_seed(atoi(argv[3]));
    ctx.debug = !!atoi(argv[4]);

    ctx.engine_type = atoi(argv[5]);
    ctx.sml = _sml_new(ctx.engine_type);
    if (!ctx.sml) {
        fprintf(stderr, "Failed to create sml\n");
        return 2;
    }

    if (!read_config(argv[1], &ctx)) {
        fprintf(stderr, "Failed to read configuration: %s\n", argv[1]);
        return 3;
    }

    if (!read_values(argv[2], &ctx)) {
        fprintf(stderr, "Failed to read data: %s\n", argv[2]);
        return 4;
    }

    add_time_day(&ctx);

    sml_set_read_state_callback(ctx.sml, read_state_cb, &ctx);
    sml_set_output_state_changed_callback(ctx.sml, output_state_changed_cb,
        &ctx);
    sml_set_stabilization_hits(ctx.sml, 0);
    if (ctx.engine_type == FUZZY_ENGINE_NO_SIMPLIFICATION)
        sml_fuzzy_set_simplification_disabled(ctx.sml, true);

    if (argc >= 7) {
        int observations = atoi(argv[6]);
        if (observations < 0) {
            fprintf(stderr, "MAX_MEMORY_FOR_OBSERVATIOS (%s) must be a non "
                "negative value\n", argv[6]);
            return 5;
        }
        sml_set_max_memory_for_observations(ctx.sml, observations);
    }

    if (ctx.engine_type == ANN_ENGINE) {
        if (argc >= 8) {
            int cache_size = atoi(argv[7]);
            if (cache_size < 0 || cache_size >= UINT16_MAX) {
                fprintf(stderr, "ANN_CACHE_SIZE (%s) must be greater or equal "
                    "to 0 an less or equal to %d\n", argv[7], UINT16_MAX);
                return 6;
            }
            sml_ann_set_cache_max_size(ctx.sml, cache_size);
        }
        if (argc >= 9)
            sml_ann_use_pseudorehearsal_strategy(ctx.sml, atoi(argv[8]) != 0);
    }

    if (ctx.debug)
        print_scenario(&ctx);

    ctx.max_iteration_duration = -1;
    total_tic = clock();
    for (i = 0; i < ctx.reads; i++) {
        tic = clock();
        if ((error = sml_process(ctx.sml))) {
            fprintf(stderr, "=== Unexpected error in simulation. "
                "Error code: %d ===\n", error);
            break;
        }
        toc = clock();
        duration = ((double)toc - tic) / CLOCKS_PER_SEC;
        _process_results(&ctx, duration);
    }
    total_toc = clock();
    ctx.duration = ((double)total_toc - total_tic) / CLOCKS_PER_SEC;

    // scenario may changes thanks to new events created automatically
    if (ctx.debug) {
        print_scenario(&ctx);
        sml_print_debug(ctx.sml, true);
    }
    print_results(&ctx);

    sml_free(ctx.sml);

    g_list_free_full(ctx.inputs, free_variable);
    g_list_free_full(ctx.outputs, free_variable);
    g_list_free_full(ctx.expectations, free_expectation);
    g_list_free_full(ctx.expectation_blocks, free_element);
    g_rand_free(ctx.rand);

    return 0;
}
int
main(int argc, char *argv[])
{
    unsigned int inputs, outputs, executions, num_terms;
    char strbuf[STRLEN];
    struct sml_variable *var;
    struct sml_object *sml;

    if (argc < 5) {
        fprintf(stderr, "Usage: %s <engine type (0 fuzzy, 1 ann)> <inputs> "
            "<outputs> <executions> <num_terms> <seed>\n", argv[0]);
        fprintf(stderr, "Fuzzy Test: %s 0 10 2 100 10\n", argv[0]);
        fprintf(stderr, "ANN Test: %s 1 10 2 100 10\n", argv[0]);
        return 1;
    }

    inputs = atoi(argv[2]);
    outputs = atoi(argv[3]);
    executions = atoi(argv[4]);
    num_terms = atoi(argv[5]);

    if (argc > 6)
        srand((unsigned int)atol(argv[6]));
    else
        srand(time(NULL));

    if (num_terms < 1) {
        fprintf(stderr, "num_terms must be a positive value\n");
        return 1;
    }

    sml = _sml_new(atoi(argv[1]));
    if (!sml) {
        fprintf(stderr, "Failed to create sml\n");
        return 1;
    }


    sml_set_read_state_callback(sml, _read_state_cb, &executions);
    sml_set_stabilization_hits(sml, 0);

    while (inputs) {
        snprintf(strbuf, STRLEN, "input%d", inputs);
        var = sml_new_input(sml, strbuf);
        sml_variable_set_range(sml, var, 0, RAND_MAX);
        _add_terms(sml, var, strbuf, inputs, num_terms);

        inputs--;
    }

    while (outputs) {
        snprintf(strbuf, STRLEN, "output%d", outputs);
        var = sml_new_output(sml, strbuf);
        sml_variable_set_range(sml, var, 0, RAND_MAX);
        _add_terms(sml, var, strbuf, outputs, num_terms);
        outputs--;
    }

    while (sml_process(sml) == 0) ;
    sml_free(sml);

    return 0;
}