-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_weighted_sum.py
84 lines (77 loc) · 2.95 KB
/
calc_weighted_sum.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
"""
Calculate a set of metrics, then calculate the weighted sum for the specified
metrics for a given input map.
"""
def ws_3_metrics(v1, v2, v3, w1, w2, w3, r1, r2, r3, s1, s2, s3):
# Determine the scaled values (range of 0 - 1)
scaled_value_1 = (v1 - min(r1))/(max(r1) - min(r1))
scaled_value_2 = (v2 - min(r2)) / (max(r2) - min(r2))
scaled_value_3 = (v3 - min(r3)) / (max(r3) - min(r3))
# Initialise the total weighted sum.
total = 0
# If want to maximise the first objective, add to total.
if s1 == "maximise":
total = total + w1 * scaled_value_1
# Otherwise, subtract from total.
elif s1 == "minimise":
total = total - w1 * scaled_value_1
# If want to maximise the second objective, add to total.
if s2 == "maximise":
total = total + w2 * scaled_value_2
# Otherwise, subtract from total.
elif s2 == "minimise":
total = total - w2 * scaled_value_2
# If want to maximise the third objective, add to total.
if s3 == "maximise":
total = total + w3 * scaled_value_3
# Otherwise, subtract from total.
elif s3 == "minimise":
total = total - w3 * scaled_value_3
# Return to weighted total sum.
return total
def ws_2_metrics(v1, v2, w1, w2, r1, r2, s1, s2):
# Determine the scaled values (range of 0 - 1)
scaled_value_1 = (v1 - min(r1)) / (max(r1) - min(r1))
scaled_value_2 = (v2 - min(r2)) / (max(r2) - min(r2))
# Initialise the total weighted sum.
total = 0
# If want to maximise the first objective, add to total.
if s1 == "maximise":
total = total + w1 * scaled_value_1
# Otherwise, subtract from total.
elif s1 == "minimise":
total = total - w1 * scaled_value_1
# If want to maximise the second objective, add to total.
if s2 == "maximise":
total = total + w2 * scaled_value_2
# Otherwise, subtract from total.
elif s2 == "minimise":
total = total - w2 * scaled_value_2
# Return to weighted total sum.
return total
def ws(metrics, no_metrics):
# Determine the weighted sum for a set of input metrics.
# Metrics is a dictionary that stores a list of information corresponding
# to each metrics used. The form is:
# metrics[index #][value, weight, min_value, max_value, form (0 = min, 1 = max)]
# Initialise the total weighted sum.
total = 0
for i in range(0, no_metrics):
scaled_value = (metrics[i][0] - metrics[i][2]) / (metrics[i][3] -
metrics[i][2])
if metrics[i][4] == "maximise":
total = total + scaled_value * metrics[i][1]
elif metrics[i][4] == "minimise":
total = total - scaled_value * metrics[i][1]
return total
# Module test.
"""
metrics = {
0: [0.89, 0.33, 0.85, 1, "maximise"],
1: [0.05, 0.33, 0, 0.1, "maximise"],
2: [0.003, 0.5, 0, 0.004, "minimise"]
}
no_metrics = 3
weighted_sum = ws(metrics, no_metrics)
print(weighted_sum)
"""