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)
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()
except: raise Exception("Please give the polycube size as an int") if EVO_MODE == 1: print("evolving for robustness") else: print("evolving for maximum fitness") def GetNewController(): return np.random.choice(controllerTypes) aggregates = FIXEDAGGPOP(AGGREGATE, num_cubes=num_cubes) controllers = POPULATION(GetNewController(), pop_size=N, unique=True) aggregates.initialize() controllers.initialize() coevolve = COEVOLVE(aggregates, controllers, EVO_MODE, seed) latestGen = 1 if os.path.exists("./saved_generations/gen%d.p" % latestGen): while os.path.exists("./saved_generations/gen%d.p" % latestGen): print("Gen", latestGen, "exists") latestGen += 1 f = open("./saved_generations/gen%d.p" % (latestGen - 1), 'rb') saveState = pickle.load(f) coevolve = saveState[0]
import pickle from population import POPULATION ''' Instructions: Implement hill climber search www.reddit.com/r/ludobots/wiki/pyrosim/parallelhillclimber Blue diamond is the y coordinate ''' save_best = True visualize_best = True num_generations = 200 # Initial population parents = POPULATION(pop_size=100) parents.create_population() parents.evaluate() print('Generation 0...') parents.print_pop() for gen in range(num_generations): children = copy.deepcopy(parents) children.mutate() children.evaluate() parents.replace(children) print('Generation ', gen + 1, '...') parents.print_pop()
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)
import math import pip import pyrosim import random import copy import pickle from individual import INDIVIDUAL from population import POPULATION popsize = 2 gen = 2 fitvector = [[0 for c in range(popsize)] for f in range(gen)] fitprom = [[0] for f in range(gen)] parents = POPULATION(popsize, 0, fitvector, fitprom, gen) parents.Initialize() parents.Evaluate(True) print(0), parents.Print() for g in range(1, gen): children = POPULATION(popsize, g, fitvector, fitprom, gen) children.Fill_From(parents) children.Evaluate(True) print(g), children.Print() parents.p = children.p f = open('robot.p', 'w') pickle.dump(parents.p[0], f)
########### # IMPORTS # ########### 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()
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 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
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)
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()
################ # ENVIRONMENTS # ################ envs_1 = ENVIRONMENTS_1() envs_2 = ENVIRONMENTS_2() envs_3 = ENVIRONMENTS_3() ########### # PARENTS # ########### #---NEW POPULATION---# if (c.newMode): # initialize parents parents_1 = POPULATION(c.popSize) parents_1.Initialize() #--------------------# parents_2 = POPULATION(c.popSize) parents_2.Initialize() #--------------------# parents_3 = POPULATION(c.popSize) parents_3.Initialize() #---LOAD POPULATION---# if (c.loadMode): # load in parents parents_1 = pickle.load(file_1) parents_2 = pickle.load(file_2) parents_3 = pickle.load(file_3) # close files
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 pop = POPULATION(5) pop.Evaluate() #parent = INDIVIDUAL() #parent.Evaluate(True) #print(parent.fitness) #for i in range(0,100): # child = copy.deepcopy(parent) # child.Mutate() # child.Evaluate(True) # print('[g: ' , i + 1 , ' ] [pw: ' , parent.genome , ' ] [p: ' , parent.fitness, ' ] [c: ' , child.fitness , ' ]') # if(child.fitness > parent.fitness): # child.Evaluate(False) # parent = child # f = open('robot.p','wb') # pickle.dump(parent, f) # f.close()
######################################################################################################################## # Script ######################################################################################################################## ###### Variables ###### pickledPop = "robot.p" loadPrecursor = False #cSurv = int(c.popSize * c.perElitist) cSurv = int(c.perElitist * c.popSize) ###### required variables ###### 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):
import random from individual import INDIVIDUAL import copy import pickle from population import POPULATION from environments import ENVIORNMENTS import constants as c import datetime import time from recorder import Recorder import os import pprint envs = ENVIORNMENTS() parents = POPULATION(c.popSize) parents.Initialize() parents.Evaluate(envs, pb=True, pp=False) parents.Print() startTimes = [] copyTimes = [] evalTimes = [] generations = [] fillTimes = [] entire_fill = [] recorder = Recorder() children = None for g in range(1, c.numGens): start = time.time()
from recorder import Recorder import os import constants as c from archive import Archive envs = PLATFORMS() # f = open('saved_files/bestJumpEver2019-04-27.p', 'rb') # f = open('saved_files/4hidden_500gen_apfo_thruwall2019-05-02.p', 'rb') # f = open('saved_files/2019-05-02.p', 'rb') # f = open('saved_files/samplesave2019-05-01.p', 'rb') # f = open('saved_files/4hidden_6legs_apfo_500gen_ind.p', 'rb') f = open('saved_files/1env_500gens_genetic_6legs_4hidden_trial32019-05-05.p', 'rb') best_genome = pickle.load(f) f.close() best_pop = POPULATION(1) if isinstance(best_genome, Archive): best_pop.Initialize(best_genome.genome, best_genome.cppn) elif isinstance(best_genome, list): best_pop.Initialize(best_genome) else: print("Could not determine class") exit() # best_pop.Initialize(best_genome) best_pop.Evaluate_Best(envs, pb=False, pp=True, retain_data=True) recorder = Recorder() recorder.plot_touch_values(best_pop.pop[0])
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()
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 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)
Load = False popSize = 1 ## pretrain-total-popsize-eval_time saveFile = str(c.gens) + '-' + str(c.totGens) + '-' + str(popSize) + '-' + str( c.eval_time) #saveFile = '5-10-50-1600' folder = saveFile 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):
from copy import deepcopy from population import POPULATION parents = POPULATION(10) print('running...') for g in range(1,201): # print(g, end=' ') children = deepcopy(parents) children.mutate() children.evaluate(play_blind=True) parents.replaceWith(children) # parents.print() print('.', end='', flush=True) print() parents.evaluate(best=True)
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()
from population import POPULATION #from robot import ROBOT #from individual import INDIVIDUAL #import pickle import copy #import random #import matplotlib.pyplot as plt #import sys #sys.path.insert(0, '../..') #import pyrosim parents = POPULATION(10) parents.Initialize() parents.Evaluate(True) parents.Print() for g in range(1, 200): children = POPULATION(10) children.Fill_From(parents) children.Evaluate(True) # children.Print() # children = copy.deepcopy(parents) # children.Mutate() # children.Evaluate(True) parents.ReplaceWith(children) print(g), children.Print() parents.p[0].Evaluate(False) #parent = INDIVIDUAL()
# # parent.Evaluate(hideSim=True) # 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
from population import POPULATION import constants as c from environments import ENVIRONMENTS import pickle import csv import os import glob finalData = [] for n in range(0, c.sampleSize): envs = ENVIRONMENTS() envs.Initialize() parents = POPULATION(c.popSize) parents.Initialize() parents.Evaluate(envs, pp=False, pb=True, dt=0.05) parents.Print() data = [] # for loop that creates n iterations for g in range(1, c.numGens): # create the child population children = POPULATION(c.popSize) # fill the children population from the parents keeping best in first place and # filling the rest with mutations children.Fill_From(parents) # evaluate the children children.Evaluate(envs, pp=False, pb=True)
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):
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) print(g + 1, parents) parents.playbest()
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)
# Header creation for columns - stores gen num, parent solution ID, and solution number in pop header = ['Generation'] for i in range(c.popSize): 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)