torressa / cspy

A collection of algorithms for the (Resource) Constrained Shortest Path problem in Python / C++ / C#
https://torressa.github.io/cspy/
MIT License
77 stars 24 forks source link

cspy.PSOLGENT example code fails #111

Open czeni opened 1 year ago

czeni commented 1 year ago

Describe the bug

The example code for cspy.PSOLGENT fails with latest version (1.0.3) and Python3.11 and also with Python3.10.

To Reproduce

Python 3.11.2 (main, Feb  8 2023, 14:49:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cspy import PSOLGENT
>>> from networkx import DiGraph
>>> from numpy import zeros, ones, array
>>> G = DiGraph(directed=True, n_res=2)
>>> G.add_edge('Source', 'A', res_cost=array([1, 1]), weight=1)
>>> G.add_edge('Source', 'B', res_cost=array([1, 1]), weight=1)
>>> G.add_edge('Source', 'C', res_cost=array([10, 1]), weight=10)
>>> G.add_edge('A', 'C', res_cost=array([1, 1]), weight=1)
>>> G.add_edge('A', 'E', res_cost=array([10, 1]), weight=10)
>>> G.add_edge('A', 'F', res_cost=array([10, 1]), weight=10)
>>> G.add_edge('B', 'C', res_cost=array([2, 1]), weight=-1)
>>> G.add_edge('B', 'F', res_cost=array([10, 1]), weight=10)
>>> G.add_edge('B', 'E', res_cost=array([10, 1]), weight=10)
>>> G.add_edge('C', 'D', res_cost=array([1, 1]), weight=-1)
>>> G.add_edge('D', 'E', res_cost=array([1, 1]), weight=1)
>>> G.add_edge('D', 'F', res_cost=array([1, 1]), weight=1)
>>> G.add_edge('D', 'Sink', res_cost=array([10, 10]), weight=10)
>>> G.add_edge('F', 'Sink', res_cost=array([10, 1]), weight=1)
>>> G.add_edge('E', 'Sink', res_cost=array([1, 1]), weight=1)
>>> n_nodes = len(G.nodes())
>>> psolgent = PSOLGENT(G, [5, 5], [0, 0],
...                         max_iter=200,
...                         swarm_size=50,
...                         member_size=n_nodes,
...                         neighbourhood_size=50)
>>> psolgent.run()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/czentye/.local/lib/python3.11/site-packages/cspy/algorithms/psolgent.py", line 179, in run
    self._init_swarm()
  File "/home/czentye/.local/lib/python3.11/site-packages/cspy/algorithms/psolgent.py", line 232, in _init_swarm
    self.pos[:, [0, -1]] = min(10 * self.lower_bound, np.min(self.pos))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 

Expected behavior

The code should pass.

Desktop (please complete the following information):

Additional context

Example fails with Python3.10 as well.

torressa commented 1 year ago

And that's why doctests exist Good catch, cheers, I will check. I think it's a small bug on the initialisation of lower bound. It ends up being an array only when the member_size argument is provided and therefore not None for the initialisation. https://github.com/torressa/cspy/blob/master/src/python/algorithms/psolgent.py#L150