-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathde_testing.py
More file actions
53 lines (44 loc) · 2.19 KB
/
de_testing.py
File metadata and controls
53 lines (44 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# encoding: utf-8
import unittest
import numpy as np
from population import Population
from differential_evolution import DifferentialEvolution
class TestDifferentialEvolution(unittest.TestCase):
def setUp(self):
# Objective function: simple quadratic func of x^2 + y^2 where we expect the minimum to be [0,0]
self.objective_function = lambda x: np.sum(x ** 2)
self.bounds = np.array([[-10, -10], [10, 10]])
self.de1 = DifferentialEvolution(self.objective_function, self.bounds, population_size=10, mutation_factor=0.5, crossover_prob=0.7, max_iter=100)
def test_mutation(self):
# Test if mutation produces a valid mutant vector within bounds
mutant = self.de1.mutate(0)
self.assertEqual(len(mutant), len(self.bounds))
self.assertTrue(np.all(mutant >= -10) and np.all(mutant <= 10))
def test_crossover(self):
# Test if crossover produces a valid trial vector
target = self.de1.population.get_individuals(0)
mutant = self.de1.mutate(0)
trial = self.de1.crossover(target, mutant)
self.assertEqual(len(trial), len(self.bounds))
self.assertTrue(np.all(trial >= -10) and np.all(trial <= 10))
def test_selection(self):
# Test if selection updates the population correctly
target = self.de1.population.get_individuals(0)
mutant = self.de1.mutate(0)
trial = self.de1.crossover(target, mutant)
self.de1.select(0, trial)
updated_individual = self.de1.population.get_individuals(0)
if self.objective_function(trial) < self.objective_function(target):
np.testing.assert_array_equal(updated_individual, trial)
else:
np.testing.assert_array_equal(updated_individual, target)
def test_run(self):
# Test the full run method
best_solution, best_fitness = self.de1.run()
print("best solution is:",best_solution,"best fitness is:", best_fitness)
self.assertIsNotNone(best_solution)
self.assertTrue(np.all(best_solution >= -10) and np.all(best_solution <= 10))
self.assertAlmostEqual(best_fitness, 0, delta=1e-5)
if __name__ == '__main__':
unittest.main()