-
Notifications
You must be signed in to change notification settings - Fork 56
Description
中文版本
您的功能请求是否与某个问题相关?请描述。
在llm4ad/task/optimization/tsp_construct模块中,我发现了一个效率问题。目前在get_instance.py中的GetData.generate_instances()方法会计算城市间的距离矩阵,但在evaluation.py的evaluate函数中,这个预先计算好的距离矩阵并未被使用,而是通过generate_neighborhood_matrix函数重新计算了一次距离。这种重复计算会导致性能浪费,特别是在处理大规模TSP实例时。
描述您希望的解决方案
建议修改代码以避免重复计算距离矩阵:
- 方案一:在
get_instance.py中不计算距离矩阵,只返回城市坐标。 - 方案二:在
evaluation.py中直接使用已经计算好的距离矩阵,而不是重新计算。
个人更倾向于方案二,因为它可以保持现有API的兼容性,同时提高性能。
您考虑过的替代方案
另一种方案是在generate_neighborhood_matrix函数中添加一个参数,允许传入预先计算好的距离矩阵,这样可以在有距离矩阵时直接使用,没有时再计算。但这会增加接口的复杂性,可能不如直接修改更清晰。
附加上下文
这个优化虽然在小规模问题上影响不大,但对于大规模TSP实例或需要频繁评估的场景,可以显著提高性能。同时,这也符合DRY(Don't Repeat Yourself)原则,使代码更加简洁和高效。
英文版本
Is your feature request related to a problem? Please describe.
I've identified an efficiency issue in the llm4ad/task/optimization/tsp_construct module. Currently, the GetData.generate_instances() method in get_instance.py calculates a distance matrix between cities, but in the evaluate function in evaluation.py, this pre-calculated distance matrix is not used. Instead, distances are recalculated through the generate_neighborhood_matrix function. This redundant computation leads to performance waste, especially when dealing with large-scale TSP instances.
Describe the solution you'd like
I suggest modifying the code to avoid recalculating the distance matrix:
- Option 1: Don't calculate the distance matrix in
get_instance.py, only return city coordinates. - Option 2: Directly use the pre-calculated distance matrix in
evaluation.pyinstead of recalculating it.
I personally prefer Option 2 as it maintains compatibility with the existing API while improving performance.
Describe alternatives you've considered
Another approach would be to add a parameter to the generate_neighborhood_matrix function that allows passing a pre-calculated distance matrix. This way, the function could use the matrix if available or calculate it if not. However, this would increase the complexity of the interface and might not be as clear as a direct modification.
Additional context
While this optimization may have minimal impact on small-scale problems, it can significantly improve performance for large-scale TSP instances or scenarios requiring frequent evaluations. Additionally, it aligns with the DRY (Don't Repeat Yourself) principle, making the code more concise and efficient.