使用R中的线性编程将变量设置为值 [英] setting variables to a value using linear programming in r

查看:115
本文介绍了使用R中的线性编程将变量设置为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在R中开发了线性编程模型,我想知道将变量设置为值的命令,这是我的代码和结果:

I have developed a linear programming model in R and I would like to know the command to set a variable to a value, here is my code and the results:

install.packages("lpSolveAPI")
library(lpSolveAPI)

#want to solve for 6 variables, these correspond to the number of bins
lprec <- make.lp(0, 6)

lp.control(lprec, sense="max")


#MODEL 1
set.objfn(lprec, c(13.8, 70.52,122.31,174.73,223.49,260.65))

add.constraint(lprec, c(13.8, 70.52, 122.31, 174.73, 223.49, 260.65), "=", 204600)

add.constraint(lprec, c(1,1,1,1,1,1), "=", 5000)

以下是结果:

> solve(lprec)
[1] 0
> get.objective(lprec)
[1] 204600
> get.variables(lprec)
[1] 2609.309 2390.691    0.000    0.000    0.000    0.000

我想将第一个结果(2609)设置为3200,将最后一个结果设置为48,然后对其他变量进行优化,我们将不胜感激.

I would like to set the first result (2609) to 3200,and the last result to 48, and then optimize on the other variables, any help would be much appreciated.

推荐答案

理想情况下,您的期望是针对约束优化,您应根据需要为其添加更多约束.我不熟悉lpSolveAPI,因此无法进行正确的编码,但您需要使用类似的东西:

Ideally your expectation is for constrained optimization for which you should add more constraints as per your requirement. I am not familiar with lpSolveAPI and so not able to do correct coding but you need something like:

add.constraint(lprec, c(1, 0, 0, 0, 0, 0), "=", 3200)
add.constraint(lprec, c(0, 0, 0, 0, 0, 1), "=", 48)

伴随您现有的约束条件.

Along with your existing constraints.

这篇关于使用R中的线性编程将变量设置为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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