我正在尝试使用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,
]
我的计算函数如下所示:
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中得到的结果并不准确。
以下是解算器设置的屏幕截图:
你可以看到它使用的解决方法" GRG Nonlinear"但该选项不在可用方法下的minimize docs中。我看到了BFGS'和' L-BFGS-B'建议哪些结果更接近,但我不能做一个很好的比较,因为它不允许将约束放在结果数组上。
我的整体问题是什么是复制" GRG Nonlinear"用约束最小化的方法?