You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tsp_api.py 874 B

1234567891011121314151617181920212223242526272829303132333435
  1. from .tsp import tsp_data
  2. def change_dist(dist: dict, i: int, j: int, new_cost: float) -> float:
  3. """Change the distance between two points.
  4. Args:
  5. dist (dict): distance matrix, where the key is a pair and value is
  6. the cost (aka, distance).
  7. i (int): the source node
  8. j (int): the destination node
  9. new_cost (float): the new cost for the distance
  10. Returns:
  11. float: the previous cost
  12. """
  13. prev_cost = dist[i, j]
  14. dist[i, j] = new_cost
  15. return prev_cost
  16. def compare_costs(prev_cost, new_cost) -> float:
  17. """Compare the previous cost and the new cost.
  18. Args:
  19. prev_cost (float): the previous cost
  20. new_cost (float): the updated cost
  21. Returns:
  22. float: the ratio between these two costs
  23. """
  24. return (new_cost - prev_cost) / prev_cost
  25. dists = tsp_data(5, seed=1)