This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
426 lines (327 loc) · 17.8 KB
/
eval.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import sys
import os
import datetime
import pandas as pd
import multiprocessing
import itertools
sys.path.append("./src")
sys.path.append("./evaluation")
from model.product_structure_model import ProductStructureModel
from model.preferences_model import Preferences
from model.configuration_model import ConfigurationModel
from managers.recommendation_manager import SimpleConfigurationMaxSelector
from scoring.scoring_functions import ReduceScoringFunctionFactory
from user_type_mappings import TYPE_ATHLETE, TYPE_CONSUMER, TYPE_ENVIRONMENTALIST, TYPE_OWNER, TYPE_RANDOM
import operator
import time
import numpy as np
import matplotlib.pyplot as pp
import random
import math
import json
with open('./evaluation/product_structure.json') as json_file:
data = json.load(json_file)
product_structure = ProductStructureModel(data)
from tinydb import TinyDB
def DB():
return TinyDB('eval.json')
def DB_CONFIG():
return DB().table('CONFIG')
def DB_PRODUCT_STRUCTURE():
return DB().table('PRODUCT_STRUCTURE')
CONFIGURATIONS_UNFINISHED = []
PREFERENCES_RANDOM_MEMBER = []
PREFERENCES_ALL = []
def generate_group_preferences(user_type_mappings, amount = 1000):
global PREFERENCES_RANDOM_MEMBER
global PREFERENCES_ALL
characteristics = product_structure.get_list_of_characteristics()
PREFERENCES_ALL = []
PREFERENCES_RANDOM_MEMBER = []
for i in range(amount):
users = []
single_user = []
counter = random.randint(0, len(user_type_mappings) - 1)
for mapping in user_type_mappings:
ratings = []
for char in characteristics:
value = mapping[char.elementId].generateNumber()
ratings.append({
"code": char.elementId,
"value": value,
})
user = {
"user": mapping['name'],
"ratings": ratings,
}
users.append(user)
if counter == 0:
single_user.append(user)
counter -= 1
PREFERENCES_ALL.append( Preferences({'preferences' : users}) )
PREFERENCES_RANDOM_MEMBER.append( Preferences({'preferences' : single_user}) )
return PREFERENCES_ALL
def generate_unfinished_configurations(fullness=0.3, amount=1000):
configurations = TinyDB('./evaluation/eval.json').table('CONFIG').all()
global CONFIGURATIONS_UNFINISHED
characteristics = list(map(lambda x: x.elementId,ProductStructureModel(data).get_list_of_characteristics()))
CONFIGURATIONS_UNFINISHED = []
for i in range(amount):
final_config = configurations[random.randint(0, len(configurations) - 1)]
codes = list(filter(lambda x: x in characteristics, final_config['configuration']))
conf_size = math.ceil(len(codes) * fullness)
unfishied_config = random.sample(codes, conf_size)
CONFIGURATIONS_UNFINISHED.append(ConfigurationModel({
"configuration": unfishied_config,
"variables": []
}))
return CONFIGURATIONS_UNFINISHED
def get_ratings(requests, finished_configurations, product_structure, scoring_function=None):
if scoring_function == None :
scoring_function = ReduceScoringFunctionFactory.build_scoring_function(
["penalty_ratio", "pref_product_simpleSelectedCharacterstics_average"],
#["pref_average_flat"],
product_structure,
oper = operator.mul
)
list_ofScoreLists = []
for (preference, config) in requests:
list_ofScoreLists.append(list(map(lambda to_rate: scoring_function.calc_score(config, preference, to_rate), finished_configurations)))
return list_ofScoreLists
def plot_at_y(arr, val):
pp.plot(arr, np.zeros_like(arr) + val, 'x')
def get_scores_for_one(configurationState, preference, finished_configurations, product_structure, scoring_function=None):
if scoring_function == None:
scoring_function = ReduceScoringFunctionFactory.build_scoring_function(
["penalty_ratio", "pref_product_simpleSelectedCharacterstics_average"],
product_structure,
oper = operator.mul
)
return list(map(lambda to_rate: scoring_function.calc_score(configurationState, preference, to_rate), finished_configurations))
def get_scoring_functions():
product = ReduceScoringFunctionFactory.build_scoring_function(
["penalty_ratio", "pref_product_simpleSelectedCharacterstics_average"],
product_structure,
oper = operator.mul)
misery = ReduceScoringFunctionFactory.build_scoring_function(
["penalty_ratio", "pref_min_simpleSelectedCharacterstics_average"],
product_structure,
oper = operator.mul)
average = ReduceScoringFunctionFactory.build_scoring_function(
["penalty_ratio", "pref_average_simpleSelectedCharacterstics_average"],
product_structure,
oper = operator.mul)
return [("multiplication",product), ("least misery", misery), ("best average", average)]
def main(amount=1000, fullness=0.1, center=50, threshold_distance_from_centre = 0, group_type='heterogeneous', outdir="./out"):
global CONFIGURATIONS_UNFINISHED
global PREFERENCES_RANDOM_MEMBER
global PREFERENCES_ALL
print("Started Evaluation")
if group_type == 'homogenous':
group_type_mappings = [TYPE_OWNER, TYPE_OWNER, TYPE_OWNER, TYPE_OWNER]
elif group_type == 'random':
group_type_mappings = [TYPE_RANDOM, TYPE_RANDOM, TYPE_RANDOM, TYPE_RANDOM]
else:
group_type='heterogeneous'
group_type_mappings = [TYPE_ATHLETE, TYPE_CONSUMER, TYPE_ENVIRONMENTALIST, TYPE_OWNER]
settings = "amount-{}__center-{}__tdistance-{}__fullness-{}__group-{}".format(amount, center, threshold_distance_from_centre, fullness, group_type)
outdir += "/{}__{}".format(datetime.datetime.utcnow().strftime("%Y_%m_%d_T%H-%M-%S%z"), settings)
# check the directory does not exist
if not(os.path.exists(outdir)):
# create the directory you want to save to
os.mkdir(outdir)
if not(os.path.exists("{}/data".format(outdir))):
os.mkdir("{}/data".format(outdir))
if not(os.path.exists("{}/fig".format(outdir))):
os.mkdir("{}/fig".format(outdir))
random.seed(10924892319)
np.random.seed(seed=956109142)
start_total = start = time.time()
# Generating preferences and unfinished configurations
generate_group_preferences(group_type_mappings, amount=amount)
generate_unfinished_configurations(fullness=0.1, amount = amount)
requests_random_member = list(zip(PREFERENCES_RANDOM_MEMBER, CONFIGURATIONS_UNFINISHED))
requests_all = list(zip(PREFERENCES_ALL, CONFIGURATIONS_UNFINISHED))
end = time.time()
print("Done generating data! It took: {} seconds".format(end - start))
start = time.time()
finished_configurations = list(map(lambda x: ConfigurationModel(x), TinyDB('./evaluation/eval.json').table('CONFIG').all()))
random.shuffle(finished_configurations)
end = time.time()
print("Done loading finished configurations! It took: {} seconds".format(end - start))
scoring_function_list = get_scoring_functions()
results_happiness_db_size_avg_diff = []
results_unhappiness_db_size_avg_diff = []
results_happiness_db_size_avg_total_all = []
results_unhappiness_db_size_avg_total_all = []
piece_counts = [16, 8, 4, 2, 1]
scoring_function_labels = list(map(lambda x: x[0], scoring_function_list))
db_sizes_label = list(map(lambda x: len(finished_configurations) // x, piece_counts))
for label, scoring_function in scoring_function_list:
print("!!! Starting evaluation of: {} !!!".format(label))
# Rate configurations
start = time.time()
np_scores_random = np.array(get_ratings(requests_random_member,finished_configurations,product_structure, scoring_function=scoring_function))
np_scores_all = np.array(get_ratings(requests_all,finished_configurations,product_structure, scoring_function=scoring_function))
end = time.time()
print("Done rating stored configurations! It took: {} seconds".format(end - start))
happiness_db_size_avg_diff = []
unhappiness_db_size_avg_diff = []
happiness_db_size_avg_total_all = []
unhappiness_db_size_avg_total_all = []
happiness_db_size_stdd = []
unhappiness_db_size_stdd = []
for piece_count in piece_counts:
happiness_diff_list = []
unhappiness_diff_list = []
happiness_all_list = []
unhappiness_all_list = []
step_size = len(finished_configurations) // piece_count
residual = len(finished_configurations) % piece_count
for run_count in range(piece_count):
print("Starting run {} of {} with {} as store size.".format(run_count, (piece_count - 1) ,step_size))
offset_start = 0
offset_end = 0
if residual > 0:
residual -= 1
offset_end = 1
start_pos = run_count * step_size + offset_start
end_pos = (run_count + 1) * step_size + offset_start + offset_end
offset_start += offset_end
start = time.time()
# Filtering data
modifier_random = np.zeros(np_scores_random.shape)
modifier_all = np.zeros(np_scores_all.shape)
modifier_random[:,start_pos:end_pos] += 1
modifier_all[:,start_pos:end_pos] += 1
#np_scores_modified_random = np.multiply(np_scores_random[:], modifier_random)
np_scores_modified_random = np_scores_random[:]
np_scores_modified_all = np.multiply(np_scores_all[:], modifier_all)
index_max_random = np.argmax(np_scores_modified_random, axis=1)
index_max_all = np.argmax(np_scores_modified_all, axis=1)
end = time.time()
print("Done getting recommendations! It took: {} seconds".format(end - start))
# Generate individual scores
start = time.time()
scores_individual = [[[] for i in range(len(group_type_mappings))] for i in range(amount)]
j = 0
for preference, configurationState in requests_all:
individuals = preference.getIndividualPreferences()
i = 0
for individual in individuals:
scores_individual[j][i] = get_scores_for_one(configurationState, individual, finished_configurations, product_structure, scoring_function=scoring_function)
i += 1
j += 1
end = time.time()
print("Done generating individual scores! It took: {} seconds".format(end - start))
#Generate hapiness level
start = time.time()
avg_happy_diff = 0
avg_unhappy_diff = 0
avg_happy_all = 0
avg_unhappy_all = 0
individual_index = 0
for individuals_scores in scores_individual:
unhappy_rand = 0
unhappy_all = 0
happy_rand = 0
happy_all = 0
for individual_score in individuals_scores:
np_individual_score = np.array(individual_score)
unhappy_threshold = np.percentile(np_individual_score, center - threshold_distance_from_centre)
happy_threshold = np.percentile(np_individual_score, center + threshold_distance_from_centre)
score_rand = np_individual_score[index_max_random[individual_index]]
score_all = np_individual_score[index_max_all[individual_index]]
if score_all > happy_threshold:
happy_all += 1
elif score_all < unhappy_threshold:
unhappy_all += 1
if score_rand > happy_threshold:
happy_rand += 1
elif score_rand < unhappy_threshold:
unhappy_rand += 1
avg_happy_diff += happy_all - happy_rand
avg_unhappy_diff += unhappy_all - unhappy_rand
avg_happy_all += happy_all
avg_unhappy_all += unhappy_all
individual_index += 1
avg_happy_diff /= amount
avg_unhappy_diff /= amount
avg_happy_all /= amount
avg_unhappy_all /= amount
happiness_diff_list.append(avg_happy_diff)
unhappiness_diff_list.append(avg_unhappy_diff)
happiness_all_list.append(avg_happy_all)
unhappiness_all_list.append(avg_unhappy_all)
print("-- Average increase in happiness: {} | Average increase in unhappiness: {}".format(avg_happy_diff, avg_unhappy_diff))
print("-- Average happiness: {} | Average unhappiness: {}".format(avg_happy_all, avg_unhappy_all))
end = time.time()
print("Done rating recommendations! It took: {} seconds".format(end - start))
happiness_db_size_avg_diff.append(np.average(np.array(happiness_diff_list)))
unhappiness_db_size_avg_diff.append(np.average(np.array(unhappiness_diff_list)))
happiness_db_size_avg_total_all.append(np.average(np.array(happiness_all_list)))
unhappiness_db_size_avg_total_all.append(np.average(np.array(unhappiness_all_list)))
results_happiness_db_size_avg_diff.append(happiness_db_size_avg_diff)
results_unhappiness_db_size_avg_diff.append(unhappiness_db_size_avg_diff)
results_happiness_db_size_avg_total_all.append(happiness_db_size_avg_total_all)
results_unhappiness_db_size_avg_total_all.append(unhappiness_db_size_avg_total_all)
column_names = db_sizes_label
row_names = scoring_function_labels
pd.DataFrame(results_happiness_db_size_avg_diff, index=row_names, columns=column_names).to_csv("{}/data/_happy_increase.csv".format(outdir), index=True, header=True, sep=',')
pd.DataFrame(results_unhappiness_db_size_avg_diff, index=row_names, columns=column_names).to_csv("{}/data/_unhappy_increase.csv".format(outdir).format(outdir), index=True, header=True, sep=',')
pd.DataFrame(results_happiness_db_size_avg_total_all, index=row_names, columns=column_names).to_csv("{}/data/_happy_total_all.csv".format(outdir), index=True, header=True, sep=',')
pd.DataFrame(results_unhappiness_db_size_avg_total_all, index=row_names, columns=column_names).to_csv("{}/data/_unhappy_total_all.csv".format(outdir).format(outdir), index=True, header=True, sep=',')
end_total = time.time()
print("Done! Total time: {} seconds".format(end_total - start_total))
axis=[0,150, -1, 0.5]
pp.figure(figsize=(8,4), dpi=300)
pp.subplots_adjust(hspace = 0.8, wspace=0.4)
pp.subplot(1, 2, 1, title="happiness increase average", )
for result_happy in results_happiness_db_size_avg_diff:
pp.plot(db_sizes_label, result_happy)
pp.legend(scoring_function_labels)
pp.xlabel("number of stored configurations")
pp.ylabel("number of people")
pp.axis(axis)
pp.subplot(1, 2, 2, title="unhappiness increase average")
for result_unhappy in results_unhappiness_db_size_avg_diff:
pp.plot(db_sizes_label, result_unhappy)
pp.legend(scoring_function_labels)
pp.xlabel("number of stored configurations")
pp.ylabel("number of people")
pp.axis(axis)
pp.savefig("{}/fig/happy_unhappy_increase.pdf".format(outdir),format="pdf")
pp.figure(figsize=(8,4), dpi=300)
axis=[0,150, 0, 4]
pp.subplots_adjust(hspace = 0.8, wspace=0.4)
pp.subplot(1, 2, 1, title="happiness absolute average", )
for result_happy in results_happiness_db_size_avg_total_all:
pp.plot(db_sizes_label, result_happy)
pp.legend(scoring_function_labels)
pp.xlabel("number of stored configurations")
pp.ylabel("number of people")
pp.axis(axis)
pp.subplot(1, 2, 2, title="unhappiness absolute average")
for result_unhappy in results_unhappiness_db_size_avg_total_all:
pp.plot(db_sizes_label, result_unhappy)
pp.legend(scoring_function_labels)
pp.xlabel("number of stored configurations")
pp.ylabel("number of people")
pp.axis(axis)
pp.savefig("{}/fig/happy_unhappy_total_all.pdf".format(outdir),format="pdf")
def main_tuple(param):
print("----------------------------------------------------------------------------------------")
print("----------------------Starting: {}----------------------".format(param))
print("----------------------------------------------------------------------------------------")
main(amount=param[0], fullness=param[1],center=param[2] ,threshold_distance_from_centre = param[3], group_type= param[4])
return True
if __name__ == "__main__":
num_cores = multiprocessing.cpu_count()
amounts = [1]
fullnesses = [0.1]
centers = [10, 20, 30, 40, 50, 60, 70, 80, 90]
dists = [5]
g_types = ["heterogeneous", "random", "homogenous"]
params = list(itertools.product(amounts, fullnesses, centers, dists, g_types))
pool = multiprocessing.Pool(processes=num_cores)
res = pool.map(main_tuple, params)