lpsolve java_如何使用LpSolve在R中设置线性编程优化?

您可以使用lpSolveAPI来解决您的问题 . 鉴于您的约束,您声明的解决方案并不可行 . 所以,让我们一起想要让X和Y在解决方案中不再重复 .

您将需要8个新的二进制变量 . 每个变量指定 d 中的该行是被拾取(1)还是被丢弃(0) .

根据OP的请求更新

是的,lpSolveAPI代码(下面)使它看起来比实际上更复杂 . 这个LP公式(lpSolveAPI的输出)应该让事情更清晰:

/* Objective function */

max: +pick_1 +2 pick_2 +3 pick_3 +4 pick_4 +5 pick_5 +6 pick_6 +7 pick_7 +8 pick_8;

/* Constraints */

OneX_1: +pick_1 +pick_2 +pick_3 <= 1;

OneX_2: +pick_4 +pick_5 <= 1;

OneX_4: +pick_7 +pick_8 <= 1;

OneY_5: +pick_1 +pick_6 +pick_8 <= 1;

OneY_6: +pick_2 +pick_7 <= 1;

OneY_7: +pick_3 +pick_5 <= 1;

/* Variable bounds */

pick_1 <= 1;

pick_2 <= 1;

pick_3 <= 1;

pick_4 <= 1;

pick_5 <= 1;

pick_6 <= 1;

pick_7 <= 1;

pick_8 <= 1;

说明:第二个约束(OneX_2)仅表示 pick_4 或 pick_5 中只有一个可以为1,因为数据框 d 中的第4行和第5行具有X = 2

解决方案

请注意,上面的公式产生了一个在数据框中选择4行的最佳解决方案 d

> d[c(3,4,6,7),]

x y w

3 1 7 3

4 2 8 4

6 3 5 6

7 4 6 7

w的总和是20,这比问题中的解决方案更好 .

代码

library(lpSolveAPI)

d

ncol

lp_rowpicker

set.type(lp_rowpicker, columns=1:ncol, type = c("binary"))

obj_vals

set.objfn(lp_rowpicker, obj_vals)

lp.control(lp_rowpicker,sense='max')

#Add constraints to limit X values from repeating

add.constraint(lp_rowpicker, xt=c(1,1,1), #xt specifies which rows of the LP

indices=c(1,2,3), rhs=1, type="<=")

add.constraint(lp_rowpicker, xt=c(1,1), #xt specifies which rows of the LP

indices=c(4,5), rhs=1, type="<=")

add.constraint(lp_rowpicker, xt=c(1,1), #xt specifies which rows of the LP

indices=c(7,8), rhs=1, type="<=") #x's in dataframe rows 7 & 8 are both '4'

#Add constraints to limit Y values from repeating

add.constraint(lp_rowpicker, xt=c(1,1,1), #xt specifies which rows of the LP

indices=c(1,6,8), rhs=1, type="<=") #Y's in df rows 1,6 & 8 are all '5'

add.constraint(lp_rowpicker, xt=c(1,1), #xt specifies which rows of the LP

indices=c(2,7), rhs=1, type="<=") #Y's in dataframe rows 2&7 are both '6'

add.constraint(lp_rowpicker, xt=c(1,1), #xt specifies which rows of the LP

indices=c(3,5), rhs=1, type="<=") #y's in dataframe rows 3&5 are both '7'

solve(lp_rowpicker)

get.objective(lp_rowpicker) #20

get.variables(lp_rowpicker)

#[1] 0 0 1 1 0 1 1 0

#This tells you that from d you pick rows: 3,4,6 & 7 in your optimal solution.

#If you want to look at the full formulation:

rownames1

rownames2

colnames

dimnames(lp_rowpicker)

print(lp_rowpicker)

#write it to a text file

write.lp(lp_rowpicker,filename="max_w.lp")

希望这能让您了解如何使用lpSolveAPI来制定您的问题 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值