11import unittest
22import numpy as np
3- from rao_algorithms import BMR_algorithm , BWR_algorithm , objective_function , constraint_1 , constraint_2
3+ from rao_algorithms import BMR_algorithm , BWR_algorithm , run_optimization , objective_function , rastrigin_function , ackley_function , rosenbrock_function , constraint_1 , constraint_2
44
55class TestOptimizationAlgorithms (unittest .TestCase ):
66
77 def setUp (self ):
8- self .bounds = np .array ([[- 100 , 100 ]] * 2 )
8+ self .bounds = np .array ([[- 100 , 100 ]] * 2 ) # Change as needed for higher dimensional problems
99 self .num_iterations = 100
1010 self .population_size = 50
11- self .num_variables = 2
11+ self .num_variables = 2 # You can increase this for higher-dimensional tests
1212
1313 def test_bmr_unconstrained (self ):
1414 best_solution , _ = BMR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , objective_function )
@@ -18,6 +18,18 @@ def test_bwr_unconstrained(self):
1818 best_solution , _ = BWR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , objective_function )
1919 self .assertIsInstance (best_solution , np .ndarray )
2020
21+ def test_bmr_rastrigin (self ):
22+ best_solution , _ = BMR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , rastrigin_function )
23+ self .assertIsInstance (best_solution , np .ndarray )
24+
25+ def test_bwr_ackley (self ):
26+ best_solution , _ = BWR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , ackley_function )
27+ self .assertIsInstance (best_solution , np .ndarray )
28+
29+ def test_bmr_rosenbrock (self ):
30+ best_solution , _ = BMR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , rosenbrock_function )
31+ self .assertIsInstance (best_solution , np .ndarray )
32+
2133 def test_bmr_constrained (self ):
2234 constraints = [constraint_1 , constraint_2 ]
2335 best_solution , _ = BMR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , objective_function , constraints )
@@ -28,5 +40,20 @@ def test_bwr_constrained(self):
2840 best_solution , _ = BWR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , objective_function , constraints )
2941 self .assertIsInstance (best_solution , np .ndarray )
3042
43+ def test_multiple_runs (self ):
44+ """Run BMR multiple times and calculate mean and standard deviation."""
45+ num_runs = 30
46+ results = []
47+
48+ for _ in range (num_runs ):
49+ best_solution , _ = BMR_algorithm (self .bounds , self .num_iterations , self .population_size , self .num_variables , objective_function )
50+ results .append (np .sum (best_solution ** 2 )) # Sum of squares for comparison
51+
52+ mean_result = np .mean (results )
53+ std_result = np .std (results )
54+
55+ self .assertGreaterEqual (mean_result , 0 )
56+ print (f"BMR Mean: { mean_result } , Std Dev: { std_result } " )
57+
3158if __name__ == '__main__' :
32- unittest .main ()
59+ unittest .main ()
0 commit comments