Example #1
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
Example #3
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"))
Example #4
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()
Example #5
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()
Example #6
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()
Example #7
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)
Example #8
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