HaaLeo / swarmlib

This repository implements several swarm optimization algorithms and visualizes them. Implemented algorithms: Particle Swarm Optimization (PSO), Firefly Algorithm (FA), Cuckoo Search (CS), Ant Colony Optimization (ACO), Artificial Bee Colony (ABC), Grey Wolf Optimizer (GWO) and Whale Optimization Algorithm (WOA)
BSD 3-Clause "New" or "Revised" License
505 stars 111 forks source link

Index Error #42

Closed statimo closed 2 years ago

statimo commented 2 years ago

| Python Version | swarmlib Version | OS | | 3.8.13 | 0.14.1 | Windows 10 |

Description

Try to run the Firefly Algorithm and the test code

problem = FireflyProblem(function=FUNCTIONS['michalewicz'], firefly_number=20) best_firefly = problem.solve() problem.replay()

problem.replay() doesn't work:

IndexError Traceback (most recent call last) --> problem.replay()

---> 25 self._visualizer.replay()

File ...\Miniconda3\envs\optimization\lib\site-packages\swarmlib\util\base_visualizer.py:85, in BaseVisualizer.replay(self, *kwargs) 82 frames = int(self.__intervalsself.interval_ms/self.__frame_interval) 84 # iterationnumber+1 for initialization frame ---> 85 = animation.FuncAnimation(self._fig, self._animate, frames=frames, interval=self.frame_interval, 86 blit=True, init_func=self._init, repeat=self.__continuous, fargs=[frames]) 88 plt.show()

File. ..\Miniconda3\envs\optimization\lib\site-packages\matplotlib\animation.py:1634, in FuncAnimation.init(self, fig, func, frames, init_func, fargs, save_count, cache_frame_data, kwargs) 1631 # Needs to be initialized so the draw functions work without checking 1632 self._save_seq = [] -> 1634 super().init(fig, kwargs) ... --> 564 (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'), 565 np.asarray(self.convert_yunits(offsets[:, 1]), 'float'))) 566 self.stale = True

IndexError: index 0 is out of bounds for axis 1 with size 0

statimo commented 2 years ago

Also tried it in Google Colab


IndexError Traceback (most recent call last) in () 4 problem = FireflyProblem(function=FUNCTIONS['michalewicz'], firefly_number=20) 5 best_firefly = problem.solve() ----> 6 problem.replay()

19 frames /usr/local/lib/python3.7/dist-packages/swarmlib/util/problem_base.py in replay(self) 23 Start the problems visualization. 24 """ ---> 25 self._visualizer.replay()

/usr/local/lib/python3.7/dist-packages/swarmlib/util/base_visualizer.py in replay(self, **kwargs) 84 # iterationnumber+1 for initialization frame 85 = animation.FuncAnimation(self._fig, self._animate, frames=frames, interval=self.__frame_interval, ---> 86 blit=True, init_func=self._init, repeat=self.__continuous, fargs=[frames]) 87 88 plt.show()

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in init(self, fig, func, frames, init_func, fargs, save_count, cache_frame_data, kwargs) 1632 self._save_seq = [] 1633 -> 1634 super().init(fig, kwargs) 1635 1636 # Need to reset the saved seq, since right now it will contain data

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in init(self, fig, interval, repeat_delay, repeat, event_source, *args, *kwargs) 1394 if event_source is None: 1395 event_source = fig.canvas.new_timer(interval=self._interval) -> 1396 super().init(fig, event_source=event_source, args, *kwargs) 1397 1398 def _step(self, args):

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in init(self, fig, event_source, blit) 881 self._stop) 882 if self._blit: --> 883 self._setup_blit() 884 885 def del(self):

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in _setup_blit(self) 1195 self._resize_id = self._fig.canvas.mpl_connect('resize_event', 1196 self._on_resize) -> 1197 self._post_draw(None, self._blit) 1198 1199 def _on_resize(self, event):

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in _post_draw(self, framedata, blit) 1148 self._blit_draw(self._drawn_artists) 1149 else: -> 1150 self._fig.canvas.draw_idle() 1151 1152 # The rest of the code in this class is to facilitate easy blitting

/usr/local/lib/python3.7/dist-packages/matplotlib/backend_bases.py in draw_idle(self, *args, *kwargs) 2058 if not self._is_idle_drawing: 2059 with self._idle_draw_cntx(): -> 2060 self.draw(args, **kwargs) 2061 2062 @property

/usr/local/lib/python3.7/dist-packages/matplotlib/backends/backend_agg.py in draw(self) 434 (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar 435 else nullcontext()): --> 436 self.figure.draw(self.renderer) 437 # A GUI class may be need to update a window using this draw, so 438 # don't forget to call the superclass.

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, kwargs) 71 @wraps(draw) 72 def draw_wrapper(artist, renderer, *args, *kwargs): ---> 73 result = draw(artist, renderer, args, kwargs) 74 if renderer._rasterizing: 75 renderer.stop_rasterizing()

/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in draw_wrapper(artist, renderer) 48 renderer.start_filter() 49 ---> 50 return draw(artist, renderer) 51 finally: 52 if artist.get_agg_filter() is not None:

/usr/local/lib/python3.7/dist-packages/matplotlib/figure.py in draw(self, renderer) 2845 self.stale = False 2846 -> 2847 self.canvas.draw_event(renderer) 2848 2849 def draw_without_rendering(self):

/usr/local/lib/python3.7/dist-packages/matplotlib/backend_bases.py in draw_event(self, renderer) 1777 s = 'draw_event' 1778 event = DrawEvent(s, self, renderer) -> 1779 self.callbacks.process(s, event) 1780 1781 def resize_event(self):

/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/init.py in process(self, s, *args, **kwargs) 290 except Exception as exc: 291 if self.exception_handler is not None: --> 292 self.exception_handler(exc) 293 else: 294 raise

/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/init.py in _exception_printer(exc) 94 def _exception_printer(exc): 95 if _get_running_interactive_framework() in ["headless", None]: ---> 96 raise exc 97 else: 98 traceback.print_exc()

/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/init.py in process(self, s, *args, *kwargs) 285 if func is not None: 286 try: --> 287 func(args, **kwargs) 288 # this does not capture KeyboardInterrupt, SystemExit, 289 # and GeneratorExit

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in _start(self, *args) 905 906 # Now do any initial draw --> 907 self._init_draw() 908 909 # Add our callback for stepping the animation and

/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in _init_draw(self) 1696 self._draw_frame(frame_data) 1697 else: -> 1698 self._drawn_artists = self._init_func() 1699 if self._blit: 1700 if self._drawn_artists is None:

/usr/local/lib/python3.7/dist-packages/swarmlib/util/base_visualizer.py in _init(self) 92 Init function for animations. Only used for FuncAnimation 93 """ ---> 94 self.particles.set_offsets([[]]) 95 self._marker_colors = np.full(len(self._positions[0][0]), self._marker_color) # Create array of correct size 96 self.particle_vel.X = []

