Exemple #1
0
    def __init__(self, idividualProperty, individualGeneticMaterial):
        self.property = idividualProperty
        self.fitness = 0
        self.robotConfig = LoadRobotConfiguration()
        self.configuration = LoadSystemConfiguration()
        self.genomeMatrix = individualGeneticMaterial.getGeneticMatrix()
        self.poseSize = len(self.genomeMatrix)
        self.genomeMatrixJointNameIDMapping = {}
        self.sysConf = LoadSystemConfiguration()

        i = 0
        for jointName in self.robotConfig.getJointsName():
            self.genomeMatrixJointNameIDMapping[jointName] = i
            i = i + 1

        dontSupportedJoints = self.configuration.getVrepNotImplementedBioloidJoints()
        self.robotImplementedJoints = []
        robotJoints = self.robotConfig.getJointsName()
        for joint in robotJoints:
            if joint not in dontSupportedJoints:
                self.robotImplementedJoints.append(joint)

        # is important to use only supported joints to avoid errors obtaining the handler of a joint that doesn't exists
        for i in xrange(self.poseSize):
            for joint in self.robotImplementedJoints:
                # print "i: ", i, "j: ", joint
                value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] + self.property.getPoseFix(
                    joint
                )  # TODO this can be a problem for the physical robot
                self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] = value
Exemple #2
0
def eval_func(chromosome):
    conf = LoadSystemConfiguration()
    if not initialPopulationSetted:
        setInitialPopulation(gaEngine)

    prop = DTIndividualPropertyVanillaEvolutive()
    if int(conf.getProperty("Concatenate walk cycles?")):
        embryo = DTIndividualGeneticMatrixWalk(chromosomeToLucyGeneticMatrix(chromosome))
    else:
        embryo = DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(chromosome))
    precycleFile = os.getcwd()+"/mocap/cmu_mocap/xml/util/walk_precycle.xml"
    preCycleEmbryo = DTIndividualGeneticTimeSerieFile(precycleFile)
    preCycleEmbryo.concatenate(embryo)
    newEmbryo = preCycleEmbryo
    #embryoLength = newEmbryo.getLength()
    individual = Individual(prop, newEmbryo)
    #individual.setLength(embryoLength)
    fitness = individual.execute() #return the fitness resulting from the simulator execution

    if int(conf.getProperty("re-evaluate fittest?"))==True:
        if fitness > max_score: #is really a better fitness ?
            candidateFitness = fitness
            fitness = individual.execute()
            print "candidateFitness: ", candidateFitness, "fitness: ", fitness
            while abs(candidateFitness-fitness) > 0.01:
                candidateFitness=fitness
                fitness = individual.execute()
                print "candidateFitness: ", candidateFitness, "fitness: ", fitness
            #the candidateFitness was validated!
            fitness = candidateFitness
    return fitness
 def __init__(self):
     conf = LoadSystemConfiguration()
     self.geneticMatrix = []
     self.cyclesQty = int(conf.getProperty("Concatenate walk cycles?"))
     self.robotConfig = LoadRobotConfiguration()
     self.jointNameIDMapping = {}
     jointIDCounter = 0
     for j in self.robotConfig.getJointsName():
         self.jointNameIDMapping[jointIDCounter] = j
         jointIDCounter += 1
Exemple #4
0
 def __init__(self, file):
     self.parser = BvhImport(file)
     ##dtCycleProp = DTWalkPreCyclePropertyLR(file)
     ##dtCycleProp = DTWalkCycleStartingLeftFootProperty(file)
     #dtCycleProp = DTWalkPreCyclePropertyRL(file)
     dtCycleProp = DTWalkCycleStartingRightFootProperty(file)
     self.start = dtCycleProp.getIndividualStart()
     self.end = dtCycleProp.getIndividualEnd()
     self.direction = dtCycleProp.getMoveDirection()
     conf = LoadSystemConfiguration()
     self.skipping = int(conf.getProperty("number of frames to skip"))
