예제 #1
0
    def test_error_checking_1_parent(self) -> None:
        c1 = copy.deepcopy(self.c1)
        set_numpy_seed()

        # Test assertionError is raised with too few parents
        with self.assertRaises(AssertionError):
            self.mate([c1], n_crossover_points=1)
예제 #2
0
 def setUp(self) -> None:
     """
     Sets up the roulette wheel selector and a dummy population
     """
     set_numpy_seed()
     self.population = [DummyPopMember(f) for f in np.linspace(-10, 0, 10)]
     self.selector = RouletteWheelSelection()
예제 #3
0
 def setUpClass(cls) -> None:
     """
     Sets up the tournament selector and a small dummy population
     """
     set_numpy_seed()
     cls.population = [DummyPopMember(f) for f in np.linspace(-10.0, -1.0, 10)]
     cls.selector = TournamentSelector()
예제 #4
0
 def setUp(self) -> None:
     """
     Sets up the boltzmann weighted selector and a dummy population
     """
     set_numpy_seed()
     self.population = [DummyPopMember(f) for f in np.linspace(-10.0, -9.0, 10)]
     self.selector = BoltzmannSelector(kb=0.0019872041, temperature=130)  # using kb in kcal/mol*K
예제 #5
0
    def test_error_checking_too_much_crossover(self) -> None:
        c1 = copy.deepcopy(self.c1)
        c2 = copy.deepcopy(self.c2)
        set_numpy_seed()

        # Test assertionError is raised with too many mating points
        with self.assertRaises(AssertionError):
            self.mate([c1, c2], n_crossover_points=5)
예제 #6
0
    def test_error_checking_3_parent_crossover(self) -> None:
        c1 = copy.deepcopy(self.c1)
        c2 = copy.deepcopy(self.c2)
        c3 = copy.deepcopy(self.c3)
        set_numpy_seed()

        # Test assertionError is raised with too few mating points for three parents
        with self.assertRaises(AssertionError):
            self.mate([c1, c2, c3], n_crossover_points=1)
예제 #7
0
    def test_single_point_crossover(self) -> None:
        c1 = copy.deepcopy(self.c1)
        c2 = copy.deepcopy(self.c2)
        set_numpy_seed()

        child = self.mate([c1, c2], n_crossover_points=1)

        self.assertFalse(c1.get_molecular_positions()[1] == child.get_molecular_positions()[1])
        self.assertFalse(c2.get_molecular_positions()[1] == child.get_molecular_positions()[1])
예제 #8
0
    def test_tournament_distribution(self) -> None:

        # Test default distribution
        set_numpy_seed()
        population = get_dummy_population(-100, -90, 1000)

        values_def = self.fetch_distribution(population)

        self.assertListEqual([547, 243, 110, 41, 34, 11, 9, 2, 0, 3], list(values_def))
예제 #9
0
    def test_tournament_distribution_stochastic(self) -> None:

        # Test stochastic distribution when k=1
        set_numpy_seed()
        population = get_dummy_population(-100, -90, 1000)

        self.selector.k = 1
        values_stochastic = self.fetch_distribution(population)
        self.selector.__init__()  # reinitialise to reset k to default
        self.assertTrue((20-np.sum(np.log10(values_stochastic))) <= 0.02)
예제 #10
0
    def test_tournament_distribution_deterministic(self) -> None:

        # Test deterministic tournament selection
        set_numpy_seed()
        population = get_dummy_population(-100, -90, 1000)

        values_def = self.fetch_distribution(population)
        self.selector.p = 1  # Set p_select for the fittest member in the tournament to 1
        values_deterministic = self.fetch_distribution(population)
        self.selector.__init__()  # reinitialise to reset p to default
        self.assertTrue(values_deterministic[0] > values_def[0])
예제 #11
0
    def test_random_cluster(self) -> None:
        set_numpy_seed()

        new_cluster = self.mutate(self.c1)

        self.assertIsInstance(new_cluster, Cluster)

        # Check that we are not getting the same cluster back
        self.assertNotEqual(new_cluster, self.c1)

        # Check that we are getting the same masses back
        self.assertListEqual(new_cluster.molecules[0].masses.tolist(), [1.01, 1.01, 4.0])

        atomic_positions = new_cluster.get_particle_positions()[0]

        self.assertFalse(check_list_almost_equal(atomic_positions, self.c1.get_particle_positions()[0]))
예제 #12
0
    def setUpClass(cls) -> None:
        """Sets up a database in memory and various other classes required by PoolGA"""

        cls.log = logging.getLogger(__name__)

        cls.database = Database(
            new_database=True, compare_clusters=SimpleEnergeticCharacterizer())
        cls.selector = BaseSelector()
        cls.mating = DeavenHoCrossover()
        cls.mock_system = unittest.mock.Mock(spec=DefineSystem)

        cls.mock_pool = [unittest.mock.MagicMock(spec=Cluster)] * 5

        cls.log.info(str(cls.mock_system))

        cls.GA = PoolGA(database=cls.database,
                        min_pool_size=10,
                        system=cls.mock_system,
                        crossover=cls.mating,
                        selector=cls.selector)

        set_numpy_seed()

        def make_ga(cls,
                    database=":memory:",
                    min_pool_size=10,
                    selector=cls.selector,
                    mating=cls.mating,
                    system=cls.mock_system) -> PoolGA:
            """Returns a PoolGA instance"""

            return PoolGA(database=database,
                          min_pool_size=min_pool_size,
                          selector=selector,
                          crossover=mating,
                          system=system)

        cls.make_ga = make_ga
예제 #13
0
 def setUp(self) -> None:
     """Sets the numpy seed to a known value"""
     set_numpy_seed()
예제 #14
0
 def test_tournament_get_parents(self) -> None:
     set_numpy_seed()
     result = self.selector.get_parents(self.population)
     self.assertIsInstance(result, List)
     self.assertIsInstance(result[0], DummyPopMember)
예제 #15
0
 def setUp(self) -> None:
     """Sets the numpy seed before each test"""
     set_numpy_seed()
 def setUp(self):
     set_numpy_seed()
예제 #17
0
 def setUp(self) -> None:
     """Sets the random seed and sets up the dummy population and Rank selector"""
     set_numpy_seed()
     self.selector = RankSelector()
     self.population = get_dummy_population()