Changeset 1912


Ignore:
Timestamp:
May 4, 2011 11:37:16 AM (9 years ago)
Author:
bradbell
Message:

In the event of a Nan during forward mode,
or an infinity during cppad_ipopt_nlp call back, generate an error message.

makefile.in: change from corresponding makefile.am.
cppad_ipopt_nlp.hpp: store plus infinity in class object.
cppad_ipopt_nlp.cpp: check that call back values are finite.
forward.hpp: check for nans. reorganize zero order case.
fun_construct.hpp: check for nans at zero order.
whats_new_11.omh: user's view of change.
forward.cpp: test of change.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/cppad/local/forward.hpp

    r1460 r1912  
    44
    55/* --------------------------------------------------------------------------
    6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
     6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
    77
    88CppAD is distributed under multiple licenses. This distribution is under
     
    7878
    7979        // evaluate the derivatives
     80        if( p == 0 )
     81        {
    8082# if CPPAD_USE_FORWARD0SWEEP
    81         if( p == 0 ) compare_change_ = forward0sweep(
    82                 true, n, total_num_var_, &play_, taylor_col_dim_, taylor_
    83         );
    84         else
    85         forward_sweep(
     83                compare_change_ = forward0sweep(
     84                        true, n, total_num_var_, &play_, taylor_col_dim_, taylor_
     85                );
     86# else
     87                compare_change_ = forward_sweep(
     88                true, p, n, total_num_var_, &play_, taylor_col_dim_, taylor_
     89                );
     90# endif
     91        }
     92        else forward_sweep(
    8693                true, p, n, total_num_var_, &play_, taylor_col_dim_, taylor_
    8794        );
    88 # else
    89         size_t compare_change = forward_sweep(
    90                 true, p, n, total_num_var_, &play_, taylor_col_dim_, taylor_
    91         );
    92         if( p == 0 )
    93                 compare_change_ = compare_change;
    94 # endif
    9595
    9696        // return the p-th order taylor_ coefficients for dependent variables
     
    100100                vp[i] = taylor_[dep_taddr_[i] * taylor_col_dim_ + p];
    101101        }
     102# ifndef NDEBUG
     103        if( hasnan(vp) )
     104        {       if( p == 0 )
     105                {       CPPAD_ASSERT_KNOWN(false,
     106                                "y_p = f.Forward(p, x_p): has a nan in y_p for p = 0."
     107                        ); 
     108                }
     109                else
     110                {       CPPAD_ASSERT_KNOWN(false,
     111                                "y_p = f.Forward(p, x_p): has a nan in y_p for p > 0\n"
     112                                "but not for p = 0."
     113                        );
     114                }
     115        }
     116# endif
     117
    102118
    103119        // now we have p + 1  taylor_ coefficients per variable
  • trunk/cppad/local/fun_construct.hpp

    r1641 r1912  
    457457
    458458# ifndef NDEBUG
    459         for(i = 0; i < m; i++) if( taylor_[dep_taddr_[i]] != y[i].value_ )
     459        for(i = 0; i < m; i++)
     460        if( taylor_[dep_taddr_[i]] != y[i].value_ || isnan( y[i].value_ ) )
    460461        {       using std::endl;
    461462                std::ostringstream buf;
  • trunk/cppad_ipopt/example/makefile.in

    r1911 r1912  
    9494# $Id$
    9595# -----------------------------------------------------------------------------
    96 # CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
     96# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
    9797#
    9898# CppAD is distributed under multiple licenses. This distribution is under
  • trunk/cppad_ipopt/src/cppad_ipopt_nlp.cpp

    r1733 r1912  
    1010Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
    1111-------------------------------------------------------------------------- */
     12# include <limits>
     13
    1214# include "cppad_ipopt_nlp.hpp"
    1315# include "sparse_map2vec.hpp"
     
    118120          g_u_ ( g_u ),
    119121          fg_info_ ( fg_info ) ,
    120           solution_ (solution)
     122          solution_ (solution) ,
     123          infinity_ ( std::numeric_limits<Number>::infinity() )
    121124{       size_t k;
    122125
     
    449452                printf("cppad_ipopt_nlp::eval_f::x[%d] = %20.14g\n", j, x[j]);
    450453        printf("cppad_ipopt_nlp::eval_f::obj_value = %20.14g\n", obj_value);
     454# endif
     455# ifndef NDEBUG
     456        CPPAD_ASSERT_KNOWN(
     457                (-infinity_ < obj_value) && (obj_value < infinity_),
     458                "cppad_ipopt_nlp::eval_f:: objective value is not finite"
     459        );
    451460# endif
    452461        return true;
     
    539548        );
    540549# endif
     550# ifndef NDEBUG
     551        for(j = 0; j < n_; j++) CPPAD_ASSERT_KNOWN(
     552                (-infinity_ < grad_f[j]) && (grad_f[j] < infinity_),
     553                "cppad_ipopt_nlp::grad_f:: gradient of objective is not finite"
     554        );
     555# endif
    541556        return true;
    542557}
     
    622637        for(i = 0; i < m_; i++)
    623638                printf("cppad_ipopt_nlp::eval_g::g[%d] = %20.14g\n", i, g[i]);
     639# endif
     640# ifndef NDEBUG
     641        for(i = 0; i < m_; i++) CPPAD_ASSERT_KNOWN(
     642                (-infinity_ < g[i]) && (g[i] < infinity_),
     643                "cppad_ipopt_nlp::eval_g:: not all constraints are not finite"
     644        );
    624645# endif
    625646        return true;
     
    751772                }
    752773        }
     774# ifndef NDEBUG
     775        for(l = 0; l < nnz_jac_g_; l++) CPPAD_ASSERT_KNOWN(
     776                (-infinity_ < values[l]) && (values[l] < infinity_),
     777                "cppad_ipopt_nlp::eval_jac_g:: a component of "
     778                "gradient of g is not finite"
     779        );
     780# endif
     781
    753782        return true;
    754783}
     
    916945                }
    917946        }
     947# ifndef NDEBUG
     948        for(l = 0; l < nnz_h_lag_; l++) CPPAD_ASSERT_KNOWN(
     949                (-infinity_ < values[l]) && (values[l] < infinity_),
     950                "cppad_ipopt_nlp::eval_h:: a component of "
     951                "Hessian of Lagragian is not finite"
     952        );
     953# endif
    918954        return true;
    919955}
  • trunk/cppad_ipopt/src/cppad_ipopt_nlp.hpp

    r1733 r1912  
    792792        /// (passed to ctor)
    793793        cppad_ipopt_solution* const     solution_;
     794        /// plus infinity as a value of type Number
     795        const Number                    infinity_;
    794796
    795797        // ------------------------------------------------------------------
  • trunk/omh/whats_new_11.omh

    r1911 r1912  
    1313$begin whats_new_11$$
    1414$spell
     15        hasnan
    1516        instantiation
    1617        Microsoft
     
    4142assist you in learning about changes between various versions of CppAD.
    4243
     44$head 05-03$$
     45$list number$$
     46If $code NDEBUG$$ is not defined,
     47the $cref/hasnan/nan/$$ function is used to make sure that the
     48results of any $cref/Forward/$$ operation does not contain a
     49nan (not a number).
     50If so, an error message is generated and the program terminates.
     51This error message and termination can be caught; see $cref/ErrorHandler/$$.
     52
     53$lnext
     54In the event that the
     55$cref cppad_ipopt_nlp$$ objective function, the constraints,
     56or their derivatives are infinite,
     57an error message is generated and the program terminates
     58(proved that $code NDEBUG$$ is not defined and the default error
     59handler has not been replaced).
     60$lend
     61
    4362$head 04-29$$
    4463$list number$$
  • trunk/test_more/forward.cpp

    r1370 r1912  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    182182        return ok;
    183183}
     184
     185void my_error_handler(
     186        bool known           ,
     187        int  line            ,
     188        const char *file     ,
     189        const char *exp      ,
     190        const char *msg      )
     191{       // error hander must not return, so throw an exception
     192        throw std::string(msg);
     193}
     194
     195bool forward_nan(void)
     196{       
     197
     198        using CppAD::vector;
     199        using CppAD::AD;
     200
     201        size_t n = 2, m = 1;
     202        vector< AD<double> > a_x(n), a_y(m);
     203        a_x[0] = 1.;
     204        a_x[1] = 2.;
     205        Independent(a_x);
     206        a_y[0] = a_x[0] / a_x[1];
     207        CppAD::ADFun<double> f(a_x, a_y);
     208        //
     209        vector<double> x(n), y(m);
     210        x[0] = 0.;
     211        x[1] = 0.;
     212
     213        // replace the default CppAD error handler
     214        CppAD::ErrorHandler info(my_error_handler);
     215
     216        bool ok = false;
     217        try {
     218                y    = f.Forward(0, x);
     219        }
     220        catch( std::string msg )
     221        {       // check that the message contains "nan"
     222                ok = msg.find("nan") != std::string::npos;
     223        }
     224
     225        return ok;
     226}
    184227} // END empty namespace
    185228
     
    188231bool Forward(void)
    189232{       bool ok = true;
    190         ok &= ForwardOlder();
    191233        ok &= ForwardCases< CppAD::vector  <double> >();
    192234        ok &= ForwardCases< std::vector    <double> >();
    193235        ok &= ForwardCases< std::valarray  <double> >();
    194         return ok;
    195 }
     236        ok &= ForwardOlder();
     237        ok &= forward_nan();
     238        return ok;
     239}
Note: See TracChangeset for help on using the changeset viewer.