File size: 3,911 Bytes
6dd006c 2573a4b 6dd006c a50ab0e 6dd006c a50ab0e 6dd006c 3a1f29e 6dd006c 3a1f29e 6dd006c f620fdf a50ab0e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import time
import shutil
import gradio as gr
from data4co import TSPConcordeSolver, draw_tsp_problem, draw_tsp_solution
TSP_DEFAULT_PATH = "media/tsp_default.png"
TSP_PROBLEM_PATH = "media/tsp_problem.png"
TSP_SOLUTION_PATH = "media/tsp_solution.png"
def _handle_tsp_solve(
file_path: str,
norm: str,
):
if file_path is None:
raise gr.Error("Please upload a '.tsp' file!")
if norm == '':
norm = "EUC_2D"
if norm != "EUC_2D" and norm != "GEO":
raise gr.Error("Invaild edge_weight_type! Only support 'GEO' and 'EUC_2D'.")
solver = TSPConcordeSolver(scale=1)
solver.from_tsp(file_path, norm=norm)
start_time = time.time()
solver.solve()
solved_time = time.time() - start_time
tours = solver.tours
points = solver.points
draw_tsp_problem(
save_path=TSP_PROBLEM_PATH,
points=points,
)
draw_tsp_solution(
save_path=TSP_SOLUTION_PATH,
points=points,
tours=tours
)
message = "Successfully solve the TSP problem, using time ({:.3f}s).".format(solved_time)
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH
def handle_tsp_solve(
file_path: str,
norm: str,
):
try:
message = _handle_tsp_solve(file_path, norm)
return message
except Exception as e:
message = str(e)
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH
def handle_tsp_clear():
shutil.copy(
src=TSP_DEFAULT_PATH,
dst=TSP_PROBLEM_PATH
)
shutil.copy(
src=TSP_DEFAULT_PATH,
dst=TSP_SOLUTION_PATH
)
message = "successfully clear the files!"
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH
def convert_image_path_to_bytes(image_path):
with open(image_path, "rb") as f:
image_bytes = f.read()
return image_bytes
with gr.Blocks() as tsp_page:
gr.Markdown(
'''
This space displays the solution to the TSP problem.
## How to use this Space?
- Upload a '.tsp' file from tsplib .
- The images of the TSP problem and solution will be shown after you click the solve button.
- Click the 'clear' button to clear all the files.
'''
)
with gr.Row(variant="panel"):
with gr.Column(scale=7):
with gr.Row():
tsp_file = gr.File(
file_types=[".tsp"],
scale=3
)
info = gr.Textbox(
value="",
label="Log",
scale=4,
)
norm = gr.Textbox(
label="Please input the edge_weight_type of the TSP file",
)
with gr.Column(scale=4):
tsp_problem_img = gr.Image(
value=TSP_PROBLEM_PATH,
type="filepath",
label="TSP Problem",
)
with gr.Column(scale=4):
tsp_solution_img = gr.Image(
value=TSP_SOLUTION_PATH,
type="filepath",
label="TSP Solution",
)
with gr.Row():
with gr.Column(scale=1, min_width=100):
solve_button = gr.Button(
value="Solve",
variant="primary",
scale=1
)
with gr.Column(scale=1, min_width=100):
clear_button = gr.Button(
"Clear",
variant="secondary",
scale=1
)
with gr.Column(scale=8):
pass
solve_button.click(
handle_tsp_solve,
[tsp_file, norm],
outputs=[info, tsp_problem_img, tsp_solution_img]
)
clear_button.click(
handle_tsp_clear,
inputs=None,
outputs=[info, tsp_problem_img, tsp_solution_img]
)
if __name__ == "__main__":
tsp_page.launch(debug = True) |