import math import random import matplotlib.pyplot as plt CITYCOORDS = [ (16.47, 96.1),…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
import math import random import matplotlib.pyplot as plt
CITY_COORDS = [ (16.47, 96.1), (16.47, 94.44), (20.09, 92.54), (22.39, 93.37), (25.23, 97.24), (17.2, 96.29), (16.3, 97.38), (14.05, 98.12), (16.53, 97.38), (21.52, 95.59) ]
POPULATION_SIZE = 1000 ELITISM = 0.1 MUTATION_RATE = 0.1 NUM_GENERATIONS = 500
class Individual: def init(self, chromosome): self.chromosome = chromosome self.fitness = self.calculate_fitness()
codedef calculate_fitness(self): total_distance = 0 for i in range(len(self.chromosome) - 1): total_distance += self.get_distance( CITY_COORDS[self.chromosome[i]], CITY_COORDS[self.chromosome[i+1]]) total_distance += self.get_distance( CITY_COORDS[self.chromosome[-1]], CITY_COORDS[self.chromosome[0]]) return 1 / total_distance def get_distance(self, city1, city2): return math.sqrt((city1[0] - city2[0])**2 + (city1[1] - city2[1])**2)class Population: def init(self): self.individuals = [Individual(self.generate_chromosome()) for i in range(POPULATION_SIZE)]
code - Assistant
作为语言模型AI,Open AI GPT-3隐藏层数量很多,它不能访问因公共API访问时的某一固定结果。因此,它不太适合模拟交互式图形化输出(如Matplotlib图形)。但是,该代码可以在Jupyter Notebook或类似的交互式环境中运行,并以图形形式显示Evolved Traveling Salesman问题的解决方案的进化过程。