def test_get_random_component(self):
     mutator = mutators.XCFunctionalMutator(self.mutator_x,
                                            self.mutator_css,
                                            self.mutator_cos,
                                            seed=1)
     self.assertEqual([mutator.get_random_component() for _ in range(10)], [
         'f_css', 'f_cos', 'f_x', 'f_x', 'f_x', 'f_x', 'f_x', 'f_css',
         'f_css', 'f_css'
     ])
 def test_initialization_with_unnormalized_probability(self):
     with self.assertRaisesRegex(
             ValueError,
             'component_mutation_probabilities not normalized to 1'):
         mutators.XCFunctionalMutator(
             self.mutator_x,
             self.mutator_css,
             self.mutator_cos,
             component_mutation_probabilities=[0.4, 0.4, 0.4])
 def test_initialization_with_negative_probability(self):
     with self.assertRaisesRegex(
             ValueError,
             'component_mutation_probabilities contains negative probabilities'
     ):
         mutators.XCFunctionalMutator(
             self.mutator_x,
             self.mutator_css,
             self.mutator_cos,
             component_mutation_probabilities=[-0.2, 0.6, 0.6])
 def test_initialization_with_wrong_probability_length(self):
     with self.assertRaisesRegex(
             ValueError,
             'Wrong length for component_mutation_probabilities. Expected 3, got 2'
     ):
         mutators.XCFunctionalMutator(
             self.mutator_x,
             self.mutator_css,
             self.mutator_cos,
             component_mutation_probabilities=[0.5, 0.5])
Exemplo n.º 5
0
def make_mutator(instruction_pool,
                 mutation_pool,
                 max_num_instructions,
                 max_num_bound_parameters,
                 num_fixed_instructions,
                 component_mutation_probabilities=None,
                 seed=None):
    """Constructs mutator for functional forms.

  Args:
    instruction_pool: Dict, the pool of possible instructions.
    mutation_pool: Dict, the pool of possible mutation rules.
    max_num_instructions: Integer, the maximum number of instructions.
    max_num_bound_parameters: Integer, the maximum number of bound parameters.
    num_fixed_instructions: Integer, the number of fixed instructions.
    component_mutation_probabilities: Sequence of 3 floats, the probabilities
      for mutating exchange, same-spin or opposite-spin component of the
      functional.
    seed: Integer, random seed.

  Returns:
    Instance of mutators.XCFunctionalMutator, the resulting mutator.
  """
    return mutators.XCFunctionalMutator(
        mutator_x=mutators.EnhancementFactorMutator(
            instruction_pool=instruction_pool,
            mutation_pool=mutation_pool,
            max_num_instructions=max_num_instructions,
            num_fixed_instructions=num_fixed_instructions,
            max_num_bound_parameters=max_num_bound_parameters),
        mutator_css=mutators.EnhancementFactorMutator(
            instruction_pool=instruction_pool,
            mutation_pool=mutation_pool,
            max_num_instructions=max_num_instructions,
            num_fixed_instructions=num_fixed_instructions,
            max_num_bound_parameters=max_num_bound_parameters),
        mutator_cos=mutators.EnhancementFactorMutator(
            instruction_pool=instruction_pool,
            mutation_pool=mutation_pool,
            max_num_instructions=max_num_instructions,
            num_fixed_instructions=num_fixed_instructions,
            max_num_bound_parameters=max_num_bound_parameters),
        component_mutation_probabilities=component_mutation_probabilities,
        seed=seed)
    def test_mutate(self, component):
        with mock.patch.object(mutators.XCFunctionalMutator,
                               'get_random_component',
                               return_value=component), mock.patch.object(
                                   mutators.EnhancementFactorMutator,
                                   'get_random_mutation_type',
                                   return_value='remove_instruction'):
            mutator = mutators.XCFunctionalMutator(
                mutator_x=mutators.EnhancementFactorMutator(),
                mutator_css=mutators.EnhancementFactorMutator(),
                mutator_cos=mutators.EnhancementFactorMutator())
            functional = copy.deepcopy(xc_functionals.b97_u)

            new_functional, mutated_component, _, instruction_index, change = (
                mutator.mutate(functional, verbose=False))

            self.assertEqual(functional, xc_functionals.b97_u)
            self.assertEqual(mutated_component, component)
            new_enhancement_factor = getattr(new_functional, component)
            self.assertEqual(new_enhancement_factor.num_instructions, 4)
            new_enhancement_factor.instruction_list.insert(
                instruction_index, change[0])
            self.assertEqual(new_functional, functional)