-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExampleOnTestFunction.py
78 lines (65 loc) · 2.58 KB
/
ExampleOnTestFunction.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
import matplotlib.pyplot as plt
from SVMCBO_code.SVMCBO import SVMCBO
from Test_Functions.test_function_suite import *
## Example optimization Mishra Bird test function ############################################
test_f = mishra_bird_c
optimum_for_gap = -106.7645367
## Experiment with SVM-CBO
exp_svmcbo = SVMCBO(f=test_f, surrogate="GP")
exp_svmcbo.init_opt()
exp_svmcbo.phase1()
exp_svmcbo.phase2()
res = exp_svmcbo.generate_result()
print(f"Optimum point: {res.get('x')}")
print(f"Optimal value: {res.get('fun')}")
gap_metric_svmcbo = exp_svmcbo.gap_metric(optimum_value=optimum_for_gap)
## Experiment with SVM-CBO_RF
exp_svmcbo_rf = SVMCBO(f=test_f, surrogate="RF")
exp_svmcbo_rf.init_opt()
exp_svmcbo_rf.phase1()
exp_svmcbo_rf.phase2()
res = exp_svmcbo_rf.generate_result()
print(f"Optimum point: {res.get('x')}")
print(f"Optimal value: {res.get('fun')}")
gap_metric_svmcbo_rf = exp_svmcbo_rf.gap_metric(optimum_value=optimum_for_gap)
## Comparison gap metric between SVMCBO and SVMCBO_RF
plt.plot(gap_metric_svmcbo, label="SVM-CBO")
plt.plot(gap_metric_svmcbo_rf, label="SVM-CBO_RF")
plt.ylim(0.0,1.1)
plt.ylabel("Gap Metric")
plt.xlabel("Iterations")
plt.title("Comparison gap metric on {} test function".format(test_f.__name__))
plt.legend(loc="best")
plt.show()
##################################################################################################
## Example optimization Michalewicz test function ############################################
test_f = michalewicz_c
optimum_for_gap = -1.801140718473825
## Experiment with SVM-CBO
exp_svmcbo = SVMCBO(f=test_f, surrogate="GP")
exp_svmcbo.init_opt()
exp_svmcbo.phase1()
exp_svmcbo.phase2()
res = exp_svmcbo.generate_result()
print(f"Optimum point: {res.get('x')}")
print(f"Optimal value: {res.get('fun')}")
gap_metric_svmcbo = exp_svmcbo.gap_metric(optimum_value=optimum_for_gap)
## Experiment with SVM-CBO_RF
exp_svmcbo_rf = SVMCBO(f=test_f, surrogate="RF")
exp_svmcbo_rf.init_opt()
exp_svmcbo_rf.phase1()
exp_svmcbo_rf.phase2()
res = exp_svmcbo_rf.generate_result()
print(f"Optimum point: {res.get('x')}")
print(f"Optimal value: {res.get('fun')}")
gap_metric_svmcbo_rf = exp_svmcbo_rf.gap_metric(optimum_value=optimum_for_gap)
## Comparison gap metric between SVMCBO and SVMCBO_RF
plt.plot(gap_metric_svmcbo, label="SVM-CBO")
plt.plot(gap_metric_svmcbo_rf, label="SVM-CBO_RF")
plt.ylim(0.0,1.1)
plt.ylabel("Gap Metric")
plt.xlabel("Iterations")
plt.title("Comparison gap metric on {} test function".format(test_f.__name__))
plt.legend(loc="best")
plt.show()
##################################################################################################