本文介绍了scipy's shgo 优化器未能最小化方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了熟悉全局优化方法,特别是 scipy.optimize v1.3.0 中的 shgo 优化器,我尝试最小化方差 向量 x = [x1,...,xN] 的 var(x)0 在约束下x 有一个给定的平均值:

In order to get familiar with global optimization methods and in particular with the shgo optimizer from scipy.optimize v1.3.0 I have tried to minimize the variance var(x) of a vector x = [x1,...,xN] with 0 <= xi <= 1 under the constraint that x has a given average value:

import numpy as np
from scipy.optimize import shgo

# Constraint
avg = 0.5  # Given average value of x
cons = {'type': 'eq', 'fun': lambda x: np.mean(x)-avg}

# Minimize the variance of x under the given constraint
res = shgo(lambda x: np.var(x), bounds=6*[(0, 1)], constraints=cons)

shgo 方法在这个问题上失败了:

The shgo method fails on this problem:

>>> res
     fun: 0.0
 message: 'Failed to find a feasible minimiser point. Lowest sampling point = 0.0'
    nfev: 65
     nit: 2
   nlfev: 0
   nlhev: 0
   nljev: 0
 success: False
       x: array([0., 0., 0., 0., 0., 0.])

正确的解决方案是均匀分布 x = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5] 并且可以通过使用局部优化器 minimize 轻松找到来自 scipy.optimize 的代码>:

The correct solution would be the uniform distribution x = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5] and it can be easily found by using the local optimizer minimize from scipy.optimize:

from scipy.optimize import minimize
from numpy.random import random

x0 = random(6)  # Random start vector
res2 = minimize(lambda x: np.var(x), x0, bounds=6*[(0, 1)], constraints=cons)

minimize 方法为任意起始向量生成正确的结果:

The minimize method yields the correct result for arbitrary start vectors:

>>> res2.success
True

>>> res2.x
array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5])

我的问题是:为什么 shgo 在这个相对简单的任务上失败了?是我弄错了还是 shgo 根本无法解决这个问题?任何帮助将不胜感激.

My question is: Why shgo fails on this relatively simple task? Did I made a mistake or is shgo simply not usable for this problem? Any help would be greatly appreciated.

推荐答案

AStefan-Endres 在 github 上的 scipy 项目页面中提供了对这个问题的非常详细的答案.在此再次感谢 Stefan-Endres

A very detailed answer to this question has been provided by Stefan-Endres in the scipy project page on github. At this point many thanks to Stefan-Endres again!

这篇关于scipy&amp;#39;s shgo 优化器未能最小化方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 22:20