Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的单纯形优化得到目标向量_R_Object_Vector_Simplex Algorithm - Fatal编程技术网

从R中的单纯形优化得到目标向量

从R中的单纯形优化得到目标向量,r,object,vector,simplex-algorithm,R,Object,Vector,Simplex Algorithm,我开始使用R来解决LP问题,使用库中的simplex函数(“boot”)。当我尝试下面的代码时,我只得到目标向量作为字符串,而不是向量 library("boot") # This example is taken from Exercise 7.5 of Gill, Murray and Wright (1991). enj <- c(200, 6000, 3000, -200) fat <- c(800, 6000, 1000, 400) vitx <- c(50, 3,

我开始使用R来解决LP问题,使用库中的simplex函数(“boot”)。当我尝试下面的代码时,我只得到目标向量作为字符串,而不是向量

library("boot") 
# This example is taken from Exercise 7.5 of Gill, Murray and Wright (1991).
enj <- c(200, 6000, 3000, -200)
fat <- c(800, 6000, 1000, 400)
vitx <- c(50, 3, 150, 100)
vity <- c(10, 10, 75, 100)
vitz <- c(150, 35, 75, 5)
j<-simplex(a = enj, A1 = fat, b1 = 13800, A2 = rbind(vitx, vity, vitz),
b2 = c(600, 300, 550), maxi = TRUE)
xx<-(j["soln"])
print(xx)

但是,这不是一个向量对象,因此我无法使用
x[1]
获得第一维度的元素。你能帮我把结果作为一个向量吗?

你的变量
xx
是一个列表,因此很容易在其中返回一个特定的值

例如,如果需要x1,可以执行以下操作:

xx2 <- unlist(xx)
xx[[“soln”][“x1”]
xx[[1]][1]

但是,如果要将
xx
转换为向量,可以执行以下操作:

xx2 <- unlist(xx)
如果不想保留向量中每个元素的名称,请将arg
use.names
置于
FALSE

> xx2 <- unlist(xx, use.names = FALSE)
> xx2
[1]  0.0  0.0 13.8  0.0

> is.vector(xx2)
[1] TRUE
>xx2 xx2
[1]  0.0  0.0 13.8  0.0
>is.向量(xx2)
[1] 真的

您的变量
xx
是一个列表,因此很容易在其中返回特定值

例如,如果需要x1,可以执行以下操作:

xx2 <- unlist(xx)
xx[[“soln”][“x1”]
xx[[1]][1]

但是,如果要将
xx
转换为向量,可以执行以下操作:

xx2 <- unlist(xx)
如果不想保留向量中每个元素的名称,请将arg
use.names
置于
FALSE

> xx2 <- unlist(xx, use.names = FALSE)
> xx2
[1]  0.0  0.0 13.8  0.0

> is.vector(xx2)
[1] TRUE
>xx2 xx2
[1]  0.0  0.0 13.8  0.0
>is.向量(xx2)
[1] 真的