Esempio n. 1
0
def runGA(genomeFilepath, resultsFilepath, numSpecies, speciesSize, mutationRate, evalTime, numEnvs, numGens):
	allFitnessScores = {}
	envs = ENVIRONMENTS(numEnvs)
	parents = initialize_population(numSpecies, speciesSize, mutationRate, evalTime, numEnvs)

	for g in range(1, numGens+1):
		children = SWARM(numSpecies, speciesSize, mutationRate, evalTime, numEnvs)
		children.Fill_From(parents)
		children.evaluateSwarms(envs, pp=False, pb=True)
		print(g),
		children.Print()
		parents = children
		allFitnessScores[g] = parents.getFitnesses()
		pickle.dump(allFitnessScores, open(os.path.join("results", resultsFilepath + ".txt"), "wb"))
		pickle.dump(parents, open(os.path.join("results", genomeFilepath + ".txt"), "wb"))
def main():
	envs = ENVIRONMENTS(c.numEnvs)
	if (c.loadPickledPopulation and os.path.isfile("save.txt")):
		parents = pickle.load(open("save.txt", "rb"))
	else:
		parents = initialize_population()

	for g in range(1, c.numGens+1):
		children = SWARM(c.numSpecies, c.speciesSize, c.mutationRate, c.evalTime, c.numEnvs)
		children.Fill_From(parents)
		children.evaluateSwarms(envs, pp=False, pb=True)
		print(g),
		children.Print()
		parents = children
		pickle.dump(parents, open("save.txt", "wb"))
Esempio n. 3
0
from __future__ import print_function
import pyrosim
import matplotlib.pyplot as plt
import random
import copy
from robot import ROBOT
import pickle
from population import POPULATION
from individual import INDIVIDUAL
import constants as c
from environments import ENVIRONMENTS
from environment import ENVIRONMENT

envs = ENVIRONMENTS()

parents = POPULATION(c.popSize)
parents.Initialize()
parents.Evaluate(envs, pp=False, pb=True)
#
parents.Print()

for g in range(1, c.numGens):

    children = POPULATION(c.popSize)
    children.Fill_From(parents)
    children.Evaluate(envs, pp=False, pb=True)
    print(g),
    children.Print(),
    print(),
    parents.ReplaceWith(children)
import random
import copy
import logging
logging.basicConfig(level=logging.INFO)
import pickle
import pyrosim
import matplotlib.pyplot as plt
import numpy as np
from robot import ROBOT
from individual import INDIVIDUAL
from population import POPULATION
import constants
from environments import ENVIRONMENTS

# create environment with "light source"
envs = ENVIRONMENTS(numEnvs=constants.numEnvs, eval_time=constants.eval_time)

# create initial population
parents = POPULATION(envs,
                     popSize=constants.popSize,
                     eval_time=constants.eval_time)
parents.initialize()
parents.evaluate(play_blind=True, play_paused=False)
print(0, parents)

# evolve:
for g in range(constants.numGen):
    children = POPULATION(envs, parents=parents)
    children.fillFrom(parents)
    children.evaluate(play_blind=True, play_paused=False)
    parents.replaceWith(children)
Esempio n. 5
0
from environments import ENVIRONMENTS
from swarm import SWARM
import constants as c
import pickle

envs = ENVIRONMENTS(c.numEnvs)
parents = pickle.load(open("save.txt", "rb"))
parents.evaluateSwarms(envs, pp=True, pb=False)
Esempio n. 6
0
def initialize_population(numSpecies, speciesSize, mutationRate, evalTime, numEnvs):
	parents = SWARM(numSpecies, speciesSize, mutationRate, evalTime, numEnvs)
	parents.Initialize()
	envs = ENVIRONMENTS(numEnvs)
	parents.evaluateSwarms(envs, pp=True, pb=True)
	return (parents)
    if play_result:
        parents.evaluate(envs, best=True)

    if store_fitness is not None:
        parents.p[0].print(to=store_fitness)


def test_loop(pop_range: list, t_fraction: float, m_fraction: float):
    with open('data_out3.csv', 'w') as output:
        output.write('pop_size,t_size,m_rate,score')

        for pop_size in range(*pop_range):

            for t_size in range(1,
                                int(t_fraction * pop_size) + 1,
                                int(pop_size / 10)):
                C.set_cons('t_size', t_size)

                for m_rate in range(1, int(m_fraction * 72) + 1, int(72 / 10)):

                    C.set_cons('m_rate', m_rate)
                    output.write(f'\n{pop_size},{t_size},{m_rate}')
                    genetic_algorithm(
                        pop_size, f'playbacks2/p{pop_size}t{t_size}m{m_rate}',
                        False, output)


envs = ENVIRONMENTS() if C.numEnvs > 0 else None
# test_loop([25, 26, 5], 0.7, 0.5)
genetic_algorithm()