Exemple #5
0
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()
Exemple #6
0
    def getPose(self, frameNumber):
        sysconf = LoadSystemConfiguration()
        robotConf = LoadRobotConfiguration()
        dontSupportedJoints = sysconf.getVrepNotImplementedBioloidJoints()
        robotImplementedJoints = []
        robotJoints = robotConf.getJointsName()

        for joint in robotJoints:
            if joint not in dontSupportedJoints:
                robotImplementedJoints.append(joint)

        frame = self.framelist[frameNumber]
        for jointName in robotImplementedJoints:
            joint = frame.getElementsByTagName(jointName)[0]
            angle = joint.getAttribute("angle")
            self.jointAngleMapping[jointName] = float(angle)
        return Pose(self.jointAngleMapping)
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
def storeExperimentGAparameters():
    conf = LoadSystemConfiguration()
    file = open(os.path.join(experimentDir,"info.txt"),"w")
    
    file.write("initialPopulationSize = " + conf.getProperty("Population size") + "\n")
    file.write("generations = " + conf.getProperty("Number of generations") + "\n")
    file.write("genome.crossover = " + conf.getProperty("Crossover operator") + "\n")
    file.write("genome.mutator = " + conf.getProperty("Mutator operator") + "\n")
    
    file.write("MutationRate = " + conf.getProperty("MutationRate") + "\n")
    
    file.write("selector = " + conf.getProperty("Selection operator") + "\n")
    file.write("CrossoverRate = " + conf.getProperty("CrossoverRate") + "\n")
    file.write("ElitismReplacement percentage = " + conf.getProperty("Elitism replacement percentage") + "\n")
    
    file.close()
Exemple #10
0
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
Exemple #11
0
    def __init__(self, idividualProperty, individualGeneticMaterial, modelReposeValue=DTModelVrepReda()):
        self.property = idividualProperty
        self.modelReposeValue = modelReposeValue
        self.fitness = 0
        self.robotConfig = LoadRobotConfiguration()
        self.configuration = LoadSystemConfiguration()
        self.genomeMatrix = individualGeneticMaterial.getGeneticMatrix()
        self.length = individualGeneticMaterial.getLength()
        self.genomeMatrixJointNameIDMapping = {}
        self.sysConf = LoadSystemConfiguration()
        self.individualGeneticMaterial = individualGeneticMaterial
        self.moveArmFirstTime = True
        self.precycleLength = 0
        self.cycleLength = 0 #Not used yet

        i=0
        for jointName in self.robotConfig.getJointsName():
            self.genomeMatrixJointNameIDMapping[jointName]=i
            i=i+1

        dontSupportedJoints = self.configuration.getVrepNotImplementedBioloidJoints()
        self.robotImplementedJoints = []
        robotJoints = self.robotConfig.getJointsName()
        for joint in robotJoints:
            if joint not in dontSupportedJoints:
                self.robotImplementedJoints.append(joint)

        #is important to use only supported joints to avoid errors obtaining the handler of a joint that doesn't exists
        for i in xrange(self.length):
            for joint in self.robotImplementedJoints:
                #print "i: ", i, "j: ", joint
                if self.property.avoidJoint(joint):
                    value = modelReposeValue.getReposeValue(joint)
                else:
                    value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] + self.property.getPoseFix(joint) #TODO this can be a problem for the physical robot

                self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]]=value
Exemple #12
0
# 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

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
Exemple #13
0
def storeExperimentGAparameters():
    conf = LoadSystemConfiguration()
    file = open(os.path.join(experimentDir,"info.txt"),"w")
    
    file.write("initialPopulationSize = " + conf.getProperty("Population size") + "\n")
    file.write("generations = " + conf.getProperty("Number of generations") + "\n")
    file.write("genome.crossover = " + conf.getProperty("Crossover operator") + "\n")
    file.write("genome.mutator = " + conf.getProperty("Mutator operator") + "\n")
    
    file.write("MutationRate = " + conf.getProperty("MutationRate") + "\n")
    
    file.write("selector = " + conf.getProperty("Selection operator") + "\n")
    file.write("CrossoverRate = " + conf.getProperty("CrossoverRate") + "\n")
    file.write("ElitismReplacement percentage = " + conf.getProperty("Elitism replacement percentage") + "\n")
    file.write("Concatenate walk cycles = " + conf.getProperty("Concatenate walk cycles?") + "\n")
    file.write("concatenate external cycle file = " + conf.getProperty("concatenate external cycle file?") + "\n")
    file.write("Convergence criteria enable? = " + conf.getProperty("Convergence criteria enable?") + "\n")
    file.write("Vrep robot model scene = " + conf.getFile("Lucy vrep model") + "\n")

    file.close()
