73 |
74 | 🔩
So, what's next?
75 |
76 |
77 | Do you want to explore more public VIKTOR apps?
78 |
79 |
Explore apps
80 |
81 | Do you want to build your own VIKTOR app?
82 |
83 |
Get started!
84 |
85 | Curious how this app was built? Go check out the repository:
86 |
87 |
Github repository
88 |
89 |
90 |
--------------------------------------------------------------------------------
/source/traveling_saleman_problem.py:
--------------------------------------------------------------------------------
1 | from enum import Enum
2 | from typing import Union
3 |
4 | import numpy as np
5 |
6 | from .geneticalgorithm import geneticAlgorithm
7 | from .plotting import create_animation
8 | from .self_organizing_maps import self_organizing_maps
9 | from .two_opt import two_opt
10 |
11 |
12 | class Method(Enum):
13 | two_opt = 0
14 | GA = 1
15 | SOM = 2
16 |
17 |
18 | def tsp(
19 | cities: np.ndarray,
20 | method: Method = Method.two_opt,
21 | improvement_threshold: float = 0.001,
22 | popSize: int = 100,
23 | eliteSize: int = 20,
24 | mutationRate: float = 0.01,
25 | generations: int = 500,
26 | learning_rate: float = 0.8,
27 | decay: float = 0.0003,
28 | ) -> Union[str, dict]:
29 | """Solve the traveling salesman problem with the selected method, the different parameters are used for different methods.
30 |
31 | Args:
32 | cities: The topology of the problem.
33 | method: The algorithm to be used.
34 | improvement_threshold: The minimum improvement for two-opt to continue searching.
35 | popSize: Population size for evolutionary algorithms.
36 | eliteSize: The selection size for evolutionary algorithms.
37 | mutationRate: Mutation rate for evolutionary algorithms.
38 | generations: The number of generations an evolutionary algorithm must go through.
39 | learning_rate: Displacement of the neurons to search a route.
40 | decay: Decays the learning rate so we get less aggressive searches over time.
41 |
42 | Returns:
43 | str: The plotly figure to json so we can use it on the plotlyview.
44 | data: dictionary of the maximum iteration and calculated distance.
45 | """
46 |
47 | # If method is an int change to Enum
48 | if type(method) == int:
49 | method = Method(method)
50 |
51 | # Select the correct method
52 | if method == Method.two_opt:
53 | routes, i, distance = two_opt(cities, improvement_threshold)
54 | elif method == Method.GA:
55 | routes, i, distance = geneticAlgorithm(cities, popSize, eliteSize, mutationRate, generations)
56 | elif method == Method.SOM:
57 | routes, i, distance = self_organizing_maps(cities, generations, learning_rate, popSize, decay)
58 | else:
59 | raise NotImplementedError(f"Method {method.name} not implemented.")
60 |
61 | # Reorder the cities matrix by route order in a new matrix for plotting.
62 | new_cities_orders = [
63 | np.concatenate((np.array([cities[route[i]] for i in range(len(route))]), np.array([cities[route[0]]])))
64 | for route in routes
65 | ]
66 |
67 | # Create figure for plotly view
68 | fig = create_animation(new_cities_orders)
69 |
70 | # Create data for data view
71 | data = {"Max iteration": i, "Calculated distance": distance}
72 |
73 | return fig.to_json(), data
74 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |