Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pbcost default values set to inf #522

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 6 additions & 7 deletions docs/examples/tutorials/custom_optimization_loop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration: 1 | my_swarm.best_cost: 0.0009\n",
"Iteration: 1 | my_swarm.best_cost: 0.0070\n",
"Iteration: 21 | my_swarm.best_cost: 0.0000\n",
"Iteration: 41 | my_swarm.best_cost: 0.0000\n",
"Iteration: 61 | my_swarm.best_cost: 0.0000\n",
"Iteration: 81 | my_swarm.best_cost: 0.0000\n",
"The best cost found by our swarm is: 0.0000\n",
"The best position found by our swarm is: [3.30572365e-19 2.93696483e-19]\n"
"The best position found by our swarm is: [1.77943664e-18 2.49653561e-18]\n"
]
}
],
Expand All @@ -126,7 +126,6 @@
"for i in range(iterations):\n",
" # Part 1: Update personal best\n",
" my_swarm.current_cost = f(my_swarm.position) # Compute current cost\n",
" my_swarm.pbest_cost = f(my_swarm.pbest_pos) # Compute personal best pos\n",
" my_swarm.pbest_pos, my_swarm.pbest_cost = P.compute_pbest(my_swarm) # Update and store\n",
" \n",
" # Part 2: Update global best\n",
Expand Down Expand Up @@ -163,15 +162,15 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2019-05-18 15:39:20,737 - pyswarms.single.global_best - INFO - Optimize for 100 iters with {'c1': 0.6, 'c2': 0.3, 'w': 0.4}\n",
"pyswarms.single.global_best: 100%|██████████|100/100, best_cost=0.00418\n",
"2019-05-18 15:39:21,942 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.004177699645933291, best pos: [0.03663518 0.05325001]\n"
"2023-10-20 14:44:14,240 - pyswarms.single.global_best - INFO - Optimize for 100 iters with {'c1': 0.6, 'c2': 0.3, 'w': 0.4}\n",
"pyswarms.single.global_best: 100%|██████████|100/100, best_cost=0.00596\n",
"2023-10-20 14:44:14,358 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 0.0059581279236955356, best pos: [0.04219012 0.06463839]\n"
]
},
{
"data": {
"text/plain": [
"(0.004177699645933291, array([0.03663518, 0.05325001]))"
"(0.0059581279236955356, array([0.04219012, 0.06463839]))"
]
},
"execution_count": 4,
Expand Down
5 changes: 4 additions & 1 deletion pyswarms/backend/swarms.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Swarm(object):
)
pbest_cost = attrib(
type=np.ndarray,
default=np.array([]),
validator=instance_of(np.ndarray),
)
best_cost = attrib(
Expand All @@ -119,3 +118,7 @@ def dimensions_default(self):
@pbest_pos.default
def pbest_pos_default(self):
return self.position

@pbest_cost.default
def pbest_cost_default(self):
return np.full(shape=(self.position.shape[0],), fill_value=np.inf)