标题:求助: 主函数里怎么找不出所要的东西
只看楼主
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
结帖率:91.67%
已结贴  问题点数:20 回复次数:6 
求助: 主函数里怎么找不出所要的东西
看如下代码(这是世界级大侠的代码,没有经过任何改动),是个求极值的主函数,里面怎么找不到目标函数呢?求高手指导!,相应的头文件和程序见附件
#include "lbfgsb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>


/*
                            DRIVER 3 in Fortran 77
    --------------------------------------------------------------
           TIME-CONTROLLED DRIVER FOR L-BFGS-B (version 3.0)
    --------------------------------------------------------------

       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 driver shows how to terminate a run after some prescribed
       CPU time has elapsed, and how to print the desired information
       before exiting.

    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.)

                             *  *  *

        February 2011   (latest revision)
        Optimization Center at Northwestern University
        Instituto Tecnologico Autonomo de Mexico

        Jorge Nocedal and Jose Luis Morales, Remark on "Algorithm 778:
        L-BFGS-B: Fortran Subroutines for Large-Scale Bound Constrained
        Optimization"  (2011). To appear in  ACM Transactions on
        Mathematical Software,
        */


/*     ************** */


int main(void)
{

    /* System generated locals */
    integer i__1;
    double d__1, d__2;

    /* Local variables */
    static double f, g[1024];
    static integer i__, j;
    static double l[1024];
    static integer m, n;
    static double u[1024], x[1024], t1, t2, wa[43251];
    static integer nbd[1024], iwa[3072];
/*     static char task[60]; */
    static integer taskValue;
    static integer *task=&taskValue; /* must initialize !! */
/*      http:// */
    static double time1, time2, factr;
/*     static char csave[60]; */
    static integer csaveValue;
    static integer *csave=&csaveValue;
    static double dsave[29];
    static integer isave[44];
    static logical lsave[4];
    static double pgtol;
    static double tlimit;
    static integer iprint;


    /*
    This time-controlled driver shows that it is possible to terminate
    a run by elapsed CPU time, and yet be able to print all desired
    information. This driver also illustrates the use of two
    stopping criteria that may be used in conjunction with a limit
    on execution time. The sample problem used here is the same as in
    driver1 and driver2 (the extended Rosenbrock function with bounds
    on the variables).
    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
    driver1.
    Declare a few additional variables for the sample problem
    and for keeping track of time.
    */

    /*     We specify a limite on the CPU time (in seconds). */
    tlimit = .2f;
    /*     We suppress the default output.  (The user could also elect to */
    /*       use the default output by choosing iprint >= 0.) */
    iprint = -1;
    /*     We suppress the code-supplied stopping tests because we will */
    /*       provide our own termination conditions */
    factr = 0.;
    pgtol = 0.;
    /*     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 = 1000;
    m = 10;
    /*     We now specify 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. */
    i__1 = n;
    for (i__ = 1; i__ <= i__1; i__ += 2) {
        nbd[i__ - 1] = 2;
        l[i__ - 1] = 1.;
        u[i__ - 1] = 100.;
        /* L10: */
    }
    /*     Next set bounds on the even-numbered variables. */
    i__1 = n;
    for (i__ = 2; i__ <= i__1; i__ += 2) {
        nbd[i__ - 1] = 2;
        l[i__ - 1] = -100.;
        u[i__ - 1] = 100.;
        /* L12: */
    }
    /*     We now define the starting point. */
    i__1 = n;
    for (i__ = 1; i__ <= i__1; ++i__) {
        x[i__ - 1] = 3.;
        /* L14: */
    }
    /*     We now write the heading of the output. */
    printf("     Solving sample problem.\n");
    printf("      (f = 0.0 at the optimal solution.)\n");
    /*     We start the iteration by initializing task. */

    *task = START;
    /*        ------- the beginning of the loop ---------- */
    /*     We begin counting the CPU time. */
    timer(&time1);
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 (IS_FG(*task) ) {
        /*        the minimization routine has returned to request the */
        /*        function f and gradient g values at the current x. */
        /*        Before evaluating f and g we check the CPU time spent. */
        timer(&time2);
        if (time2 - time1 > tlimit) {
            *task = STOP_CPU;
/*             s_copy(task, "STOP: CPU EXCEEDING THE TIME LIMIT.", (ftnlen)60, ( */
/*                         ftnlen)35); */
            /*          Note: Assigning task(1:4)='STOP' will terminate the run; */
            /*          setting task(7:9)='CPU' will restore the information at */
            /*          the latest iterate generated by the code so that it can */
            /*          be correctly printed by the driver. */
            /*          In this driver we have chosen to disable the */
            /*          printing options of the code (we set iprint=-1); */
            /*          instead we are using customized output: we print the */
            /*          latest value of x, the corresponding function value f and */
            /*          the norm of the projected gradient |proj g|. */
            /*          We print out the information contained in task. */

            /*          We print the latest iterate contained in wa(j+1:j+n), where */

            /* Computing 2nd power */
            j = n * 3 + (m << 1) * n + m * m * 11;
            printf("Latest iterate X=");
            for (i__ = j + 1; i__ <= m ; ++i__) {
                printf("%.2e ", wa[i__ -1] );
            }
            printf("\n");
            /*          We print the function value f and the norm of the projected */
            /*          gradient |proj g| at the last iterate; they are stored in */
            /*          dsave(2) and dsave(13) respectively. */
            printf("At latest iterate   f = %.2e, |proj g| = %.2e\n", dsave[1], dsave[12] );
        } else {
            /*          The time limit has not been reached and we compute */
            /*          the function value f for the sample problem. */
            /* Computing 2nd power */
            d__1 = x[0] - 1.;
            f = d__1 * d__1 * .25;
            i__1 = n;
            for (i__ = 2; i__ <= i__1; ++i__) {
                /* 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;
                /* L20: */
            }
            f *= 4.;
            /*          Compute gradient g for the sample problem. */
            /* Computing 2nd power */
            d__1 = x[0];
            t1 = x[1] - d__1 * d__1;
            g[0] = (x[0] - 1.) * 2. - x[0] * 16. * t1;
            i__1 = n - 1;
            for (i__ = 2; i__ <= i__1; ++i__) {
                t2 = t1;
                /* Computing 2nd power */
                d__1 = x[i__ - 1];
                t1 = x[i__] - d__1 * d__1;
                g[i__ - 1] = t2 * 8. - x[i__ - 1] * 16. * t1;
                /* L22: */
            }
            g[n - 1] = t1 * 8.;
        }
        /*          go back to the minimization routine. */
        goto L111;
    }

    if ( *task == NEW_X ) {
        /*        the minimization routine has returned with a new iterate. */
        /*        The time limit has not been reached, and we test whether */
        /*        the following two stopping tests are satisfied: */
        /*        1) Terminate if the total number of f and g evaluations */
        /*             exceeds 900. */
        if (isave[33] >= 900) {
            *task = STOP_ITER;
/*             s_copy(task, "STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIM" */
/*                     "IT", (ftnlen)60, (ftnlen)52); */
        }
        /*        2) Terminate if  |proj g|/(1+|f|) < 1.0d-10. */
        if (dsave[12] <= (abs(f) + 1.) * 1e-10) {
            *task = STOP_GRAD;
/*             s_copy(task, "STOP: THE PROJECTED GRADIENT IS SUFFICIENTLY SMALL", */
/*                     (ftnlen)60, (ftnlen)50); */
        }
        /*        We wish to print the following information at each iteration: */
        /*          1) the current iteration number, isave(30), */
        /*          2) the total number of f and g evaluations, isave(34), */
        /*          3) the value of the objective function f, */
        /*          4) the norm of the projected gradient,  dsve(13) */

        /*        See the comments at the end of driver1 for a description */
        /*        of the variables isave and dsave. */
        printf("Iterate %ld, nfg = %ld, f = %.2e, |proj g| = %.2e\n", isave[29], isave[33], f, dsave[12] );
        /*        If the run is to be terminated, we print also the information */
        /*        contained in task as well as the final value of x. */
/*         if (s_cmp(task, "STOP", (ftnlen)4, (ftnlen)4) == 0) { */
        if (IS_STOP(*task)) {
            i__1 = n;
            printf("Final X = ");
            if ( n < 30 ) {
                for (i__ = 1; i__ <= i__1; ++i__) {
                    printf("%.2e ", x[i__ -1] );
                }
                printf("\n");
            } else {
                /* Print out just some */
                for (i__ = 1; i__ <= 10 ; ++i__) {
                    printf("%.2e ", x[i__ -1] );
                }
                printf("... \n ...(suppressing some output)...\n ... ");
                for (i__ = n-9; i__ <= n ; ++i__) {
                    printf("%.2e ", x[i__ -1] );
                }
                printf("\n");
            }
        }
        /*          go back to the minimization routine. */
        goto L111;
    }
    /*           ---------- the end of the loop ------------- */
    /*     If task is neither FG nor NEW_X we terminate execution. */
    return 0;
} /* MAIN__ */
L-BFGS-B-C-master.zip (161.25 KB)

搜索更多相关主题的帖子: version include 
2015-09-01 15:59
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
得分:0 
已经看了好几天了,感觉自己水平太差了,人家给个代码都搞不明白啊!
2015-09-01 16:01
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:7 
把所有注释都弄掉  代码看起来就不那么乱了

DO IT YOURSELF !
2015-09-01 16:40
大爱CATs
Rank: 4
来 自:重庆
等 级:业余侠客
威 望:3
帖 子:108
专家分:250
注 册:2015-1-13
得分:7 
顶楼上

弹指一挥间
2015-09-01 17:22
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
得分:0 
注释很必要啊,我倒是 注释更详细点,因为我要找目标函数
2015-09-02 10:09
暴风晕
Rank: 12Rank: 12Rank: 12
来 自:冥界
等 级:火箭侠
威 望:6
帖 子:402
专家分:3107
注 册:2015-8-8
得分:7 
关键是注释你可能都看不懂
2015-09-02 12:23
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
得分:0 
来几个解决问题的,不要说些没用的话,好不?
2015-09-02 12:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-456934-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.299091 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved