-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized.py
More file actions
110 lines (94 loc) · 4.02 KB
/
optimized.py
File metadata and controls
110 lines (94 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import csv
from math import ceil
from pathlib import Path
def create_top_profit_list(csv_dataset, budget):
"""
Takes a CSV-dataset (share name, price, win in %) and creates a new
CSV-file with the top profit shares only within
a total price of >MAX_TO_SPEND< €.
Args:
csv_dataset: str - path to csv-file
budget: int - budget (maximum total costs)
"""
# read the dataset-file and get the required values
with open(csv_dataset, newline="") as dataset:
data = csv.reader(dataset, delimiter=",")
# get share-name, price and profit in % of each share
share_list = []
for row in data:
if row[0] != "name":
name = row[0]
price = float(row[1])
profit_percent = float(row[2])
profit_euro = round((price / 100 * profit_percent), 2)
# exclude non or negative profit shares from the list
if profit_euro > 0:
share_list.append({
"name": name,
"price(€)": price,
"profit(€)": profit_euro
})
# create a new list of only the top profit shares
# without exceeding the budget
best_combination = get_best_combination(share_list, budget)
total_cost = round(sum(i["price(€)"] for i in best_combination), 2)
total_profit = round(sum(i["profit(€)"] for i in best_combination), 2)
# append "footer" with total of cost and profit
best_combination.append({
"name": "Total",
"price(€)": total_cost,
"profit(€)": total_profit
})
# Create and save a new CSV-file from the most profit list
Path("Optimized CSV Files").mkdir(parents=True, exist_ok=True)
file_name = "top shares of " + csv_dataset.split("\\")[-1].split(".")[0]
field_names = [key for key, value in best_combination[0].items()]
with open(f"Optimized CSV Files/{file_name}.csv",
"w", newline="", encoding="utf-8"
) as file:
writer = csv.DictWriter(file, fieldnames=field_names)
writer.writeheader()
writer.writerows(best_combination)
def get_best_combination(share_list, budget):
"""
Takes a list of share objects (dictionary format) and a max-budget and
returns the combination with the highest profit
that doesn't exceed the budget.
Args:
share_list: list - list of dictionaries in format of:
{"name": x, "price(€)": x,xx, "profit(€)": x,xx}
budget: int - the max budget that isn't allowed to be exceeded.
"""
# n = number of shares
n = len(share_list)
# create table of empty result lists
# (2 dimensional array! Memory use = N*Budget]
table = [[[] for b in range(budget + 1)] for i in range(n + 1)]
# populate table in bottom up manner
for i in range(n + 1):
for b in range(budget + 1):
# if i or b equals 0 no share included (empty list)
if i == 0 or b == 0:
table[i][b] = []
# if share-price smaller or equal to budget
# check profit of two cases
# (share included / not included in combination)
# save the case with the greater profit at current table position
elif ceil(share_list[i - 1]["price(€)"]) <= b:
case1 = table[i - 1][b - ceil(
share_list[i - 1]["price(€)"]
)] + [share_list[i - 1]]
case2 = table[i - 1][b]
value1 = sum(i["profit(€)"] for i in case1)
value2 = sum(i["profit(€)"] for i in case2)
if value1 > value2:
table[i][b] = case1
else:
table[i][b] = case2
# else save the combination on the position
# of the last share at the same budget
else:
table[i][b] = table[i - 1][b]
# get and return result
res = list(table[n][budget])
return res