Skip to content

MMA causes division by zero when using relative tolerance (ftol_rel) #635

@rafaelrojasmiliani

Description

@rafaelrojasmiliani

What happened?

When using the NLopt MMA optimizer (LD_MMA) with relative stopping tolerances (ftol_rel), a division-by-zero floating-point exception occurs.

I observed this phenomenon trying to solve the unconstrainted Matyas and Three-hump camel function reported in wikipedia with ftol_rel set and (0, 0) as initial condition. No matter the value of ftol_rel a division by zero is performed.

While debugging, I arrived here and observed that dd.wval is zero.

How to reproduce the issue?

Run this


// test_mma_divzero.cpp

#include <cfenv>    // feenableexcept
#include <iostream>
#include <nlopt.hpp>    // NLopt C++ wrapper
#include <stdexcept>
#include <vector>

// f(x) = x^T x, with gradient 2x
double objective(const std::vector<double>& x, std::vector<double>& grad, void* /*data*/)
{
	if (!grad.empty())
	{
		grad[0] = 2.0 * x[0];
		grad[1] = 2.0 * x[1];
	}
	return (x[0] * x[0]) + (x[1] * x[1]);
}

int main()
{
	// Enable floating point exceptions (except inexact, underflow, overflow)
	feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT & ~FE_UNDERFLOW & ~FE_OVERFLOW);

	const int n = 2;
	nlopt::opt opt(nlopt::LD_MMA, n);


	// MMA requires bounds; choose [0, 1] so that x=0 is feasible and optimal
	std::vector<double> lb(n, -1.0);
	std::vector<double> ub(n, 1.0);
	opt.set_lower_bounds(lb);
	opt.set_upper_bounds(ub);

	opt.set_min_objective(objective, nullptr);
	opt.set_ftol_rel(1e-2);

	// Initial guess x = (0, 0)
	std::vector<double> x(n, 0.5);
	double minf = 0.0;

	nlopt::result res = opt.optimize(x, minf);
	std::cout << "NLopt result code: " << res << "\n";
	std::cout << "x = [" << x[0] << ", " << x[1] << "], f = " << minf << "\n";

	return 0;
}

Version

2.7.1

Operating System

Linux

Installation media

other

Additional Context

Installed with apt-get.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions