Home > Software engineering >  Knapsack problem with dynamic programming for investment portfolio and traceback
Knapsack problem with dynamic programming for investment portfolio and traceback

Time:05-22

I am working on a python project utilizing the knapsack problem with dynamic programming to find the best investments based on how much money can be invested. So far I am able to come up with the best investment by name, but I am having trouble with formatting and getting the rest of the information as well as implementing a traceback table. Here is what I have so far and the output:

import pandas as pd
from itertools import chain

def investmentFilename(file):
    df = pd.read_csv(file)
    frame = pd.DataFrame(df)
    frame = frame.drop(0) # dropping the United States
    # print(frame)
    return frame

def loadInvestments(frame):
    portfolio = []
    state = frame['RegionName'].tolist()
    avg = frame['Zhvi'].tolist()
    dfAvg = pd.DataFrame(avg)
    # print(dfAvg)
    tenyr = (frame['10Year'].tolist())
    tenyr = pd.DataFrame(tenyr)
    roi = tenyr.multiply(dfAvg, axis='columns', level=None, fill_value=None)
    # print(roi)
    # roi = pd.DataFrame(roi)
    ROI = roi.values.tolist()
    ROI = list(chain.from_iterable(ROI))
    print("InvestmentName InvestmentCost EstimatedReturnOnInvestment")
    for i in range(len(state)):
        portfolio.append([state[i], int(avg[i]), int(ROI[i])])
        print(state[i], '\t', avg[i], '\t', ROI[i])

    # print(portfolio)
    # portfolio = list(chain.from_iterable(portfolio))
    # print(portfolio)

    # printing list data
    # print("Investment Name:",state)
    # print("Investment Cost:", avg)
    # print("Estimated Return on Investment:", ROI)
    return portfolio

def optimizeInvestments(invstmt, money):
    """ knapsack problem """
    n = len(invstmt)
    val = []
    name = []
    roi = []

    for i in invstmt:
        name.append(i[0])
        val.append(i[1])
        roi.append(i[2])

    K = [[0 for x in range(money   1)] for x in range(n   1)]
    I = [[0 for x in range(money   1)] for x in range(n   1)]

    for i in range(n   1):
        for w in range(money   1):
            if i == 0 or w == 0:
                K[i][w] = 0
                I[i][w] = ""

            elif roi[i - 1] <= w:

                if (val[i - 1]   K[i - 1][w - roi[i - 1]] > K[i - 1][w]):
                    K[i][w] = val[i - 1]   K[i - 1][w - roi[i - 1]]
                    I[i][w] = name[i - 1]   I[i - 1][w - roi[i - 1]]
                else:
                    K[i][w] = K[i - 1][w]
                    I[i][w] = I[i - 1][w]

            else:
                K[i][w] = K[i - 1][w]
                I[i][w] = I[i - 1][w]

    return (I[n][money])

dataFrame = investmentFilename('zhvi-short.csv')
items = loadInvestments(dataFrame)
print(items)
money = 15000 # change the amount of money you want to invest here
# items = [["A", 60, 120], ["B", 100, 20],["C", 120, 30]] # test
# print(items)
val = []
roi = []
for i in items:
    val.append(i[1])
    roi.append(i[2])
print(optimizeInvestments(items, money))

This gives the output: FloridaNew York

Which I would want to be separated with an "and" or a comma. And then I would like the specific ROI for each of these names to be output as well.

I also need to implement a traceback table for the optimal investments as well. I know how to implement traceback tables in theory but I am not sure how to implement in regards to the knapsack problem.

CodePudding user response:

This is what I ended up coming up with for solving this problem.

def optimizeInvestments(invstmt, money):
""" knapsack problem """
n = len(invstmt)
val = []
name = []
roi = []
traceback = [[0 for i in range(n)] for i in range(n)]

for i in invstmt:
    name.append(i[0])
    val.append(i[-1])
    roi.append(i[1])

K = [[0 for x in range(money   1)] for x in range(n   1)]
I = [[0 for x in range(money   1)] for x in range(n   1)]

for i in range(n   1):
    for w in range(money   1):
        if i == 0 or w == 0:
            K[i][w] = 0
            I[i][w] = ""

        elif roi[i - 1] <= w:

            if (val[i - 1]   K[i - 1][w - roi[i - 1]] > K[i - 1][w]):
                K[i][w] = val[i - 1]   K[i - 1][w - roi[i - 1]]
                if len(I[i - 1][w - roi[i - 1]]) > 0:
                    I[i][w] = name[i - 1]   " & "   I[i - 1][w - roi[i - 1]]
                else:
                    I[i][w] = name[i - 1]

            else:
                K[i][w] = K[i - 1][w]
                I[i][w] = I[i - 1][w]

        else:
            K[i][w] = K[i - 1][w]
            I[i][w] = I[i - 1][w]

portfolio = 'With $'  str(money)   ", invest in "   str(I[n][money])   " for a ROI of $"   str(K[n][money])
return portfolio
  • Related