简体   繁体   中英

Why does scipy.optimize.minimize not find the minimum?

I am looking to find for which (x) the following function is minimal given a parameter dim.
This is the function:

def func(x, dim):
    return np.abs(np.abs(np.mean(np.sqrt(np.sum(np.diff(
        np.random.rand(100000,dim,2)/x, axis=2)**2, axis=1))))
        - 1/3)

And this is how it looks:

for xx in np.arange(1,5,0.1):
    plt.scatter(xx, func(xx,2), color='blue')

在此处输入图像描述

But when I try to find the x value which should be around 1.5 the result very close to my x0 guess (here around 1.0).

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


params = minimize(func, x0=1, args=(2))

I also tried different solvers, but I cannot get it to minimize.

Your function needs to be deterministic for the minimization to work. So you need to remove your call to np.random.rand . Once solution could be to generate those random numbers once at the beginning and fix them throughout the minimization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM