本文整理汇总了Python中population.Population.evolve方法的典型用法代码示例。如果您正苦于以下问题:Python Population.evolve方法的具体用法?Python Population.evolve怎么用?Python Population.evolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.evolve方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_evolve
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import evolve [as 别名]
def test_evolve(self):
pop1 = Population(4)
pop1.evolve()
self.assertEqual(len(pop1.pop), 4)
self.assertEqual(pop1.selected, [])
self.assertEqual(pop1.selected_indexes, [])
self.assertEqual(pop1.fitness_table, [])
示例2: GameOfLifeApp
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import evolve [as 别名]
class GameOfLifeApp(App):
_board = None
_population = None
def build(self):
self._population = Population(cols=256, rows=256)
self._board = BoardWidget(width=256, height=256, population = self._population)
Clock.schedule_interval(self.callback, 0.1)
return self._board
def callback(self, dt):
self._population.evolve()
self._board.update_graphics()
示例3: run
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import evolve [as 别名]
def run(self):
"""Runs the simulation."""
parser = argparse.ArgumentParser(description="Simulates evolution"
" for tracks on a given map.")
parser.add_argument("--config_file", help="file name of the config file",
default="default.cfg")
args = parser.parse_args()
self.init_msg("Reading config ...", ok=False)
config = configparser.ConfigParser()
if os.path.isfile(args.config_file):
config.read(args.config_file)
map_file_name = config["Map"]["filename"]
population_size = config["Map"]["population_size"]
max_acceleration = config["Map"]["max_acceleration"]
distance_factor = config["Map"]["distance_factor"]
collision_penalty = config["Map"]["collision_penalty"]
retain_percentage = config["Map"]["retain_percentage"]
random_select_chance = config["Map"]["random_select_chance"]
mutate_chance = config["Map"]["mutate_chance"]
self.max_timesteps = int(config["Map"]["max_timesteps"])
confidence_level = int(config["Map"]["confidence_level"])
write_plots = literal_eval(config["Plots"]["enabled"])
self.out_directory = config["Plots"]["out_directory"]
write_frequency = int(config["Plots"]["frequency"])
clean_previous = literal_eval(config["Plots"]["clean_previous"])
plot_grades = literal_eval(config["Plots"]["plot_grades"])
else:
raise Exception("Config file " + args.config_file + " not found. Exiting.")
self.init_msg("Reading config", progress=1, ok=True)
if clean_previous and os.path.exists(self.out_directory):
self.init_msg("Cleaning previous plots ...", ok=False)
os.system("rm -rf " + self.out_directory + "/*")
self.init_msg("Cleaning previous plots", progress=1, ok=True)
map = Map(max_acceleration, map_file_name)
if write_plots:
self.init_msg("Saving map ...", ok=False)
save_svg("map.svg", map, out_directory=self.out_directory)
self.init_msg("Saving map", progress=1, ok=True)
self.init_msg("Generating population ", progress=0, ok=False)
population = Population(
map=map,
population_size=population_size,
distance_factor=distance_factor,
collision_penalty=collision_penalty,
retain_percentage=retain_percentage,
random_select_chance=random_select_chance,
mutate_chance=mutate_chance)
self.init_msg("Generating population", progress=1, ok=True)
# write first plot
if write_plots:
self.save(0, map, population.tracks)
for i in range(1, self.max_timesteps + 1):
self.status_msg(i, writing=False)
with Timer() as timer:
self.grade = population.evolve()
self.timestep_durations.append(timer.elapsed)
# write plots regularly
self.grades.append((i, self.grade))
if (write_plots and i % write_frequency == 0):
self.save(i, map, population.tracks)
# check if population changes
if len(self.grades) > confidence_level:
mean = (sum(y for (x,y) in self.grades[-confidence_level:])
/confidence_level)
not_changing = True
for _, grade in self.grades[-confidence_level:]:
if grade - mean > 1:
not_changing = False
if not_changing:
break
# write final plot
if (write_plots and not (i % write_frequency == 0)):
self.save(i, map, population.tracks)
print("\nPopulation isn't changing anymore. Exiting ...")
# write solution plot
self.init_msg("Writing solution ...", ok=False)
if write_plots:
save_svg("solution.svg", map, [population.tracks[0]], out_directory=self.out_directory)
with open(self.out_directory + "/solution", "w") as solution_file:
length = str(len(str(max_acceleration)) + 1)
for vector in population.tracks[0].acceleration_vectors:
solution_file.write(("({:>" + length + "d}, {:>" + length
+ "d})\n").format(vector.x, vector.y))
self.init_msg("Writing solution", progress=1, ok=True)
if plot_grades:
with open(self.out_directory + "/grades", "w") as grade_file:
for timestep, grade in self.grades:
string = ("{:>" + str(len(str(self.max_timesteps)))
+ "} {:>4.5f}\n").format(timestep, grade)
grade_file.write(string)
with open(self.out_directory + "/plot", "w") as plot_file:
plot_file.write("set term pdf\n")
plot_file.write("set out '" + self.out_directory + "/grades.pdf'\n")
#.........这里部分代码省略.........