diff --git a/docs/examples/tutorials/custom_optimization_loop.ipynb b/docs/examples/tutorials/custom_optimization_loop.ipynb index 51b1ae6e..413c4d53 100644 --- a/docs/examples/tutorials/custom_optimization_loop.ipynb +++ b/docs/examples/tutorials/custom_optimization_loop.ipynb @@ -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" ] } ], @@ -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", @@ -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, diff --git a/pyswarms/backend/swarms.py b/pyswarms/backend/swarms.py index f3817cb3..e41a34d2 100644 --- a/pyswarms/backend/swarms.py +++ b/pyswarms/backend/swarms.py @@ -96,7 +96,6 @@ class Swarm(object): ) pbest_cost = attrib( type=np.ndarray, - default=np.array([]), validator=instance_of(np.ndarray), ) best_cost = attrib( @@ -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)