Exemplo n.º 1
0
    def choice_for_tournament(self, array):
        self._tournament_calls += 1

        if self._tournament_calls == 1:
            return {
                'chromosome': [
                    chromo.Gene('1', domain.DiscreteDomain(domain.STANDARD_INTERVAL), 0.1),
                ],
                'fitness': 50
            }
        elif self._tournament_calls == 2:
            return {
                'chromosome': [
                    chromo.Gene('1', domain.DiscreteDomain(domain.STANDARD_INTERVAL), 0.2),
                ],
                'fitness': 0
            }
        elif self._tournament_calls == 3:
            return  {
                'chromosome': [
                    chromo.Gene('1', domain.DiscreteDomain(domain.STANDARD_INTERVAL), 0.3),
                ],
                'fitness': 0
            }
        elif self._tournament_calls == 4:
            return  {
                'chromosome': [
                    chromo.Gene('1', domain.DiscreteDomain(domain.STANDARD_INTERVAL), 0.4),
                ],
                'fitness': 50
            }
        else:
            raise RuntimeError("Too many tournament calls for random.choice")
Exemplo n.º 2
0
    def test_random_init(self):
        values = [3, 5, 6, 7]
        g = chromo.Gene('new_name', domain.DiscreteDomain(values))

        self.assertEqual('new_name', g.name)
        self.assertTrue(g.is_valid())
        self.assertTrue(g.value in values)
Exemplo n.º 3
0
    def __init__(self, value_array=None):
        '''
        Initializes the genes of this chromosome with the values in the value_array.
        Values must follow the order in the GENE_NAMES array
        :param value_array:
        :return:
        '''

        #creates the chromosome. Genes with specific domains are fixed later
        self._genes = {}
        for i in range(0, len(self.GENE_NAMES)):

            #assigns none if no value array was passed
            value = None
            if value_array is not None:
                value = value_array[i]

            self._genes[self.GENE_NAMES[i]] = Gene(
                self.GENE_NAMES[i],
                domain.DiscreteDomain(domain.STANDARD_INTERVAL), value)

        #--- BEGIN: fixes information of genes with specific domains
        self._genes['s_build_barracks_denominator'].set_domain(
            domain.DiscreteDomain([1, 2, 3, 4, 5]))
        self._genes['s_train_scv_denominator'].set_domain(
            domain.DiscreteDomain([1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]))
        self._genes['s_train_medic_ratio'].set_domain(
            domain.DiscreteDomain(
                [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]))
        self._genes['m_pack_size'].set_domain(
            domain.DiscreteDomain(range(6, 24, 2)))

        if value_array is None:
            self._genes['s_build_barracks_denominator'].randomize()
            self._genes['s_train_scv_denominator'].randomize()
            self._genes['s_train_medic_ratio'].randomize()
            self._genes['m_pack_size'].randomize()
        #--- END: fixes information of genes with specific domains

        #checks if values are valid
        for gene in self._genes.values():
            if not gene.is_valid():
                raise ValueError("Value %d not valid in %s domain" %
                                 (gene.value, gene.name))
Exemplo n.º 4
0
    def test_standard_interval(self):
        d = domain.DiscreteDomain(domain.STANDARD_INTERVAL)
        self.assertEqual(0, d.min_value())
        self.assertEqual(1, d.max_value())

        self.assertFalse(d.has_value(0.17))
        self.assertTrue(d.has_value(.25))

        for i in range(0, 100):
            self.assertTrue(d.random_value() in domain.STANDARD_INTERVAL)
Exemplo n.º 5
0
    def test_basic_calls(self):
        values = [2, 3, 4, 5]
        d = domain.DiscreteDomain(values)

        self.assertTrue(d.has_value(2))
        self.assertFalse(d.has_value(2.5))
        self.assertTrue(d.has_value(4))

        self.assertEqual(2, d.min_value())
        self.assertEqual(5, d.max_value())

        for i in range(0, 100):
            self.assertTrue(d.random_value() in values)
Exemplo n.º 6
0
 def test_invalid_init(self):
     values = [3, 5, 6, 7]
     g = chromo.Gene('new_name', domain.DiscreteDomain(values), 4)
     self.assertFalse(g.is_valid())