-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.py
More file actions
2527 lines (2115 loc) · 108 KB
/
parallel.py
File metadata and controls
2527 lines (2115 loc) · 108 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
from extrap.entities.coordinate import Coordinate
from extrap.entities.experiment import Experiment
from extrap.entities.parameter import Parameter
from extrap.entities.callpath import Callpath
from extrap.entities.metric import Metric
from extrap.entities.measurement import Measurement
from extrap.modelers.model_generator import ModelGenerator
from extrap.modelers.abstract_modeler import MultiParameterModeler
from extrap.util.progress_bar import ProgressBar
import numpy as np
import math
from math import log2
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
from sklearn.gaussian_process.kernels import WhiteKernel
import warnings
from sklearn.exceptions import ConvergenceWarning
from temp import add_measurements_to_gpr
from temp import add_measurement_to_gpr
import sys
from generic_strategy import add_additional_point_generic
from extrap.util.options_parser import ModelerOptionsAction, ModelerHelpAction
from extrap.util.options_parser import SINGLE_PARAMETER_MODELER_KEY, SINGLE_PARAMETER_OPTIONS_KEY
from extrap.util.options_parser import ModelerOptionsAction, ModelerHelpAction
from extrap.modelers import multi_parameter
from extrap.modelers import single_parameter
import random
import itertools
from collections import defaultdict
from bayesian_strategy import expected_improvement, propose_location
def add_additional_point_grid(remaining_points, selected_coord_list, new_point):
remaining_points = copy.deepcopy(remaining_points)
selected_coord_list = copy.deepcopy(selected_coord_list)
#print("old:",selected_coord_list)
if new_point not in selected_coord_list:
selected_coord_list.append(new_point)
#print("new:",selected_coord_list)
# calc the cost of the new point
#print("DEBUG remaining_points:", remaining_points)
#print("DEBUG new_point:", new_point)
cost_values = remaining_points[new_point]
#print("DEBUG cost_values:", cost_values)
new_point_cost = np.sum(cost_values)
#print("DEBUG new_point_cost:", new_point_cost)
#print("old:", remaining_points)
# remove this point from the remaining points list
try:
del remaining_points[new_point]
except ValueError as e:
print(e)
#print("new:", remaining_points)
return remaining_points, selected_coord_list, new_point_cost
def add_additional_point_random(remaining_points, selected_coord_list, measurements_random, nr_reps, callpath, metric):
remaining_points = copy.deepcopy(remaining_points)
selected_coord_list = copy.deepcopy(selected_coord_list)
# choose a random point from the remaining point list
random_key = random.choice(list(remaining_points.keys()))
random_value = remaining_points[random_key]
# get the cost of the new measurement value
new_point_cost = random_value[0]
index_measurement_value = nr_reps - len(random_value)
#print("DEBUG measurements_random:", measurements_random)
# get the actual measurement value
for i in range(len(measurements_random[(callpath, metric)])):
if measurements_random[(callpath, metric)][i].coordinate == random_key:
new_measurement_value = measurements_random[(callpath, metric)][i].values[index_measurement_value]
break
# pop new value from remaining points list
remaining_points[random_key].pop(0)
# check if point was already selected
# make sure this point was not selected yet
exists = False
for k in range(len(selected_coord_list)):
if random_key == selected_coord_list[k]:
exists = True
break
# if point was selected already, delete it
if exists == False:
# add not yet selected cord to selected cord list
selected_coord_list.append(random_key)
else:
# if there is no value left for this cord then delete it completely from the list
if len(remaining_points[random_key]) == 0:
del remaining_points[random_key]
selected_cord_new = random_key
return new_point_cost, selected_cord_new, remaining_points, selected_coord_list, new_measurement_value
def create_experiment2(cord, experiment, new_value, callpath, metric):
# only append the new measurement value to experiment
cord_found = False
for i in range(len(experiment.measurements[(callpath, metric)])):
if cord == experiment.measurements[(callpath, metric)][i].coordinate:
experiment.measurements[(callpath, metric)][i].add_value(new_value)
#x = np.append(x, new_value)
#experiment.measurements[(callpath, metric)][i].values = x
cord_found = True
break
if cord_found == False:
# add new coordinate to experiment and then add a new measurement object with the new value to the experiment
experiment.add_coordinate(cord)
new_measurement = Measurement(cord, callpath, metric, [new_value])
experiment.add_measurement(new_measurement)
return experiment
def calculate_selected_point_cost2(experiment, callpath, metric):
selected_cost = 0
for i in range(len(experiment.measurements[(callpath, metric)])):
x = experiment.measurements[(callpath, metric)][i]
coordinate_cost = 0
for k in range(len(x.values)):
runtime = np.mean(x.values[k])
nr_processes = x.coordinate.as_tuple()[0]
core_hours = runtime * nr_processes
coordinate_cost += core_hours
selected_cost += coordinate_cost
return selected_cost
def create_experiment_base(selected_coord_list, nr_parameters, parameter_placeholders, metric, callpath, nr_base_points, experiment_coordinates, experiment_measurements):
# create new experiment with only the selected measurements and points as coordinates and measurements
experiment_new = Experiment()
for j in range(nr_parameters):
experiment_new.add_parameter(Parameter(parameter_placeholders[j]))
experiment_new.add_callpath(callpath)
experiment_new.add_metric(metric)
for j in range(len(selected_coord_list)):
coordinate = selected_coord_list[j]
experiment_new.add_coordinate(coordinate)
coordinate_id = -1
for k in range(len(experiment_coordinates)):
if coordinate == experiment_coordinates[k]:
coordinate_id = k
measurement_temp = experiment_measurements[(callpath, metric)][coordinate_id]
#print("haha:",measurement_temp.median)
if measurement_temp != None:
values = []
counter = 0
while counter < nr_base_points:
values.append(np.mean(measurement_temp.values[counter]))
counter += 1
#value = selected_measurement_values[selected_coord_list[j]]
#experiment_generic.add_measurement(Measurement(coordinate, callpath, metric, value))
experiment_new.add_measurement(Measurement(coordinate, callpath, metric, values))
return experiment_new
def increment_accuracy_bucket(acurracy_bucket_counter, percentage_error):
# increase the counter of the accuracy bucket the error falls into for strategy
if percentage_error <= 5:
acurracy_bucket_counter["5"] = acurracy_bucket_counter["5"] + 1
acurracy_bucket_counter["10"] = acurracy_bucket_counter["10"] + 1
acurracy_bucket_counter["15"] = acurracy_bucket_counter["15"] + 1
acurracy_bucket_counter["20"] = acurracy_bucket_counter["20"] + 1
acurracy_bucket_counter["rest"] = acurracy_bucket_counter["rest"] + 1
elif percentage_error <= 10:
acurracy_bucket_counter["10"] = acurracy_bucket_counter["10"] + 1
acurracy_bucket_counter["15"] = acurracy_bucket_counter["15"] + 1
acurracy_bucket_counter["20"] = acurracy_bucket_counter["20"] + 1
acurracy_bucket_counter["rest"] = acurracy_bucket_counter["rest"] + 1
elif percentage_error <= 15:
acurracy_bucket_counter["15"] = acurracy_bucket_counter["15"] + 1
acurracy_bucket_counter["20"] = acurracy_bucket_counter["20"] + 1
acurracy_bucket_counter["rest"] = acurracy_bucket_counter["rest"] + 1
elif percentage_error <= 20:
acurracy_bucket_counter["20"] = acurracy_bucket_counter["20"] + 1
acurracy_bucket_counter["rest"] = acurracy_bucket_counter["rest"] + 1
else:
acurracy_bucket_counter["rest"] = acurracy_bucket_counter["rest"] + 1
return acurracy_bucket_counter
def percentage_error(true_value, measured_value):
error = abs(true_value - measured_value)
if true_value == 0.0:
percentage_error = 0.0
else:
percentage_error = (error / true_value) * 100
return percentage_error
def calculate_selected_point_cost(selected_points, callpath, metric, experiment_coordinates, experiment_measurements):
# calculate selected point cost
selected_cost = 0
for j in range(len(selected_points)):
coordinate = selected_points[j]
coordinate_id = -1
for k in range(len(experiment_coordinates)):
if coordinate == experiment_coordinates[k]:
coordinate_id = k
measurement_temp = experiment_measurements[(callpath, metric)][coordinate_id]
coordinate_cost = 0
if measurement_temp != None:
for k in range(len(measurement_temp.values)):
runtime = np.mean(measurement_temp.values[k])
nr_processes = coordinate.as_tuple()[0]
core_hours = runtime * nr_processes
coordinate_cost += core_hours
selected_cost += coordinate_cost
return selected_cost
def get_extrap_model2(experiment, args, callpath, metric):
# initialize model generator
model_generator = ModelGenerator(
experiment, modeler=args.modeler)
# apply modeler options
modeler = model_generator.modeler
if isinstance(modeler, MultiParameterModeler) and args.modeler_options:
# set single-parameter modeler of multi-parameter modeler
single_modeler = args.modeler_options[SINGLE_PARAMETER_MODELER_KEY]
if single_modeler is not None:
modeler.single_parameter_modeler = single_parameter.all_modelers[single_modeler]()
# apply options of single-parameter modeler
if modeler.single_parameter_modeler is not None:
for name, value in args.modeler_options[SINGLE_PARAMETER_OPTIONS_KEY].items():
if value is not None:
setattr(modeler.single_parameter_modeler, name, value)
for name, value in args.modeler_options.items():
if value is not None:
setattr(modeler, name, value)
# create models from data
with ProgressBar(desc='Generating models', disable=True) as pbar:
model_generator.model_all(pbar)
modeler = experiment.modelers[0]
models = modeler.models
model = models[(callpath, metric)]
hypothesis = model.hypothesis
function = hypothesis.function
function_string = function.to_string(*experiment.parameters)
extrap_function_string = function_string
return extrap_function_string, model
def create_experiment(selected_coord_list, nr_parameters, parameter_placeholders, callpath, metric, experiment_coordinates, experiment_measurements):
# create new experiment with only the selected measurements and points as coordinates and measurements
experiment_generic = Experiment()
for j in range(nr_parameters):
experiment_generic.add_parameter(Parameter(parameter_placeholders[j]))
experiment_generic.add_callpath(callpath)
experiment_generic.add_metric(metric)
for j in range(len(selected_coord_list)):
coordinate = selected_coord_list[j]
experiment_generic.add_coordinate(coordinate)
coordinate_id = -1
for k in range(len(experiment_coordinates)):
if coordinate == experiment_coordinates[k]:
coordinate_id = k
measurement_temp = experiment_measurements[(callpath, metric)][coordinate_id]
if measurement_temp != None:
experiment_generic.add_measurement(Measurement(coordinate, callpath, metric, measurement_temp.values))
return experiment_generic
def calculate_selected_point_cost_base(selected_points, callpath, metric, nr_base_points, experiment_coordinates, experiment_measurements):
selected_cost = 0
for j in range(len(selected_points)):
coordinate = selected_points[j]
coordinate_id = -1
for k in range(len(experiment_coordinates)):
if coordinate == experiment_coordinates[k]:
coordinate_id = k
measurement_temp = experiment_measurements[(callpath, metric)][coordinate_id]
coordinate_cost = 0
if measurement_temp != None:
counter = 0
while counter < nr_base_points:
runtime = np.mean(measurement_temp.values[counter])
nr_processes = coordinate.as_tuple()[0]
core_hours = runtime * nr_processes
coordinate_cost += core_hours
counter += 1
selected_cost += coordinate_cost
return selected_cost
def analyze_callpath(inputs):
# get the values from the parallel input dict
callpath_id = inputs[0]
shared_dict = inputs[1]
cost = inputs[2]
callpath = inputs[3]
cost_container = inputs[4]
total_costs_container = inputs[5]
grid_search = inputs[6]
experiment_measurements = inputs[7]
nr_parameters = inputs[8]
experiment_coordinates = inputs[9]
metric = inputs[10]
base_values = inputs[11]
metric_id = inputs[12]
nr_repetitions = inputs[13]
parameters = inputs[14]
args = inputs[15]
budget = inputs[16]
eval_point = inputs[17]
all_points_functions_strings = inputs[18]
coordinate_evaluation = inputs[19]
measurement_evaluation = inputs[20]
normalization = inputs[21]
min_points = inputs[22]
hybrid_switch = inputs[23]
newonly = inputs[24]
result_container = {}
# prepare dicts for saving the accuracy analysis data
acurracy_bucket_counter_full = {}
acurracy_bucket_counter_full["rest"] = 0
acurracy_bucket_counter_full["5"] = 0
acurracy_bucket_counter_full["10"] = 0
acurracy_bucket_counter_full["15"] = 0
acurracy_bucket_counter_full["20"] = 0
acurracy_bucket_counter_generic = {}
acurracy_bucket_counter_generic["rest"] = 0
acurracy_bucket_counter_generic["5"] = 0
acurracy_bucket_counter_generic["10"] = 0
acurracy_bucket_counter_generic["15"] = 0
acurracy_bucket_counter_generic["20"] = 0
percentage_cost_generic_container = []
add_points_generic_container = []
acurracy_bucket_counter_gpr = {}
acurracy_bucket_counter_gpr["rest"] = 0
acurracy_bucket_counter_gpr["5"] = 0
acurracy_bucket_counter_gpr["10"] = 0
acurracy_bucket_counter_gpr["15"] = 0
acurracy_bucket_counter_gpr["20"] = 0
percentage_cost_gpr_container = []
add_points_gpr_container = []
acurracy_bucket_counter_hybrid = {}
acurracy_bucket_counter_hybrid["rest"] = 0
acurracy_bucket_counter_hybrid["5"] = 0
acurracy_bucket_counter_hybrid["10"] = 0
acurracy_bucket_counter_hybrid["15"] = 0
acurracy_bucket_counter_hybrid["20"] = 0
percentage_cost_hybrid_container = []
add_points_hybrid_container = []
# random
acurracy_bucket_counter_random = {}
acurracy_bucket_counter_random["rest"] = 0
acurracy_bucket_counter_random["5"] = 0
acurracy_bucket_counter_random["10"] = 0
acurracy_bucket_counter_random["15"] = 0
acurracy_bucket_counter_random["20"] = 0
percentage_cost_random_container = []
add_points_random_container = []
# grid
acurracy_bucket_counter_grid = {}
acurracy_bucket_counter_grid["rest"] = 0
acurracy_bucket_counter_grid["5"] = 0
acurracy_bucket_counter_grid["10"] = 0
acurracy_bucket_counter_grid["15"] = 0
acurracy_bucket_counter_grid["20"] = 0
percentage_cost_grid_container = []
add_points_grid_container = []
# bayesian
acurracy_bucket_counter_bayesian = {}
acurracy_bucket_counter_bayesian["rest"] = 0
acurracy_bucket_counter_bayesian["5"] = 0
acurracy_bucket_counter_bayesian["10"] = 0
acurracy_bucket_counter_bayesian["15"] = 0
acurracy_bucket_counter_bayesian["20"] = 0
percentage_cost_bayesian_container = []
add_points_bayesian_container = []
callpath_string = callpath.name
# get the cost values for this particular callpath
cost = cost_container[callpath_string]
total_cost = total_costs_container[callpath_string]
# create copy of the cost dict
remaining_points = copy.deepcopy(cost)
##########################
## Base point selection ##
##########################
# create copy of the cost dict
remaining_points = copy.deepcopy(cost)
# create copy of the cost dict for the minimum experiment with gpr and hybrid strategies
remaining_points_min = copy.deepcopy(cost)
if grid_search == 2 or grid_search == 3:
measurements_gpr = copy.deepcopy(experiment_measurements)
measurements_hybrid = copy.deepcopy(experiment_measurements)
measurements_bayesian = copy.deepcopy(experiment_measurements)
if nr_parameters == 2:
# find the cheapest line of 5 points for y
y_lines = {}
for i in range(len(experiment_coordinates)):
cord_values = experiment_coordinates[i].as_tuple()
x = cord_values[0]
y = []
for j in range(len(experiment_coordinates)):
cord_values2 = experiment_coordinates[j].as_tuple()
if cord_values2[0] == x:
y.append(cord_values2[1])
if len(y) == 5:
#print("x:",x)
#print("y:",y)
if x not in y_lines:
y_lines[x] = y
#print("y_lines:",y_lines)
# calculate the cost of each of the lines
line_costs = {}
for key, value in y_lines.items():
line_cost = 0
for i in range(len(value)):
point_cost = sum(cost[Coordinate(key, value[i])])
line_cost += point_cost
line_costs[key] = line_cost
#print("line_costs:",line_costs)
x_value = min(line_costs, key=line_costs.get)
y_values = y_lines[min(line_costs, key=line_costs.get)]
#print("y_values:",y_values)
# remove these points from the list of remaining points
for j in range(len(y_values)):
try:
cord = Coordinate(x_value, y_values[j])
remaining_points.pop(cord)
if grid_search == 2 or grid_search == 3:
for x in measurements_gpr[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_hybrid[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_bayesian[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
# also delete the cost values from the remaining min dict
for i in range(base_values):
remaining_points_min[cord].pop(0)
except KeyError:
pass
# add these points to the list of selected points
selected_points = []
for i in range(len(y_values)):
cord = Coordinate(x_value, y_values[i])
selected_points.append(cord)
#print("selected_points:",selected_points)
# find the cheapest line of 5 points for x
x_lines = {}
for i in range(len(experiment_coordinates)):
cord_values = experiment_coordinates[i].as_tuple()
y = cord_values[1]
x = []
for j in range(len(experiment_coordinates)):
cord_values2 = experiment_coordinates[j].as_tuple()
if cord_values2[1] == y:
x.append(cord_values2[0])
if len(x) == 5:
#print("x:",x)
#print("y:",y)
if y not in x_lines:
x_lines[y] = x
#print("x_lines:",x_lines)
# calculate the cost of each of the lines
line_costs = {}
for key, value in x_lines.items():
line_cost = 0
for i in range(len(value)):
point_cost = sum(cost[Coordinate(value[i], key)])
line_cost += point_cost
line_costs[key] = line_cost
#print("line_costs:",line_costs)
y_value = min(line_costs, key=line_costs.get)
x_values = x_lines[min(line_costs, key=line_costs.get)]
#print("x_values:",x_values)
# remove these points from the list of remaining points
for j in range(len(x_values)):
try:
cord = Coordinate(x_values[j], y_value)
remaining_points.pop(cord)
if grid_search == 2 or grid_search == 3:
for x in measurements_gpr[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_hybrid[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_bayesian[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
except KeyError:
pass
# add these points to the list of selected points
for i in range(len(x_values)):
cord = Coordinate(x_values[i], y_value)
exists = False
for j in range(len(selected_points)):
if selected_points[j] == cord:
exists = True
break
if exists == False:
selected_points.append(cord)
elif nr_parameters == 3:
# find the cheapest line of 5 points for y
y_lines = {}
for i in range(len(experiment_coordinates)):
cord_values = experiment_coordinates[i].as_tuple()
x = cord_values[0]
y = []
z = cord_values[2]
for j in range(len(experiment_coordinates)):
cord_values2 = experiment_coordinates[j].as_tuple()
if cord_values2[0] == x and cord_values2[2] == z:
y.append(cord_values2[1])
#print("y:",y)
if len(y) >= 5:
#print("x:",x)
#print("y:",y)
#print("z:",z)
if (x,z) not in y_lines:
y_lines[(x,z)] = y
#print("y_lines:",y_lines)
# calculate the cost of each of the lines
line_costs = {}
for key, value in y_lines.items():
line_cost = 0
for i in range(len(value)):
point_cost = sum(cost[Coordinate(key[0], value[i], key[1])])
line_cost += point_cost
line_costs[key] = line_cost
#print("line_costs:",line_costs)
x_value, z_value = min(line_costs, key=line_costs.get)
y_values = y_lines[min(line_costs, key=line_costs.get)]
#print("values:",x_value, z_value)
#print("y_values:",y_values)
# remove these points from the list of remaining points
for j in range(len(y_values)):
try:
cord = Coordinate(x_value, y_values[j], z_value)
remaining_points.pop(cord)
if grid_search == 2 or grid_search == 3:
for x in measurements_gpr[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_hybrid[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_bayesian[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
# also delete the cost values from the remaining min dict
for i in range(base_values):
remaining_points_min[cord].pop(0)
except KeyError:
pass
# add these points to the list of selected points
selected_points = []
for i in range(len(y_values)):
cord = Coordinate(x_value, y_values[i], z_value)
selected_points.append(cord)
#print("selected_points:",selected_points)
# find the cheapest line of 5 points for x
x_lines = {}
for i in range(len(experiment_coordinates)):
cord_values = experiment_coordinates[i].as_tuple()
y = cord_values[1]
x = []
z = cord_values[2]
for j in range(len(experiment_coordinates)):
cord_values2 = experiment_coordinates[j].as_tuple()
if cord_values2[1] == y and cord_values2[2] == z:
x.append(cord_values2[0])
if len(x) >= 5:
#print("x:",x)
#print("y:",y)
if (y,z) not in x_lines:
x_lines[(y,z)] = x
#print("x_lines:",x_lines)
# calculate the cost of each of the lines
line_costs = {}
for key, value in x_lines.items():
line_cost = 0
for i in range(len(value)):
point_cost = sum(cost[Coordinate(value[i], key[0], key[1])])
line_cost += point_cost
line_costs[key] = line_cost
#print("line_costs:",line_costs)
y_value, z_value = min(line_costs, key=line_costs.get)
x_values = x_lines[min(line_costs, key=line_costs.get)]
#print("x_values:",x_values)
# remove these points from the list of remaining points
for j in range(len(x_values)):
try:
cord = Coordinate(x_values[j], y_value, z_value)
remaining_points.pop(cord)
if grid_search == 2 or grid_search == 3:
for x in measurements_gpr[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_hybrid[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_bayesian[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
# also delete the cost values from the remaining min dict
for i in range(base_values):
remaining_points_min[cord].pop(0)
except KeyError:
pass
# add these points to the list of selected points
for i in range(len(x_values)):
cord = Coordinate(x_values[i], y_value, z_value)
exists = False
for j in range(len(selected_points)):
if selected_points[j] == cord:
exists = True
break
if exists == False:
selected_points.append(cord)
#print("selected_points:",selected_points)
# find the cheapest line of 5 points for z
z_lines = {}
for i in range(len(experiment_coordinates)):
cord_values = experiment_coordinates[i].as_tuple()
x = cord_values[0]
z = []
y = cord_values[1]
for j in range(len(experiment_coordinates)):
cord_values2 = experiment_coordinates[j].as_tuple()
if cord_values2[0] == x and cord_values2[1] == y:
z.append(cord_values2[2])
if len(z) >= 5:
#print("z:",z)
#print("y:",y)
#print("x:",x)
if (x,y) not in z_lines:
z_lines[(x,y)] = z
#print("z_lines:",z_lines)
# calculate the cost of each of the lines
line_costs = {}
for key, value in z_lines.items():
line_cost = 0
for i in range(len(value)):
point_cost = sum(cost[Coordinate(key[0], key[1], value[i])])
line_cost += point_cost
line_costs[key] = line_cost
#print("line_costs:",line_costs)
x_value, y_value = min(line_costs, key=line_costs.get)
z_values = z_lines[min(line_costs, key=line_costs.get)]
#print("z_values:",z_values)
# remove these points from the list of remaining points
for j in range(len(z_values)):
try:
cord = Coordinate(x_value, y_value, z_values[j])
remaining_points.pop(cord)
if grid_search == 2 or grid_search == 3:
for x in measurements_gpr[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_hybrid[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
for x in measurements_bayesian[(callpath, metric)]:
if x.coordinate == cord:
temp = x.values
for i in range(base_values):
temp = np.delete(temp, 0, 0)
x.values = temp
# also delete the cost values from the remaining min dict
for i in range(base_values):
remaining_points_min[cord].pop(0)
except KeyError:
pass
# add these points to the list of selected points
for i in range(len(z_values)):
cord = Coordinate(x_value, y_value, z_values[i])
exists = False
for j in range(len(selected_points)):
if selected_points[j] == cord:
exists = True
break
if exists == False:
selected_points.append(cord)
#print("selected_points:",selected_points)
else:
return 1
# calculate the cost for the selected base points
if grid_search == 2 or grid_search == 3:
#print("DEBUG2:",metric_id)
base_point_cost = calculate_selected_point_cost_base(selected_points, callpath, metric, base_values, experiment_coordinates, experiment_measurements)
elif grid_search == 1 or grid_search == 4:
base_point_cost = calculate_selected_point_cost(selected_points, callpath, metric, experiment_coordinates, experiment_measurements)
if total_cost == 0.0:
base_point_cost = 0.0
else:
base_point_cost = base_point_cost / (total_cost / 100)
#print("base_point_cost %:",base_point_cost)
result_container["base_point_cost"] = base_point_cost
added_points_generic = len(selected_points) * (nr_repetitions)
######################
## Generic strategy ##
######################
remaining_points_generic = copy.deepcopy(remaining_points)
selected_points_generic = copy.deepcopy(selected_points)
# create first model
experiment_generic_base = create_experiment(selected_points_generic, nr_parameters, parameters, callpath, metric, experiment_coordinates, experiment_measurements)
_, model = get_extrap_model2(experiment_generic_base, args, callpath, metric)
hypothesis = model.hypothesis
# calculate selected point cost
current_cost = calculate_selected_point_cost(selected_points_generic, callpath, metric, experiment_coordinates, experiment_measurements)
if total_cost == 0.0:
current_cost_percent = 0.0
else:
current_cost_percent = current_cost / (total_cost / 100)
if current_cost_percent <= budget:
if newonly == False:
while True:
# find another point for selection
remaining_points_new, selected_coord_list_new, new_point = add_additional_point_generic(remaining_points_generic, selected_points_generic)
# calculate selected point cost
current_cost = calculate_selected_point_cost(selected_coord_list_new, callpath, metric, experiment_coordinates, experiment_measurements)
if total_cost == 0.0:
current_cost_percent = 0.0
else:
current_cost_percent = current_cost / (total_cost / 100)
# current cost exceeds budget so break the loop
#print("current_cost_percent > budget", current_cost_percent, budget)
# to make sure no mistakes occur here
# sometimes the numbers do not perfectly add up to the target budget
# but to 100.00001
# this is the fix for this case
current_cost_percent = float("{0:.2f}".format(current_cost_percent))
#print("current_cost_percent:",current_cost_percent)
if current_cost_percent > budget:
break
# add the new found point
else:
# increment counter value, because a new measurement point was added
#added_points_generic += 1
added_points_generic += nr_repetitions
# create new model
experiment_generic_base = create_experiment(selected_coord_list_new, nr_parameters, parameters, callpath, metric, experiment_coordinates, experiment_measurements)
selected_points_generic = selected_coord_list_new
remaining_points_generic = remaining_points_new
# if there are no points remaining that can be selected break the loop
if len(remaining_points_generic) == 0:
break
else:
pass
# calculate selected point cost
selected_cost = calculate_selected_point_cost(selected_points_generic, callpath, metric, experiment_coordinates, experiment_measurements)
# calculate the percentage of cost of the selected points compared to the total cost of the full matrix
if total_cost == 0.0:
percentage_cost_generic = 0.0
else:
percentage_cost_generic = selected_cost / (total_cost / 100)
if percentage_cost_generic >= 99.9:
percentage_cost_generic = 100
result_container["percentage_cost_generic"] = percentage_cost_generic
# calculate number of additionally used data points (exceeding the base requirement of the sparse modeler)
result_container["add_points_generic"] = added_points_generic
#add_points_generic = added_points_generic
# create model using point selection of generic strategy
model_generic, _ = get_extrap_model2(experiment_generic_base, args, callpath, metric)
# evaluate model accuracy against the first point in each direction of the parameter set for each parameter
if parameters[0] == "p" and parameters[1] == "size":
p = int(eval_point[0])
size = int(eval_point[1])
elif parameters[0] == "p" and parameters[1] == "n":
p = int(eval_point[0])
n = int(eval_point[1])
elif parameters[0] == "p" and parameters[1] == "s":
p = int(eval_point[0])
s = int(eval_point[1])
elif parameters[0] == "p" and parameters[1] == "d" and parameters[2] == "g":
p = int(eval_point[0])
d = int(eval_point[1])
g = int(eval_point[2])
elif parameters[0] == "p" and parameters[1] == "m" and parameters[2] == "n":
p = int(eval_point[0])
m = int(eval_point[1])
n = int(eval_point[2])
prediction_full = eval(all_points_functions_strings[callpath_string])
#print("prediction_full:",prediction_full)
prediction_generic = eval(model_generic)
#print("prediction_generic:",prediction_generic)
# get the actual measured value
eval_measurement = None
if nr_parameters == 2:
for o in range(len(coordinate_evaluation)):
parameter_values = coordinate_evaluation[o].as_tuple()
#print("parameter_values:",parameter_values)
if parameter_values[0] == float(eval_point[0]) and parameter_values[1] == float(eval_point[1]):
eval_measurement = measurement_evaluation[callpath, metric][o]
break
elif nr_parameters == 3:
for o in range(len(coordinate_evaluation)):
parameter_values = coordinate_evaluation[o].as_tuple()
#print("parameter_values:",parameter_values)
if parameter_values[0] == float(eval_point[0]) and parameter_values[1] == float(eval_point[1]) and parameter_values[2] == float(eval_point[2]):
eval_measurement = measurement_evaluation[callpath, metric][o]
break
else:
return 1
#print("eval_measurement:",eval_measurement)
actual = eval_measurement.mean
#print("actual:",actual)
# get the percentage error for the full matrix of points
error_full = abs(percentage_error(actual, prediction_full))
#print("error_full:",error_full)
# get the percentage error for the full matrix of points
#error_generic = abs(percentage_error(actual, prediction_generic))
#print("error_generic:",error_generic)
# get the percentage error for the generic strategy
if percentage_cost_generic <= budget:
error_generic = abs(percentage_error(actual, prediction_generic))
else:
error_generic = 100
#print("error_generic:",error_generic)
# increment accuracy bucket for full matrix of points
acurracy_bucket_counter_full = increment_accuracy_bucket(acurracy_bucket_counter_full, error_full)
# increment accuracy bucket for generic strategy
acurracy_bucket_counter_generic = increment_accuracy_bucket(acurracy_bucket_counter_generic, error_generic)
##################
## GPR strategy ##
##################
# GPR parameter-value normalization for each measurement point
normalization_factors = {}
if normalization:
for i in range(nr_parameters):
param_value_max = -1
for coord in experiment_coordinates:
temp = coord.as_tuple()[i]
if param_value_max < temp:
param_value_max = temp
param_value_max = 100 / param_value_max
normalization_factors[Parameter(parameters[i])] = param_value_max
#print("normalization_factors:",normalization_factors)
# do an noise analysis on the existing points
mm = experiment_measurements
#print("DEBUG:",mm)
nn = mm[(callpath, metric)]
#print("DEBUG:",nn)