查找线性系统的最小和最大
因此,我正在尝试理解我的课堂练习中的一个方案,即找到功能的最大值和最小值。我有两个矢量W和V的权重,总计为1。向量为w = [0.6,0.2,0.2]^t v = [0.8,-0.2,0.4
]重量m = aw + bv,a和b必须总和1。 然后,我们优化的功能为r = [0.1,0.2,0.1]•m的
约束如下:0≤(0≤(0.6a + 0.8b)< = 1,0≤(0.2a -0.2a -0.2b)< = 1,0≤(0.2a + 0.4b)< = 1
我们应该得到的答案是a = b = .5,对于R的最小值为0.1。对于最大值,我们应该得到a = 2,b = -1,r = 0.16。但是我要获得的最大值的值为a = 3.5714286,b = -1.4285714,在最小值中,我得到了a = b = 0。
下面是代码。
import pulp as p
from pulp import *
problem = LpProblem('Car Factory', LpMaximize)
A = LpVariable('Amound of w', cat=LpContinuous)
B = LpVariable('Amount of v', cat=LpContinuous)
#Objective Function
problem += (0.1)*(0.6*A + 0.8*B) + (0.2)*(0.2*A - 0.2*B) + (0.1)*(0.2*A + 0.4*B) , 'Objective Function'
#Constraints
problem += (0.6*A + 0.8*B) <= 1 , 'A'
problem += (0.6*A + 0.8*B) >= 0 , 'AL'
problem += (0.2*A - 0.2*B) <= 1, 'B'
problem += (0.2*A - 0.2*B) >= 0, 'BL'
problem += (0.2*A + 0.4*B) <= 1, 'C'
problem += (0.2*A + 0.4*B) >= 0, 'CL'
problem.solve()
print("Amount of w: ", A.varValue)
print("Amount of v: ", B.varValue)
print("total: ", value(problem.objective))
我敢肯定,这与我没有看到的设置有关。还有一种更有效的方法将它们放在一起吗?
So I'm trying to make sense of a scenario in my class exercise which is to find the max and min value of a function. I have two vectors, w and v, of weights which are to sum to 1. The vectors are w = [0.6, 0.2, 0.2]^T v = [0.8, -0.2, 0.4]^T
These vectors form a linear combination of weights M = Aw + Bv, and A and B must sum to 1.
The function we are then optimizing is r = [0.1, 0.2, 0.1] • M
The constraints are as follows: 0 ≤ (0.6A + 0.8B) <= 1 , 0 ≤ (0.2A - 0.2B) <= 1 , 0 ≤ (0.2A + 0.4B) <= 1
The answer we should get are A = B = .5 for the minimum value of r which is 0.1. For the maximum we should get A = 2, B = -1 with r = 0.16. But the values I'm getting for the max are A = 3.5714286, B = -1.4285714, and for the min I'm getting A = B = 0.
Below is the code.
import pulp as p
from pulp import *
problem = LpProblem('Car Factory', LpMaximize)
A = LpVariable('Amound of w', cat=LpContinuous)
B = LpVariable('Amount of v', cat=LpContinuous)
#Objective Function
problem += (0.1)*(0.6*A + 0.8*B) + (0.2)*(0.2*A - 0.2*B) + (0.1)*(0.2*A + 0.4*B) , 'Objective Function'
#Constraints
problem += (0.6*A + 0.8*B) <= 1 , 'A'
problem += (0.6*A + 0.8*B) >= 0 , 'AL'
problem += (0.2*A - 0.2*B) <= 1, 'B'
problem += (0.2*A - 0.2*B) >= 0, 'BL'
problem += (0.2*A + 0.4*B) <= 1, 'C'
problem += (0.2*A + 0.4*B) >= 0, 'CL'
problem.solve()
print("Amount of w: ", A.varValue)
print("Amount of v: ", B.varValue)
print("total: ", value(problem.objective))
I'm sure it has to do with the set up which I'm just not seeing. And also is there a more efficient way to put this together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您缺少一个约束,这将解释您与预期结果的偏差。您的约束在哪里:
另外,您将两次导入
纸浆
,这可能会在代码的名称空间中引起一些混乱。做一个或另一个,而不是两者。在更有效地表达问题...?不。您可以将您的两个列向量视为长度3的阵列,并且在目标中进行数学的方式有所不同,但这可能不值得,您的变量只是标量,所以我会写就像你一样。现在,如果向量更大,或者变量是向量,那么当然,我会做其他事情。
纸浆
自然不会处理向量(例如numpy
数组)。如果您要在矢量 - 矩阵格式中进行大量优化,并且对线性代数感到满意,则可以查看cvxpy
自然处理它们。如果您正在使用纸浆
的课程,那么学习基础知识就可以了。I think you are missing a constraint, which would explain your deviation from the expected result. Where is your constraint that:
Also, you are importing
pulp
twice, which may cause some confusion in the namespace of your code. Do one or the other, not both.On expressing the problem more efficiently...? Nahh. You could treat your two column vectors as arrays of length 3 and do the math in your objective a bit differently, but it probably isn't worth it and your variables are just scalars, so I'd write it as you did. Now if the vectors were much larger, or if the variables were vectors, sure, I'd do something else.
pulp
doesn't naturally handle vectors (likenumpy
arrays) to my knowledge. If you are going to be doing a lot of optimization in vector-matrix format and you are comfortable with the linear algebra, you might look atcvxpy
which handles them naturally. If you're in a class that usespulp
, it's just fine to learn the basics.