/usr/local/lib/python3.7/dist-packages/matplotlib/collections.py in set_offsets(self, offsets) 562 offsets = offsets[None, :] 563 self._offsets = np.column_stack( --> 564 (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'), 565 np.asarray(self.convert_yunits(offsets[:, 1]), 'float'))) 566 self.stale = True

IndexError: index 0 is out of bounds for axis 1 with size 0

statimo commented 2 years ago

Also in Shell:

swarm fireflies 14 2022-06-27 12:54:28,458 [INFO ] Start firefly algorithm with parameters="{'dark': False, 'interval': 1000, 'continuous': False, 'seed': None, 'function': 'michalewicz', 'upper_boundary': 4.0, 'lower_boundary': 0.0, 'alpha': 0.25, 'beta': 1.0, 'gamma': 0.97, 'iteration_number': 10, 'firefly_number': 14}" 2022-06-27 12:54:29,272 [INFO ] Current best value: -0.6543338821854172, Overall best value: -0.6543338821854172 2022-06-27 12:54:29,281 [INFO ] Current best value: -1.046547634297289, Overall best value: -1.046547634297289 2022-06-27 12:54:29,291 [INFO ] Current best value: -1.7810052971375656, Overall best value: -1.7810052971375656 2022-06-27 12:54:29,299 [INFO ] Current best value: -1.7928715391071766, Overall best value: -1.7928715391071766 2022-06-27 12:54:29,308 [INFO ] Current best value: -1.7999274187707126, Overall best value: -1.7999274187707126 2022-06-27 12:54:29,317 [INFO ] Current best value: -1.800946593957634, Overall best value: -1.800946593957634 2022-06-27 12:54:29,327 [INFO ] Current best value: -1.8009744891690338, Overall best value: -1.8009744891690338 2022-06-27 12:54:29,334 [INFO ] Current best value: -1.8009726637321928, Overall best value: -1.8009744891690338 2022-06-27 12:54:29,342 [INFO ] Current best value: -1.800891240644705, Overall best value: -1.8009744891690338 2022-06-27 12:54:29,350 [INFO ] Current best value: -1.799779373119411, Overall best value: -1.8009744891690338 Traceback (most recent call last): File "\envs\optimization\lib\site-packages\matplotlib\cbook__init.py", line 287, in process func(*args, **kwargs) File "\envs\optimization\lib\site-packages\matplotlib\animation.py", line 907, in _start self._init_draw() File "\envs\optimization\lib\site-packages\matplotlib\animation.py", line 1698, in _init_draw self._drawn_artists = self._init_func() File \envs\optimization\lib\site-packages\swarmlib\util\base_visualizer.py", line 94, in _init self.__particles.set_offsets([[]]) File "\envs\optimization\lib\site-packages\matplotlib\collections.py", line 564, in set_offsets (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'), IndexError: index 0 is out of bounds for axis 1 with size 0 Traceback (most recent call last): File "\envs\optimization\lib\site-packages\matplotlib\cbook\init.py", line 287, in process func(*args, **kwargs) File "\envs\optimization\lib\site-packages\matplotlib\animation.py", line 1207, in _on_resize self._init_draw() File "\envs\optimization\lib\site-packages\matplotlib\animation.py", line 1698, in _init_draw self._drawn_artists = self._init_func() File "\envs\optimization\lib\site-packages\swarmlib\util\base_visualizer.py", line 94, in _init self.particles.set_offsets([[]]) File "\envs\optimization\lib\site-packages\matplotlib\collections.py", line 564, in set_offsets (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'), IndexError: index 0 is out of bounds for axis 1 with size 0

statimo commented 2 years ago

Fixed the issue with downgrading matplotlib (3.5.2 -> 3.5.0)

Now it works.