SciPy使用“eq”约束函数来最小化函数,但不能使用SLSQPalgorithm(最好是GRG)

我正在使用NumPy和SciPy将Excel解算器转换为python,虽然我在技术上已经将其工作,但与结果略有不同。

正在通过公式提供示例数据:

array_1 = [ 0.0943417538897551, 0.0799476059590533, 0.0486689860368342, 0.052434296201351 , 0.231614207638357, 0.0808322159046283, 0.0819215585688325, 0.140991841045595, 0.189247534431047 ] matrix_1 = [ [0.0235147682238835, 0.0194641686338689, 0.0116344823277316, 0.0214041868628604, 0.010980242438382, 0.013085897256736, 0.0107928914491097, -0.000350116147653561, 0.000323182155233255], [0.0194641686338689, 0.0227374787807333, 0.0147128129866735, 0.023168038099643, 0.00899606198312112, 0.0108035747784655, 0.00989996824509696, -0.000271072225966624, 0.000337765823991017], [0.0116344823277316, 0.0147128129866735, 0.0265282312707786, 0.0140689757522423, 0.021443195995926, 0.0252024393105442, 0.0187159868856209, 0.000969972826332724, -0.0000408711850838449], [0.0214041868628604, 0.023168038099643, 0.0140689757522423, 0.0288819629130722, 0.00890430456025532, 0.00987603454385401, 0.00914278195359995, -0.000127719476583381, 0.000340663672172775], [0.010980242438382, 0.00899606198312111, 0.021443195995926, 0.00890430456025532, 0.029544562002381, 0.0314280240062057, 0.017176083705661, 0.00103517056623238, 0.0000307234067635423], [0.013085897256736, 0.0108035747784655, 0.0252024393105442, 0.00987603454385401, 0.0314280240062057, 0.0552293560476392, 0.0198882118118466, 0.000830285068544538, -0.000328125366438888], [0.0107928914491097, 0.00989996824509696, 0.0187159868856209, 0.00914278195359995, 0.017176083705661, 0.0198882118118466, 0.0174188411860936, 0.000638033896492422, -0.00000837412076787606], [-0.000350116147653561, -0.000271072225966624, 0.000969972826332724, -0.000127719476583381, 0.00103517056623238, 0.000830285068544538, 0.000638033896492422, 0.00207309710065845, 0.00038309084175021], [0.000323182155233255, 0.000337765823991017, -0.0000408711850838449, 0.000340663672172775, 0.0000307234067635422, -0.000328125366438888, -0.00000837412076787614, 0.00038309084175021, 0.000315889643542944] ] user_input_number = 0.2 # fluctuates between 0 and 1 array_2 = [ 0.0555192520462957, 0.0521964263617645, 0.0727151670043896, 0.0541841497069959, 0.0797919901829936, 0.0972924758058667, 0.057466786568159, 0.00266000663087858, 0.000440591301372533, ] 

我的计算function如下所示:

 def calculate(array_1): step_one = np.dot(np.dot(array_1, matrix_1), array_1) step_two = user_input_number * np.dot(array_2, array_1) return step_one - step_two # matrix_1, array_2, and user_input_number are defined outside of this scope 

array_1数字的边界需要保持在0和1之间:

 myBounds = ((0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1)) 

我需要传递一个约束,其中sum(array_1)将等于1:

 myConstraint = ({'type': 'eq', 'fun': lambda x: 1 - sum(x)}) 

所以我的SciPy minimize呼叫结束了这样的事情:

 result = minimize(calculate, myArray, method="SLSQP", bounds=myBounds, constraints=myConstraint) # 'SLSQP' is used because I read it is the only one that allows a constraint. 

就像我提到的这个工作,但不准确的结果,我们在Excel中获得。

以下是求解器设置的屏幕截图:

excel求解器

你可以看到它正在使用求解方法“GRG非线性”,但是该选项不在可用方法下的最小化文档中 。 我看到“BFGS”和“L-BFGS-B”的推荐结果非常接近,但是我不能做一个很好的比较,因为它不允许将结果放在结果数组上。

我的整体问题是复制“GRG非线性”最小化约束的最佳方法是什么?