求解具有多个变量和不等式约束的多个方程 [英] solving multiple equations with many variables and inequality constraints

查看:204
本文介绍了求解具有多个变量和不等式约束的多个方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scipy和线性编程来解决许多变量的问题.我有一组变量X,它们是0.5到3之间的实数,我必须解决以下方程式:

I am trying to solve a problem with many variables using scipy and linear programming. I have a set of variables X which are real numbers between 0.5 and 3 and I have to solve the following equations :

346 <= x0*C0 + x1*C1 + x2*C2 +......xN*CN <= 468
25 <= x0*p0 + x1*p1 + x2*p2 +......xN*pN <= 33
12 <= x0*c0 + x1*c1 + x2*c2 +......xN*cN <= 17
22 <= x0*f0 + x1*f1 + x2*f2 +......xN*fN <= 30

数字C0 ... CN,p0 ... pN,c0 ... cN,f0 ... fN已经给我了.我尝试通过以下方式解决此问题:

the numbers C0...CN , p0...pN , c0...cN , f0...fN are already given to me. I tried to solve this in the following way:

import numpy as np
from scipy.optimize import linprog
from numpy.linalg import solve
A_ub = np.array([
    [34, 56, 32, 21, 24, 16, 19, 22, 30, 27, 40, 33],
    [2, 3, 2, 1.5, 3, 4, 1, 2, 2.5, 1, 1.2, 1.3],
    [1, 2, 3, 1.2, 2, 3, 0.6, 1, 1, 1.2, 1.1, 0.8],
    [0.5, 2, 2, 1, 3, 4, 1, 1, 1, 0.5, 0.3, 1.2],
    [-34, -56, -32, -21, -24, -16, -19, -22, -30, -27, -40, -33],
    [-2, -3, -2, -1.5, -3, -4, -1, -2, -2.5, -1, -1.2, -1.3],
    [-1, -2, -3, -1.2, -2, -3, -0.6, -1, -1, -1.2, -1.1, -0.8],
    [-0.5, -2, -2, -1, -3, -4, -1, -1, -1, -0.5, -0.3, -1.2]])
b_ub = np.array([468, 33, 17, 30, -346, -25, -12, -22])
c = np.array([34, 56, 32, 21, 24, 16, 19, 22, 30, 27, 40, 33])
res = linprog(c, A_eq=None, b_eq=None, A_ub=A_ub, b_ub=b_ub, bounds=(0.5, 3))

方程式的说明A_ub的第一行与b_ub相同,因为我们正在尝试使方程式最大化,并确保其在给定的边界范围内,即468和346,这意味着我想获取该值尽可能接近上限.

Explanation for the equations the first row of A_ub is the same as b_ub, as we are trying to maximize the equation as well as make sure it is within the given boundary limits i.e 468 and 346 meaning that I want to get the value as close as possible to the upper limit.

我将[-34, -56, -32, -21, -24, -16, -19, -22, -30, -27, -40, -33]放在A_ub中,将-346放在b_ub中,其逻辑是:

I put [-34, -56, -32, -21, -24, -16, -19, -22, -30, -27, -40, -33] in A_ub and -346 in b_ub with the logic :

-346 > -(x0*C0 + x1*C1 + x2*C2 +......xN*CN)将解决该方程的下界问题.我对其余的人也一样.

-346 > -(x0*C0 + x1*C1 + x2*C2 +......xN*CN) which would solve the problem of lower bounds for the equation. I do the same with the rest of them.

但是我觉得我的方法是错误的,因为我得到的答案是0.425,而nanres.x

But I feel my approach is wrong as I get the answer as 0.425 for res.fun and nan as the value of res.x

x的上限为3,下限为0.5

The upper bound for x is 3 and the lower bound is 0.5

我如何如上所述定义问题,以便在牢记上限的情况下获得接近468的最大值?如何使用scipy定义下限?我是第一次进行线性编程,所以我可能错过了可以帮助我的想法.

How do I define the problem as shown above in order to get a maximum value close to 468 while keeping in mind the upper bounds? How do I define lower bounds using scipy? I am working on linear programming for the first time so I may have missed out on ideas that can help me out.

我也愿意接受其他任何解决方案.

I am also open to any other solutions.

推荐答案

这种不平等系统是不可行的:没有一种解决方案可以满足所有约束条件.您可以从res看到这一点:

This system of inequalities is not feasible: there is no solution that satisfies all constraints. You can see that from res:

     fun: 0.42500000000000243
 message: 'Optimization failed. Unable to find a feasible starting point.'
     nit: 28
  status: 2
 success: False
       x: nan

我相信这是正确的结果(我已使用另一个LP系统对此进行了验证).

I believe this is a correct result (I verified this with another LP system).

注意:如果将边界更改为(0,3),则将获得可行的解决方案.

Note: if you change the bounds to (0,3), you will get a feasible solution.

这篇关于求解具有多个变量和不等式约束的多个方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