简体   繁体   English

如何创建用于最小化目标的函数

[英]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. 任何帮助表示赞赏。

Summary 摘要

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 . 最重要的是, 它不必计算 (Another note: I'm assuming we can't "see into the future" and use 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. 您更有可能提出一些可以直接为您提供最佳结果的函数/表达式。

Solution

...not quite sure yet :\\ sorry ...还不确定:\\对不起

Comment 1 评论1

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]的那一刻的sumIpsumIm值是在每个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

Comment 2 评论2

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.

相关问题 Scipy最小化-无法最小化目标函数 - Scipy Minimize - Unable to minimize objective function Scipy.optimize.minimize目标函数ValueError - Scipy.optimize.minimize objective function ValueError Scipy 最小化不遵守约束和不最小化目标 function - Scipy minimize not obeying the constraint and not minimizing the objective function 使用Scipy最小化最小化功能 - Minimize function with Scipy minimize 将约束应用于scipy.optimize.minimize?中的目标函数? 如0 &lt;=目标&lt;= 1 - Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1 当你想要计算梯度和目标函数时,如何使用scipy.optimize.minimize函数? - How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function? 除了自变量外,如何为scipy.optimize.minimize的目标函数提供附加输入 - How to give additional input to objective function of scipy.optimize.minimize other than independent variables 当目标函数有多个参数时,如何使用 scipy.optimize minimum_scalar? - How to use scipy.optimize minimize_scalar when objective function has multiple arguments? Scipy.Minimize,如何在目标function和约束之间共享相同的对象? - Scipy.Minimize, how to share the same objects among objective function and constraints? 如何使用 scipy.optimize.minimize() 指定目标 function 最小化的参数? - How to specify the parameter an objective function is minimized with respect to, using scipy.optimize.minimize()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM