class GENEALG:
    def __init__(self, play_back=False):
        if play_back:
            f = open('robot.p', 'rb')
            self.parent = pickle.load(f)
            f.close()
        else:
            self.parent = POPULATION(c.popSize)
            self.parent.Initialize()
            self.parent.Evaluate()

    def Evolve(self):
        for g in range(c.numGens):
            print(g, end=' ')
            self.parent.Print()
            child = POPULATION(c.popSize)
            child.Fill_From(self.parent)
            child.Evaluate()
            self.parent.ReplaceWith(child)

    def Show_Best(self):
        bestIndividual = self.parent.Best_Individual()
        self.parent.p[bestIndividual].Start_Evaluation(pb=False, pp=True)
        self.parent.p[bestIndividual].Compute_Fitness()
        print(self.parent.p[bestIndividual].fitness)

    def Save(self):
        f = open('robot.p', 'wb')
        pickle.dump(self.parent, f)
        f.close()
 def Evolve(self):
     for g in range(c.numGens):
         print(g, end=' ')
         self.parent.Print()
         child = POPULATION(c.popSize)
         child.Fill_From(self.parent)
         child.Evaluate()
         self.parent.ReplaceWith(child)
Beispiel #3
0
from population import POPULATION
import copy
import pickle

parents = POPULATION(10)
parents.Initialize()
parents.Evaluate(pb=True)
parents.Print()

# for loop that creates n iterations
for g in range(1, 200):

    children = POPULATION(10)

    children.Fill_From(parents)

    children.Evaluate(pb=True)

    print(g, end='  ')
    children.Print()

    parents.ReplaceWith(children)

    # save structure of child that fares well
    f = open('robot1.p', 'wb')
    pickle.dump(children, f)
    f.close()

f = open('robot1.p', 'rb')

best = pickle.load(f)
Beispiel #4
0
from population import POPULATION
import copy

parents = POPULATION(10)

parents.Initialize()  # 09

parents.Evaluate(pp=False, pb=True)

parents.Print()

for g in range(1, 200):
    children = copy.deepcopy(parents)
    children.Mutate()
    children.Evaluate(pp=False, pb=True)
    parents.ReplaceWith(children)
    #parents.Sort()
    print g,
    parents.Print()

parents.Evaluate(pp=True, pb=False)
Beispiel #5
0
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)

if (g == c.numGens - 1):
    print(parents.p[0].fitness)
Beispiel #6
0
from environments import ENVIRONMENTS
from population import POPULATION
import constants as c

envs = ENVIRONMENTS()

##import copy
##
parents = POPULATION(c.popSize)
parents.Initialize()
parents.Evaluate(envs, False, False)
##print('P', end = ' ')
##parents.Print()
##
##for g in range(1, c.numGens + 1):
##    children = POPULATION(c.popSize)
##    children.Fill_From(parents)
##    children.Evaluate(False, True)
##    print(g, end = ' ')
##    children.Print()
##    parents.ReplaceWith(children)
##
##children.p[0].Start_Evaluation(True, False)
import pyrosim
import random
import math
import copy
import pickle
import matplotlib.pyplot as plt
from robot import ROBOT
from individual import INDIVIDUAL
from population import POPULATION

parents = POPULATION(5)
parents.Evaluate()
parents.Print()

for g in range(1, 100):
    children = copy.deepcopy(parents)
    children.Mutate()
    children.Evaluate()
    parents.ReplaceWith(children)
    print(g, end=' ')
    parents.Print()
Beispiel #8
0
SEED = int(sys.argv[1])
D = int(sys.argv[2])
random.seed(SEED)
file = open(f'results/history/Bezier-D{D}-Seed-{SEED}-Best.csv', 'w')
file.write('G, Fitness\n')
file.close()
file = open(f'results/history/Bezier-D{D}-Seed-{SEED}-Avg.csv', 'w')
file.write('G, Fitness\n')
file.close()

call(['mkdir', f'results/history/Bezier-genomes-D{D}-Seed{SEED}'])

print("GENERATION: " + str(0))
print("========================================================")
parents = POPULATION(c.POP_SIZE, D)
parents.Evaluate()
parents.SaveFitnessHistory(0, SEED, D)
parents.SaveBestIndiv(SEED, D)

# for r in range(1, c.RUNS+1)
for g in range(1, c.GENS):
    print("GENERATION: " + str(g))
    print("========================================================")
    children = POPULATION(c.POP_SIZE, D)
    children.Fill_From(parents)
    children.Evaluate()

    parents.ReplaceWith(children)
    parents.SaveFitnessHistory(g, SEED, D)
    parents.SavePopulation(g, SEED, D)
    parents.SaveBestIndiv(SEED, D)
Beispiel #9
0
import pyrosim
import random
import copy
import pickle
import matplotlib.pyplot as plt
from population import POPULATION

# enter simulation parameters
N = 10 # number of individuals in a population
G = 1000 # number of generations
pb = True # set play blind setting: True = blind mode (recommended); False = draw to the screen

# create a population of parents
parents = POPULATION(N) # construct a POPULATION class instance : N parents with different genomes
parents.Evaluate(pb, 'doNotSave') # evaluate N unrelated robots in turn

print('Fitness values of the 1st generation of parents:')
parents.Print()
print # end the current line of printing

print('Fitness values of consequent generations of parents:')

for g in range(0, G): # iterate over generations 0 to G

    # create a population of children
    children = copy.deepcopy(parents) # create children which are clones of the parents
    children.Mutate() # add mutations to the children population
    children.Evaluate(pb, 'doNotSave') # evaluate N unrelated robots in turn

    parents.ReplaceWith(children) # replace less fit parents with their more fit children; leave more fit parents in the parent population
Beispiel #10
0
import pyrosim
import matplotlib.pyplot as plt
from robot import ROBOT
from individual import INDIVIDUAL
import random
import copy
import pickle
from population import POPULATION

parents = POPULATION(10)
parents.Evaluate(True)
parents.Print()

for g in range(1, 200):
    children = copy.deepcopy(parents)
    children.Mutate()
    children.Evaluate(True)
    parents.ReplaceWith(children)
    print(g)
    parents.Print()

max_index = 0
max_fitness = 0
for i in range(len(parents.p)):
    if (parents.p[i].fitness > max_fitness):
        max_index = i
        max_fitness = parents.p[i].fitness

parents.p[max_index].Start_Evaluation(False)
parents.p[max_index].Compute_Fitness()
Beispiel #11
0
        header.append("ParentID")
        header.append('Solution_' + str(i + 1))
    writer = csv.writer(f, delimiter=',')
    writer.writerow(header)

    envs = ENVIRONMENTS()

    # setting the random seed
    np.random.seed(c.randomSeed)
    random.seed(c.randomSeed)

    # generate initial population
    parents = POPULATION(popsize=c.popSize)
    parents.Initialize()
    # Evaluating the first gen of parents
    parents.Evaluate(envs=envs)

    parents.Print()

    for i in range(c.numGens):

        # Write data to csv
        line = [i]
        for j in range(c.popSize):
            line.append(parents.p[j].ID)
            line.append(parents.p[j].fitness)
        writer.writerow(line)

        # If the gen number is right, then it saves the replay of the best individual in the that generation
        if i in instant_replay:
            filename = "RobotReplayGen_" + str(i) + ".p"
Beispiel #12
0
from population import POPULATION
import copy
import constants as c
from environments import ENVIRONMENTS

envs = ENVIRONMENTS()

parents = POPULATION(c.popSize)
parents.Initialize()
parents.Evaluate(envs, False, True, c.evalTime)
parents.Print()
for g in range(1, c.numGens):
    children = POPULATION(c.popSize)
    children.Fill_From(parents)
    children.Evaluate(envs, False, True, c.evalTime)
    #    children.Print()
    #    children = copy.deepcopy(parents)
    #    children.Mutate()
    #    children.Evaluate(True)
    parents.ReplaceWith(children)
    print(g),
    children.Print()
#for e in range(0, c.numEnvs):

#parents.p[0].Start_Evaluation(envs.envs[3], True, False, c.evalTime)
#parents.p[0].Start_Evaluation(envs.envs[2], True, False, c.evalTime)
#parents.p[0].Start_Evaluation(envs.envs[1], True, False, c.evalTime)
parents.p[0].Start_Evaluation(envs.envs[0], True, False, c.evalTime)

#parent = INDIVIDUAL()
#parent.Evaluate(True)
Beispiel #13
0
# print results for each parent population
print("\nfitness of parents:"),
parents.Print()

###################
# EVOLVE CHILDREN #
###################
startTime = time.time()
g = 0
while (time.time() - startTime < c.runTime):
    # initialize children
    children = POPULATION(c.popSize)
    children.Fill_From(parents)

    # evaluate children
    children.Evaluate(envs, True)

    # print generation and time
    print("\n results of generation: ", g)
    print("\n evaluation time:", time.time() - startTime)
    print("\n remaining time:", c.runTime - (time.time() - startTime))

    # print results for each parent population
    print("\n fitness of children:"),
    children.Print()
    print("\n")

    # ---REPLACE---#
    parents.ReplaceWith(children)

    # ---TABULATE FITNESS---#
Beispiel #14
0
###########

import pickle
import random
from individual import INDIVIDUAL
from population import POPULATION
from environments import ENVIRONMENTS
import constants as c

#---ENVIRONMENTS---#
envs = ENVIRONMENTS()

#---PARENTS---#
parents = POPULATION(c.popSize)
parents.Initialize()
parents.Evaluate(envs, True)
parents.Print()

#---CHILDREN---#
for g in range(0, c.numGens):

    children = POPULATION(c.popSize)
    children.Fill_From(parents)
    children.Evaluate(envs, True)
    print("\n")
    print(g),
    children.Print()

    #---REPLACE---#
    parents.ReplaceWith(children)
Beispiel #15
0
import pickle
import constants as c
from population import POPULATION
# import matplotlib.pyplot as plt

parent = POPULATION(c.popSize)
parent.Initialize()
parent.Evaluate(True)
for g in range(c.numGens):
    print(g, end=' ')
    parent.Print()
    child = POPULATION(c.popSize)
    child.Fill_From(parent)
    child.Evaluate()
    parent.ReplaceWith(child)
bestIndividual = parent.Best_Individual()
parent.p[bestIndividual].Start_Evaluation(pb=False, pp=True)
parent.p[bestIndividual].Compute_Fitness()

f = open('robot.p', 'wb')
pickle.dump(parent, f)
f.close()
Beispiel #16
0
from robot import ROBOT
from individual import INDIVIDUAL
from population import POPULATION
import pyrosim
import random
import copy
import pickle

pop = POPULATION(5)
pop.Evaluate()
pop.Print()

# parent = INDIVIDUAL()
# parent.Evaluate(True)
# print(parent.fitness)

# for i in range (0, 500):
#     child = copy.deepcopy( parent )
#     child.Mutate()
#     child.Evaluate(True)
#     print('[g:' ,i , '] [p:' ,parent.fitness , ']', '[c:' ,child.fitness , ']')
#     if ( child.fitness > parent.fitness ):
#       parent = child
#       parent.Evaluate(True)
    
#     f = open('robot.p','wb')
#     pickle.dump(parent , f )
#     f.close()

from population import POPULATION
import copy
import pickle


parents = POPULATION(10)
parents.Evaluate(pb=True)
#exit()
parents.Print()

results = []

# for loop that creates n iterations
for g in range(1, 200):

    # makes a copy of parents
    children = copy.deepcopy(parents)

    # mutates the parents to make the children
    children.Mutate()

    # evaluates the children
    children.Evaluate(pb=True)

    # if they do better, they replace the parents
    parents.ReplaceWith(children)

    # print generation
    print(g, end='')

    # print the fitness
Beispiel #18
0
###########

import pickle
import random
from individual import INDIVIDUAL
from population import POPULATION
from environments import ENVIRONMENTS
import constants as c

#---ENVIRONMENTS---#
envs = ENVIRONMENTS()

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

