Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 75 additions & 33 deletions docs/examples/tutorials/basic_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,24 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:39:13,096 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=1.09e-41\n",
"2019-05-18 15:39:25,448 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 1.093473857947962e-41, best pos: [3.27682830e-21 4.43998725e-22]\n"
"2023-10-20 20:31:20,814 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 0%| |0/1000, best_cost=0.299"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=7.27e-42\n",
"2023-10-20 20:31:22,219 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 7.274022421135793e-42, best pos: [-2.69536254e-21 9.50956183e-23]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3.02 s, sys: 774 ms, total: 3.79 s\n",
"Wall time: 12.4 s\n"
"CPU times: user 1.42 s, sys: 210 ms, total: 1.63 s\n",
"Wall time: 1.41 s\n"
]
}
],
Expand All @@ -77,10 +84,10 @@
"options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}\n",
"\n",
"# Call instance of PSO\n",
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)\n",
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, max_generations=1000, options=options)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.sphere, iters=1000)"
"cost, pos = optimizer.optimize(fx.sphere)"
]
},
{
Expand All @@ -106,17 +113,24 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:39:25,476 - pyswarms.single.local_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k': 2, 'p': 2}\n",
"pyswarms.single.local_best: 100%|██████████|1000/1000, best_cost=3.28e-41\n",
"2019-05-18 15:39:37,110 - pyswarms.single.local_best - INFO - Optimization finished | best cost: 3.275639739592901e-41, best pos: [-5.62944989e-21 -1.40094066e-21]\n"
"2023-10-20 20:31:22,272 - pyswarms.single.local_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k': 2, 'p': 2}\n",
"pyswarms.single.local_best: 0%| |0/1000, best_cost=0.000431"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.local_best: 100%|██████████|1000/1000, best_cost=3.35e-44\n",
"2023-10-20 20:31:24,134 - pyswarms.single.local_best - INFO - Optimization finished | best cost: 3.348384631794686e-44, best pos: [-1.82343675e-22 1.53176551e-23]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.93 s, sys: 271 ms, total: 2.2 s\n",
"Wall time: 11.6 s\n"
"CPU times: user 1.88 s, sys: 270 ms, total: 2.15 s\n",
"Wall time: 1.87 s\n"
]
}
],
Expand All @@ -126,10 +140,10 @@
"options = {'c1': 0.5, 'c2': 0.3, 'w':0.9, 'k': 2, 'p': 2}\n",
"\n",
"# Call instance of PSO\n",
"optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options)\n",
"optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, max_generations=1000, options=options)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.sphere, iters=1000)"
"cost, pos = optimizer.optimize(fx.sphere)"
]
},
{
Expand Down Expand Up @@ -179,17 +193,24 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:39:37,279 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=0 \n",
"2019-05-18 15:39:48,976 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0, best pos: [-1.11729550e-09 3.10827139e-09]\n"
"2023-10-20 20:31:24,200 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 0%| |0/1000, best_cost=3.66"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=0.995\n",
"2023-10-20 20:31:25,592 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.9949590570932898, best pos: [1.13585631e-10 9.94958639e-01]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.95 s, sys: 254 ms, total: 2.21 s\n",
"Wall time: 11.7 s\n"
"CPU times: user 1.41 s, sys: 197 ms, total: 1.61 s\n",
"Wall time: 1.4 s\n"
]
}
],
Expand All @@ -199,10 +220,10 @@
"options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}\n",
"\n",
"# Call instance of PSO with bounds argument\n",
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds)\n",
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, max_generations=1000, options=options, bounds=bounds)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.rastrigin, iters=1000)"
"cost, pos = optimizer.optimize(fx.rastrigin)"
]
},
{
Expand Down Expand Up @@ -257,9 +278,16 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:39:49,204 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=7.02e-10\n",
"2019-05-18 15:40:01,463 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 7.019703679797182e-10, best pos: [1.0000264 1.00005302]\n"
"2023-10-20 20:31:25,663 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 0%| |0/1000, best_cost=0.958"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=5.83e-15\n",
"2023-10-20 20:31:27,268 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 5.829665734825736e-15, best pos: [0.99999992 0.99999985]\n"
]
}
],
Expand All @@ -271,11 +299,11 @@
"x_min = -1 * x_max\n",
"bounds = (x_min, x_max)\n",
"options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds)\n",
"optimizer = GlobalBestPSO(n_particles=10, dimensions=2, max_generations=1000, options=options, bounds=bounds)\n",
"\n",
"# now run the optimization, pass a=1 and b=100 as a tuple assigned to args\n",
"\n",
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, a=1, b=100, c=0)"
"cost, pos = optimizer.optimize(rosenbrock_with_args, a=1, b=100, c=0)"
]
},
{
Expand All @@ -294,15 +322,22 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:40:01,475 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"2023-10-20 20:31:27,296 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 0%| |0/1000, best_cost=1.35e-16"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=0 \n",
"2019-05-18 15:40:13,805 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0, best pos: [1. 1.]\n"
"2023-10-20 20:31:28,579 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0, best pos: [1. 1.]\n"
]
}
],
"source": [
"kwargs={\"a\": 1.0, \"b\": 100.0, 'c':0}\n",
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, **kwargs)"
"cost, pos = optimizer.optimize(rosenbrock_with_args, **kwargs)"
]
},
{
Expand All @@ -321,14 +356,21 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:40:13,819 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"2023-10-20 20:31:28,625 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"pyswarms.single.global_best: 0%| |0/1000, best_cost=0"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"pyswarms.single.global_best: 100%|██████████|1000/1000, best_cost=0\n",
"2019-05-18 15:40:25,963 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0, best pos: [1. 1.]\n"
"2023-10-20 20:31:30,090 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0, best pos: [1. 1.]\n"
]
}
],
"source": [
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, a=1, b=100)"
"cost, pos = optimizer.optimize(rosenbrock_with_args, a=1, b=100)"
]
}
],
Expand All @@ -349,7 +391,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading