Ejemplo n.º 1
0
 def simulate_genome(
         self,
         storage_genome: StorageGenome,
         inputs: Dict[str, float]
 ) -> Dict[str, float]:
     simulation_genome = SimulationGenome(
         self._gene_repository,
         storage_genome
     )
     inputs = {k: v for k, v in inputs.items()} #  TODO: use fractions all the way
     return simulation_genome.calculate_step(inputs)
    def test_basicExampleWithoutCycles(self):
        genes = {
            1: (1, 6),
            2: (2, 6),
            3: (1, 7),
            4: (3, 7),
            5: (6, 4),
            6: (7, 4),
            7: (2, 5),
            8: (7, 5),
        }

        storage_genome = StorageGenome()
        analysis_result = AnalysisResult()

        storage_genome.inputs['input_1'] = 1
        storage_genome.inputs['input_2'] = 2
        storage_genome.inputs['input_3'] = 3
        storage_genome.outputs['output_1'] = 4
        storage_genome.outputs['output_2'] = 5
        storage_genome.genes[1] = (False, float(Fraction(2, 10)))
        storage_genome.genes[2] = (False, float(Fraction(7, 10)))
        storage_genome.genes[3] = (False, float(Fraction(1, 10)))
        storage_genome.genes[4] = (False, float(Fraction(3, 10)))
        storage_genome.genes[5] = (False, float(Fraction(6, 10)))
        storage_genome.genes[6] = (False, float(Fraction(8, 10)))
        storage_genome.genes[7] = (False, float(Fraction(9, 10)))
        storage_genome.genes[8] = (False, float(Fraction(5, 10)))

        analysis_result.topologically_sorted_nodes = [3, 2, 1, 7, 5, 6, 4]
        analysis_result.topologically_sorted_cycle_nodes = []

        storage_genome.analysis_result = analysis_result

        mock_gene_repository = MagicMock()
        mock_gene_repository.get_node_labels_by_gene_id =\
            lambda node_id: genes[node_id]

        gen = SimulationGenome(
            mock_gene_repository,
            storage_genome
        )

        result = gen.calculate_step({
            'input_1': float(Fraction(5, 10)),
            'input_2': float(Fraction(5, 10)),
            'input_3': float(Fraction(5, 10))
        })  # type: Dict[str, float]

        self.assertEqual(float(Fraction(43, 100)), result['output_1'])
        self.assertEqual(float(Fraction(55, 100)), result['output_2'])