BraveDistribution / pytsp

TSP algorithms and graph support utilities
MIT License
13 stars 7 forks source link

Unable to get lin_kerdighan_tsp() working #11

Open folterj opened 1 year ago

folterj commented 1 year ago

I'm unable to get lin_kerdighan_tsp() working. e.g. lin_kerdighan_tsp(distance_matrix)

... lin_kerdighan_tsp.py", line 46, in _get_mst_and_add_two_edges
    result = np.insert(np.insert(mst_without_starting_node, 0, np.array([0, 0, 0, 0]), axis=0), 0,
ValueError: could not broadcast input array from shape (1,4) into shape (1,229)

From the function arguments I'm assuming it is used the same as christofides_tsp() - this function works fine using the same distance_matrix. Reading the readme and ReadTheDocs.io pages seems to miss function usage.

Python version 3.8 Pytsp version 0.2.7

BraveDistribution commented 1 year ago

Hi,

could you give minimal working example? What does your matrix look like?

folterj commented 1 year ago

Hi @BraveDistribution No problem:

import numpy as np
from pytsp.lin_kerdighan_tsp import lin_kerdighan_tsp

distance_matrix = np.array([
    [    np.inf, 0.316493  , 0.33081796, 0.26269698, 0.31504481, 0.27552974, 0.30164198, 0.32506585, 0.28196352, 0.3035948 ],
    [0.316493  ,     np.inf, 0.2585015 , 0.27938199, 0.25343369, 0.26129902, 0.25567885, 0.27246096, 0.2726916 , 0.29304181],
    [0.33081796, 0.2585015 ,     np.inf, 0.29488479, 0.28153239, 0.31080125, 0.28012469, 0.32240093, 0.34206161, 0.32961145],
    [0.26269698, 0.27938199, 0.29488479,     np.inf, 0.31083542, 0.2752112 , 0.28986822, 0.32170974, 0.28103724, 0.29275725],
    [0.31504481, 0.25343369, 0.28153239, 0.31083542,     np.inf, 0.26896711, 0.29635096, 0.28088729, 0.26179473, 0.28329585],
    [0.27552974, 0.26129902, 0.31080125, 0.2752112 , 0.26896711,     np.inf, 0.27898233, 0.33569306, 0.26398696, 0.28110992],
    [0.30164198, 0.25567885, 0.28012469, 0.28986822, 0.29635096, 0.27898233,     np.inf, 0.29100124, 0.33064719, 0.29014288],
    [0.32506585, 0.27246096, 0.32240093, 0.32170974, 0.28088729, 0.33569306, 0.29100124,     np.inf, 0.33081947, 0.29602109],
    [0.28196352, 0.2726916 , 0.34206161, 0.28103724, 0.26179473, 0.26398696, 0.33064719, 0.33081947,     np.inf, 0.29918839],
    [0.3035948 , 0.29304181, 0.32961145, 0.29275725, 0.28329585, 0.28110992, 0.29014288, 0.29602109, 0.29918839,     np.inf]
])

order = lin_kerdighan_tsp(distance_matrix)