Exemple #14
0
class Individual:

    def __init__(self, idividualProperty, individualGeneticMaterial, modelReposeValue=DTModelVrepReda()):
        self.property = idividualProperty
        self.modelReposeValue = modelReposeValue
        self.fitness = 0
        self.robotConfig = LoadRobotConfiguration()
        self.configuration = LoadSystemConfiguration()
        self.genomeMatrix = individualGeneticMaterial.getGeneticMatrix()
        self.length = individualGeneticMaterial.getLength()
        self.genomeMatrixJointNameIDMapping = {}
        self.sysConf = LoadSystemConfiguration()
        self.individualGeneticMaterial = individualGeneticMaterial
        self.moveArmFirstTime = True
        self.precycleLength = 0
        self.cycleLength = 0 #Not used yet

        i=0
        for jointName in self.robotConfig.getJointsName():
            self.genomeMatrixJointNameIDMapping[jointName]=i
            i=i+1

        dontSupportedJoints = self.configuration.getVrepNotImplementedBioloidJoints()
        self.robotImplementedJoints = []
        robotJoints = self.robotConfig.getJointsName()
        for joint in robotJoints:
            if joint not in dontSupportedJoints:
                self.robotImplementedJoints.append(joint)

        #is important to use only supported joints to avoid errors obtaining the handler of a joint that doesn't exists
        for i in xrange(self.length):
            for joint in self.robotImplementedJoints:
                #print "i: ", i, "j: ", joint
                if self.property.avoidJoint(joint):
                    value = modelReposeValue.getReposeValue(joint)
                else:
                    value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] + self.property.getPoseFix(joint) #TODO this can be a problem for the physical robot

                self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]]=value

    def stopLucy(self):
        self.lucy.stopLucy()

    def execute(self):
        #create the corresponding instance of Lucy, depending if it's real or simulated.
        if int(self.sysConf.getProperty("Lucy simulated?"))==1:
            self.lucy = SimulatedLucy(int(self.configuration.getProperty("Lucy render enable")))
        else:
            self.lucy = PhysicalLucy()
   
        poseExecute={}

        i=0
        while (self.lucy.isLucyUp() and i < self.length):
            for joint in self.robotConfig.getJointsName():
                if not(self.property.avoidJoint(joint)):
                    value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]]
                    poseExecute[joint] = value
            i = i + self.lucy.getPosesExecutedByStepQty()
            self.lucy.executePose(Pose(poseExecute))

            '''if self.precycleLength > 0 and i == self.precycleLength - 1:
                self.lucy.moveHelperArm()

            if self.precycleLength > 0 and i > self.precycleLength - 1:
                if (i - self.precycleLength) > 0 and (i - self.precycleLength) % self.cycleLength == 0:
                    self.lucy.moveHelperArm()'''

        startingCyclePose = self.getPrecycleLength()
        executionConcatenationGap = self.individualGeneticMaterial.getConcatenationGap(startingCyclePose)
        self.fitness = self.lucy.getFitness(self.length, executionConcatenationGap)
        self.lucy.stopLucy()  #this function also updates time and distance
        return self.fitness

    def getGenomeMatrix(self):
        return self.genomeMatrix

    def setGenomeMatrix(self, geneMatrix):
        self.genomeMatrix = geneMatrix
        dtGenome = DTGenomeFunctions()
        self.length = dtGenome.individualLength(geneMatrix)

    def persist(self,file):
        root = ET.Element("root")
        lucy = ET.SubElement(root, "Lucy")
        for frameIt in xrange(self.length):
            frame = ET.SubElement(lucy, "frame")
            frame.set("number" , str(frameIt))
            for joint in self.robotImplementedJoints: #TODO because the notImplementedJoints doesn't participate in the simulation and fitness evaluation, they are not stored
                xmlJoint = ET.SubElement(frame, joint)
                joint_id = self.robotConfig.loadJointId(joint)
                pos = self.genomeMatrix[frameIt][self.genomeMatrixJointNameIDMapping[joint]]
                xmlJointAngle = xmlJoint.set("angle" , str(pos))
        tree = ET.ElementTree(root)
        tree.write(file)

    def getJointMatrixIDFromName(self, jointName):
        return self.genomeMatrixJointNameIDMapping[jointName]

    def setLength(self, length):
        self.length = length

    def setPrecycleLength(self, precycleLength):
        self.precycleLength = precycleLength

    def setCycleLength(self, cycleLength):
        self.cycleLength = cycleLength

    def getLength(self):
        return self.length

    def getPrecycleLength(self):
        return self.precycleLength

    def getCycleLength(self):
        return self.cycleLength
