<commit_before>//
// gradient_search_example.c
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "liquid.h"

#define OUTPUT_FILENAME "gradient_search_example.m"

// N-dimensional rosenbrock function
float rosenbrock(void * _x, float* _opt, unsigned int _len);

int main() {
    unsigned int num_parameters = 8;    // dimensionality of search (minimum 2)
    unsigned int num_iterations = 1000; // number of iterations to run

    float optimum_vect[num_parameters];
    unsigned int i;
    for (i=0; i<num_parameters; i++)
        optimum_vect[i] = randnf();

    float optimum_utility;

    // open output file
    FILE*fid = fopen(OUTPUT_FILENAME,"w");
    fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
    fprintf(fid,"clear all;\n");
    fprintf(fid,"close all;\n");

    // create gradient_search object
    gradient_search * gs;
    gs = gradient_search_create(
        NULL, optimum_vect, num_parameters, &rosenbrock, LIQUID_OPTIM_MINIMIZE);

    // execute search
    //optimum_utility = gradient_search_run(gs, num_iterations, -1e-6f);

    // execute search one iteration at a time
    fprintf(fid,"u = zeros(1,%u);\n", num_iterations);
    for (i=0; i<num_iterations; i++) {
        optimum_utility = rosenbrock(NULL,optimum_vect,num_parameters);
        fprintf(fid,"u(%3u) = %12.4e;\n", i+1, optimum_utility);

        gradient_search_step(gs);

        if (((i+1)%100)==0)
            gradient_search_print(gs);
    }

    // print results
    printf("\n");
    gradient_search_print(gs);

    fprintf(fid,"figure;\n");
    fprintf(fid,"semilogy(u);\n");
    fprintf(fid,"xlabel('iteration');\n");
    fprintf(fid,"ylabel('utility');\n");
    fprintf(fid,"title('gradient search results');\n");
    fprintf(fid,"grid on;\n");
    fclose(fid);
    printf("results written to %s.\n", OUTPUT_FILENAME);

    // test results, optimum at [1, 1, 1, ... 1];

    free_gradient_search(gs);

    return 0;
}

// N-dimensional rosenbrock function
float rosenbrock(void * _x, float* _opt, unsigned int _len)
{
    if (_len < 2)
        return 0.0f;

    float u=0;
    unsigned int i;
    for (i=0; i<(_len-1); i++)
        u += powf(1-_opt[i],2) + 100*powf( _opt[i+1] - powf(_opt[i],2), 2);

    return u;
}

<commit_msg>gradient|example: initializing input to zeros<commit_after>//
// gradient_search_example.c
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "liquid.h"

#define OUTPUT_FILENAME "gradient_search_example.m"

// N-dimensional rosenbrock function
float rosenbrock(void * _x, float* _opt, unsigned int _len);

int main() {
    unsigned int num_parameters = 8;    // dimensionality of search (minimum 2)
    unsigned int num_iterations = 4000; // number of iterations to run

    float optimum_vect[num_parameters];
    unsigned int i;
    for (i=0; i<num_parameters; i++)
        optimum_vect[i] = 0.0f;

    float optimum_utility;

    // open output file
    FILE*fid = fopen(OUTPUT_FILENAME,"w");
    fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
    fprintf(fid,"clear all;\n");
    fprintf(fid,"close all;\n");

    // create gradient_search object
    gradient_search * gs;
    gs = gradient_search_create(
        NULL, optimum_vect, num_parameters, &rosenbrock, LIQUID_OPTIM_MINIMIZE);

    // execute search
    //optimum_utility = gradient_search_run(gs, num_iterations, -1e-6f);

    // execute search one iteration at a time
    fprintf(fid,"u = zeros(1,%u);\n", num_iterations);
    for (i=0; i<num_iterations; i++) {
        optimum_utility = rosenbrock(NULL,optimum_vect,num_parameters);
        fprintf(fid,"u(%3u) = %12.4e;\n", i+1, optimum_utility);

        gradient_search_step(gs);

        if (((i+1)%100)==0)
            gradient_search_print(gs);
    }

    // print results
    printf("\n");
    gradient_search_print(gs);

    fprintf(fid,"figure;\n");
    fprintf(fid,"semilogy(u);\n");
    fprintf(fid,"xlabel('iteration');\n");
    fprintf(fid,"ylabel('utility');\n");
    fprintf(fid,"title('gradient search results');\n");
    fprintf(fid,"grid on;\n");
    fclose(fid);
    printf("results written to %s.\n", OUTPUT_FILENAME);

    // test results, optimum at [1, 1, 1, ... 1];

    free_gradient_search(gs);

    return 0;
}

// N-dimensional rosenbrock function
float rosenbrock(void * _x, float* _opt, unsigned int _len)
{
    if (_len < 2)
        return 0.0f;

    float u=0;
    unsigned int i;
    for (i=0; i<(_len-1); i++)
        u += powf(1-_opt[i],2) + 100*powf( _opt[i+1] - powf(_opt[i],2), 2);

    return u;
}


{'fraction_non_alphanumeric': 0.1570048309178744, 'fraction_numerical': 0.018006148440931048, 'mean_word_length': 3.272983114446529, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803980', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1643, 'n_words': 356}