0

I have been trying to run a code a found and I get this kind of error . I don't know how to deal with this because I am new to python and trying to understand the concept of TSP problem. Any help would be appreciated Code below

from libs.GeneticAlgorithm import GeneticAlgorithm

def main():
    poland = Country()
    poland.add([
        City('Gorlice', (49.655299, 21.159769)),
        City('Sosnowiec', (50.286263, 19.104078)),
        City('Łódź', (51.760229, 19.457209)),
        City('Wrocław', (51.108314, 17.037802)),
        City('Poznań', (52.406376, 16.925167)),
        City('Toruń', (53.013790, 18.598444)),
        City('Zielona Góra', (51.935619, 15.506186)),
        City('Szczecin', (53.428543, 14.552812)),
        City('Rzeszów', (50.041187, 21.999121)),
        City('Kraków', (50.049683, 19.944544)),
        City('Olsztyn', (53.770226, 20.490189)),
        City('Lublin', (51.245376, 22.568278))
    ])
    print('Cities:', end=' ')
    print(*(city for city in poland.cities), sep=', ')
    ga = GeneticAlgorithm(100, mutation_rate=0.5, ptype=Route, args=(poland.cities,))
    ga.run(seconds=10)
    fittest = ga.alltime_best
    best_fitness = fittest.fitness
    print('Best route:', fittest)
    print('Best fitness:', best_fitness)
    print('Generations:', ga.generation)


if __name__ == '__main__':
    main()
double-beep
  • 5,031
  • 17
  • 33
  • 41
loukous
  • 15
  • 1
  • 9

2 Answers2

1

If you "found" this code (most probably on the internet?) then the one who posted this had a libs folder in his machine, with a GeneticAlgorhythm.py module in it, so you either find this GeneticAlgorhythm module, or you won't be able to run this code successfully.

When you see, in python from baz.bar import Foo, Python is going to look for the bar module into the baz folder, and import class Foo from it. So you need to have baz module on your PC, or else that error will appear

lupodellasleppa
  • 124
  • 1
  • 1
  • 11
1

It comes from this repository: https://github.com/reconndev/Genetic-Algorithm-TSP. In order to get it running, simply clone or download the repository. Then, in the main (root) folder, run python TSP-Text.py.

python TSP-Text.py 
Cities: Gorlice, Sosnowiec, Łódź, Wrocław, Poznań, Toruń, Zielona Góra, Szczecin, Rzeszów, Kraków, Olsztyn, Lublin
Best route: Toruń->Olsztyn->Łódź->Lublin->Rzeszów->Gorlice->Sosnowiec->Kraków->Wrocław->Zielona Góra->Szczecin->Poznań
Best fitness: 0.4219619417258425
Generations: 807
Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53