Exemple #15
0
import glob
import os
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"
Exemple #16
0
def run_main():
    conf = LoadSystemConfiguration()
    initialPopulationSize = int(conf.getProperty("Population size"))
    generations = int(conf.getProperty("Number of generations"))
    # Genome instance
    framesQty = sysConstants.GENOMA_MAX_LENGTH
    genome = G2DList.G2DList(framesQty, 18)
    genome.setParams(rangemin=0, rangemax=360)
    genome.setParams(gauss_sigma=sysConstants.MUTATION_SIGMA, gauss_mu=0)

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)
    if conf.getProperty("Crossover operator") == "crossovers.G2DListCrossoverSingleNearHPoint":
        genome.crossover.set(crossovers.G2DListCrossoverSingleNearHPoint)
    elif conf.getProperty("Crossover operator") == "Crossovers.G2DListCrossoverSingleHPoint":
        genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint)

    #genome.crossover.set(crossovers.G2DListCrossoverSingleNearHPointImprove)
    #genome.crossover.set(crossovers.G2DListCrossoverSingleHPoint)
    
    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setGenerations(generations)
    ga.setPopulationSize(initialPopulationSize)

    #genome.mutator.set(Mutators.G2DListMutatorIntegerRange)
    if conf.getProperty("Mutator operator") == "mutators.G2DListMutatorRealGaussianSpline":
        genome.mutator.set(mutators.G2DListMutatorRealGaussianSpline)
    elif conf.getProperty("Mutator operator") == "Mutators.G2DListMutatorRealGaussianGradient":
        genome.mutator.set(Mutators.G2DListMutatorRealGaussianGradient)

    
    ga.setMutationRate(float(conf.getProperty("MutationRate")))
    
    if conf.getProperty("Selection operator") == "Selectors.GRankSelector" :
        ga.selector.set(Selectors.GRankSelector)
    elif conf.getProperty("Selection operator") == "Selectors.GTournamentSelector" :
        ga.selector.set(Selectors.GTournamentSelector)
    elif conf.getProperty("Selection operator") == "Selectors.GRouletteWheel" :
        ga.selector.set(Selectors.GRouletteWheel)
    elif conf.getProperty("Selection operator") == "Selectors.GUniformSelector" :
        ga.selector.set(Selectors.GUniformSelector)

    '''For crossover probability, maybe it is the ratio of next generation population born by crossover operation. 
    While the rest of population...maybe by previous selection or you can define it as best fit survivors'''
    ga.setCrossoverRate(float(conf.getProperty("CrossoverRate")))

    elitism = float(conf.getProperty("Elitism replacement percentage")) > 0
    ga.setElitism(True)
    '''Set the number of best individuals to copy to the next generation on the elitism'''
    if elitism:
        numberIndividualsForNextGen = int(initialPopulationSize*float(conf.getProperty("Elitism replacement percentage")))
        ga.setElitismReplacement(numberIndividualsForNextGen)

    if int(conf.getProperty("Convergence criteria enable?"))==True:
        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 every generation
    ga.evolve(freq_stats=0)

    # 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()
    bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best)))

    bestIndividual.persist(os.path.join(experimentDir,filename))

    #store all the final population, not only the fitest
    population = ga.getPopulation()
    popSize = len(population)
    for pos in range(popSize):
        individual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(population[pos])))
        timestr = time.strftime("%Y%m%d-%H%M%S")
        filename = "final-" + str(pos) + "-" + timestr + ".xml"
        individual.persist(os.path.join(experimentDir, filename))
    #ga.getDBAdapter().commit()
    
    shutil.copy2('pyevolve.db', experimentDir)
    shutil.copy2(conf.getProperty("System Log"), experimentDir)
    os.system("pyevolve_graph.py -i \"Lucy walk\" -3 -o gene_pool/experiment_img/" + experimentTime + " -e png")
    
    #do the stats    
    print ga.getStatistics()
