Ejemplo n.º 1
0
	def evolve(self, pop, x_b, task):
		npop, x_bc = list(), pop[argmin([x.f for x in pop])]
		for i in range(self.NP):
			ind = Individual(x=self.Selection(pop, i, self.Ts, x_bc, self.Rand), e=False)
			ind.x = self.Crossover(pop, i, self.Cr, self.Rand)
			ind.x = self.Mutation(pop, i, self.Mr, task, self.Rand)
			ind.evaluate(task)
			npop.append(ind)
			if x_b.f > ind.f: x_b = ind
		return npop, x_b
Ejemplo n.º 2
0
class IndividualTestCase(TestCase):
    r"""Test case for testing Individual class.

	Date:
		April 2019

	Author:
		Klemen Berkovič

	See Also:
		* :class:`NiaPy.algorithms.Individual`
	"""
    def setUp(self):
        self.D = 20
        rng = default_rng()
        self.x, self.task = default_rng().uniform(
            -100, 100, self.D), StoppingTask(D=self.D,
                                             nFES=230,
                                             nGEN=np.inf,
                                             benchmark=MyBenchmark())
        self.s1, self.s2, self.s3 = Individual(x=self.x, e=False), Individual(
            task=self.task, rng=rng), Individual(task=self.task)

    def test_generateSolutin_fine(self):
        self.assertTrue(self.task.isFeasible(self.s2))
        self.assertTrue(self.task.isFeasible(self.s3))

    def test_evaluate_fine(self):
        self.s1.evaluate(self.task)
        self.assertAlmostEqual(self.s1.f, self.task.eval(self.x))

    def test_repair_fine(self):
        s = Individual(x=np.full(self.D, 100))
        self.assertFalse(self.task.isFeasible(s.x))

    def test_eq_fine(self):
        self.assertFalse(self.s1 == self.s2)
        self.assertTrue(self.s1 == self.s1)
        s = Individual(x=self.s1.x)
        self.assertTrue(s == self.s1)

    def test_str_fine(self):
        self.assertEqual(str(self.s1), '%s -> %s' % (self.x, np.inf))

    def test_getitem_fine(self):
        for i in range(self.D):
            self.assertEqual(self.s1[i], self.x[i])

    def test_len_fine(self):
        self.assertEqual(len(self.s1), len(self.x))
Ejemplo n.º 3
0
class IndividualTestCase(TestCase):
    def setUp(self):
        self.D = 20
        self.x, self.task = rnd.uniform(-100, 100,
                                        self.D), Task(self.D, 230, inf,
                                                      MyBenchmark())
        self.s1, self.s2, self.s3 = Individual(x=self.x, e=False), Individual(
            task=self.task, rand=rnd), Individual(task=self.task)

    def test_x_fine(self):
        self.assertTrue(array_equal(self.x, self.s1.x))

    def test_generateSolutin_fine(self):
        self.assertTrue(self.task.isFeasible(self.s2))
        self.assertTrue(self.task.isFeasible(self.s3))

    def test_evaluate_fine(self):
        self.s1.evaluate(self.task)
        self.assertAlmostEqual(self.s1.f, self.task.eval(self.x))

    def test_repair_fine(self):
        s = Individual(x=full(self.D, 100))
        self.assertFalse(self.task.isFeasible(s.x))
        s.repair(self.task)
        self.assertTrue(self.task.isFeasible(s.x))

    def test_eq_fine(self):
        self.assertFalse(self.s1 == self.s2)
        self.assertTrue(self.s1 == self.s1)
        s = Individual(x=self.s1.x)
        self.assertTrue(s == self.s1)

    def test_str_fine(self):
        self.assertEqual(str(self.s1), '%s -> %s' % (self.x, inf))

    def test_getitem_fine(self):
        for i in range(self.D):
            self.assertEqual(self.s1[i], self.x[i])

    def test_len_fine(self):
        self.assertEqual(len(self.s1), len(self.x))