Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thieu1995 committed Jan 2, 2024
1 parent c4b5dbe commit 40dd513
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
34 changes: 24 additions & 10 deletions examples/applications/discrete-problems/knapsack_problem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Knapsack Problem\n",
"\n",
Expand All @@ -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",
Expand Down Expand Up @@ -211,7 +221,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": []
}
Expand Down
19 changes: 8 additions & 11 deletions examples/applications/gear_box/gear_box_optimization.py
Original file line number Diff line number Diff line change
@@ -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

"""
Expand Down Expand Up @@ -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}")
12 changes: 4 additions & 8 deletions examples/applications/keras/mha-hybrid-mlp-classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions examples/applications/keras/mha-hybrid-mlp-time-series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

17 changes: 10 additions & 7 deletions examples/run_multitask.ipynb
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/run_multitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 40dd513

Please sign in to comment.