import numpy as np from sklearn.neural_network import MLPRegressor from gekko import GEKKO, ML # Create training data for the unknown physics f_true(x) = -0.2*x^2 X = np.linspace(0, 2, 21).reshape(-1, 1) y = -0.2 * X.ravel()**2 mlp = MLPRegressor(hidden_layer_sizes=(5,), activation='tanh', max_iter=5000).fit(X, y) # Set up GEKKO model and import the trained MLP m = GEKKO(remote=False) m.time = np.linspace(0, 5, 101) # simulate 5 seconds x = m.Var(value=1.0) # Create scaling for GEKKO's ML interface (custom scaler with min/max values) data = np.concatenate([X, y.reshape(-1, 1)], axis=1) # Assume the scaler is defined with known min/max (details omitted for brevity) scaler = ML.CustomMinMaxGekkoScaler(data, features=[0], label=[1]) mma = scaler.minMaxValues() ml_model = ML.Gekko_NN_SKlearn(mlp, mma, m) f = ml_model.predict([x]) # Define the hybrid ODE: dx/dt = -0.5*x + f(x) m.Equation(x.dt() == -0.5 * x + f) m.options.IMODE = 4 # dynamic simulation mode m.solve(disp=False) print("Hybrid model final x:", x.value[-1])