toruseo / UXsim

Vehicular traffic flow simulator in road network, written in pure Python
https://toruseo.jp/UXsim/docs/
MIT License
121 stars 56 forks source link

TypeError: unhashable type: 'list' in signal control examples #28

Closed EwoutH closed 6 months ago

EwoutH commented 6 months ago

Several Python scripts within the demos_and_examples directory are failing due to a TypeError related to hashability. Specifically, the scripts attempt to use a list as a key in a dictionary, which is not permitted in Python as lists are mutable and thus unhashable.

Affected Files:

  1. example_08_signal_reactive_control.py
  2. example_08en_signal_reactive_control.py
  3. example_11en_signal_4legged_intersection_reactive_control.py

Steps to Reproduce:

  1. Execute the scripts individually or through a test runner like pytest.
  2. Observe the TypeError that occurs when attempting to use a list as a dictionary key.

Expected Behavior: The scripts should run without errors, managing signal control as designed.

Actual Behavior: When running the scripts, they fail with the following error output:

TypeError: unhashable type: 'list'

The error is thrown in the following context for each script:

Error Log:

For example_08_signal_reactive_control.py:

simulation setting:
...
simulating...
      time| # of vehicles| ave speed| computation time
       0 s|        0 vehs|   0.0 m/s|     0.00 s

Traceback (most recent call last):
  File "/path/to/example_08_signal_reactive_control.py", line 37, in <module>
    vehicles_per_links[l.signal_group] = l.num_vehicles
TypeError: unhashable type: 'list'

Similar errors are observed for the other affected files.

Suggested Fix: A review and potential redesign of the data structure used to track vehicles per link is needed. If l.signal_group is indeed a list and meant to be used as a key, one could consider converting it to a tuple, which is hashable, provided the list's contents are also hashable. Otherwise, restructure the code to avoid using a list as a dictionary key.

toruseo commented 6 months ago

Thanks! I have updated the examples.

This was caused by the recent update on traffic signal modeling.