- In first code there is no error
import pandas as pd
import numpy as np
from gekko.ML import Gekko_NN_SKlearn, CustomMinMaxGekkoScaler
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from gekko import GEKKO
# Define inputs and output
X = ['Tco', 'Tci', 'Teo', 'Tei', 'Tcset', 'Tbuh', 'Twar', 'm_evap', 'm_cond', 'Qth_Total', 'CompOn']
Y = ['Pel']
# Splitting the data into training and testing sets
train, test = train_test_split(df, test_size=0.2, shuffle=True)
# Scaling the data using CustomMinMaxGekkoScaler
s = CustomMinMaxGekkoScaler(df, X, Y)
ds = s.scaledData()
mma = s.minMaxValues()
# Further splitting the scaled data into training and testing sets
trains, tests = train_test_split(ds, test_size=0.2, shuffle=True)
# Define the structure of the neural network
hl = [25, 15]
mlp = MLPRegressor(hidden_layer_sizes=hl, activation='relu',
solver='adam', batch_size=32,
learning_rate='adaptive', learning_rate_init=.0005,
tol=1e-6, n_iter_no_change=200,
max_iter=12000)
# Train the MLPRegressor model
mlp.fit(trains[X], np.ravel(trains[Y]))
# Evaluate the model on the test data
r2 = mlp.score(tests[X], np.ravel(tests[Y]))
# Print the R-squared score of the model
print('nnSK r2:', r2)
- In the second code there is no error because I’m using one variable i.e Tco
# Create a Gekko model
m = GEKKO()
# Define Tco as a decision variable
Tco = m.Var(value=(df['Tco'].min() + df['Tco'].max()) / 2, lb=df['Tco'].min(), ub=df['Tco'].max())
# Predict Pel using the trained neural network and Tco as input
predicted_Pel = Gekko_NN_SKlearn(mlp, mma, m).predict([Tco])
# Set the predicted Pel value as the objective to minimize
m.Minimize(predicted_Pel)
# Solve the optimization problem
m.solve(disp=False)
# Print the optimized Tco and corresponding predicted Pel value
print('Optimized Tco:', Tco.value[0])
print('Optimized Predicted Pel:', predicted_Pel.value[0])
print('Gekko Solvetime:', m.options.SOLVETIME, 's')
SOLUTION is=
Optimized Tco: 297.87489593
Optimized Predicted Pel: 73.999990155
Gekko Solvetime: 0.10799999999 s
- In third code it is showing error when I use two variables Tco and Tei
# Create a Gekko model
m = GEKKO()
# Define Tco as a decision variable
Tco = m.Var(value=(df['Tco'].min() + df['Tco'].max()) / 2, lb=df['Tco'].min(), ub=df['Tco'].max())
Tei = m.Var(value=(df['Tei'].min() + df['Tei'].max()) / 2, lb=df['Tei'].min(), ub=df['Tei'].max())
# Predict Pel using the trained neural network and Tco as input
predicted_Pel = Gekko_NN_SKlearn(mlp, mma, m).predict([Tco, Tei])
# Set the predicted Pel value as the objective to minimize
m.Minimize(predicted_Pel)
# Solve the optimization problem
m.solve(disp=False)
# Print the optimized Tco and corresponding predicted Pel value
print('Optimized Tco:', Tco.value[0])
print('Optimized Predicted Pel:', predicted_Pel.value[0])
print('Gekko Solvetime:', m.options.SOLVETIME, 's')
- It is showing this error
Value Error: operands could not be broadcast together with shapes (11,) (2,)
New contributor
Athar’s Chem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.