如何在目标函数python中放置一个不定的积分
我有这个目标函数,我想最大程度地减少“ t”,而在此功能中,有一个从0到t的积分必须考虑到优化。但是,我不知道如何包括我的目标功能。我已经使用了小巧而易碎的库,但是它们似乎都没有用。 这是我的代码:
Cf= 100;
Cp=50;
Eta=72.66585511711865;
Beta=1.18609324
def R(t):
return math.exp(-t/Eta)**Beta
def f(t):
return (Beta/(Eta**Beta))*t**(Beta-1)*(math.exp(-t/Eta)**Beta)
def M(t):
return t*f(t)
import sympy as sp
from scipy import *
import scipy.integrate
t = sp.Symbol('t')
Mt=t*(Beta/(Eta**Beta))*t**(Beta-1)*(sp.exp(-t/Eta)**Beta);
intM=sp.integrate(Mt,t)
print(intM)
def C(t):
return (Cp*R(t)+Cf*(1-R(t)))/(t*R(t)+ intM)
from scipy.optimize import minimize_scalar
res = minimize_scalar(C)
res
这给了我错误“ TypeError:无法确定关系的真实价值”。 在scipy.integrate.quad中,我无法将上限定义为“ t”。另一方面,当我尝试将SP.集成输出放在目标函数上时,它将无法使用。 如果您能提供帮助,我会很高兴。 谢谢。
I have this objective function that I want to minimize in regards to 't', and in this function, there is an integral from 0 to t that has to be considered for the optimization. However, I have no Idea how to include this integral to my objective function. I've used simpy and scipy library for that but none of them seems to work.
here is my code:
Cf= 100;
Cp=50;
Eta=72.66585511711865;
Beta=1.18609324
def R(t):
return math.exp(-t/Eta)**Beta
def f(t):
return (Beta/(Eta**Beta))*t**(Beta-1)*(math.exp(-t/Eta)**Beta)
def M(t):
return t*f(t)
import sympy as sp
from scipy import *
import scipy.integrate
t = sp.Symbol('t')
Mt=t*(Beta/(Eta**Beta))*t**(Beta-1)*(sp.exp(-t/Eta)**Beta);
intM=sp.integrate(Mt,t)
print(intM)
def C(t):
return (Cp*R(t)+Cf*(1-R(t)))/(t*R(t)+ intM)
from scipy.optimize import minimize_scalar
res = minimize_scalar(C)
res
this gives me error " TypeError: cannot determine truth value of Relational" .
in scipy.integrate.quad, I can't define my upper limit as 't'. And on the other hand, it won't work when I try to put the sp.integrate output to the objective function.
I would be happy if u could help.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不能在优化例程中具有不可定力的积分,因为它定义为常数。不是功能,而是一类函数。
但是看来您的一个确定的积分 - 您可以通过某些虚拟变量(例如T',从t'= 0到t'= t进行集成。所以这很好,您只需要这样做即可。
首先,让我们做出确定的积分:
这仍然取决于
t
,现在让我们将其变成数值函数:然后粘贴
intm_func(t)
而不是符号intm
进入您的c
最小功能,您应该很好地走:)对我有用的完整版本:
我得到:
作为输出。
First up, you can't have an indefinite integral within an optimization routine, as that's defined up to a constant. Not a function, but a class of functions.
But it looks like yours is a definite integral - you integrate it over some dummy variable, say, t', from t'=0 to t'=t. So that's perfectly fine, and you just have to do it.
First, let's do your definite integral:
This still depends on
t
, so now let's turn it into a numerical function:Then stick
intM_func(t)
rather than the symbolicintM
into yourC
minimizable function and you should be good to go :)The full version that works for me:
And I get:
as an output.
我很想“关闭”此问题,因为您没有给出完整的错误 - 带有追溯。但是看来您有一个答案,所以我只是尝试将错误集中在您给出的时。
这类似于带有numpy数组的常见“歧义误差”。有些事情正在尝试执行简单的true/false操作,例如,例如
。 Scipy函数可能正在测试范围值是否处于正确的顺序。或者也许在您的
func
中发生错误。如果没有追溯,我们就可以猜测或从以前的知识中推断出来。因此,您很幸运有人已经熟悉了这些问题。只是不要指望这一点!要说明:
在
isympy
会话中,请考虑一个符号:和一个关系:
将该关系置于`if:
sympy
无法从一个人返回简单的true/false关系表达。使用
Sympy
和Scipy
一起需要大量了解两者的工作方式。以数字函数的方式以不起作用的方式使用sympy表达式非常容易。这是缺少的追溯:
因此,正如我所怀疑的那样,
scipy
正在尝试检查范围;c
被给出了一个简单的标量参数会产生sympy
表达式 - 具有未化为的积分。intm
您忘了向我们展示:I tempted to 'close' this because you did not give the full error - with traceback. But it appears you got an answer, so I'll just try to focus the error as you gave it.
This is similar to a common "ambiguity error" with numpy arrays. Something is trying to do a simple True/False action, such as a
if
. The scipy function might be testing whether the range values are in correct order. Or maybe the error occurs in yourfunc
. Without traceback we are left to guessing, or deducing from previous knowledge. So you are lucky that someone was already familiar with these issues. Just don't count on that!To illustrate:
In an
isympy
session, consider a symbol:And a relational:
Putting that relational in an `if:
sympy
can't return a simple True/False from a Relational expression.Using
sympy
andscipy
together requires considerable knowledge of how both work. It's all too easy to using sympy expressions in a numeric function in ways that don't work.Here's the missing traceback:
So as I suspected,
scipy
is trying to check ranges;C
is given a simple scalar argument produces asympy
expression - with an unevaluated integral.That
intM
that you forgot to show us: