1. NPV
    • 1개년 50억, 2개년 60억, 3개년 70억의 예산을 가지고 NPV(순현재가치)가 가장 높아지는 안을 제시하시오
  1개년 2개년 3개년
1안 10 20 15
2안 15 14 19
3안 12 11 30
4안 13 25 20
5안 16 30 24
from itertools import combinations 
import pandas as pd 

investment_df = pd.DataFrame([[10, 20, 15],
                              [15, 14, 19], 
                              [12, 11, 30], 
                              [13, 25, 20], 
                              [16, 30, 24]], index=[1, 2, 3, 4, 5], columns=['yr1', 'yr2', 'yr3'])
investment_df
  yr1 yr2 yr3
1 10 20 15
2 15 14 19
3 12 11 30
4 13 25 20
5 16 30 24
items = [1, 2, 3, 4, 5]

combination_list = list(combinations(items, 2))+list(combinations(items, 3))

result_df = pd.DataFrame() 

for c in combination_list: 
    
    temp_df = pd.DataFrame()
    for item in items: 
        if item in c: 
            temp_df = pd.concat([temp_df, investment_df.loc[item]], axis=1) 
    
    result_df = pd.concat([result_df, temp_df.sum(axis=1)], axis=1)
    
result_df.columns = combination_list

df_t = result_df.T
df_t = df_t[(df_t['yr1'] < 50) & (df_t['yr2'] < 60) & (df_t['yr3'] < 70)]
df_t['total'] = df_t.sum(axis=1)
df_t.sort_values(by='total', ascending=False)
  yr1 yr2 yr3 total
(2, 3, 4) 40 50 69 159
(1, 3, 4) 35 56 65 156
(1, 2, 4) 38 59 54 151
(1, 2, 3) 37 45 64 146
(4, 5) 29 55 44 128
(3, 5) 28 41 54 123
(2, 5) 31 44 43 118
(1, 5) 26 50 39 115
(3, 4) 25 36 50 111
(2, 4) 28 39 39 106
(1, 4) 23 45 35 103
(2, 3) 27 25 49 101
(1, 3) 22 31 45 98
(1, 2) 25 34 34 93