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

cspy.PSOLGENT example code fails #111

Open
czeni opened this issue Feb 23, 2023 · 1 comment
Open

cspy.PSOLGENT example code fails #111

czeni opened this issue Feb 23, 2023 · 1 comment

Comments

@czeni
Copy link

czeni commented Feb 23, 2023

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):

  • OS: Ubuntu
  • Python 3.11.2
  • cspy == 1.0.3
  • numpy == 1.24.2
  • networkx == 3.0

Additional context

Example fails with Python3.10 as well.

@torressa
Copy link
Owner

torressa commented Feb 23, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants