1 //
2 // qnsearch_example.c
3 //
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 
9 #include "liquid.h"
10 
11 #define OUTPUT_FILENAME "qnsearch_example.m"
12 
main()13 int main() {
14     unsigned int num_parameters = 8;    // dimensionality of search (minimum 2)
15     unsigned int num_iterations = 4000; // number of iterations to run
16 
17     float optimum_vect[num_parameters];
18     unsigned int i;
19     for (i=0; i<num_parameters; i++)
20         optimum_vect[i] = 0.0f;
21 
22     float optimum_utility;
23 
24     // open output file
25     FILE*fid = fopen(OUTPUT_FILENAME,"w");
26     fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
27     fprintf(fid,"clear all;\n");
28     fprintf(fid,"close all;\n");
29 
30     // create qnsearch object
31     qnsearch gs = qnsearch_create(
32         NULL, optimum_vect, num_parameters, &liquid_rosenbrock, LIQUID_OPTIM_MINIMIZE);
33 
34     // execute search
35     //optimum_utility = qnsearch_run(gs, num_iterations, -1e-6f);
36 
37     // execute search one iteration at a time
38     fprintf(fid,"u = zeros(1,%u);\n", num_iterations);
39     for (i=0; i<num_iterations; i++) {
40         optimum_utility = liquid_rosenbrock(NULL,optimum_vect,num_parameters);
41         fprintf(fid,"u(%3u) = %12.4e;\n", i+1, optimum_utility);
42 
43         qnsearch_step(gs);
44 
45         if (((i+1)%100)==0)
46             qnsearch_print(gs);
47     }
48 
49     // print results
50     printf("\n");
51     qnsearch_print(gs);
52 
53     fprintf(fid,"figure;\n");
54     fprintf(fid,"semilogy(u);\n");
55     fprintf(fid,"xlabel('iteration');\n");
56     fprintf(fid,"ylabel('utility');\n");
57     fprintf(fid,"title('quasinewton search results');\n");
58     fprintf(fid,"grid on;\n");
59     fclose(fid);
60     printf("results written to %s.\n", OUTPUT_FILENAME);
61 
62     // test results, optimum at [1, 1, 1, ... 1];
63 
64     qnsearch_destroy(gs);
65 
66     return 0;
67 }
68