scipy.optimize.minimize和Minuit返回初始猜测值

时间:2020-02-26 00:16:01

标签: scipy-optimize-minimize

我在scipy.minimize.optimize遇到麻烦。这是我的代码。

class AddressCreateView(LoginRequiredMixin,CreateView):
    model = models.Address

    fields = ['name', 'address1', 'phone', 'zip_code', 'city', 'country']

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.user = self.request.user
        obj.save()
        return super().form_valid(form)

class AddressUpdateView(LoginRequiredMixin,UpdateView):
    model = models.Address

    fields = ['name', 'address1', 'phone', 'zip_code', 'city', 'country']

    success_url = reverse_lazy('main:address_list')

    def get_queryset(self):
        return self.model.objects.filter(user=self.request.user)

class AddressDeleteView(LoginRequiredMixin,DeleteView):
    model = models.Address
    success_url = reverse_lazy('main:address_list')

    def get_queryset(self):
        return self.model.objects.filter(user=self.request.user)

我正在尝试针对N最小化函数path('address/', views.AddressListView.as_view(), name='address_list'), path('address/create/', views.AddressCreateView.as_view(), name='address_create'), path('address/<int:pk>/', views.AddressUpdateView.as_view(), name='address_update'), path('address/<int:pk>/delete/', views.AddressDeleteView.as_view(), name='address_delete'), 以找到N的最小值,但是from time import process_time import numpy as np from scipy.optimize import minimize class NMin(object): def __init__(self, error): self.error=error def func(self, N): i = np.arange(1, N+1) f = np.abs(np.sum(4/(N*(1+((i - 0.5)/N)**2))) - np.pi)-self.error return(f) def nMin(self): x0 = 1 nMin = minimize(self.func, x0) return(nMin.x) def main(): t1_start = process_time() error=10**(-6) nMin = NMin(error).nMin() print("the minimum value of N is: " + str(nMin)) t1_stop = process_time() print("Elapsed time during the whole program in seconds:", t1_stop-t1_start) main () 似乎要返回func(x)而不是N的最小值。这是我的输出。

NMin(error).nMin()

我对此感到非常困扰,因为我似乎找不到问题,而且我不明白为什么x0 = 1无法正常工作。

1 个答案:

答案 0 :(得分:2)

scipy.optimize.minimize主要用于连续微分函数。在func中使用arange会产生一个离散的问题。由于这些不连续性,这会导致梯度发生较大的跳跃(请参见下图)。

我添加了一些调试打印:

from time import process_time
import numpy as np
from scipy.optimize import minimize
class NMin(object):
    def __init__(self, error):
        self.error=error

    def func(self, N):
        print("func called N = {}".format(N))
        i = np.arange(1, N+1)
        print("i = {}".format(i))
        f = np.abs(np.sum(4/(N*(1+((i - 0.5)/N)**2))) - np.pi)-self.error
        print("f = {}".format(f))
        return(f)

    def nMin(self):
        x0 = 1
        nMin = minimize(self.func, x0)
        return(nMin.x)


def main():
    t1_start = process_time()
    error=10**(-6)
    nMin = NMin(error).nMin()
    print("the minimum value of N is: " + str(nMin))
    t1_stop = process_time()
    print("Elapsed time during the whole program in seconds:",
                                         t1_stop-t1_start)

main()

结果是:

func called N = [1.]
i = [1.]
f = 0.05840634641020706
func called N = [1.00000001]
i = [1. 2.]
f = 1.289175555623012

也许您想使用其他更适合离散问题的求解器,或者更改您的目标以满足连续性基于梯度优化的先决条件。

enter image description here