-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjective_function.py
67 lines (47 loc) · 2.09 KB
/
objective_function.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
# coding=utf-8
import series_utilities as util
import numpy as np
PENALIZATION_VALUE = 10000
def minimize_slope_std(a_series, *args):
cuts_list, minmax_list = args
a_series_split = _split_series_for_dimension(a_series, cuts_list)
trap_series_split = _rebuild_series(a_series_split, cuts_list, minmax_list)
compute_slope_fun = util.arctan_segment_slope
slope_multi_list = [util.get_slope_list(trap_series, compute_slope_fun) for trap_series in trap_series_split]
slope_std_list = [np.std(slope_list) for slope_list in slope_multi_list]
result = sum(slope_std_list) / len(slope_std_list)
return result
def minimize_slope_sum(a_series, *args):
cuts_list, minmax_list = args
a_series_split = _split_series_for_dimension(a_series, cuts_list)
trap_series_split = _rebuild_series(a_series_split, cuts_list, minmax_list)
compute_slope_fun = util.arctan_segment_slope
slope_multi_list = [util.get_slope_list(trap_series, compute_slope_fun) for trap_series in trap_series_split]
"""
for slope_list in slope_multi_list:
if _rect_instead_trap(slope_list):
return PENALIZATION_VALUE
"""
slope_sum_list = [sum(slope_list) for slope_list in slope_multi_list]
result = sum(slope_sum_list) / len(slope_sum_list)
return -result
def _split_series_for_dimension(a_series, cuts_list):
result = []
for i in range(0, len(cuts_list)):
k = len(cuts_list[i])
result.append(a_series[0:k])
a_series = a_series[k:]
return result
def _rebuild_series(a_series_split, cuts_list, minmax_list):
result = []
for i in range(0, len(a_series_split)):
trap_series = util.generate_series(a_series_split[i], cuts_list[i], minmax_list[i])
result.append(trap_series)
return result
"""
Check if a series contains rectangle instead tapezes.
You can use this function if you want to penalize this kind of series in a objective function
"""
def _rect_instead_trap(series):
series_without_duplicate = [x for x in series if series.count(x) == 1]
return len(series) != len(series_without_duplicate)