使用LMFIT拟合曲线,限制参数之和
我创建了一种曲线拟合算法,该算法有效,直到一个点。
我有六个参数:a,b,c,d,e,f。
每个参数可以在0到100之间,但具有约束(a+b+c+d+e+f)< = 100
无论如何要这样做?
params.add('a', value=1, min=0, max = 100, vary=True)
params.add('b', value=1, min=0, max = 100, vary=True)
params.add('c', value=1, min=0, max = 100, vary=True)
params.add('d', value=1, min=0, max = 100, vary=True)
params.add('e', value=1, min=0, max = 100, vary=True)
params.add('f', value=1, min=0, max = 100, vary=True)
我确实找到了一个,可能不是很优雅,解决方案。
在我试图求解的功能中,我检查A+B ...+F的总和是否大于100。如果是,我将其重置为随机数的系数,从而迫使该算法找到另一个解决方案。
def myfunc(a, b, c, d, e, f):
sum = a+b+c+d+e+f
if sum>100:
a = np.random.randint(low=0, high=10)
b = np.random.randint(low=0, high=10)
c = np.random.randint(low=0, high=10)
d = np.random.randint(low=0, high=10)
e = np.random.randint(low=0, high=10)
f = np.random.randint(low=0, high=10)
Rest of function
I have created a curve fitting algorithm that works, up to a point.
I have six parameters: a, b, c, d, e, f.
Each parameter can be between 0 and 100, but with the constraint (a+b+c+d+e+f) <= 100
Is there anyway to do this?
params.add('a', value=1, min=0, max = 100, vary=True)
params.add('b', value=1, min=0, max = 100, vary=True)
params.add('c', value=1, min=0, max = 100, vary=True)
params.add('d', value=1, min=0, max = 100, vary=True)
params.add('e', value=1, min=0, max = 100, vary=True)
params.add('f', value=1, min=0, max = 100, vary=True)
I did manage to find a, probably not very elegant, solution.
In the function I'm trying to solve I check if the the sum of a+b...+f is greater than 100. If it is I reset the coefficients with random numbers, forcing the algorithm to find another solution.
def myfunc(a, b, c, d, e, f):
sum = a+b+c+d+e+f
if sum>100:
a = np.random.randint(low=0, high=10)
b = np.random.randint(low=0, high=10)
c = np.random.randint(low=0, high=10)
d = np.random.randint(low=0, high=10)
e = np.random.randint(low=0, high=10)
f = np.random.randint(low=0, high=10)
Rest of function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑
(A+B+C+D+E+F)&LT; = 100
可以写为f = 100-(A+B+C+C+D+E) - Epsilon
带有
epsilon&gt; = 0
。现在,将其与Epsilon一起用作变量和F,从中得出:您仍然有6个变量,但是现在
f
并不独立于epsilon
。Consider that
(a+b+c+d+e+f) <= 100
can be written asf = 100 - (a+b+c+d+e) - epsilon
with
epsilon >= 0
. Now, use that with epsilon as the variable and f derived from that:You still have 6 variables, but now
f
is not independent ofepsilon
.