[英]How to create an objective function for scipy minimize
Right, apologies for the abundance of questions, but I'm new to Python and I have been struggling. 是的,对很多问题深表歉意,但是我是Python的新手,我一直在努力。
I believe I've finally created a somewhat functional code. 我相信我终于创建了一个有点功能的代码。 However, I cannot seem to define the objective function properly.
但是,我似乎无法正确定义目标函数。 The rest seems to be calculating correctly (based on the values the objective gives me).
其余的似乎计算正确(基于目标给我的值)。 This is my objective function right now:
这是我目前的目标功能:
def objective (x):
global sumIp, sumIm
if (It[i-1] - d[i] + Qt[i-LT]) >= 0:
sumIp = sumIp + x[2]
sumIm = sumIm + 0
else:
sumIp = sumIp + 0
sumIm = sumIm - x[2]
return h*sumIp+b*sumIm
x[2] is meant to be my It[i]. x [2]就是我的It [i]。 sumIp and sumIm should both be >= 0.
sumIp和sumIm都应> = 0。
Here is the full code if someone wants to take a look: https://pastebin.com/AxC7fTVv - I believe this is the only part I'm missing to achieve what I want, but I can't figure out how to do it for the life of me, been around this for days! 如果有人想看一下,这是完整的代码: https : //pastebin.com/AxC7fTVv-我相信这是我所缺少的唯一实现我想要的部分的方法,但是我不知道该怎么做对于我的一生来说,已经有好几天了! Any help appreciated.
任何帮助表示赞赏。
Okay, so. 可以,然后呢。 I'm going to summarize your problem (kind of for you, but mostly to help me :p).
我将总结您的问题(很适合您,但主要是为了帮助我:p)。
You have a sequence of values you want to calculate, which all revolve around figuring out Qt[i]
. 您有一个要计算的值序列,所有这些都围绕找出
Qt[i]
。 Those are: 那些是:
d[i]
- some list of values provided externally in a "real-world" scenario, but for your purposes are emulated with random values; d[i]
-在“真实世界”场景中从外部提供的一些值列表,但出于您的目的,使用随机值进行仿真; most importantly, it isn't something that has to be calculated . d[i+1]
, or anything like that.) d[i+1]
或类似的东西。) It[i]
- given by It[i] = It[i-1] - d[i] + Qt[i-LT]
(with the Qt
part omitted for i < LT
); It[i]
-由It[i] = It[i-1] - d[i] + Qt[i-LT]
(对于i < LT
省略了Qt
部分); this is calculated from prior-cycle values and the d
values, so this can be easily calculated d
值计算得出的 ,因此可以轻松计算出 Ip[i]
, Im[i]
- these are both calculated directly from It[i]
, so again, easy to calculate Ip[i]
, Im[i]
-它们都是直接从It[i]
计算而来的,因此再次易于计算 NIt[i]
- given by NIt[j] = NIt[j-1] - d[j] + Qt[j-1]
, and can easily be calculated similarly to It[i]
NIt[i]
-由NIt[j] = NIt[j-1] - d[j] + Qt[j-1]
, 可以像 It[i]
一样容易地计算 Qt[i]
- ...? Qt[i]
-...? In short: the only thing that needs to be figured out is Qt[i]
. 简而言之:唯一需要弄清楚的是
Qt[i]
。 So if you do decide to use an optimizer like scipy.minimize
, the only variable you need is x[0]
. 因此,如果您决定使用像
scipy.minimize
这样的优化scipy.minimize
,则唯一需要的变量是x[0]
。 But if you only have one variable, chances are you don't even need to use an optimizer; 但是,如果您只有一个变量,则可能甚至不需要使用优化器。 more likely you can come up with some function/expression that gives you an optimized result directly.
您更有可能提出一些可以直接为您提供最佳结果的函数/表达式。
...not quite sure yet :\\ sorry ...还不确定:\\对不起
Note - I'm making changes off of the first pastebin copy, as linked in the question description. 注意-我正在对第一个 pastebin副本进行更改,如问题描述中所链接。
Try this: remove the global
statement, so the objective
function looks like 试试这个:删除
global
语句,这样objective
函数看起来像
def objective (x):
# [`global` removed here]
if (It[i-1] - d[i] + Qt[i-LT]) >= 0:
sumIp = sumIp + x[2]
sumIm = sumIm + 0
else:
sumIp = sumIp + 0
sumIm = sumIm - x[2]
return h*sumIp+b*sumIm
This way, the sumIp
and sumIm
values for the x[2]
at that moment are created locally on every objective
call, instead of edited globally. 这样,
x[2]
的那一刻的sumIp
和sumIm
值是在每个objective
调用上本地创建的 ,而不是全局编辑的。 (You may want to rename the local variables, to avoid confusion.) (您可能希望重命名局部变量,以避免造成混淆。)
Then, after minimize
is finished, you push the changes for the final, optimal x[2]
value, like so: 然后,在
minimize
完成后,将更改推送到最终的最佳x[2]
值,如下所示:
def test(T):
global i
while i < T:
# [...]
sol = minimize(objective, x0, constraints=cons)
if (It[i-1] - d[i] + Qt[i-LT]) >= 0:
sumIp = sumIp + sol.x[2]
sumIm = sumIm + 0
else:
sumIp = sumIp + 0
sumIm = sumIm - sol.x[2]
# [...]
i += 1
return Qt, NIt, It
Okay so. 可以,然后呢。
x[0] == Qt[i]
, not Qt[i-1]
, right? x[0] == Qt[i]
, 不是 Qt[i-1]
吧? If so, then you can't swap Qt[i-1]
and x[0]
trivially. 如果是这样,那么您将无法轻松地交换
Qt[i-1]
和x[0]
。 Also the fact that the optimizer stops doing things when you remove x[0]
makes sense; 另外,当您删除
x[0]
时,优化器会停止工作的事实也很有意义; the only thing it's allowed to change when trying to minimize your expressions are the x
values, and it you remove them then the minimizer has less that it's allowed to do. 尝试最小化表达式时,唯一可以更改的是
x
值,将其删除后,最小化器的允许执行的操作就更少了。
Regarding the general "strange[ness]", it might have something to do with the fact that the constraints use if
-statements, which basically makes them piecewise functions. 关于一般的“ strange [ness]”,这可能与约束使用
if
-statement有关,这实际上使它们成为了分段函数。 While there are minimization methods that work on non-linear constraints, I'm not sure if there are methods that work on non-differentiable constraints. 尽管有一些针对非线性约束的最小化方法,但是我不确定是否有一些针对非微分约束的方法。
To fix that, see the changes I made in this paste . 要解决此问题,请参阅我在此粘贴中所做的更改。 I replaced
x[2]
with two strictly-non-negative variables, x[2]
and x[3]
, where the old value is now x[2] - x[3]
. 我用两个严格非负的变量
x[2]
和x[3]
替换了x[2]
,其中旧值现在是x[2] - x[3]
。 This eliminates the need for if-statements in the objective. 这样就无需在目标中使用if语句。 To make the variables non-negative, I added boundary conditions to the problem with
x_bounds
. 为了使变量为非负数,我使用
x_bounds
为问题添加了边界条件。 (Note that this obviates the need for a constraint1
function, so I removed it. There's much more room for other simplifications in the code, but as it's not necessary I left everything else alone.) (请注意,这消除了对
constraint1
函数的需要,因此我将其删除。代码中还有更多的空间可以进行其他简化,但是由于没有必要我将其他所有内容都留了下来。)
So the only part that's left that I don't quite get is constraint2
: can you explain again what it's supposed to be doing? 因此,剩下的我还不太了解的部分是
constraint2
:您能否再次说明它应该做什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.