char __docstr__[] = "Gradient descent search algorithm demonstration.";

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

#include "liquid.h"
#include "liquid.argparse.h"

int main(int argc, char*argv[])
{
    // define variables and parse command-line arguments
    liquid_argparse_init(__docstr__);
    liquid_argparse_add(char*, filename, "gradsearch_example.m", 'o', "output filename", NULL);
    liquid_argparse_add(unsigned, num_parameters,    6,  'n', "number of parameters", NULL);
    liquid_argparse_add(unsigned, num_iterations, 2000,  'i', "number of iterations to run", NULL);
    liquid_argparse_add(char*,    function,"rosenbrock", 'f', "utility function: {rosenbrock, invgauss, multimodal, spiral}", NULL);
    liquid_argparse_parse(argc,argv);

    // validate input
    utility_function func = liquid_rosenbrock;
    if      (strcmp(function,"rosenbrock")==0) func = liquid_rosenbrock;
    else if (strcmp(function,"invgauss")==0)   func = liquid_invgauss;
    else if (strcmp(function,"multimodal")==0) func = liquid_multimodal;
    else if (strcmp(function,"spiral")==0)     func = liquid_spiral;
    else {
        return liquid_error(LIQUID_EICONFIG,"unknown/unsupported utility '%s'", function);
    }

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

    float optimum_utility;

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

    // create gradsearch object
    gradsearch gs = gradsearch_create(NULL,
                                      optimum_vect,
                                      num_parameters,
                                      func,
                                      LIQUID_OPTIM_MINIMIZE);

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

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

        gradsearch_step(gs);

        if (((i+1)%d)==0 || i==0) {
            printf("%5u: ", i+1);
            gradsearch_print(gs);

            if ((i+1)==10*d) d*=10;
        }
    }

    // print results
    printf("\n");
    printf("%5u: ", num_iterations);
    gradsearch_print(gs);
    printf("solution:\n");
    for (i=0; i<num_parameters; i++)
        printf("  %12.7f\n", optimum_vect[i]);

    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", filename);

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

    gradsearch_destroy(gs);

    return 0;
}
