-1

I tried to enter an initial simplex to Nelder Mead but got an exception in python, that the shape is wrong. However, I don't know and can't figure out the shape, nor do I know where to look it up.

When I want to use the scipy Nelder Mead algorithm, I want to have a very flexible initial simplex or also restart the optimization from a certain point without iterating the initial simplex again.

However, I get an exception that the shape of my initial simplex is wrong:

ValueError: `initial_simplex` should be an array of shape (N+1,N)

I could not find a good description or an example how to enter an initial simplex to the algorithm. Can someone provide a minimal example including the initial_simplex parameter?

  • 1
    Please provide a [mre]. See [ask] for further guidance on asking questions. – starball Jan 05 '23 at 10:28
  • I am just asking for a working example of using an initial_simplex in scipy.minimize. How would a minimal example from me help? – Felix Hermann Jan 05 '23 at 10:37
  • You got an error. Are you not asking about why the error happened? Sorry if you are not, but if that's the case, why are you even showing the error message? – starball Jan 05 '23 at 10:38
  • I basically know why the error happens, but not how to fix it. so I am asking for an example how to use the function parameter correctly :) – Felix Hermann Jan 05 '23 at 10:41
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 05 '23 at 10:45
  • Searching on `[scipy] initial_simplex` turns a few previous SO. I don't know if they help or not. But note how they ask, with actual code examples. While I've answered a number of `minimize` questions, I've never dealt with this particular method and parameter. That is probably true for most of the readers who will see this question within the next 24 hrs. – hpaulj Jan 05 '23 at 21:22

1 Answers1

0

after testing I found a way it works. This workinge xample takes a simplex into the algorithm which is then evaluated and from there the algorithm is started:

from math import pi, sin
from random import uniform

import matplotlib.pyplot as plt
from scipy.optimize import minimize


def function(x, a, b, c):
    return a * x ** 2 + b * x + c


def cost_function(guess):
    y_test = [function(x_i, *guess) for x_i in x_range]
    differences = [(y_i - data_i)**2 for y_i, data_i in zip(y_test, data)]
    opt_plot.set_ydata(y_test)
    plt.pause(1e-6)
    cost = sum(differences) / len(differences)

    print('cost', cost, 'guess', guess, end='\n')
    return cost


def get_initial_simplex(guess, delta_0=.2):
    print('get simplex')
    simplex = []
    simplex.append([cost_function(guess), guess])
    for i in range(len(guess)):
        simplex_guess = guess.copy()
        simplex_guess[i] += delta_0
        cost = cost_function(simplex_guess)
        simplex.append([cost, simplex_guess])

    simplex = sorted(simplex, key=lambda x: x[0])
    print('done')
    return [elem[1] for elem in simplex]


# create data
x_range = [i / 100 for i in range(-100, 100)]
data = [3 * sin(x_i + pi / 2) + 2 for x_i in x_range]

# plot the data:
fig, ax = plt.subplots()
ax.plot(x_range, data)
opt_plot, = ax.plot(x_range, [0 for _ in data])
guess = [uniform(-1,1) for _ in range(3)]

# start optimization of mse function


options ={
    'initial_simplex': get_initial_simplex(guess)
}

result = minimize(cost_function, guess, method='Nelder-Mead', options=options)