forked from oli-cs/HackTheBurghX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggestionAlgorithm.py
More file actions
156 lines (120 loc) · 6.02 KB
/
suggestionAlgorithm.py
File metadata and controls
156 lines (120 loc) · 6.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from random import choice
from database import *
from statistics import stdev,mean
class HouseTinder():
def __init__(self):
self.rightHouseIds = set()
self.leftHouseIds = set()
self.unseenIds = set()
self.rightPrices = []
self.rightNumBeds = []
self.rightNumBaths = []
self.priceStdDev = 0
self.numBedsStdDev = 0
self.numBathsStdDev = 0
self.priceMean = 0
self.numBedsMean = 0
self.numBathsMean = 0
self.recommendationReason = "randomised"
def get_seen_house_ids(self) -> set:
return self.rightHouseIds.union(self.leftHouseIds)
def append_right(self,id:int) -> None:
self.rightHouseIds.add(id)
connect_db()
data = get_info(id)
close_db()
self.rightPrices.append(data[4])
self.rightNumBeds.append(data[2])
self.rightNumBaths.append(data[3])
if len(self.rightHouseIds) > 1:
self.priceStdDev = stdev(self.rightPrices)/179315
self.numBedsStdDev = stdev(self.rightNumBeds)
self.numBathsStdDev = stdev(self.rightNumBaths)
self.priceMean = mean(self.rightPrices)/41658
self.numBedsMean = mean(self.rightNumBeds)
self.numBathsMean = mean(self.rightNumBaths)
return
def append_left(self,id:int) -> None:
self.leftHouseIds.add(id)
return
def populate_unseen_houses_set(self) -> None:
connect_db()
ids = set(get_ids())
close_db()
self.unseenIds = ids - self.get_seen_house_ids()#unseen ids = all ids set minus seen ids
return
def get_id_of_closest_item_to_mean(self,idArray:list,index:int,mean:any):
closestToMean = 100000000000
closestToMeanIndex = -1
connect_db()
for i in range(len(idArray)):
currVal = get_info(idArray[i])[index]
if abs(currVal - mean) < closestToMean:
closestToMean = currVal
closestToMeanIndex = i
close_db()
return idArray[closestToMeanIndex]#throwing a value error here means you need to adjust your closest to mean initial value
def choose_house(self,idArray:list) -> int:
if len(self.rightHouseIds) < 1:
return choice(idArray)
if self.priceStdDev < self.numBedsStdDev and self.priceStdDev < self.numBathsStdDev:
#price is most important - show entry closest to mean
print("recommended based on price")
self.recommendationReason = "recommended based on price"
return self.get_id_of_closest_item_to_mean(idArray,4,self.priceMean)
elif self.numBathsStdDev < self.numBedsStdDev and self.numBathsStdDev < self.priceStdDev:
#baths is most important
print("recommended based on number of baths")
self.recommendationReason = "recommended based on number of baths"
return self.get_id_of_closest_item_to_mean(idArray,3,self.numBathsMean)
elif self.numBedsStdDev < self.numBathsStdDev and self.numBedsStdDev < self.priceStdDev:
#beds is most important
print("recommended based on number of beds")
self.recommendationReason = "recommended based on number of beds"
return self.get_id_of_closest_item_to_mean(idArray,2,self.numBedsMean)
elif self.priceStdDev < self.numBedsStdDev and self.priceStdDev == self.numBathsStdDev:
#price and baths are equally important
print("recommended based on price or number of baths")
self.recommendationReason = "recommended based on price or number of baths"
priceCandidate = self.get_id_of_closest_item_to_mean(idArray,4,self.priceMean)
bathCandidate = self.get_id_of_closest_item_to_mean(idArray,3,self.numBathsMean)
return choice([priceCandidate,bathCandidate])
elif self.priceStdDev < self.numBathsStdDev and self.priceStdDev == self.numBedsStdDev:
#price and beds are equally important
print("recommended based on price or number of beds")
self.recommendationReason = "recommended based on price or number of beds"
priceCandidate = self.get_id_of_closest_item_to_mean(idArray,4,self.priceMean)
bedCandidate = self.get_id_of_closest_item_to_mean(idArray,2,self.numBedsMean)
return choice([priceCandidate,bedCandidate])
elif self.numBedsStdDev < self.priceStdDev and self.numBedsStdDev == self.numBathsStdDev:
#beds and baths are equally important
print("recommended based on number of beds or number of baths")
self.recommendationReason = "recommended based on number of beds or number of baths"
bathCandidate = self.get_id_of_closest_item_to_mean(idArray,3,self.numBathsMean)
bedCandidate = self.get_id_of_closest_item_to_mean(idArray,2,self.numBedsMean)
return choice([bathCandidate,bedCandidate])
else:
#all are equally important
print("randomised")
self.recommendationReason = "randomised"
return choice(idArray)
def get_recommendation_reason(self) -> str:
return self.recommendationReason
def get_house_to_display(self) -> list:
displayId = self.choose_house(list(self.unseenIds))
connect_db()
row = get_info(displayId)#change this line when relevant method is done
close_db()
return row
def get_next_house(self) -> list:
self.populate_unseen_houses_set()
if len(self.unseenIds) == 0:
return []
print("mean price: " + str(self.priceMean))
print("mean beds: " + str(self.numBedsMean))
print("mean baths: " + str(self.numBathsMean))
print("price std dev: " + str(self.priceStdDev))
print("bed std dev: " + str(self.numBedsStdDev))
print("bath std dev: " + str(self.numBathsStdDev))
print("\n")
return self.get_house_to_display()