def main(): parser = argparse.ArgumentParser(description="Compute the partition of a " "graph using the Spectral Partition Algorithm.") parser.add_argument('--nodes-file', '-f', help='the file containing the nodes', default='demo_nodes.txt') parser.add_argument('--output-file', '-o', help='the filename of the' ' communities PNG graph to be written') args = parser.parse_args() # part_1, part_2, nodes, edges, nodes_data, node_map = partition_graph(args.nodes_file) # create_subgraph(part_1, edges, nodes_data, node_map, 'part_1.stg') # create_subgraph(part_2, edges, nodes_data, node_map, 'part_2.stg') add_deadline() tasks = parse_tasks('deadline.stg') population_size = 100 population = create_population(tasks, population_size) fitness_history = [grade(tasks, population), ] for i in range(500): population = evolve(tasks, population) score, missed = grade(tasks, population) fitness_history.append(score) print('iteration {} population: {} score: {} missed: {}'.format(i + 1, population_size, score, missed))
def oneway(*blocks, showid=False, unfold=True, stretch=2, output=False, ga=False, **gakwargs) -> Union[str, Tuple[str, Gene]]: global _blocks, _unfold _str = StringIO() _stretch, _unfold = stretch, unfold _blocks = blocks _len = len(blocks) if _len > 0: _sequence = tuple(range(_len)) if ga: gakw_local = dict(evaluate=intersect, size=_len, elite=0.2, population=100, generation=300, showprogress=True) gakw_local.update(**gakwargs) _sequence = evolve(_sequence, **gakw_local) _inters = intersect(_sequence, output=True) print(f'{_inters[0]} intersection(s)', file=_str) print(f'< {_inters[-1][0]} | {_inters[-1][1]} >', file=_str) _blocks = tuple([_blocks[i] for i in _sequence]) _map = draw(_blocks, *_inters[1:], showid=showid, stretch=2) for row in _map: print('\n', end='', file=_str) for p in row: print(p, end='', file=_str) print(file=_str) if output: return _str.getvalue(), _sequence else: return _str.getvalue()
def test_evolve(self): print(self._str('evolve')) from genetics import evolve def f(nlist): s = 0 for n in nlist: s = s * 10 + n return s print(evolve(evaluate=f, size=15, generation=20, showprogress=False))
def evolve_further(): print('Loading population from',tempfile,'...') model = new_actor() population = [] try: # population = load_actors(savefile, model, criterion='best', num=population_size) population = undump_genomes(tempfile, model) os.remove(tempfile) except: print('Could not load from',tempfile,'.') population = list(population) # enforce type of population # Add members to population as necessary population += [new_actor() for _ in range(population_size - len(population))] print('Evolving population...') population = evolve(population, env, savefile=savefile, dumpfile=tempfile, **evolve_args) dump_genomes(tempfile, population)
from actor import GeneticNNActor from genetics import evolve, render_from_file HIDDEN_LAYERS = [10, 6] SAVEFILE = 'MsPacman_NN_10_6.txt' try: env = gym.make('MsPacman-ram-v0') population = [ GeneticNNActor(env.observation_space, env.action_space, hidden_layers=HIDDEN_LAYERS) for _ in range(100) ] model = population[0] evolve(population, env, generations=1000, simulation_reps=5, p_mutation=0.05, mutation_scale=0.25, max_steps=100000, render_gens=None, savefile=SAVEFILE, savenum=3, allow_parallel=True) except KeyboardInterrupt: print('Interrupted...') pass finally: print('Top 3 Actors found:') render_from_file(SAVEFILE, model, env, num=3)
import gym from actor import GeneticPerceptronActor from execution import simulate from genetics import evolve env = gym.make('Acrobot-v1') population = [ GeneticPerceptronActor(env.observation_space, env.action_space) for _ in range(100) ] evolve(population, env, generations=101, simulation_reps=25, max_steps=250, render_gens=5)
if __name__ == '__main__': import gym from actor import GeneticPerceptronActor from execution import simulate from genetics import evolve env = gym.make('CartPole-v1') population = [ GeneticPerceptronActor(env.observation_space, env.action_space) for _ in range(100) ] evolve(population, env, generations=21, simulation_reps=25, max_steps=20000, render_gens=1, savefile='CartPole_Actors.txt', savenum=1)