コード例 #1
0
    def unmarshal(self, dic):
        """
        Reads a dictionary into this springbot
        """
        self.nodes = []
        self.springs = []

        for node_d in dic['nodes']:
            newnode = Node(node_d['pos'], node_d['vel'], node_d['acc'])
            newnode.id = node_d['id']
            self.nodes.append(newnode)

        for spring_d in dic['springs']:
            id_a = spring_d['from']
            id_b = spring_d['to']

            for node in self.nodes:
                if node.id == id_a:
                    a = node
                    break
            else:
                raise exceptions.IndexError("node id %d(from) not found\n" % (id_a))

            # Procura id B
            for node in self.nodes:
                if node.id == id_b:
                    b = node
                    break
            else:
                raise exceptions.IndexError("node id %d(to) not found\n" % (id_b))

            self.springs.append(Spring(a, b, spring_d['amplitude'], spring_d['offset'], spring_d['normal']))

        del dic['nodes']
        del dic['springs']
        self._info = dict(dic)

        return self
コード例 #2
0
    def crossover(self, other):
        """
        Crosses this springbot with another one, returning a new one
        """
        global _bloodline_count

        # Create new springbot
        breed = EvolveSpringbot()

        # Breed springbots info
        for key in self:
            breed[key] = self[key]

        breed['fitness'] = 0
        breed['name'] = ' '.join(set(self['name'].split() + other['name'].split()))
        breed['adapted'] = "random"

        # Set bloodline
        try:
            breed['bloodline'] += 'X' + \
                other['bloodline'].split(BLOODLINE_SEP)[0] + '...' + other['bloodline'].split(BLOODLINE_SEP)[-2] + \
                BLOODLINE_SEP
        except KeyError:
            pass

        # For each spring in self and other
        for springA, springB in zip(self.springs + ([None] * ((len(other.springs)-len(self.springs))/2)),
                                    other.springs + ([None] * ((len(self.springs)-len(other.springs))/2))):

            if springA is None: springA = springB
            elif springB is None: springB = springA

            # Select spring
            spring = choice([springA,springB])

            # Select node A
            nodeA = breed.getNode(spring.a.id)
            if nodeA is None:
                nodeA = choice([self,other]).getNode(spring.a.id)
                if nodeA is None: nodeA = spring.a

                # Create node A
                new_nodeA = Node(nodeA.pos, nodeA.vel, nodeA.acc)
                new_nodeA.id = nodeA.id
                nodeA = new_nodeA

                # Add node A
                breed.add(nodeA)

            # Select node B
            nodeB = breed.getNode(spring.b.id)
            if nodeB is None:
                nodeB = choice([self,other]).getNode(spring.b.id)
                if nodeB is None: nodeB = spring.b

                # Create node B
                new_nodeB = Node(nodeB.pos, nodeB.vel, nodeB.acc)
                new_nodeB.id = nodeB.id
                nodeB = new_nodeB

                # Add node B
                breed.add(nodeB)

            # Add created spring
            breed.add(Spring(nodeA, nodeB, spring.amplitude,
                             spring.offset, spring.normal))

        # Remove unconnected elements
        breed.removeUnconnected()

        # Return new
        return breed