diff --git a/examples/applications/discrete-problems/knapsack_problem.ipynb b/examples/applications/discrete-problems/knapsack_problem.ipynb index 8923b2a7..0b42cd60 100644 --- a/examples/applications/discrete-problems/knapsack_problem.ipynb +++ b/examples/applications/discrete-problems/knapsack_problem.ipynb @@ -2,7 +2,11 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "# Knapsack Problem\n", "\n", @@ -12,23 +16,29 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", "metadata": { - "collapsed": true, "pycharm": { "name": "#%%\n" } }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], "source": [ "# Solver\n", "\n", "We gonna use WOA to solve this problem\n" - ] + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } }, { "cell_type": "code", @@ -211,7 +221,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [] } diff --git a/examples/applications/gear_box/gear_box_optimization.py b/examples/applications/gear_box/gear_box_optimization.py index 6b31b785..402bc9f5 100644 --- a/examples/applications/gear_box/gear_box_optimization.py +++ b/examples/applications/gear_box/gear_box_optimization.py @@ -1,8 +1,6 @@ from tkinter import W -from mealpy.swarm_based.WOA import OriginalWOA -from mealpy.swarm_based.GOA import OriginalGOA -from mealpy.swarm_based.GWO import OriginalGWO +from mealpy import FloatVar, WOA, GOA, GWO import numpy as np """ @@ -85,12 +83,11 @@ def violate(value): N_1 = 1500 P = 7.5 - problem_dict1 = { - "fit_func": gear_box, - "lb": [20, 10, 30, 18, 2.75], - "ub": [32, 30, 40, 25, 4], - "minmax": "min", + problem = { + "obj_func": gear_box, + "bounds": FloatVar(lb=[20, 10, 30, 18, 2.75], ub=[32, 30, 40, 25, 4]), + "minmax": "min" } - model1 = OriginalWOA(epoch=1000, pop_size=500) - best_position, best_fitness = model1.solve(problem_dict1) - print(f"Best solution: {best_position}, Best fitness: {best_fitness}") + model1 = WOA.OriginalWOA(epoch=1000, pop_size=500) + gbest = model1.solve(problem) + print(f"Best solution: {gbest.solution}, Best fitness: {gbest.target.fitness}") diff --git a/examples/applications/keras/mha-hybrid-mlp-classification.py b/examples/applications/keras/mha-hybrid-mlp-classification.py index 62176e86..c09d0616 100644 --- a/examples/applications/keras/mha-hybrid-mlp-classification.py +++ b/examples/applications/keras/mha-hybrid-mlp-classification.py @@ -6,13 +6,11 @@ # https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/ - import numpy as np from keras.models import Sequential from keras.layers import Dense from sklearn.metrics import accuracy_score -from mealpy.swarm_based import GWO -from mealpy.evolutionary_based import FPA +from mealpy import GWO, FloatVar class HybridMlp: @@ -40,12 +38,10 @@ def create_network(self): def create_problem(self): self.problem = { - "fit_func": self.fitness_function, - "lb": [-1, ] * self.n_dims, - "ub": [1, ] * self.n_dims, + "obj_func": self.fitness_function, + "bounds": FloatVar(lb=(-1.,)*self.n_dims, ub=(1.,)*self.n_dims), "minmax": "max", - "log_to": None, - "save_population": False + "log_to": "console", } def decode_solution(self, solution): diff --git a/examples/applications/keras/mha-hybrid-mlp-time-series.py b/examples/applications/keras/mha-hybrid-mlp-time-series.py index 442546b4..7372bff5 100644 --- a/examples/applications/keras/mha-hybrid-mlp-time-series.py +++ b/examples/applications/keras/mha-hybrid-mlp-time-series.py @@ -18,8 +18,7 @@ import numpy as np from keras.models import Sequential from keras.layers import Dense -from mealpy.swarm_based import GWO -from mealpy.evolutionary_based import FPA +from mealpy import FloatVar, FPA from permetrics.regression import RegressionMetric @@ -61,12 +60,10 @@ def create_problem(self): self.n_inputs = self.X_train.shape[1] self.n_dims = (self.n_inputs * self.n_hidden_nodes) + self.n_hidden_nodes + (self.n_hidden_nodes * 1) + 1 self.problem = { - "fit_func": self.fitness_function, - "lb": [-1, ] * self.n_dims, - "ub": [1, ] * self.n_dims, + "obj_func": self.fitness_function, + "bounds": FloatVar(lb=(-1.,)*self.n_dims, ub=(1.0,)*self.n_dims), "minmax": "min", "obj_weights": [0.3, 0.2, 0.5], # [mae, mse, rmse] - "save_population": False, } def prediction(self, solution, data): @@ -136,4 +133,3 @@ def fitness_function(self, solution): x_input = x_input.reshape((1, n_steps)) yhat = model.prediction(model.solution, x_input) print(yhat) - diff --git a/examples/run_multitask.ipynb b/examples/run_multitask.ipynb index 37dbf5d0..bc3e0005 100644 --- a/examples/run_multitask.ipynb +++ b/examples/run_multitask.ipynb @@ -1,19 +1,22 @@ { "cells": [ { - "cell_type": "markdown", - "metadata": { - "pycharm": { - "name": "#%%\n" - } - }, + "cell_type": "code", + "execution_count": null, + "outputs": [], "source": [ "# Scenarios\n", "1. Run 1 algorithm with 1 problem, and multiple trials \n", "2. Run 1 algorithm with multiple problems, and multiple trials\n", "3. Run multiple algorithms with 1 problem, and multiple trials\n", "4. Run multiple algorithms with multiple problems, and multiple trials" - ] + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } }, { "cell_type": "code", diff --git a/examples/run_multitask.py b/examples/run_multitask.py index 489fd7e1..5be46ca8 100644 --- a/examples/run_multitask.py +++ b/examples/run_multitask.py @@ -56,5 +56,5 @@ if __name__ == "__main__": multitask = Multitask(algorithms=(model1, model2, model3, model4), problems=(p1, p2, p3), terminations=(term, ), modes=("thread", ), n_workers=4) # default modes = "single", default termination = epoch (as defined in problem dictionary) - multitask.execute(n_trials=5, n_jobs=None, save_path="history", save_as="csv", save_convergence=True, verbose=False) + multitask.execute(n_trials=5, n_jobs=None, save_path="history7", save_as="csv", save_convergence=True, verbose=False) # multitask.execute(n_trials=5, save_path="history", save_as="csv", save_convergence=True, verbose=False)