1 //
2 // gradsearch_example.c
3 //
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
9 #include <getopt.h>
10
11 #include "liquid.h"
12
13 #define OUTPUT_FILENAME "gradsearch_example.m"
14
15 // print usage/help message
usage()16 void usage()
17 {
18 printf("%s [options]\n", __FILE__);
19 printf(" h : print help\n");
20 printf(" n : number of parameters, default: 6\n");
21 printf(" t : number of iterations, default: 2000\n");
22 printf(" u : utility function: {rosenbrock, invgauss, multimodal, spiral}\n");
23 }
24
main(int argc,char * argv[])25 int main(int argc, char*argv[])
26 {
27 unsigned int num_parameters = 6; // dimensionality of search (minimum 2)
28 unsigned int num_iterations = 2000; // number of iterations to run
29 utility_function func = liquid_rosenbrock;
30
31 int dopt;
32 while ((dopt = getopt(argc,argv,"hn:t:u:")) != EOF) {
33 switch (dopt) {
34 case 'h': usage(); return 0;
35 case 'n': num_parameters = atoi(optarg); break;
36 case 't': num_iterations = atoi(optarg); break;
37 case 'u':
38 if (strcmp(optarg,"rosenbrock")==0) func = liquid_rosenbrock;
39 else if (strcmp(optarg,"invgauss")==0) func = liquid_invgauss;
40 else if (strcmp(optarg,"multimodal")==0) func = liquid_multimodal;
41 else if (strcmp(optarg,"spiral")==0) func = liquid_spiral;
42 else {
43 fprintf(stderr,"error: %s, unknown/unsupported utility '%s'\n", argv[0], optarg);
44 exit(1);
45 }
46 break;
47 default:
48 exit(1);
49 }
50 }
51
52 float optimum_vect[num_parameters];
53 unsigned int i;
54 for (i=0; i<num_parameters; i++)
55 optimum_vect[i] = 0.1f*(float)i;
56
57 float optimum_utility;
58
59 // open output file
60 FILE*fid = fopen(OUTPUT_FILENAME,"w");
61 fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
62 fprintf(fid,"clear all;\n");
63 fprintf(fid,"close all;\n");
64
65 // create gradsearch object
66 gradsearch gs = gradsearch_create(NULL,
67 optimum_vect,
68 num_parameters,
69 func,
70 LIQUID_OPTIM_MINIMIZE);
71
72 // execute search
73 //optimum_utility = gradsearch_run(gs, num_iterations, -1e-6f);
74
75 // execute search one iteration at a time
76 fprintf(fid,"u = zeros(1,%u);\n", num_iterations);
77 unsigned int d=1;
78 for (i=0; i<num_iterations; i++) {
79 optimum_utility = func(NULL,optimum_vect,num_parameters);
80 fprintf(fid,"u(%3u) = %12.4e;\n", i+1, optimum_utility);
81
82 gradsearch_step(gs);
83
84 if (((i+1)%d)==0 || i==0) {
85 printf("%5u: ", i+1);
86 gradsearch_print(gs);
87
88 if ((i+1)==10*d) d*=10;
89 }
90 }
91
92 // print results
93 printf("\n");
94 printf("%5u: ", num_iterations);
95 gradsearch_print(gs);
96
97 fprintf(fid,"figure;\n");
98 fprintf(fid,"semilogy(u);\n");
99 fprintf(fid,"xlabel('iteration');\n");
100 fprintf(fid,"ylabel('utility');\n");
101 fprintf(fid,"title('gradient search results');\n");
102 fprintf(fid,"grid on;\n");
103 fclose(fid);
104 printf("results written to %s.\n", OUTPUT_FILENAME);
105
106 // test results, optimum at [1, 1, 1, ... 1];
107
108 gradsearch_destroy(gs);
109
110 return 0;
111 }
112