用三个变量优化

时间:2016-05-25 10:10:06

标签: r optimization mathematical-optimization linear-programming constraint-programming

我有一个方程式,其中包含三个变量到变量的不同范围

f(x)= 150*x + 92*y + 41,1*z -> max

的约束
x > 0 & x < 600
x > 0 & x < 600
x+y+z <600
if x<200 or y<200 or x+y <200 -> z=0

我想找到变量的最大值。我对R的了解太小,无法自己解决。

1 个答案:

答案 0 :(得分:1)

您可以将问题分为z=0z>=0。对于第二个子问题,您可以设置并解决这个问题:

library(lpSolveAPI)

# create object
lprec <- make.lp(0,3)
invisible(lp.control(lprec, sense="max")) # sense defaults to "min" 

# add objective and constraints
set.objfn(lprec, obj=c(150,92,41.1), indices=c(1,2,3)) 
add.constraint(lprec, 1, type="<=", rhs=600, indices=1)
add.constraint(lprec, 1, type=">=", rhs=200, indices=1)
add.constraint(lprec, 1, type="<=", rhs=600, indices=2)
add.constraint(lprec, 1, type=">=", rhs=200, indices=2)
add.constraint(lprec, c(1,1,1), type="<=", rhs=600, indices=c(1,2,3))
add.constraint(lprec, c(1,1), type=">=", rhs=200, indices=c(1,2))

# solve
print(lprec)
solve(lprec)
get.variables(lprec)

请注意,我认为您的问题意味着0<=y<=600。另请注意,设置“&lt;”并不是非常有用(或可能)约束,使用“&lt; =”要好得多。最后,请注意lpSolveAPI默认假定决策变量的非负性,因此假设为z>=0

第一个子问题可以类似地解决。如您所知,在这种情况下z=0,决策变量的数量会减少到2

问题的解决方案是两个子解决方案中最好的。正如@RHertel已经评论过的那样,这是您尽可能多地放在x上的解决方案,因此yz为0。