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

Rounding the output of self.get_weight() #14

Open
TheRolf opened this issue Aug 26, 2020 · 2 comments
Open

Rounding the output of self.get_weight() #14

TheRolf opened this issue Aug 26, 2020 · 2 comments

Comments

@TheRolf
Copy link

TheRolf commented Aug 26, 2020

  • TSPLIB 95 version: 0.7.1
  • Python version: 3.7
  • Operating System: Win10 x64

Description

Is there a reason all edge weights get automatically rounded? Could this be optional?
Could you please emphasise the fact that the function get_weight() returns rounded values? The documentation says it returns floats, which makes the user think the precise value is returned.

@songofroland
Copy link

I had the same issue.

Documentation suggests that this function returns float however this line rounds it up to an int.

def euclidean(start, end, round=utils.nint):
    """Return the Euclidean distance between start and end.
    This is capable of performing distance calculations for EUC_2D and
    EUC_3D problems. If ``round=math.ceil`` is passed, this is suitable for
    CEIL_2D problems as well.
    :param tuple start: *n*-dimensional coordinate
    :param tuple end: *n*-dimensional coordinate
    :param callable round: function to use to round the result
    :return: rounded distance
    """
    if len(start) != len(end):
        raise ValueError('dimension mismatch between start and end')

    square_distance = sum(d * d for d in utils.deltas(start, end))
    distance = math.sqrt(square_distance)

    return round(distance)

Should I create a PR for this?

@Matej-Chmel
Copy link

Matej-Chmel commented Oct 17, 2022

I have no idea why this hasn't been implemented yet but for now there is a workaround that anybody who stumbles upon this can use.

Before using the library, remove the rounding with this code.

import functools
import tsplib95 as tsp

tsp.distances.TYPES["EUC_2D"] = functools.partial(tsp.distances.euclidean, round=lambda x: x)

Similar fix can be applied to other edge weight types.

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

3 participants