Giter Site home page Giter Site logo

fides-dev / fides Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 4.0 166 KB

Trust Region Optimization in Python

License: Other

Python 100.00%
trust-region-optimization python optimization-algorithms optimization optimizer trust-region trust-region-methods hacktoberfest

fides's Introduction

Fides - A python package for Trust Region Optimization

PyPI version Code coverage ReadTheDocs status DOI

About Fides

Fides implements an Interior Trust Region Reflective for boundary constrained optimization problems based on the papers ColemanLi1994 and ColemanLi1996. Accordingly, Fides is named after the Roman goddess of trust and reliability.

Fides can be installed via pip install fides. Further documentation is available at Read the Docs.

Features

  • Boundary constrained and unconstrained interior trust-region optimization
  • Reflective, truncated and optimization based boundary heuristics
  • Exact, 2D and CG subproblem solvers
  • BFGS, DFP, SR1, Broyden (good and bad) and Broyden class iterative Hessian Approximation schemes
  • SSM, TSSM, FX, GNSBFGS and custom hybrid Hessian Approximations schemes

fides's People

Contributors

dweindl avatar ffroehlich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fides's Issues

Fides does not find the correct solution fo a simple sum of squares problem with binding bounds

When minimizing a simple sum of squares function with partially binding bounds, fides fails to find the correct solution. It correctly converges to the boundaries in the dimensions where the bounds are binding but fails in the dimension without bounds. Instead of converging to setting x to 0 in that dimension, it converges to 0.59.

The convergence message is:

Stopping as function difference 9.77E-15 was smaller than specified tolerances (atol=1.00E-08, rtol=1.00E-08)

Here is a minimal working example:

from fides import Optimizer
import numpy as np
from fides import BFGS


def sum_of_squares(x):
    """Return function value and gradient of the sum of squares function."""
    fval = (x ** 2).sum()
    gradient = 2 * x
    return fval, gradient


lower_bounds = np.array([1, -np.inf, -np.inf])
upper_bounds = np.array([np.inf, np.inf, -1])
start_x = np.array([3, 2, -3])

true_solution = np.array([1, 0, -1])

opt = Optimizer(
    fun=sum_of_squares,
    lb=lower_bounds,
    ub=upper_bounds,
    hessian_update=BFGS(),
    resfun=False,
)

res = opt.minimize(start_x)

# Fides believes to have found the minimum
assert opt.converged

# fides_solution is [1.0000000000000233, 0.59204316173441, -1.0000000000000233]
fval, fides_solution, grad, hess = res

fides version: 0.6.3

Using a modified Lanczos method instead of full factorization for large-scale problems?

Hi @FFroehlich !

Many thanks again for your implementation!
I just had an idea, what one might want to do in the case of using Fides on a really large-scale system, where complete factorization of the Hessian may become expensive (some k variables). A version of this method which can deal well with indefinite matrices is discussed by Stephen Nash.

This is anything but a request, it's rather a question whether this might be a meaningful add-on at some point...

Best!

Brentq failure

brentq linesearch sometimes fails with f(a) and f(b) must have different signs, which shouldn't happen.

exitflags

would be nice to report some more granular exitflag that can be analysed programmatically

track minimum

would be nice to actively track minimum, not necessarily the final value

Optional parameter names

It would be great to have the possibility to pass parameter names to fides to produce more informative messages. E.g. having a parameter name in Exceeded lower bounds for indices [213] by [3.78577988e-16] at iteration 17. instead of the index would be quite helpful.

Reduce logging

Many logging events are on level warning or info. I was wondering whether one wants to downgrade these one step, such that most are info or debug. Reason: In multistart settings, the generated output is rather long.

Alternatively, we would simply set the default log level for e.g. fides in pypesto to error or warning.

numpy RuntimeWarnings when specifing infinite bounds

Problem Description

In the docstring for the Optimizer class, you write that parameters without bounds should get -inf / inf.

When I do this, I get a lot of RuntimeWarnings in minimize.py:640.

Analysis

The relevant lines are 634 and 640:

        v = np.sign(self.grad) + (self.grad == 0)
        bounds = ((1 + v)*self.lb + (1 - v)*self.ub)/2

The RuntimeWarning stems not from the infinite values but from a np.nan that appears when multiplying 0*np.inf.
Here is an minimal example to see this:

import numpy as np

ub = np.array([3, 3, 3, np.inf])
lb = np.array([-np.inf, 0, 0, 0])

grad = np.array([-3, 0, 3, -3])
# v: -1 where grad < 0, 1 where grad >= 0
v = np.sign(grad) + (grad == 0)

# (1 + v) / 2 = [0, 1, 1, 0], (1 - v) / 2 = [1, 0, 0, 1]
summand1 = (1 + v) / 2 * lb  # 0 * -np.inf causes the runtime warning
summand2 = (1 - v) / 2 * ub

bounds = summand1 + summand2
bounds

If I understand your code right, you are implementing an if condition: Choose the lower bounds when the gradient is weakly positive and the upper bound otherwise.

However, there is one exception to this: when the not chosen bound is +/- inf. In that case your bounds are NaN which is what causes the RuntimeWarning.

Solution Proposal

If that NaN is not wanted, a one line solution that avoids the RuntimeWarning is np.where(grad<0, ub, lb). If that NaN is important you can still get the same behavior in three lines without triggering the Warning:

bounds2 = np.where(grad<0, ub, lb)
# you need the np.logical_and here because the arrays are numpy bools
bounds2[np.logical_and(grad<0, ~np.isfinite(lb))] = np.nan
bounds2[np.logical_and(grad>=0, ~np.isfinite(ub))] = np.nan

Specification

I am using the latest numpy: 1.21.4 and the latest Fides: 0.7.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.