File: lbfgsb-example1.c

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (241 lines) | stat: -rw-r--r-- 8,446 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* lbfgsb-example1.f -- translated by f2c (version 20050501).
   You must link the resulting object file with libf2c:
        on Microsoft Windows system, link with libf2c.lib;
        on Linux or Unix systems, link with .../path/to/libf2c.a -lm
        or, if you install libf2c.a in a standard place, with -lf2c -lm
        -- in that order, at the end of the command line, as in
                cc *.o -lf2c -lm
        Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,

                http://www.netlib.org/f2c/libf2c.zip
*/

#include "v3p_netlib.h"

/*                             DRIVER 1 */
/*     -------------------------------------------------------------- */
/*                SIMPLE DRIVER FOR L-BFGS-B (version 2.1) */
/*     -------------------------------------------------------------- */

/*        L-BFGS-B is a code for solving large nonlinear optimization */
/*             problems with simple bounds on the variables. */

/*        The code can also be used for unconstrained problems and is */
/*        as efficient for these problems as the earlier limited memory */
/*                          code L-BFGS. */

/*        This is the simplest driver in the package. It uses all the */
/*                    default settings of the code. */


/*     References: */

/*        [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*        memory algorithm for bound constrained optimization'', */
/*        SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */

/*        [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN */
/*        Subroutines for Large Scale Bound Constrained Optimization'' */
/*        Tech. Report, NAM-11, EECS Department, Northwestern University, */
/*        1994. */


/*          (Postscript files of these papers are available via anonymous */
/*           ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.) */

/*                              *  *  * */

/*        NEOS, November 1994. (Latest revision June 1996.) */
/*        Optimization Technology Center. */
/*        Argonne National Laboratory and Northwestern University. */
/*        Written by */
/*                           Ciyou Zhu */
/*        in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */

/*     NOTE: The user should adapt the subroutine 'timer' if 'etime' is */
/*           not available on the system.  An example for system */
/*           AIX Version 3.2 is available at the end of this driver. */

/*     ************** */
/*<       program driver >*/
/* Main program */ int main()
{
    /* System generated locals */
    integer i__1;
    doublereal d__1, d__2;

    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);
    integer s_cmp(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    doublereal f, g[1024];
    integer i__;
    doublereal l[1024];
    integer m, n;
    doublereal u[1024], x[1024], t1, t2, wa[42584];
    integer nbd[1024], iwa[3072];
    char task[60];
    doublereal factr;
    char csave[60];
    doublereal dsave[29];
    integer isave[44];
    logical lsave[4];
    doublereal pgtol;
    integer iprint;

/*     This simple driver demonstrates how to call the L-BFGS-B code to */
/*       solve a sample problem (the extended Rosenbrock function */
/*       subject to bounds on the variables). The dimension n of this */
/*       problem is variable. */
/*<       integer          nmax, mmax >*/
/*<       parameter        (nmax=1024, mmax=17) >*/
/*        nmax is the dimension of the largest problem to be solved. */
/*        mmax is the maximum number of limited memory corrections. */
/*     Declare the variables needed by the code. */
/*       A description of all these variables is given at the end of */
/*       the driver. */
/*<       character*60     task, csave >*/
/*<       logical          lsave(4) >*/
/*<    >*/
/*<    >*/
/*     Declare a few additional variables for this sample problem. */
/*<       double precision t1, t2 >*/
/*<       integer          i >*/
/*     We wish to have output at every iteration. */
/*<       iprint = 1 >*/
    iprint = 1;
/*     We specify the tolerances in the stopping criteria. */
/*<       factr=1.0d+7 >*/
    factr = 1e7;
/*<       pgtol=1.0d-5 >*/
    pgtol = 1e-5;
/*     We specify the dimension n of the sample problem and the number */
/*        m of limited memory corrections stored.  (n and m should not */
/*        exceed the limits nmax and mmax respectively.) */
/*<       n=25 >*/
    n = 25;
/*<       m=5 >*/
    m = 5;
/*     We now provide nbd which defines the bounds on the variables: */
/*                    l   specifies the lower bounds, */
/*                    u   specifies the upper bounds. */
/*     First set bounds on the odd-numbered variables. */
/*<       do 10 i=1,n,2 >*/
    i__1 = n;
    for (i__ = 1; i__ <= i__1; i__ += 2) {
/*<          nbd(i)=2 >*/
        nbd[i__ - 1] = 2;
/*<          l(i)=1.0d0 >*/
        l[i__ - 1] = 1.;
/*<          u(i)=1.0d2 >*/
        u[i__ - 1] = 100.;
/*<   10  continue >*/
/* L10: */
    }
/*     Next set bounds on the even-numbered variables. */
/*<       do 12 i=2,n,2 >*/
    i__1 = n;
    for (i__ = 2; i__ <= i__1; i__ += 2) {
/*<          nbd(i)=2 >*/
        nbd[i__ - 1] = 2;
/*<          l(i)=-1.0d2 >*/
        l[i__ - 1] = -100.;
/*<          u(i)=1.0d2 >*/
        u[i__ - 1] = 100.;
/*<   12   continue >*/
/* L12: */
    }
/*     We now define the starting point. */
/*<       do 14 i=1,n >*/
    i__1 = n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          x(i)=3.0d0 >*/
        x[i__ - 1] = 3.;
/*<   14  continue >*/
/* L14: */
    }
/*<       write (6,16) >*/
/*
  16  format(/,5x, 'Solving sample problem.',
     +       /,5x, ' (f = 0.0 at the optimal solution.)',/)
*/
#if 0
    printf("     Solving sample problem.\n"
           "      (f = 0.0 at the optimal solution.)\n");
#endif
/*<    >*/
/*     We start the iteration by initializing task. */

/*<       task = 'START' >*/
    s_copy(task, "START", (ftnlen)60, (ftnlen)5);
/*        ------- the beginning of the loop ---------- */
/*<  111  continue >*/
L111:
/*     This is the call to the L-BFGS-B code. */
/*<    >*/
    setulb_(&n, &m, x, l, u, nbd, &f, g, &factr, &pgtol, wa, iwa, task, &
            iprint, csave, lsave, isave, dsave);
/*<       if (task(1:2) .eq. 'FG') then >*/
    if (s_cmp(task, "FG", (ftnlen)2, (ftnlen)2) == 0) {
/*        the minimization routine has returned to request the */
/*        function f and gradient g values at the current x. */
/*        Compute function value f for the sample problem. */
/*<          f=.25d0*(x(1)-1.d0)**2 >*/
/* Computing 2nd power */
        d__1 = x[0] - 1.;
        f = d__1 * d__1 * .25;
/*<          do 20 i=2,n >*/
        i__1 = n;
        for (i__ = 2; i__ <= i__1; ++i__) {
/*<             f=f+(x(i)-x(i-1)**2)**2 >*/
/* Computing 2nd power */
            d__2 = x[i__ - 2];
/* Computing 2nd power */
            d__1 = x[i__ - 1] - d__2 * d__2;
            f += d__1 * d__1;
/*<   20     continue >*/
/* L20: */
        }
/*<          f=4.d0*f >*/
        f *= 4.;
/*        Compute gradient g for the sample problem. */
/*<          t1=x(2)-x(1)**2 >*/
/* Computing 2nd power */
        d__1 = x[0];
        t1 = x[1] - d__1 * d__1;
/*<          g(1)=2.d0*(x(1)-1.d0)-1.6d1*x(1)*t1 >*/
        g[0] = (x[0] - 1.) * 2. - x[0] * 16. * t1;
/*<          do 22 i=2,n-1 >*/
        i__1 = n - 1;
        for (i__ = 2; i__ <= i__1; ++i__) {
/*<             t2=t1 >*/
            t2 = t1;
/*<             t1=x(i+1)-x(i)**2 >*/
/* Computing 2nd power */
            d__1 = x[i__ - 1];
            t1 = x[i__] - d__1 * d__1;
/*<             g(i)=8.d0*t2-1.6d1*x(i)*t1 >*/
            g[i__ - 1] = t2 * 8. - x[i__ - 1] * 16. * t1;
/*<   22     continue >*/
/* L22: */
        }
/*<          g(n)=8.d0*t1 >*/
        g[n - 1] = t1 * 8.;
/*          go back to the minimization routine. */
/*<          goto 111 >*/
        goto L111;
/*<       endif >*/
    }

/*<       if (task(1:5) .eq. 'NEW_X')  goto 111 >*/
    if (s_cmp(task, "NEW_X", (ftnlen)5, (ftnlen)5) == 0) {
        goto L111;
    }
/*        the minimization routine has returned with a new iterate, */
/*         and we have opted to continue the iteration. */
/*           ---------- the end of the loop ------------- */
/*     If task is neither FG nor NEW_X we terminate execution. */
/*<       stop >*/
/*<       end >*/
    return 0;
} /* MAIN__ */