Exemplo n.º 1
0
    def test_blind(self):
        cf = ClusterFactory()
        for counter in xrange(1000):
            a = random.randint(0, 100)
            b = random.randint(0, 100)
            if (a + b) % 2 == 0:
                cf.add_members([a, b])

        for cluster in cf.get_clusters():
            #print cluster.members
            tmp = numpy.array(cluster.members) % 2
            #print tmp
            self.assert_((tmp == 0).all() or (tmp == 1).all())
Exemplo n.º 2
0
    def connect_state_variables(self):
        # before we connect the state variables, we must assign indices to
        # the variables. This is regulated by the constraints that apply.

        # first cluster the variables into groups. They are grouped by the constraints.
        # for example: constraint A(1, 2) and B(2, 3) will make variables 1, 2, 3 related
        # trhough the constraintderivatives matrix. All nonrelated variables will be put together
        # at the end of the state vector.
        cf = ClusterFactory(RuleCluster)
        for constraint in self.constraints:
            cf.add_related(RuleCluster(constraint.input_variables, [constraint]))
        self.constraint_clusters = cf.get_clusters()
        del cf

        # assign state indices to the variables
        state_index = 0
        excess_index = 0
        self.unconstrained_variables = set(self.state_variables)
        for cluster in self.constraint_clusters:
            cluster.state_index = state_index
            for variable in cluster.items:
                self.unconstrained_variables.remove(variable)
                variable.state_index = state_index
                state_index += variable.dimension
            cluster.input_dimension = sum([variable.dimension for variable in cluster.items])
            cluster.output_dimension = sum([constraint.output_dimension for constraint in cluster.rules])
            cluster.inputs = self.state[cluster.state_index: cluster.state_index + cluster.input_dimension]
            cluster.state_derivatives = self.derivatives[cluster.state_index: cluster.state_index + cluster.input_dimension]
            cluster.outputs = numpy.zeros(cluster.output_dimension, float)
            cluster.constraint_derivatives = numpy.zeros((cluster.output_dimension, cluster.input_dimension), float)
            output_index = 0
            for constraint in cluster.rules:
                constraint.sanity_check()
                constraint.connect_outputs(output_index, cluster.outputs)
                constraint.connect_derivatives([
                    cluster.constraint_derivatives[
                        output_index: output_index + constraint.output_dimension,
                        variable.state_index - cluster.state_index: variable.state_index - cluster.state_index + variable.dimension
                    ] for variable in constraint.input_variables
                ])
                output_index += constraint.output_dimension

        for variable in self.unconstrained_variables:
            variable.state_index = state_index
            state_index += variable.dimension

        for variable in self.state_variables:
            variable.connect(self.state, self.derivatives, self.mass)