def run_main(): initialPopulationSize = 51 generations = 1500 conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information # Genome instance genome = G2DList.G2DList(400, 18) genome.setParams(rangemin=0, rangemax=360) # The evaluator function (objective function) genome.evaluator.set(eval_func) genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint) #genome.mutator.set(Mutators.G2DListMutatorIntegerRange) genome.mutator.set(Mutators.G2DListMutatorIntegerGaussian) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(generations) #TODO class atribute ga.setPopulationSize(initialPopulationSize) #TODO class atribute ga.setMutationRate(0.2) ga.selector.set(Selectors.GRankSelector) #ga.selector.set(Selectors.GTournamentSelector) #ga.selector.set(Selectors.GRouletteWheel) ga.setElitism(True) ga.setElitismReplacement(initialPopulationSize/3) #ga.terminationCriteria.set(ConvergenceCriteria) # Create DB Adapter and set as adapter sqlite_adapter = DBAdapters.DBSQLite(identify="Lucy walk", resetDB=True) ga.setDBAdapter(sqlite_adapter) #callback to persist best individual of each generation ga.stepCallback.set(generationCallback) #keep a reference to the genetic algorithm engine global gaEngine gaEngine = ga # Do the evolution, with stats dump # frequency of 2 generations ga.evolve(freq_stats=1) # Best individual best = ga.bestIndividual() score = best.getRawScore() timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(generations) + ".xml" prop = DTIndividualPropertyVanilla() #TODO create a vanilla property as default argument in Individual constructor bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") bestIndividual.persist(geneticPoolDir + filename) #TODO store all the population, not only the fitest print ga.getStatistics()
def setInitialPopulation (ga_engine): prop = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() conf = LoadSystemConfiguration() CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") GAwalkDir = os.getcwd()+conf.getDirectory("GAWalk Files") UIBLHDir = os.getcwd()+conf.getDirectory("UIBLH mocap Files") BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") ADHOCDir = os.getcwd()+conf.getDirectory("ADHOC Files") geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") population = ga_engine.getPopulation() popSize = len(population) individualCounter = 0 # if individualCounter < popSize: # for filename in glob.glob(os.path.join(GAwalkDir, '*.xml')): # print individualCounter, " individuals processed!" # print 'inserting individual: ' + filename + " into the initial population" # walk = Individual(propVanilla, DTIndividualGeneticTimeSerieFile(filename)) # geneticMatrix = walk.getGenomeMatrix() # if individualCounter < popSize: # adan = population[individualCounter] # for i in xrange(adan.getHeight()): # if i == len(geneticMatrix): # break # for j in xrange(adan.getWidth()): # adan.setItem(i,j,geneticMatrix[i][j]) # population[individualCounter]=adan # individualCounter = individualCounter + 1 # else: # break if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(prop, DTIndividualGeneticTimeSerieFile(filename)) geneticMatrix = walk.getGenomeMatrix() if individualCounter < popSize: adan = population[individualCounter] for i in xrange(adan.getHeight()): if i == len(geneticMatrix): break for j in xrange(adan.getWidth()): adan.setItem(i,j,geneticMatrix[i][j]) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break global initialPopulationSetted initialPopulationSetted = True
def generationCallback(ga_engine): # persist best individual at the moment conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") gen = ga_engine.getCurrentGeneration() best = ga_engine.bestIndividual() score = best.getRawScore() timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(gen) + ".xml" prop = DTIndividualPropertyVanilla() #TODO create a vanilla property as default argument in Individual constructor bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) bestIndividual.persist(geneticPoolDir + filename) ga_engine.getDBAdapter().commit() ##population = ga_engine.getPopulation() ##averagePopulation = getPopulationAverage(population) #averageGeneration = getPopulationAverage(gen) ##print "current population raw score average: ", averagePopulation #print "current generation raw score average: ", averageGeneration return False
def generationCallback(ga_engine): # persist best individual at the moment conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") gen = ga_engine.getCurrentGeneration() best = ga_engine.bestIndividual() score = best.getRawScore() #check if the convergence criteria has been reached global max_score, max_score_generation, convergenceCriteria if score > max_score: max_score = score max_score_generation = gen else: #if the score doesn't improve in NUMBER_GENERATIONS_CONVERGENCE_CRITERIA generations then there is no reason to continue and we have reach a convergence if gen - max_score_generation > NUMBER_GENERATIONS_CONVERGENCE_CRITERIA: convergenceCriteria = True timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(gen) + ".xml" #at generation 0 is the firs time to create de directory and store the GA parameters if gen == 0: global experimentDir experimentDir = geneticPoolDir + timestr global experimentTime experimentTime = timestr os.mkdir(experimentDir) storeExperimentGAparameters() prop = DTIndividualPropertyVanilla() bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) bestIndividual.persist(os.path.join(experimentDir, filename)) ga_engine.getDBAdapter().commit() #population = ga_engine.getPopulation() #popSize = len(population) print "generation executed!, best fit of generation: ", score, "fittest: ", max_score, "reached in generation: ", max_score_generation return False
from datatypes.DTIndividualGeneticMaterial import DTIndividualGeneticTimeSerieFile, \ DTIndividualGeneticTimeSerieFileWalk from datatypes.DTIndividualProperty import DTIndividualPropertyCMUDaz, DTIndividualPropertyVanilla, DTIndividualPropertyBaliero, DTIndividualPropertyVanillaEvolutive, DTIndividualPropertyPhysicalBioloid, DTIndividualPropertyVanillaEvolutiveNoAvoid from Individual import Individual propCMUDaz = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() physicalProp = DTIndividualPropertyPhysicalBioloid() geneticVanillaProp = DTIndividualPropertyVanillaEvolutive() geneticVanillaPropNothingToAvoid = DTIndividualPropertyVanillaEvolutiveNoAvoid() conf = LoadSystemConfiguration() CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") GAwalkDir = os.getcwd()+conf.getDirectory("GAWalk Files") UIBLHDir = os.getcwd()+conf.getDirectory("UIBLH mocap Files") BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") ADHOCDir = os.getcwd()+conf.getDirectory("ADHOC Files") geneticPoolDir = os.pardir+conf.getDirectory("Genetic Pool") arguments = len(sys.argv) def createIndividual(filename): if int(conf.getProperty("Lucy simulated?"))==1: if int(conf.getProperty("Concatenate walk cycles?")): walkEmbryo = DTIndividualGeneticTimeSerieFileWalk(os.getcwd()+"/"+filename) else: walkEmbryo = DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename)
def setInitialPopulation(ga_engine): propCMUDaz = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() conf = LoadSystemConfiguration() lucyCycles = os.getcwd()+conf.getDirectory("Lucy evolved walk cycles Files") CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") GAwalkDir = os.getcwd()+conf.getDirectory("GAWalk Files") UIBLHDir = os.getcwd()+conf.getDirectory("UIBLH mocap Files") BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") ADHOCDir = os.getcwd()+conf.getDirectory("ADHOC Files") geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") population = ga_engine.getPopulation() popSize = len(population) individualCounter = 0 walk = Individual(propVanilla, DTIndividualGeneticMatrix()) #dummy individual to initialise the simulator and enable the time step configuration walk.execute() print "please set the proper time step in vrep" dtgenoma = DTGenomeFunctions() #the random initia population created is replaced by the imitation motion capture database if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): if individualCounter < popSize: print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(propCMUDaz, DTIndividualGeneticTimeSerieFile(filename)) teacherGeneticMatrix = walk.getGenomeMatrix() adan = population[individualCounter] adanIndividualLength=dtgenoma.getIndividualLength(adan) adanJointLength=dtgenoma.getIndividualFrameLength(adan) for i in xrange(adanIndividualLength): for j in xrange(adanJointLength): if i < len(teacherGeneticMatrix): #if the fixed gnoma representation size is less than the teacher size adan.setItem(i,j,teacherGeneticMatrix[i][j]) else: #put a sentinel joint value to mark the end of the individual adan.setItem(i,j,sysConstants.JOINT_SENTINEL) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break #uncomment this block and comment the one above to use individuals with vainilla format '''if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): if individualCounter < popSize: print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(propVanilla, DTIndividualGeneticTimeSerieFile(filename)) teacherGeneticMatrix = walk.getGenomeMatrix() adan = population[individualCounter] adanIndividualLength=dtgenoma.getIndividualLength(adan) adanJointLength=dtgenoma.getIndividualFrameLength(adan) for i in xrange(adanIndividualLength): for j in xrange(adanJointLength): if i < len(teacherGeneticMatrix): #if the fixed gnoma representation size is less than the teacher size adan.setItem(i,j,teacherGeneticMatrix[i][j]) else: #put a sentinel joint value to mark the end of the individual adan.setItem(i,j,sysConstants.JOINT_SENTINEL) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break ''' global initialPopulationSetted initialPopulationSetted = True
# along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import os import glob import ntpath from parser.BvhImport import BvhImport import matplotlib.pyplot as plt from configuration.LoadSystemConfiguration import LoadSystemConfiguration import numpy as np from scipy.signal import argrelextrema from collections import Counter sysConf = LoadSystemConfiguration() BVHDir = os.getcwd() + sysConf.getDirectory("CMU mocap Files") Y_THREADHOLD = 11 #TODO calculate this as the average of the steps_highs X_THREADHOLD = 36 def firstMax(values1, values2): res=0 for i in range(len(values1)-2): if values1[i] < values1[i+1] and values1[i+1] > values1[i+2]: #i+1 is a local maximun if (values1[i] - values2[i]) > THREADHOLD: res=i+1 elif values1[i] < values1[i+1] < values1[i+2]: #i is a local maximun if (values1[i] - values2[i]) > THREADHOLD: res=i return res
# You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from parser.JointCalculation import JointCalculation from parser.MocapLucyMapping import MocapLucyMapping from simulator.LoadRobotConfiguration import LoadRobotConfiguration from configuration.LoadSystemConfiguration import LoadSystemConfiguration from string import rstrip import os import glob import ntpath sysConf = LoadSystemConfiguration() BVHDir = os.getcwd() + sysConf.getDirectory("CMU mocap Files") XMLDir = os.getcwd() + sysConf.getDirectory("Transformed CMU mocap Files") robotConfiguration = LoadRobotConfiguration() for filename in glob.glob(os.path.join(BVHDir, '*.bvh')): print "transforming: " + filename + " ..." lucyFileConversion = MocapLucyMapping(filename, robotConfiguration) newFile = ntpath.basename(filename) transformedFileName = newFile newFile = rstrip(newFile[:-4]) + ".xml" newFile = XMLDir + newFile lucyFileConversion.generateFile(newFile) print "file: " + newFile + " generated!" os.rename(filename, os.path.join(BVHDir)+"/transformed/"+transformedFileName)
import sys import time from configuration.LoadSystemConfiguration import LoadSystemConfiguration from datatypes.DTIndividualGeneticMaterial import DTIndividualGeneticTimeSerieFile, DTIndividualGeneticMatrix from datatypes.DTIndividualProperty import DTIndividualPropertyBaliero, DTIndividualPropertyPhysicalBioloid from Individual import Individual balieroProp = DTIndividualPropertyBaliero() physicalProp = DTIndividualPropertyPhysicalBioloid() conf = LoadSystemConfiguration() BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") arguments = len(sys.argv) def createIndividual(filename): if int(conf.getProperty("Lucy simulated?"))==1: walk = Individual(balieroProp, DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename)) else: walk = Individual(physicalProp, DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename)) return walk walk = Individual(balieroProp, DTIndividualGeneticMatrix()) #dummy individual to initialise the simulator and enable the time step configuration walk.execute() print "please set the proper time step in vrep" time.sleep(5) if arguments > 1:
from parser.LoadPoses import LoadPoses from datatypes.DTIndividualProperty import DTIndividualProperty, DTIndividualPropertyCMUDaz, DTIndividualPropertyVanilla, DTIndividualPropertyBaliero, DTIndividualPropertyVanillaEvolutive, DTIndividualPropertyPhysicalBioloid from datatypes.DTIndividualGeneticMaterial import DTIndividualGeneticMaterial, DTIndividualGeneticTimeSerieFile, DTIndividualGeneticMatrix from Pose import Pose from configuration.LoadSystemConfiguration import LoadSystemConfiguration from simulator.LoadRobotConfiguration import LoadRobotConfiguration from Individual import Individual import os import glob import time import sys propCMUDaz = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() physicalProp = DTIndividualPropertyPhysicalBioloid() geneticVanillaProp = DTIndividualPropertyVanillaEvolutive() conf = LoadSystemConfiguration() CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): walk = Individual(propCMUDaz, DTIndividualGeneticTimeSerieFile(filename)) fitness = walk.execute() length = walk.getLength() walk.persist("/tmp/"+str(fitness)+".xml") print "individual: ", filename, "fitness: ", fitness, "length: ", length