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

TypeError in TSP notebook #27

Closed
siljuovix opened this issue Apr 23, 2020 · 4 comments
Closed

TypeError in TSP notebook #27

siljuovix opened this issue Apr 23, 2020 · 4 comments
Labels
info-needed Further information is requested

Comments

@siljuovix
Copy link

I am reproducing the TSP example in a Jupyter Notebook and in the following line:
initial_solution = greedy_repair(state, random_state)

I get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-181-62f3c5d57681> in <module>
----> 1 initial_solution = greedy_repair(state, random_state)


<ipython-input-178-f634089afd1a> in greedy_repair(current, random_state)
     12 
     13     while len(current.edges) != len(current.nodes):
---> 14         node = next(node for node in nodes 
     15                     if node not in current.edges)
     16 

<ipython-input-178-f634089afd1a> in <genexpr>(.0)
     13     while len(current.edges) != len(current.nodes):
     14         node = next(node for node in nodes 
---> 15                     if node not in current.edges)
     16 
     17         # Computes all nodes that have not currently been visited,

TypeError: unhashable type: 'list'

I fixed the previous with the following in line 15:

node = next(node for node in nodes if node not in current.edges.values())

But the type error keeps propagating:

TypeError                                 Traceback (most recent call last)
<ipython-input-190-62f3c5d57681> in <module>
----> 1 initial_solution = greedy_repair(state, random_state)

<ipython-input-188-fd3fe98c19d8> in greedy_repair(current, random_state)
     21             # not result in a subcycle, as that would violate the TSP
     22             # constraints.
---> 23         unvisited = {other for other in current.nodes
     24 
     25                          if other != node

<ipython-input-188-fd3fe98c19d8> in <setcomp>(.0)
     24 
     25                          if other != node
---> 26                          if other not in visited
     27                          if not would_form_subcycle(node, other, current)}
     28 

TypeError: unhashable type: 'list'

because visited is a set. If I transform the set into a list so the previous works I will have the same type of error in the next line. It would help me to have an input on the intended behavior.

Thank you in advance!

@N-Wouda
Copy link
Owner

N-Wouda commented Apr 23, 2020

Hey @siljuovix, thanks for opening an issue! I can sadly not reproduce the problem locally.

From what I can tell from your tracebacks, in your notebook node is a list, rather than a tuple (lists cannot be hashed, tuples can). That's a bit weird, as they should be tuples when loaded from tsplib.

Can you check for me what data = tsplib95.load_problem('xqf131.tsp') returns? (this is called near the top of the notebook) In particular, what are the key-value types of data.node_coords?

@N-Wouda N-Wouda added the info-needed Further information is requested label Apr 23, 2020
@siljuovix
Copy link
Author

@N-Wouda Thank you for the fast reply!
data.node_coords is a dictionary like : 

{1: [0, 13], 2: [0, 26], 3: [0, 27], 4: [0, 39], ...}

Then after 

state = TspState(list(data.node_coords.items()), {})

node_coords passes as a list of tuples like: [(1,[0,13]), (2,[0,26]),...]

Also worth mentioning that I get a deprecation warning when using problem_load:

DeprecationWarning: Call to deprecated function (or staticmethod) load_problem. (Will be removed in newer versions. Use tsplib95.load instead.) -- Deprecated since version 7.0.0.

And this line:
optimal = data.trace_tours(solution)[0]

is also giving the following error:

TypeError Traceback (most recent call last)
in
----> 1 optimal = data.trace_tours(solution)[0]

~/.local/lib/python3.6/site-packages/tsplib95/models.py in trace_tours(self, tours)
425 """
426 solutions = []
--> 427 for tour in tours:
428 edges = utils.pairwise(tour)
429 weight = sum(self.get_weight(i, j) for i, j in edges)

TypeError: 'StandardProblem' object is not iterable

N-Wouda added a commit that referenced this issue Apr 24, 2020
@N-Wouda
Copy link
Owner

N-Wouda commented Apr 24, 2020

I had a look at the recent changes to tsplib, @siljuovix. These turn out to have been substantial, and warrant changes on my end. I could reproduce the issue after upgrading my tsplib package to 0.7.0, and resolved the issues in #28.

Please have a look at the latest notebook, here!

@siljuovix
Copy link
Author

siljuovix commented Apr 25, 2020 via email

@N-Wouda N-Wouda changed the title TypeError TypeError in TSP notebook Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants