1

I have defined a Class to minimize a standard function called rosen, in order to minimize rosen, the scipy.minimize function needs to call rosen repeatedly to minimize it.

from scipy.optimize import minimize
import numpy as np


class LocalMultivariateOptimization:

    def __init__(self, initial_guess_parameters, xtol, method):
        self.xtol = xtol
        self.method = method
        self.x = initial_guess_parameters

    def minimize(self):
        res = minimize(self.rosen(self.x), self.x, method=self.method, options={'xtol': self.xtol, 'disp': True})

    def rosen(self, x):
        return sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0)


args0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
xtol = 1e-8
method = 'nelder-mead'

LocalMultivariateOptimizationObject = LocalMultivariateOptimization(args0, xtol, method)
LocalMultivariateOptimizationObject.minimize()

results in

TypeError: 'numpy.float64' object is not callable

This is my first learning attempt at OOP in Python. What am I doing wrong here? I am calling the function instead of sending just the data in minimize(). Which is basically correct.

1
  • I think in res = minimize(self.rosen(self.x),... you want res = minimize(self.rosen,... Commented Jan 26, 2019 at 17:18

1 Answer 1

3

The first parameter to:

scipy.optimize.minimize()

is a callable. This bascially means you need to pass a function or a method that can be called by the optimization code. You however passed:

self.rosen(self.x)

which has already been called, instead you need:

self.rosen

In addition your minimize() method is not returning anything and should look more like:

def minimize(self):
    return minimize(self.rosen, self.x, method=self.method,
                    options={'xtol': self.xtol, 'disp': True})
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for explaining about the callable parameter. However, after changing to the minimize(self) function you gave, it resulted in TypeError: rosen() takes 1 positional argument but 2 were given
So I just took your posted code and put in my minimize() and there was no exception thrown.
I am very sorry, I was experimenting, and had some other variations compared to the original code I posted here. Your answer is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.