forked from MiguelTaibo/MOOEasyTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (83 loc) · 2.95 KB
/
main.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
import os
import numpy as np
import gpflow
import matplotlib.pyplot as plt
import pandas as pd
from models.GaussianProcess import GaussianProcess
from acquisition_functions.MES import mes_acq, basic_mes_acq
from acquisition_functions.PESMO import pesmo_acq
from acquisition_functions.MESMO import mesmo_acq
from acquisition_functions.UseMO import usemo_acq
from arguments.arguments import MainArguments
from MOObenchmark import MOOackley, MOOquadratic
from utils.calc_pareto import get_pareto_undominated_by, getSetfromFront
### Definition of inside parameters
Aguments = MainArguments()
args = Aguments.parse()
outputFile = os.path.join(args.dir_path, args.output_file+'.csv')
if args.save:
Aguments.writeArguments(outputFile)
d = args.d
seed = args.seed
np.random.seed(seed)
total_iter = args.total_iter
initial_iter = args.initial_iter
lowerBounds = [args.lower_bound]*d
upperBounds = [args.upper_bound]*d
### Definitions of outside parameters
def evaluation(x):
return MOOquadratic(x, c1=-.5, c2=.5)
O = 2
C = 0
N = 10001
X = np.linspace(args.lower_bound,args.upper_bound,N)
Z = np.zeros((N,2))
for i in range(N):
Z[i]=evaluation(X[i])
real_pareto = get_pareto_undominated_by(np.reshape(Z,(-1,2)))
real_pareto = real_pareto[np.argsort(real_pareto[:,1])]
pareto_set = getSetfromFront(X,Z,real_pareto)
### GPs Initialization
GP = GaussianProcess(O, C, d, lowerBounds, upperBounds, noise_variance=2e-6)
if args.save:
GP.writeGPHeader(outputFile)
### Initial samples, at least 1
for l in range(initial_iter):
## Get random evaluation point
while True:
x_rand = np.random.uniform(lowerBounds[0], upperBounds[0], d)
if GP.X is None or not x_rand in GP.X:
break
## EVALUATION OF THE OUTSIDE FUNCTION
y_rand = evaluation(x_rand)
GP.addSample(x_rand,y_rand, args.save, outputFile)
GP.updateGP()
GP.optimizeKernel()
if args.showplots:
GP.plotSamples()
row = {
'ns' : len(GP.X),
'x' : x_rand,
'y' : y_rand
}
# metrics = GP.evaluatePareto(real_pareto, showparetos = False, saveparetos = True)
# row.update(metrics)
# df = pd.DataFrame({k: [v] for k, v in row.items()})
for l in range(total_iter):
## Search of the best acquisition function
x_best, acq_best = usemo_acq(GP, function = "ei", showplots = args.showplots)
## EVALUATION OF THE OUTSIDE FUNCTION
y_best = evaluation(x_best)
## UPDATE
GP.addSample(x_best,y_best, args.save, outputFile) ## Add new sample to the model
GP.updateGP() ## Update data on the GP regressor
GP.optimizeKernel() ## Optimize kernel hyperparameters
## Evaluate Pareto (distances and hypervolumes)
# row = {
# 'ns' : len(GP.X),
# 'x' : x_best,
# 'y' : y_best
# }
# metrics = GP.evaluatePareto(real_pareto, showparetos = False, saveparetos = True)
# row.update(metrics)
# df = df.append(row, ignore_index = True)