Local search algorithms solving the travelling salesman problem with matplotlib plotting
- Put all the folders and files into a directory
- Create a virtual environment in that directory using cmd:
python -m venv venv
- Activate the virtual environment:
venv\Scripts\activate.bat
- Download the dependencies:
pip install -r requirements.txt
- Use an IDE (like PyCharm) and run
travelling_salesman.py
- Another window should appear with 6 subplots
- Run
main.py
while the other is also running and see the plotting real-time
There are 4 different local search types that are coded here:
It has 2 variations, random-restart and stochastic (both are shown in the plotting)
- Random-restart (sometimes also called shotgun mode) randomizes everything and take only if the result is better than the previous
- Stochastic is also random, but it only swaps points one by one and only accepts them if they improve the result. A worse result would be reverted back to the previous one
Multiple Hill Climbing instead of just 1, takes a certain number of improvements first before selecting another certain number of improved results
Example:
- Root generates 5 improvements
- From that 5, 2 is selected
- Then from those 2, each of them generate another 5 (total 10)
- And from 10, 2 is selected, and so on
Stochastic hill climbing but it has a random chance of taking a worse option in order to escape the local minima
- High initial temperature = More chaotic start
- Low alpha/cooling = Better chance to improve
- High end temperature = Might stop at local minimum
Uses the cycle algorithm to retain the parts of DNA
- DNA is first generated randomly with a certain number of population
- The population is then put into a fitness test (how well the result is or if it is even worse)
- Afterwards, each DNA will retain a random part of it and the rest will be swapped with another DNA (they come in pairs)
- Checked again whether it fits, if it does not, revert it back
- Finally, there is a small chance of mutation (random point swap) and it will once again be checked if it fits or not
- Process repeats until iteration is complete
Uses matplotlib (although PyQtGraph might be better for smoother experience)
- If plotting window is too big, it can be changed in the code
- If real-time is too slow/too fast, change the interval/frames
- If real-time does not work, switch to normal plotting (but it only shows the initial/final result)