Skip to content

Instantly share code, notes, and snippets.

@yohanesnuwara
Created August 23, 2021 11:58
Show Gist options
  • Save yohanesnuwara/932388539ac9e20e2a07f4190bae19ae to your computer and use it in GitHub Desktop.
Save yohanesnuwara/932388539ac9e20e2a07f4190bae19ae to your computer and use it in GitHub Desktop.
Code for optimization 4
import pyswarm as pso
# Define objective function. "pipe" as model
def f(X):
return -pipe.predict(X.reshape(1,-1)) # Minus sign to optimize
# Lower bounds of feature variables in the order of X.columns
lb = np.array([3480, 2e4, 1.5, 0.09, 0.1, 1, 0.001])
# Upper bounds of feature variables in the order of X.columns
ub = np.array([3480, 9e4, 2.5, 0.09, 0.1, 1, 0.001])
ub += 1e-10
# Solve optimization
xopt, fopt = pso(f, lb, ub, swarmsize=200, omega=0.3, phip=.5,
phig=0.7, maxiter=1000, minstep=1e-8)
# Print values that causes maximum ROP from xopt
for i in range(len(X.columns)):
print(f'{X.columns[i]}: {xopt[i]:.2f}')
# Print value of maximum ROP from popt
print(f'Maximum ROP achieved: {-fopt}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment