0

我正在处理一个优化问题,为此我链接了 3 个优化器。Symfit 导入为 sft,sympy 导入为 sy:[编辑] 下面的代码是我的情况的一个最小示例,产生相同的错误消息。

k, a = sft.parameters('k, a') # parameters to be optimized
k.min = 0.01
k.max = 1
a.min, a.max = 0.01, 1
L = sft.Parameter('L', value = 5, fixed = True) #this parameter is known,
 #therefore I don't wan't is to move


#variables
x = sft.Variable('x')
A = sft.Variable('A')
P = sft.Variable('P')

#model
model_dict = {
    sy.Derivative(A, x): k * A - P**a/ L, 
    sy.Derivative(P, x): - k * (P**2)/L 
    }

odemodel = sft.ODEModel(model_dict, initial= {x : 0.,
                                             A : 0,
                                             P : 0
                                             }) 

#some mock data ( inspired of tBuLi symfit doc)
x = np.linspace(0, 20, 40)
mock_data = odemodel(x=x, k=0.1, a = 0.08, L = 5)._asdict()
sigma_data = 0.5
np.random.seed(42)
for var in mock_data:
    mock_data[var] += np.random.normal(0, sigma_data, size=len(x))
fit = sft.Fit(odemodel, x = x,
              A = mock_data[A], P = mock_data[P],
              minimizer = [DifferentialEvolution, LBFGSB, BasinHopping]) #DifferentialEvolution #BasinHopping
fit_result = fit.execute()

弹出以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-ea4a7a6e9a8e> in <module>
     34               A = mock_data[A], P = mock_data[P],
     35               minimizer = [DifferentialEvolution, LBFGSB, BasinHopping]) #DifferentialEvolution #BasinHopping
---> 36 fit_result = fit.execute()

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\fit.py in execute(self, **minimize_options)
    577         :return: FitResults instance
    578         """
--> 579         minimizer_ans = self.minimizer.execute(**minimize_options)
    580         minimizer_ans.covariance_matrix = self.covariance_matrix(
    581             dict(zip(self.model.params, minimizer_ans._popt))

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimizer_kwargs)
    270         for minimizer, kwargs in zip(self.minimizers, bound_arguments.arguments.values()):
    271             minimizer.initial_guesses = next_guess
--> 272             ans = minimizer.execute(**kwargs)
    273             next_guess = list(ans.params.values())
    274             answers.append(ans)

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\support.py in wrapped_func(*args, **kwargs)
    421                     else:
    422                         bound_args.arguments[param.name] = param.default
--> 423             return func(*bound_args.args, **bound_args.kwargs)
    424         return wrapped_func
    425 

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimize_options)
    408         if jacobian is None:
    409             jacobian = self.wrapped_jacobian
--> 410         return super(ScipyGradientMinimize, self).execute(jacobian=jacobian, **minimize_options)
    411 
    412     def scipy_constraints(self, constraints):

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, **minimize_options)
    428     def execute(self, **minimize_options):
    429         return super(ScipyBoundedMinimizer, self).execute(bounds=self.bounds,
--> 430                                                           **minimize_options)
    431 
    432 

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\support.py in wrapped_func(*args, **kwargs)
    421                     else:
    422                         bound_args.arguments[param.name] = param.default
--> 423             return func(*bound_args.args, **bound_args.kwargs)
    424         return wrapped_func
    425 

C:\ProgramData\Anaconda3\lib\site-packages\symfit\core\minimizers.py in execute(self, bounds, jacobian, hessian, constraints, **minimize_options)
    353             jac=jacobian,
    354             hess=hessian,
--> 355             **minimize_options
    356         )
    357         return self._pack_output(ans)

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    608     elif meth == 'l-bfgs-b':
    609         return _minimize_lbfgsb(fun, x0, args, jac, bounds,
--> 610                                 callback=callback, **options)
    611     elif meth == 'tnc':
    612         return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,

C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py in _minimize_lbfgsb(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, **unknown_options)
    275         bounds = [(None, None)] * n
    276     if len(bounds) != n:
--> 277         raise ValueError('length of x0 != length of bounds')
    278     # unbounded variables must use None, not +-inf, for optimizer to work properly
    279     bounds = [(None if l == -np.inf else l, None if u == np.inf else u) for l, u in bounds]

ValueError: length of x0 != length of bounds

正如这个(长)消息中所提到的,它是第二个最小化器 lbfgsb造成麻烦,但我根本不知道如何克服这个问题。在我的完整代码中,当我尝试只放置一个最小化器时,程序会永远运行,我无法找出原因。这就是为什么一个最小化器似乎是不够的,我想把它们链接起来。我认为这是由于问题的复杂性:两个耦合的 ODE,需要优化 7 个参数,以及使用 InteractiveGuess 执行的初始猜测(顺便说一下非常棒的工具)。

提前感谢您的帮助!

4

0 回答 0