-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOPBO.py
129 lines (74 loc) · 3.33 KB
/
DOPBO.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import random
import physbo
######################
# parameters
filename = "data.csv" # target dataset, final row indicates the present objective function and amounts of liquid samples
drainage_index = 4 # drainage_index * 10 % is the maximum amount of drainage
delta = 0.1 # minimum unit of injection amounts
add_max = 10 # Delta = delta * add_max is the maximum total injection amounts
random_seed = 1
######################
def read_initial_data(filename):
arr = np.genfromtxt(filename, skip_header=1, delimiter=',')
return arr
def bayesianopt(arr, seed_index, emi_num):
num_search_each_probe = 1
score = 'TS'
num_random_selection = 1
t_train, X_all, train_actions, test_actions, recipe = load_data(arr, emi_num)
X_all_concent=[]
for i in range(len(X_all)):
X_all_concent.append(X_all[i]/sum(X_all[i]))
X_all_concent = np.array(X_all_concent)
if len(t_train) > num_random_selection:
calculated_ids=train_actions
t_initial=np.array(t_train)
X = physbo.misc.centering( X_all_concent )
policy = physbo.search.discrete.policy(test_X=X,initial_data=[calculated_ids, t_initial])
policy.set_seed(seed_index)
actions = policy.bayes_search(max_num_probes=1, num_search_each_probe=num_search_each_probe,
simulator=None, score=score, interval=0, num_rand_basis = 5000)
else:
actions = [random.choice(test_actions)]
recommendation = X_all[actions[0]]
add_recommendation = [recipe[actions[0]][i+1]*delta for i in range(3)]
reduce_volume = sum(X_all[len(train_actions)-1])*0.1*recipe[actions[0]][0]
print("present concentration:", X_all[len(train_actions)-1], )
print("drainage:", np.round(reduce_volume, decimals=4))
print("injection:", np.round(add_recommendation, decimals=4))
print("next concentration:", recommendation)
return recommendation, sum(add_recommendation), reduce_volume, sum(recommendation)
def load_data(arr, emi_num):
if arr.ndim == 1:
X_train = [arr[1:]]
t_train = [arr[0]]
else:
X_train = arr[:, 1:]
t_train = arr[:, 0]
X_all=[]
recipe=[]
#training
for i in range(len(X_train)):
X_all.append(X_train[i])
recipe.append([0, 0, 0, 0])
for emit in range(emi_num+1):
for k in range(1,add_max+1):
X_all.append([X_train[len(X_train)-1][0]*(1-emit*0.1)+k*delta,X_train[len(X_train)-1][1]*(1-emit*0.1),X_train[len(X_train)-1][2]*(1-emit*0.1)])
recipe.append([emit, k, 0, 0])
for i in range(k):
a = k-i-1
for j in range(k-a+1):
b = j
c = k-a-b
X_all.append([X_train[-1][0]*(1-emit*0.1)+a*delta, X_train[-1][1]*(1-emit*0.1)+b*delta, X_train[-1][2]*(1-emit*0.1)+c*delta])
recipe.append([emit, a, b, c])
X_all = np.round(X_all, decimals=5)
all_actions = [i for i in range(len(X_all))]
train_actions = [i for i in range(len(X_train))]
test_actions = list(set(all_actions) - set(train_actions))
test_actions.sort()
return t_train, X_all, train_actions, test_actions, recipe
if __name__ == '__main__':
data = read_initial_data(filename)
recommendation, cost, reduce, amount = bayesianopt(data, random_seed, drainage_index)