"""
#---CHILDREN---#
for g in range (0,c.numGens):
	
	children = POPULATION(c.popSize)
	children.Fill_From(parents)
	children.Evaluate(envs, True)
	print("\n")
	print(g),
	children.Print()

	#---REPLACE---#
	parents.ReplaceWith(children)
Beispiel #19
0
avgFitnesses = []
tValues = []

t = c.Min_dist

parents = POPULATION(popSize)
parents.Initialize()

if (Load):
    f = open('parents1.p', 'rb')
    parents = pickle.load(f)
    f.close()
    #parents.p[0] = best

parents.Evaluate(True, t)
print('Gen 0: ', end='')
parents.Print()

#count = 0

for g in range(1, c.totGens):
    if (g >= c.gens):
        t = c.Max_dist
    #if (count == c.inc_time):
    #   count = 0
    #  if (t+c.gen_incremement <= c.Max_dist):
    #     t += c.gen_incremement

    children = POPULATION(popSize)
    children.Fill_From(parents)
fits = []
envs = ENVIRONMENTS()

###### Make a population ######
parents = POPULATION(c.popSize)
parents.Initialize()

###### Load Precursor into Population ######
if os.path.isfile(pickledPop) and loadPrecursor:

    f = open(pickledPop, "rb")
    parents.Copy_Best_From(pickle.load(f), cSurv)
    f.close()

###### Print out fitness of initial parent ######
parents.Evaluate(envs, hideSim=True, startPaused=False, printFit=False)
parents.Print()

###### Iterate over generations ######
for g in range(0, c.numGens):

    # Initialize Children and fill population
    children = POPULATION(c.popSize)

    children.Copy_Best_From(parents, cSurv)

    # Rapture chance
    if random.random() < c.perRapture:

        print('Rapture')
        children.Rapture()
import pyrosim
import matplotlib.pyplot as plt
import random
import numpy
import copy
import pickle

from individual import INDIVIDUAL
from robot import ROBOT
from population import POPULATION

parents = POPULATION(
    10)  # creates a empty population but stores the population size
parents.Initialize()  #start a population of individuals of size 'i'
parents.Evaluate(True)  # Evaluates the fitness of each individual
#exit() # exit the program immediately
parents.Print()  #print ID and fitness of each individual

for i in range(1, 200):
    children = POPULATION(5)
    #children.Print()
    children.Fill_From(parents)
    #children.Print()
    children.Evaluate(True)
    children.Print()
    parents = children

parents.Evaluate(False)
parents.Print()
Beispiel #22
0
### created by Max Marder

###########
# IMPORTS #
###########

import pickle
import random
from individual import INDIVIDUAL
from population import POPULATION

#---PARENTS---#
parents = POPULATION(10)
parents.Initialize()
parents.Evaluate(True)
print("0"),
parents.Print()

#---CHILDREN---#
for g in range(1, 201):

    children = POPULATION(5)
    children.Fill_From(parents)
    children.Evaluate(True)
    print("\n")
    print(g),
    children.Print()

    #---REPLACE---#

    parents.ReplaceWith(children)
Beispiel #23
0
#     child.Evaluate(hideSim=True)
#
#     print("[g: ", i, "] [PW: ", parent.genome," ][P: ", parent.fitness, "] [C:", child.fitness, "]")
#     fits.append(parent.fitness)
#
#     if (child.fitness > parent.fitness):
#         parent = child
#         f = open("robot.p", "wb")
#         pickle.dump(parent, f)
#         f.close()
# parent.Evaluate(hideSim=False, startPaused=True)

# Make a population
parents = POPULATION(10)
parents.Initialize()
parents.Evaluate(startPaused=False)
parents.Print()

for g in range(0, 200):

    children = POPULATION(10)
    children.FillFrom(parents)
    parents.ReplaceWith(children)
    parents.Evaluate()

    print(g, end=' ')
    children.Print()

#     # Make a copy and evaluate them
#     children = cp.deepcopy( parents )
#     children.Mutate()
Beispiel #24
0
import copy
import pickle
from individual import INDIVIDUAL
from population import POPULATION
# import matplotlib.pyplot as plt

parent = POPULATION(5)
parent.Evaluate()
for g in range(100):
    print(g, end=' ')
    parent.Print()
    child = copy.deepcopy(parent)
    child.Mutate()
    child.Evaluate()
    parent.ReplaceWith(child)
# parent = INDIVIDUAL()
# parent.Evaluate(False)
# for g in range(0, 100):
#     child = copy.deepcopy(parent)
#     child.Mutate()
#     child.Evaluate(True)
#     print('[g: ', g, ' ] ', '[pw: ', parent.genome, ' ] ',
#           '[p: ', parent.fitness, ' ] ', '[c: ', child.fitness, ' ]')
#     if child.fitness > parent.fitness:
#         parent = child
#         parent.fitness = child.fitness
#         parent.Evaluate(False)
# f = open('robot.p', 'wb')
# pickle.dump(parent, f)
# f.close()
Beispiel #25
0
import pyrosim
import matplotlib.pyplot as plt 
import random
import pickle
import copy

from robot import ROBOT
from individual import INDIVIDUAL
from population import POPULATION

parents = POPULATION(10)
parents.Evaluate(True)
print('Parents: ', end='')
parents.Print()

for g in range(0,200):
    children = copy.deepcopy(parents)
    children.Mutate()
    children.Evaluate(True)
    parents.ReplaceWith(children)
    print(g, end=' ')
    parents.Print()

parents.Evaluate(False)


#parent = INDIVIDUAL()
#parent.Evaluate(True)
#print(parent.fitness)

#for i in range(0,100):
Beispiel #26
0
    # load in parents
    parents_1 = pickle.load(file_1)
    parents_2 = pickle.load(file_2)
    parents_3 = pickle.load(file_3)
    # close files
    file_1.close()
    file_2.close()
    file_3.close()

# skip parent evaluation, child evolution, and saving in movie mode
if (not c.movieMode):

    #---EVALUATE PARENTS---#

    # evaulate parents in respective environments types
    parents_1.Evaluate(envs_1, True, pp=False)
    parents_2.Evaluate(envs_2, True, pp=False)
    parents_3.Evaluate(envs_3, True, pp=False)

    # print results for each parent population
    print("\nfitness of parents 1 in envs 1:"),
    parents_1.Print()
    print("\nfitness of parents 2 in envs 2:"),
    parents_2.Print()
    print("\nfitness of parents 3 in envs 3:"),
    parents_3.Print()
    print("\n")

    ###################
    # EVOLVE CHILDREN #
    ###################
Beispiel #27
0
from Individual import INDIVIDUAL
from population import POPULATION
import matplotlib.pyplot as plt
import random
import pickle
import copy
import constants as c
from ThrowingRobot import ROBOT

parents = POPULATION(1)
parents.Evaluate(False)

# for j in range(0,1):
#     print(j, end="")
#     parents.Print()
#     children = copy.deepcopy(parents)
#     children.Mutate()
#     children.Evaluate(True)
#     parents.ReplaceWith(children)
#     parents.fitness_graph()
#
# parents.Evaluate(False)
Beispiel #28
0
import copy
import pickle
from individual import INDIVIDUAL
from population import POPULATION
# import matplotlib.pyplot as plt

parent = POPULATION(10)
parent.Evaluate(False)
for g in range(200):
    print(g, end=' ')
    parent.Print()
    child = copy.deepcopy(parent)
    child.Mutate()
    child.Evaluate()
    parent.ReplaceWith(child)
parent.Evaluate(False)
# parent = INDIVIDUAL()
# parent.Evaluate(False)
# for g in range(0, 100):
#     child = copy.deepcopy(parent)
#     child.Mutate()
#     child.Evaluate(True)
#     print('[g: ', g, ' ] ', '[pw: ', parent.genome, ' ] ',
#           '[p: ', parent.fitness, ' ] ', '[c: ', child.fitness, ' ]')
#     if child.fitness > parent.fitness:
#         parent = child
#         parent.fitness = child.fitness
#         parent.Evaluate(False)
# f = open('robot.p', 'wb')
# pickle.dump(parent, f)
# f.close()