Scipy最小化一个变量,同时保持某些变量持续不变 - args定义问题

发布于 2025-01-28 12:08:01 字数 1091 浏览 0 评论 0原文

我有一个功能,需要最大程度地减少C_INIT才能使TotalF输出零。问题在于,我在此功能上有一些常数,即ES,f_slong和f_c。对于30种不同的情况,我需要重复整个最小化,这意味着我有30个不同的常数变量和30个不同的C_INIT初始值。然后,我的目的是获取在算法末尾的c_init值。但是,那些不断的变量给了我麻烦。我没有任何不等式(我不确定是否必须定义它),我强烈觉得我的问题是定义 *args的位置和输入,我已经测试了许多不同的方案,但它们都失败了。有人可以帮我吗?这些常数变量应来自它们在每个迭代中的数组列表,并通过最小化函数发送。

def c_neutral_un(c_init, Es, f_slong, f_c):
    eps_s = e_cu_un * (d - c_init) / c_init
    eps_s_prime = e_cu_un * (c_init - d_prime) / c_init
    
    if Es * eps_s > f_slong:
        f_s = f_slong
    else:
        f_s = Es * eps_s
        
    if Es * eps_s_prime > f_slong:
        f_s_prime = f_slong
    else:
        f_s_prime = Es * eps_s_prime
        
    T = As * f_s
    Cs_prime = As_prime * (f_s_prime - alfa1 * f_c)
    Cc_conc = alfa1 * f_c * b * beta1 * c_init
    totalF = Cc_conc + Cs_prime - T
    return totalF
c = []
for i in range(31)
    bnd = ([0, 200])
    x0 = c_init[i]
    Es = Es[i]
    f_slong = f_slong[i]
    f_c = f_c[i]
    res = minimize(c_neutral_un, x0, args=([Es, f_slong, f_c], True) method = "SLSQP", bounds = bnd)
    c.append(res.x)


I have a function where I need to minimize c_init to make totalF output zero. the problem is that I have some constants over this function which are Es, f_slong and f_c. I need to repeat this whole minimization for 30 different cases meaning that I have 30 different constant variables and 30 different c_init initial values. Then, my aim is to obtain what the values for c_init will be at the end of the algorithm. However, those constant variables give me trouble. I do not have any inequality (I am not sure if I have to define it anyway), I strongly feel that my problem is to define the location and inputs of *args, I've tested out many different scenarios but they all failed. Could anyone help me out? Those constant variables should be coming from their list of array in every iteration and sending through the minimization function.

def c_neutral_un(c_init, Es, f_slong, f_c):
    eps_s = e_cu_un * (d - c_init) / c_init
    eps_s_prime = e_cu_un * (c_init - d_prime) / c_init
    
    if Es * eps_s > f_slong:
        f_s = f_slong
    else:
        f_s = Es * eps_s
        
    if Es * eps_s_prime > f_slong:
        f_s_prime = f_slong
    else:
        f_s_prime = Es * eps_s_prime
        
    T = As * f_s
    Cs_prime = As_prime * (f_s_prime - alfa1 * f_c)
    Cc_conc = alfa1 * f_c * b * beta1 * c_init
    totalF = Cc_conc + Cs_prime - T
    return totalF
c = []
for i in range(31)
    bnd = ([0, 200])
    x0 = c_init[i]
    Es = Es[i]
    f_slong = f_slong[i]
    f_c = f_c[i]
    res = minimize(c_neutral_un, x0, args=([Es, f_slong, f_c], True) method = "SLSQP", bounds = bnd)
    c.append(res.x)


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-02-04 12:08:01

恒定分配存在错误,例如es = es [i]

def c_neutral_un(c_init, Es, f_slong, f_c):
    eps_s = e_cu_un * (d - c_init) / c_init
    eps_s_prime = e_cu_un * (c_init - d_prime) / c_init

    if Es * eps_s > f_slong:
        f_s = f_slong
    else:
        f_s = Es * eps_s

    if Es * eps_s_prime > f_slong:
        f_s_prime = f_slong
    else:
        f_s_prime = Es * eps_s_prime

    T = As * f_s
    Cs_prime = As_prime * (f_s_prime - alfa1 * f_c)
    Cc_conc = alfa1 * f_c * b * beta1 * c_init
    totalF = Cc_conc + Cs_prime - T
    return totalF


c = []
for i in range(31):
    bnd = ([0, 200])
    x0 = c_init[i]
    Es2 = Es[i]
    f_slong2 = f_slong[i]
    f_c2 = f_c[i]
    res = minimize(c_neutral_un, x0, args=([Es2, f_slong2, f_c2], True),method = "SLSQP", bounds = bnd)
    c.append(res.x)

THere is an error for constant assignment like Es = Es[i]

def c_neutral_un(c_init, Es, f_slong, f_c):
    eps_s = e_cu_un * (d - c_init) / c_init
    eps_s_prime = e_cu_un * (c_init - d_prime) / c_init

    if Es * eps_s > f_slong:
        f_s = f_slong
    else:
        f_s = Es * eps_s

    if Es * eps_s_prime > f_slong:
        f_s_prime = f_slong
    else:
        f_s_prime = Es * eps_s_prime

    T = As * f_s
    Cs_prime = As_prime * (f_s_prime - alfa1 * f_c)
    Cc_conc = alfa1 * f_c * b * beta1 * c_init
    totalF = Cc_conc + Cs_prime - T
    return totalF


c = []
for i in range(31):
    bnd = ([0, 200])
    x0 = c_init[i]
    Es2 = Es[i]
    f_slong2 = f_slong[i]
    f_c2 = f_c[i]
    res = minimize(c_neutral_un, x0, args=([Es2, f_slong2, f_c2], True),method = "SLSQP", bounds = bnd)
    c.append(res.x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文