-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimizer.py
96 lines (75 loc) · 5.19 KB
/
optimizer.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
import time
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore', category = FutureWarning)
def row_optimizer(model, row_data, pct_range, constant_params = [], flex_params = [], all_result = True):
"""
Helper function for finding optimal parameters within a specified range that minimizes predicted cuttings concentration
Parameters
-----------
model : trained model to be used for optimizing drilling parameters
row_data : series object containing the current drilling parameters
pct_range : float, specifies what percentage range of the current values the search should be conducted in
constant_params : parameters to be left the same
flex_params : parameters to permutate over
all_result : returns the optimized values and the predicted concentration if true, else returns just the predicted concentration of the optimized parameters
Returns
--------
This function retuns a dataframe containing the set of parameter values that produces the minimum concentration observed for the given parameter space
"""
idx_names = {'Density': 'density', 'YP': 'yp', 'Temperature': 'temp', 'ROP': 'rop', 'Pipe rotation': 'pipe_rot', 'Flow rate': 'flow_rate',
'Inclination':'inclination', 'Eccentricity':'eccentricity'}
row_data.rename(idx_names, inplace = True)
constant_params = [idx_names[x] for x in constant_params]
flex_params = [idx_names[x] for x in flex_params]
myVars = globals()
for col in set(row_data.index).difference(flex_params).union(constant_params):
myVars[col] = pd.DataFrame({col: row_data[col]}, index = np.repeat(0, 1))
for col in set(row_data.index).difference(constant_params).union(flex_params):
myVars[col] = np.linspace(row_data[col] * (1-pct_range), row_data[col] * (1+pct_range), 10)
myVars[col] = pd.DataFrame({col: myVars[col]}, index = np.repeat(0, len(myVars[col])))
feature_combinations = pd.merge(density, pd.merge(yp, pd.merge(temp, pd.merge(rop, pd.merge(pipe_rot,
pd.merge(flow_rate, pd.merge(inclination, eccentricity, 'cross'), 'cross'),
'cross'), 'cross'), 'cross'), 'cross'), 'cross')
feature_combinations['Concentration'] = np.round(model.predict(feature_combinations), 3)
if not all_result:
return min(feature_combinations['Concentration'])
return [pd.DataFrame({str(model.__class__).split('.')[-1][:-2]:feature_combinations.iloc[feature_combinations['Concentration'].argmin(),:-1]}), min(feature_combinations['Concentration'])]
def grid_optimizer(model, density, yp, temp, rop, pipe_rot, flow_rate, inclination, eccentricity):
"""
Helper function for finding optimal parameters within a specified range that minimizes predicted cuttings concentration
Parameters
-----------
model : trained model to be used for optimizing drilling parameters
density : list containing densities to be investigated
yp : list containing yield points to be investigated
temp : list containing temperatures to be investigated
rop: list containing rates of penetration to be investigated
pipe_rot : list containing pipe rotation speeds to be investigated
flow_rate : list containing flow rates to be investigated
inclination : list containing inclinations to be investigated
eccentricity : list containing eccentricity values to be investigated
Returns
--------
This function retuns a dataframe containing the set of parameter values that produces the minimum concentration observed for the given parameter space
"""
tic = time.time()
density = pd.DataFrame({'Density': density}, index = np.repeat(0, len(density)))
yp = pd.DataFrame({'YP': yp}, index = np.repeat(0, len(yp)))
temp = pd.DataFrame({'Temperature': temp}, index = np.repeat(0, len(temp)))
rop = pd.DataFrame({'ROP': rop}, index = np.repeat(0, len(rop)))
pipe_rot = pd.DataFrame({'Pipe rotation': pipe_rot}, index = np.repeat(0, len(pipe_rot)))
flow_rate = pd.DataFrame({'Flow rate': flow_rate}, index = np.repeat(0, len(flow_rate)))
inclination = pd.DataFrame({'Inclination': inclination}, index = np.repeat(0, len(inclination)))
eccentricity = pd.DataFrame({'Eccentricity': eccentricity}, index = np.repeat(0, len(eccentricity)))
feature_combinations = pd.merge(density, pd.merge(yp, pd.merge(temp, pd.merge(rop, pd.merge(pipe_rot,
pd.merge(flow_rate, pd.merge(inclination, eccentricity, 'cross'), 'cross'),
'cross'), 'cross'), 'cross'), 'cross'), 'cross')
feature_combinations['Concentration'] = np.round(model.predict(feature_combinations), 3)
toc = time.time()
print(str(model.__class__).split('.')[-1][:-2])
print('-----------------------------------------------------------------------')
print(f'Time taken to search parameter space containing {len(feature_combinations)} combinations: {round(toc - tic, 3)}s')
print('-----------------------------------------------------------------------')
return pd.DataFrame({str(model.__class__).split('.')[-1][:-2]:feature_combinations.iloc[feature_combinations['Concentration'].argmin()]})