R LPSolve返回错误“;没有找到可行的解决方案”;

R LPSolve返回错误“;没有找到可行的解决方案”;,r,lpsolve,R,Lpsolve,我一直在关注LPSolve,这是我过去使用过的,但现在我得到的结果是“错误:没有找到可行的解决方案” 我的data.table如下所示: Position Name...ID Name ID Roster.Position Salary Game.Info TeamAbbrev AvgPointsPerGame 1 G Ben Bishop (11402

我一直在关注LPSolve,这是我过去使用过的,但现在我得到的结果是“错误:没有找到可行的解决方案”

我的data.table如下所示:

Position                    Name...ID              Name       ID Roster.Position Salary                     Game.Info TeamAbbrev AvgPointsPerGame
1        G        Ben Bishop (11402606)        ben bishop 11402606               G   8400 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.40
2        G    Anton Khudobin (11402601)    anton khudobin 11402601               G   8200 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.36
3        G Connor Hellebuyck (11402708) connor hellebuyck 11402708               G   7700 WPG@STL 10/04/2018 08:00PM ET        WPG             5.42
4        C      Tyler Seguin (11402648)      tyler seguin 11402648          C/UTIL   7700 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.75
5        G  Laurent Brossoit (11402681)  laurent brossoit 11402681               G   7500 WPG@STL 10/04/2018 08:00PM ET        WPG             2.26
6        G       Eric Comrie (11402730)       eric comrie 11402730               G   7500 WPG@STL 10/04/2018 08:00PM ET        WPG             2.47
从那里我改成

New_proj$Position <- as.factor(New_proj$Position)
New_proj$Salary <-as.numeric(New_proj$Salary)


#### Prepare constraint matrix of zeros #####
A <- matrix(0, nrow = 6, ncol = nrow(New_proj))

#Designate the positions that are equivalent to each other when generating the optimal lineup
#There are 7 distinct positions and 1 constraint in which salary is < 50,000
#I.e. A player with the position 1B/2B can fill the 1B or the 2B position
#Add a "1" to all position that can fill that position slot


#Set C parameters
j<-1
i<-1
for (i in 1:nrow(New_proj)){
  if (New_proj$Position[i]=="C"   )
    A[j,i]<-1
}
#W
j<-2
i<-1
for (i in 1:nrow(New_proj)){
  if (New_proj$Position[i]=="LW"    || 
      New_proj$Position[i]=="RW" )
    A[j,i]<-1
}
#D
j<-3
i<-1
for (i in 1:nrow(New_proj)){
  if (New_proj$Position[i]=="D"   )
    A[j,i]<-1
}
#G
j<-4
i<-1
for (i in 1:nrow(New_proj)){
  if (New_proj$Position[i]=="G"    )
    A[j,i]<-1
}
#U
j<-5
i<-1
for (i in 1:nrow(New_proj)){
  if (New_proj$Position[i]=="C"    || 
      New_proj$Position[i]=="LW" || 
      New_proj$Position[i]== "RW"||
      New_proj$Position[i]=="D" )
    A[j,i]<-1
}


A[6, ] <- New_proj$Salary                # salary <= 50000

# Prepare input for LP solver
objective.in <- New_proj$AvgPointsPerGame
const.mat <- A[]
const.dir <- c("==", "==", "==", "==","==", "<=")
const.rhs <- c(2, 3, 2, 1,1, 50000)

# Generate optimal lineup with lp solve
require(lpSolve)
sol <- lp(direction = "max", objective.in, # maximize objective function
          const.mat, const.dir, const.rhs   # constraints
          )                    # use binary variables only

### View the solution
inds <- which(sol$solution == 1)
 Error: object 'dataset' not found

 sol
 Error: no feasible solution found

新建项目$Position编辑:

问题是第五个约束。我注意到在
A
的第5行中有更多的1,因此我想在那里很难找到解决方案,因为对于解决方案,您只能将其中一个变量设置为
1

rowSums(A[1:5,])
[1]  28  56  47  17 131
如果您查看
LHS
,问题会变得更清楚。如果
A[5,]
中的系数(即第五个约束)为
1
,则只能将1个自由变量设置为
1
。因此,假设您已经将这些变量中的1个设置为1,您仍然需要满足约束
1
3
。但是:这些行中只有
o
系数,因此您将无法满足这些约束

A[,A[5,]==0]

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
[1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[4,]    1    1    1    1    1    1    1    1    1     1     1     1     1     1     1     1     1
[5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[6,] 8400 8200 7700 7500 7500 7400 7200 7100 6900  6500  6500  6500  6500  6500  6500  6500  6500
如果仅使用此约束,则问题不可行。如果使用除此之外的所有约束,则它是可行的。此外,如果将第5个约束的RHS更改为
2
,则问题也变得可行

原始答案:

这是你的问题的数学公式:

Objective Function:
4.4*x1 + 4.36*x2 + 5.42*x3 + 4.75*x4 + 2.26*x5 + 2.47*x6

Constraints: 

0*x1 + 0*x2 + 0*x3 + 1*x4 + 0*x5 + 0*x6 == 2
0*x1 + 0*x2 + 0*x3 + 0*x4 + 0*x5 + 0*x6 == 3
0*x1 + 0*x2 + 0*x3 + 0*x4 + 0*x5 + 0*x6 == 2
1*x1 + 1*x2 + 1*x3 + 0*x4 + 1*x5 + 1*x6 == 1
0*x1 + 0*x2 + 0*x3 + 1*x4 + 0*x5 + 0*x6 == 1
8400*x1 + 8200*x2 + 7700*x3 + 7700*x4 + 7500*x5 + 7500*x6 <= 50000
目标函数:
4.4*x1+4.36*x2+5.42*x3+4.75*x4+2.26*x5+2.47*x6
限制条件:
0*x1+0*x2+0*x3+1*x4+0*x5+0*x6==2
0*x1+0*x2+0*x3+0*x4+0*x5+0*x6==3
0*x1+0*x2+0*x3+0*x4+0*x5+0*x6==2
1*x1+1*x2+1*x3+0*x4+1*x5+1*x6==1
0*x1+0*x2+0*x3+1*x4+0*x5+0*x6==1

8400*x1+8200*x2+7700*x3+7700*x4+7500*x5+7500*x6编辑:

问题是第五个约束。我注意到在
A
的第5行中有更多的1,因此我想在那里很难找到解决方案,因为对于解决方案,您只能将其中一个变量设置为
1

rowSums(A[1:5,])
[1]  28  56  47  17 131
如果您查看
LHS
,问题会变得更清楚。如果
A[5,]
中的系数(即第五个约束)为
1
,则只能将1个自由变量设置为
1
。因此,假设您已经将这些变量中的1个设置为1,您仍然需要满足约束
1
3
。但是:这些行中只有
o
系数,因此您将无法满足这些约束

A[,A[5,]==0]

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
[1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[4,]    1    1    1    1    1    1    1    1    1     1     1     1     1     1     1     1     1
[5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0
[6,] 8400 8200 7700 7500 7500 7400 7200 7100 6900  6500  6500  6500  6500  6500  6500  6500  6500
如果仅使用此约束,则问题不可行。如果使用除此之外的所有约束,则它是可行的。此外,如果将第5个约束的RHS更改为
2
,则问题也变得可行

原始答案:

这是你的问题的数学公式:

Objective Function:
4.4*x1 + 4.36*x2 + 5.42*x3 + 4.75*x4 + 2.26*x5 + 2.47*x6

Constraints: 

0*x1 + 0*x2 + 0*x3 + 1*x4 + 0*x5 + 0*x6 == 2
0*x1 + 0*x2 + 0*x3 + 0*x4 + 0*x5 + 0*x6 == 3
0*x1 + 0*x2 + 0*x3 + 0*x4 + 0*x5 + 0*x6 == 2
1*x1 + 1*x2 + 1*x3 + 0*x4 + 1*x5 + 1*x6 == 1
0*x1 + 0*x2 + 0*x3 + 1*x4 + 0*x5 + 0*x6 == 1
8400*x1 + 8200*x2 + 7700*x3 + 7700*x4 + 7500*x5 + 7500*x6 <= 50000
目标函数:
4.4*x1+4.36*x2+5.42*x3+4.75*x4+2.26*x5+2.47*x6
限制条件:
0*x1+0*x2+0*x3+1*x4+0*x5+0*x6==2
0*x1+0*x2+0*x3+0*x4+0*x5+0*x6==3
0*x1+0*x2+0*x3+0*x4+0*x5+0*x6==2
1*x1+1*x2+1*x3+0*x4+1*x5+1*x6==1
0*x1+0*x2+0*x3+1*x4+0*x5+0*x6==1

8400*x1+8200*x2+7700*x3+7700*x4+7500*x5+7500*x6我希望有人会提到数据样本没有显示足够的位置玩家,但有很多:str(新项目$position)系数w/5级“C”、“D”、“G”、“LW”…:3 3 3 3 3 3 3 3 3 4 3…我希望有人会提到数据样本没有显示足够的位置玩家,但是有很多:str(新项目$Position)系数w/5级“C”、“D”、“G”、“LW”…:3 3 3 3 3 4 3…我想这可能是我发布的示例的一个限制,我提供了一个完整的版本。为了再次检查,我对矩阵的每一行求和。sum(A[3,])[1]47 sum(A[2,])[1]56所以有很多选择……你是对的,第5排确实有更多的1,这是有意的:大多数人都有资格填补这个位置(任何不是守门员的人)。我不认为这是问题所在1)因为这以前从来都不是问题,2)当我用python做这件事时,它工作得很好。我遵循了这个指南,它在python中运行,你可以将
New\u proj
子集,只包含那些在python中“挑选”的玩家。然后创建
A
并查看约束条件,看看是否可以找到问题。如果你愿意,你也可以和我分享这些ID。我想这可能是我发布的示例的一个限制,我提供了一个完整的版本。为了再次检查,我对矩阵的每一行求和。sum(A[3,])[1]47 sum(A[2,])[1]56所以有很多选择……你是对的,第5排确实有更多的1,这是有意的:大多数人都有资格填补这个位置(任何不是守门员的人)。我不认为这是问题所在1)因为这以前从来都不是问题,2)当我用python做这件事时,它工作得很好。我遵循了这个指南,它在python中运行,你可以将
New\u proj
子集,只包含那些在python中“挑选”的玩家。然后创建
A
并查看约束条件,看看是否可以找到问题。如果你愿意,你也可以和我分享这些ID。