예제 #1
0
    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
예제 #2
0
파일: Individual.py 프로젝트: aguirrea/lucy
    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
예제 #3
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)
예제 #4
0
파일: Individual.py 프로젝트: aguirrea/lucy
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