-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart_B_d)_2.py
More file actions
146 lines (116 loc) · 6.41 KB
/
Copy pathPart_B_d)_2.py
File metadata and controls
146 lines (116 loc) · 6.41 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
import pandas as pd
import numpy as np
import os
from pathlib import Path
from sqlalchemy import create_engine
database = "group_5_2024"
user = "group_5_2024"
password = "f9JsjkRlQee4"
host = "dbcourse.cs.aalto.fi"
port = "5432"
engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}')
connection = engine.connect()
volunteer_application_df = pd.read_sql("SELECT * FROM volunteer_application", connection)
volunteer_df = pd.read_sql("SELECT * FROM volunteer", connection)
request_df = pd.read_sql("SELECT * FROM request", connection)
beneficiary_df = pd.read_sql("SELECT * FROM beneficiary", connection)
city_df = pd.read_sql("SELECT * FROM city", connection)
request_skill_df = pd.read_sql("SELECT * FROM request_skill", connection)
skill_assignment_df = pd.read_sql("SELECT * FROM skill_assignment", connection)
interest_assignment_df = pd.read_sql("SELECT * FROM interest_assignment", connection)
max_travel_readiness = 1500
max_distance = 20000
def haversine(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
# Haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
c = 2 * np.arcsin(np.sqrt(a))
r = 6371 # Radius of earth in kilometers
return c * r
# Parse geolocation
def parse_geolocation(geolocation):
lat, lon = map(float, geolocation.split('/'))
return lat, lon
def calculate_volunteer_range_score(volunteer_city_id, beneficiary_city_id):
volunteer_geolocation = city_df[city_df['id'] == volunteer_city_id]['geolocation'].values[0]
volunteer_lat, volunteer_lon = parse_geolocation(volunteer_geolocation)
beneficiary_geolocation = city_df[city_df['id'] == beneficiary_city_id]['geolocation'].values[0]
beneficiary_lat, beneficiary_lon = parse_geolocation(beneficiary_geolocation)
distance = haversine(volunteer_lon, volunteer_lat, beneficiary_lon, beneficiary_lat)
range_score = 10 - (distance / max_distance) * 10
return max(range_score, 0)
interest_mapping = {
"work in team needed": "WorkInATeam",
"work with young needed": "WorkWithYoung",
"organise activities needed": "Organizational",
"guide and teach needed": "TrainPeople",
"work with elderly needed": "WorkWithElderly",
"first aid needed": "HealthCareOrFirstAid",
"collect donations needed": "CollectDonations",
"promote wellbeing needed": "PromoteWellbeing",
"work in multicultural environment needed": "WorkInMulticulturalEnvironment",
"food help needed": "FoodHelp",
"help in crisis needed": "HelpInCrisis",
"immigrant support needed": "ImmigrantSupport"
}
results_list = []
grouped_by_request_df = volunteer_application_df.groupby('request_id')['volunteer_id'].apply(list).reset_index()
# Iterate over each request_id
for index, row in grouped_by_request_df.iterrows():
request_id = row['request_id']
volunteer_ids = row['volunteer_id']
request_title = request_df[request_df['id'] == request_id]['title'].values[0]
priority_value = request_df[request_df['id'] == request_id]['priority_value'].values[0]
priority_score = (priority_value / 5) * 10
beneficiary_id = request_df[request_df['id'] == request_id]['beneficiary_id'].values[0]
beneficiary_city_id = beneficiary_df[beneficiary_df['id'] == beneficiary_id]['city_id'].values[0]
skills_needed = request_skill_df[request_skill_df['request_id'] == request_id]['skill_name'].tolist()
total_skills_needed = len(skills_needed)
for volunteer_id in volunteer_ids:
if volunteer_id in volunteer_df['id'].values:
volunteer_city_id = volunteer_df[volunteer_df['id'] == volunteer_id]['city_id'].values[0]
travel_readiness = volunteer_df[volunteer_df['id'] == volunteer_id]['travel_readiness'].values[0]
if volunteer_city_id == beneficiary_city_id:
travel_score = 20
else:
travel_score = 20 - (travel_readiness / max_travel_readiness) * 20
volunteer_skills = skill_assignment_df[skill_assignment_df['volunteer_id'] == volunteer_id]['skill_name'].tolist()
skill_matching = set(skills_needed) & set(volunteer_skills)
skill_match_count = len(skill_matching)
skill_score = (skill_match_count / total_skills_needed) * 40 if total_skills_needed > 0 else 0
volunteer_interests = interest_assignment_df[interest_assignment_df['volunteer_id'] == volunteer_id]['interest_name'].tolist()
mapped_interest = interest_mapping.get(request_title.lower())
interest_score = 0
if mapped_interest and mapped_interest in volunteer_interests:
interest_score = 20
range_score = calculate_volunteer_range_score(volunteer_city_id, beneficiary_city_id)
total_score = skill_score + interest_score + priority_score + travel_score + range_score
results_list.append({
'request_id': request_id,
'request_title': request_title,
'volunteer_id': volunteer_id,
'volunteer_city_id': volunteer_city_id,
'beneficiary_city_id': beneficiary_city_id,
'travel_readiness': travel_readiness,
'travel_score': travel_score,
'volunteer_skills': volunteer_skills,
'skills_needed': skills_needed,
'skill_matching': list(skill_matching),
'skill_match_count': skill_match_count,
'skill_score': skill_score,
'volunteer_interests': volunteer_interests,
'mapped_interest': mapped_interest,
'interest_score': interest_score,
'priority_value': priority_value,
'priority_score': priority_score,
'range_score': range_score,
'total_score': total_score
})
results_df = pd.DataFrame(results_list)
desktop_path = str(Path(__file__).parent)
output_file_path = os.path.join(desktop_path, "volunteer_match_scores.xlsx")
results_df.to_excel(output_file_path, index=False)
print(f"File saved to: {output_file_path}")
connection.close()