2016-10-11 10 views
1

私はRを学び始めたばかりです。コードで扱う方法がわからないことがあります。を使って最適なプロジェクトチームを見つけるR

私は、プロジェクトに割り当てることができる個人のプールでdata.frameを作成しています。このプロジェクトには、1つのBA、1つのPM、2つのSA、とSAまたはBAのいずれかの追加の人が必要です。それぞれの人には格付けとコストが関連付けられていますが、私は最大の格付けが必要です。

上記のシナリオの太字部分をどのように達成するかはわかりません。以下のコードは動作していますが、追加のBA/SAを考慮していません。

(これは自習で..宿題を割り当てられていない)最後の行がSAまたはBAの位置のいずれかとすることができる

EDIT-所望の出力

name  position rating cost BA PM SA 
Matt  SA  95 9500 0 0 1  
Aaron  BA  85 4700 1 0 0  
Stephanie SA  95 9200 0 0 1  
Molly  PM  88 5500 0 1 0  
Jake  SA  74 5300 0 0 1 

コード:

#load libraries 
library(lpSolve) 

# create data.frame 
name = c("Steve", "Jeremy", "Matt", "Aaron", "Stephanie", "Molly", "Jake", "Tony", "Jay", "Katy", "Alison") 
position = c("BA", "PM", "SA", "BA", "SA", "PM", "SA", "SA", "PM", "BA", "SA") 
rating = c(75, 90, 95, 85, 95, 88, 74, 81, 55, 65, 68) 
cost = c(5000, 8000, 9500, 4700, 9200, 5500, 5300, 7300, 3300, 4100, 4400) 
df = data.frame(name, position, rating, cost) 

# create restrictions 
num_ba = 1 
num_pm = 1 
num_sa = 2 
max_cost = 35000 

# create vectors to constrain by position 
df$BA = ifelse(df$position == "BA", 1, 0) 
df$PM = ifelse(df$position == "PM", 1, 0) 
df$SA = ifelse(df$position == "SA", 1, 0) 

# vector to optimize against 
objective = df$rating 

# constraint directions 
const_dir <- c("=", "=", "=", "<=") 

# matrix 
const_mat = matrix(c(df$BA, df$PM, df$SA, df$cost), 4, byrow=TRUE) 
const_rhs = c(num_ba, num_pm, num_sa, max_cost) 

#solve 
x = lp("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE) 
print(df[which(x$solution==1), ]) 
+0

あなたが出力として持ちたいことは本当に明確ではありません。あなたは最終目標が何であるかを教えてください。 – gented

+0

希望の出力を追加しました。私の意図をクリアする希望。 – M3SSYM4RV1N

+1

Ramの答えを見てください。http://stackoverflow.com/questions/19250787/either-or-constraints-in-lpsolveapi –

答えて

1

私は右の質問を得た場合、これは仕事ができる:

library(lpSolve) 

# create data.frame 
name = c("Steve", "Jeremy", "Matt", "Aaron", "Stephanie", "Molly", "Jake", "Tony", "Jay", "Katy", "Alison") 
position = c("BA", "PM", "SA", "BA", "SA", "PM", "SA", "SA", "PM", "BA", "SA") 
rating = c(75, 90, 95, 85, 95, 88, 74, 81, 55, 65, 68) 
cost = c(5000, 8000, 9500, 4700, 9200, 5500, 5300, 7300, 3300, 4100, 4400) 

df = data.frame(name, position, rating, cost) 

# create restrictions 
num_pm = 1 
min_num_ba = 1 
min_num_sa = 2 
tot_saba = 4 
max_cost = 35000 

# create vectors to constrain by position 
df$PM = ifelse(df$position == "PM", 1, 0) 
df$minBA = ifelse(df$position == "BA", 1, 0) 
df$minSA = ifelse(df$position == "SA", 1, 0) 
df$SABA = ifelse(df$position %in% c("SA","BA"), 1, 0) 

# vector to optimize against 
objective = df$rating 

# constraint directions 
const_dir <- c("==", ">=", "<=", "==", "<=") 

# matrix 
const_mat = matrix(c(df$PM, df$minBA, df$minSA, df$SABA, df$cost), 5, byrow=TRUE) 
const_rhs = c(num_pm, min_num_ba,min_num_sa, tot_saba, max_cost) 

#solve 
x = lp("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE) 
print(df[which(x$solution==1), ]) 

私がやっているが、いくつかの制約を変更し、新しいものを追加することです: SAの数> = 2、および BAとSAの合計でなければなりません。いつも5人を選択してください。

しかし、これはOPで書いたものとは別のソリューションを提供します:

 name position rating cost PM minBA minSA SABA 
1  Steve  BA  75 5000 0  1  0 1 
3  Matt  SA  95 9500 0  0  1 1 
4  Aaron  BA  85 4700 0  1  0 1 
5 Stephanie  SA  95 9200 0  0  1 1 
6  Molly  PM  88 5500 1  0  0 0 

しかし、この解決策の評価を合計すると、オペ結果は437である一方で、438を与えるので、これは正しいはずです。

HTH。

+0

mmm、私はあなたが恐らく二つの制約を避けることができると見ています:あなたは実際には4つだけ必要です:PM = 1、BA> = 1、SA> = 2、BA + SA == 4です。 – lbusett

+0

ありがとう、これは理にかなっています!また、練習のために制約をオフラインで最適化する作業も行います。 – M3SSYM4RV1N

+0

喜んでお手伝いします - ここでは改良されたよりシンプルなバージョンです。 – lbusett

関連する問題