2017-11-13 15 views
-3

モンテカルロシミュレーション:このコードは、260の要求をシミュレートし、必要な2次シフトの量を計算するためのものです。しかし、私は10回目の実行後にNAの価値に遭遇します。 私はこれを達成しようとしています。 'B' enter image description hereRのモンテカルロシミュレーション:ループの戻り値NA

 set.seed(1234) 
     n = 260 
     demand = runif(260, min = 80, max = 130) 
     production_capacity = 100 
     begining_inventory[] = 100 
     post_inventory[] = 0 
     counter = 0 
     for(i in 1:n){ 
      if (i == 1){ 
      begining_inventory[i] = 100 
      ending_inventory[i] = begining_inventory[i] + production_capacity - demand [i] 
      ending_inventory[i] 
      post_inventory[i] = ending_inventory[i] 

      } 
      else{ 
      post_inventory[i] = ending_inventory[i] 
      begining_inventory[i] = post_inventory[i-1] 
      ending_inventory[i] = begining_inventory[i] + production_capacity - demand [i] 
      } 
      if(ending_inventory[i] <= 50){ 
      counter = counter + 1 
      } 
     print(ending_inventory[i]) 
     P.first_shift = (1-counter/n) 
     P.second_shift = 1-P.first_shift 
     } 
+0

出力: [1] 78.22905 を[1] 85.84038 [1 ] 87.66084 [1] -24.41198 [1] -34.38333 [1] -28.49417 [1] -2.289705 [1] 36.06229 [1] 106.3816もし(ending_inventory [i]が<= 50){: 欠損値TRUE/FALSE必要 >でエラーpost_inventory [I] = ending_inventory [i]は > post_inventory [i]は [1] –

+1

NAの追加情報を入れてくださいあなたの質問(コメントではありません)、**あなたの質問を編集する:** https://stackoverflow.com/review/suggested-edits/17930041 – jogo

+0

質問は明らかでした。この値は、10番目のループの後にNAを返します。 –

答えて

0

から1列目のスタートはそれを考え出した:

set.seed(1234) 
n = 260 
demand = runif(n, min = 80, max = 130) 
production_capacity = 100 
begining_inventory = 100 

counter = 0 
second_shift = rep(0,n) 
ending_inventory = rep(0,n) 

second_shift_production = rep(0,n) 
post_second = rep(0,n) 

for(i in 1:n){ 

    ending_inventory[i] = begining_inventory + production_capacity - demand[i] 

    if(ending_inventory[i] < 50){ 
    print(ending_inventory[i]) 
    second_shift[i] = 1 
    counter = counter + 1 
    }else{ 
    second_shift[i] = 0 
    } 
    second_shift_production[i] = second_shift[i]*100 
    post_second[i] = second_shift_production[i] + ending_inventory[i] 

    if(i > 1){ 
    begining_inventory = post_second[i-1] 
    } 

} 
hist(ending_inventory) 
abline(v = 50, col = "red") 

P.first_shift = (1-counter/n) 
P.second_shift = 1-P.first_shift 

print(paste("Probabiity Only first shift = ", (1-counter/n)*100, "%", sep = " ")) 
print(paste("Probabiity second shift = ", 100-(1-counter/n)*100, "%", sep = " "))