help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multidimensional minimization with V-shaped minimum


From: Stephan Lorenzen
Subject: Re: Multidimensional minimization with V-shaped minimum
Date: Fri, 5 Feb 2021 17:29:03 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0

Do I get undefined behavior if the first derivative is not continuous?
What is the worst thing that might happen? Do I have to use another
minimizer? Is BFGS a proper choice?

Here is a minimal example using the "absolute value" function and
starting at 1 (returns status GSL_ENOPROG and 1 as minimum)

#include <iostream>
#include <gsl/gsl_multimin.h>

double f(const gsl_vector* x, void* params)
{
    double xVal = gsl_vector_get(x, 0);
    if (xVal < 0) return -xVal;
    return xVal;
}
void df(const gsl_vector* x, void* params, gsl_vector* df)
{
    double xVal = gsl_vector_get(x, 0);
    if (xVal < 0) gsl_vector_set(df, 0, -1);
    else          gsl_vector_set(df, 0,  1);
}
void fdf(const gsl_vector* x, void* params, double* f, gsl_vector* df)
{
    double xVal = gsl_vector_get(x, 0);
    if (xVal < 0)
    {
        gsl_vector_set(df, 0, -1);
        *f = -xVal;
    }
    else
    {
        gsl_vector_set(df, 0, 1);
        *f = xVal;
    }
}
int main()
{
    size_t iter = 0;
    int status;
    const gsl_multimin_fdfminimizer_type *T =
gsl_multimin_fdfminimizer_vector_bfgs2;
    gsl_multimin_function_fdf func;
    func.n = 1;
    func.f = f;
    func.df = df;
    func.fdf = fdf;
    func.params = nullptr;
    gsl_multimin_fdfminimizer *m = gsl_multimin_fdfminimizer_alloc (T, 1);
    gsl_vector *x= gsl_vector_alloc (1);
    gsl_vector_set (x, 0, 1);

    gsl_multimin_fdfminimizer_set (m, &func, x, 0.01/*step size*/,
0.01/*tol*/);
    do
    {
        iter++;
        status = gsl_multimin_fdfminimizer_iterate (m);
        if (status) break;
        status = gsl_multimin_test_gradient (m->gradient, 1);
    }
    while (status == GSL_CONTINUE);

    std::cout << "Status: " << status << std::endl;
    std::cout << "xMin: " <<
gsl_vector_get(gsl_multimin_fdfminimizer_x(m), 0) << std::endl;

    gsl_multimin_fdfminimizer_free (m);
    gsl_vector_free (x);
}

On 05.02.21 17:09, Mark Galassi wrote:

[...] with discontinuous first derivative at the minimum. So, the
first derivative never comes close to zero [...] f and df [...]
I'm curious as to why you would use the methods that require the derivative - 
they tend to require that the function be at least continuously differentiable 
(i.e. derivative is continuous), and sometimes even twice differentiable.

But in any case, to try to understand what's happening with 
gsl_multimin_fdfminimizer_m(), could you give us an MWE (minimum working 
example) that exhibits this behavior?  Then we can jump onto the problem.

https://en.wikipedia.org/wiki/Minimal_working_example

--
Dr. Stephan Lorenzen
Mühlenredder 35
21493 Schwarzenbek
Tel.: 0177 872 8817




reply via email to

[Prev in Thread] Current Thread [Next in Thread]