thieu1995 / mealpy

A Collection Of The State-of-the-art Metaheuristic Algorithms In Python (Metaheuristic/Optimizer/Nature-inspired/Biology)
https://mealpy.readthedocs.io
GNU General Public License v3.0
870 stars 176 forks source link

[BUG]: TypeError: Problem.__init__() missing 1 required positional argument: 'bounds' #165

Closed cs-heibao closed 1 month ago

cs-heibao commented 1 month ago

Description of the bug

Split the data

X_train1, X_test1, y_train1, y_test1 = train_test_split(features, target, test_size=0.2, random_state=42)

sc = StandardScaler() X_train1 = sc.fit_transform(X_train1) X_test1 = sc.transform(X_test1)

sc_y = StandardScaler() y_train1 = sc_y.fit_transform(y_train1) y_test1 = sc_y.transform(y_test1)

Define the SVR fitness function

def svr_fitness(solution): kernel_encoded = int(solution[0]) c = solution[1] gamma = solution[2] epsilon = solution[3]

kernel_decoded = ['linear', 'poly', 'rbf', 'precomputed'][kernel_encoded]

svr = SVR(C=c, kernel=kernel_decoded, gamma=gamma, epsilon=epsilon)
svr.fit(X_train1, y_train1.ravel())
y_predict = svr.predict(X_test1)
fitness = - r2_score(y_test1, y_predict)  # Minimize negative R^2 score
return fitness

Define the hyperparameter bounds

param_bounds = { "kernel": [0, 1, 2, 3], # 0: linear, 1: poly, 2: rbf 3: precomputed "C": list(range(100, 1001, 100)), "gamma": list(np.logspace(-10, 0, 10)), "epsilon": list(np.logspace(-3, 0, 10)) }

Define the optimization problem dictionary

svr_problem = { "fit_func": svr_fitness, "lb": [min(param_bounds["kernel"]), min(param_bounds["C"]), min(param_bounds["gamma"]), min(param_bounds["epsilon"])], "ub": [max(param_bounds["kernel"]), max(param_bounds["C"]), max(param_bounds["gamma"]), max(param_bounds["epsilon"])], "minmax": "min" # Minimize fitness (negative R^2) }

epoch = 50 # Number of generations/epochs pop_size = 100 # Size of the population in each generation pc = 0.95 # Crossover probability pm = 0.05 # Mutation probability

Instantiate the Genetic Algorithm model

ga_model = BaseGA(epoch, pop_size, pc, pm)

Solve the optimization problem using the genetic algorithm

best_params, best_fitness = ga_model.solve(svr_problem)

best_kernel_encoded = int(best_params[0]) best_c = best_params[1] best_gamma = best_params[2] best_epsilon = best_params[3] best_kernel_decoded = ['linear', 'poly', 'rbf', 'precomputed'][best_kernel_encoded]

print("Best Hyperparameters using GA:") print("Kernel:", best_kernel_decoded) print("C:", best_c) print("Gamma:", best_gamma) print("Epsilon:", best_epsilon) print("Best Fitness (R^2):", -best_fitness) # Convert back to positive (R^2)

Create SVR model with optimized hyperparameters from GA

svr_optimized = SVR(kernel=best_kernel_decoded, C=best_c, gamma=best_gamma, epsilon=best_epsilon) svr_optimized.fit(X_train1, y_train1.ravel())

Make predictions on train and test set and calculate R^2 score

pred_test = svr_optimized.predict(X_test1) pred_train = svr_optimized.predict(X_train1) r2_test = r2_score(y_test1, pred_test) r2_train = r2_score(y_train1, pred_train) print("R^2 score of Train set prediction using GA:", r2_train) print("R^2 score of Test set prediction using GA:", r2_test)

Steps To Reproduce

TypeError: Problem.init() missing 1 required positional argument: 'bounds'

Additional Information

@thieu1995 Dear thieu,

Thanks for the great works. When I run this code, I got the "TypeError: Problem.init() missing 1 required positional argument: 'bounds'" problem. Could you give me some suggestions to solve this problem?

cs-heibao commented 1 month ago

@Evul88 what's this?

cs-heibao commented 1 month ago

solved.

thieu1995 commented 1 month ago

@cs-heibao thank you. It is so easy to fix right. If you read the provided examples.

Fa20 commented 5 days ago

@cs-heibao @thieu1995 I got the same error how can I fix it