#
# 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)
Exemple #18
0
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
Exemple #19
0
class Individual:
    def __init__(self, idividualProperty, individualGeneticMaterial):
        self.property = idividualProperty
        self.fitness = 0
        self.robotConfig = LoadRobotConfiguration()
        self.configuration = LoadSystemConfiguration()
        self.genomeMatrix = individualGeneticMaterial.getGeneticMatrix()
        self.poseSize = len(self.genomeMatrix)
        self.genomeMatrixJointNameIDMapping = {}
        self.sysConf = LoadSystemConfiguration()

        i = 0
        for jointName in self.robotConfig.getJointsName():
            self.genomeMatrixJointNameIDMapping[jointName] = i
            i = i + 1

        dontSupportedJoints = self.configuration.getVrepNotImplementedBioloidJoints()
        self.robotImplementedJoints = []
        robotJoints = self.robotConfig.getJointsName()
        for joint in robotJoints:
            if joint not in dontSupportedJoints:
                self.robotImplementedJoints.append(joint)

        # is important to use only supported joints to avoid errors obtaining the handler of a joint that doesn't exists
        for i in xrange(self.poseSize):
            for joint in self.robotImplementedJoints:
                # print "i: ", i, "j: ", joint
                value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] + self.property.getPoseFix(
                    joint
                )  # TODO this can be a problem for the physical robot
                self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]] = value

    def stopLucy(self):
        self.lucy.stopLucy()

    def execute(self):
        # create the corresponding instance of Lucy, depending if it's real or simulated.
        if int(self.sysConf.getProperty("Lucy simulated?")) == 1:
            self.lucy = SimulatedLucy(int(self.configuration.getProperty("Lucy render enable")))
        else:
            self.lucy = PhysicalLucy()

        poseExecute = {}
        i = 0
        while self.lucy.isLucyUp() and i < self.poseSize:
            for joint in self.robotConfig.getJointsName():
                if not (self.property.avoidJoint(joint)):
                    value = self.genomeMatrix[i][self.genomeMatrixJointNameIDMapping[joint]]
                    poseExecute[joint] = value
            i = i + 1
            self.lucy.executePose(Pose(poseExecute))
        self.lucy.stopLucy()  # this function also updates time and distance

        if i < self.poseSize:
            self.fitness = self.lucy.getFitness()
        else:
            endFrameExecuted = True
            self.fitness = self.lucy.getFitness(endFrameExecuted)

        print "fitness: ", self.fitness
        return self.fitness

    def getPoseQty(self):
        return self.lp.getFrameQty()

    def getPose(self, poseNumber):
        return self.lp.getFramePose(poseNumber)

    def getMostSimilarPose(self, pose):
        diff = MAX_INT
        moreSimilarPose = self.getPose(1)
        for i in xrange(self.getPoseQty()):
            myPose = getPose(i)
            newDiff = pose.diff(myPose)
            if newDiff < diff:
                diff = newDiff
                moreSimilarPose = myPose
        return moreSimilarPose

    def getGenomeMatrix(self):
        return self.genomeMatrix

    def setGenomeMatrix(self, geneMatrix):
        self.genomeMatrix = geneMatrix
        self.poseSize = len(geneMatrix)

    def persist(self, file):
        root = ET.Element("root")
        lucy = ET.SubElement(root, "Lucy")
        for frameIt in xrange(self.poseSize):
            frame = ET.SubElement(lucy, "frame")
            frame.set("number", str(frameIt))
            for joint in self.robotImplementedJoints:
                xmlJoint = ET.SubElement(frame, joint)
                joint_id = self.robotConfig.loadJointId(joint)
                pos = self.genomeMatrix[frameIt][self.genomeMatrixJointNameIDMapping[joint]]
                xmlJointAngle = xmlJoint.set("angle", str(pos))
        tree = ET.ElementTree(root)
        tree.write(file)
Exemple #20
0
from configuration.LoadSystemConfiguration      import LoadSystemConfiguration
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)
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