Skip to content

Commit 2368665

Browse files
Adding more objective functions and test cases
1 parent e133bb5 commit 2368665

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

rao_algorithms/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
from .algorithms import BMR_algorithm, BWR_algorithm
22
from .optimization import run_optimization, save_convergence_curve
3-
from .objective_functions import objective_function, constraint_1, constraint_2
3+
from .objective_functions import (
4+
objective_function,
5+
rastrigin_function,
6+
ackley_function,
7+
rosenbrock_function,
8+
constraint_1,
9+
constraint_2,
10+
nonlinear_objective_function,
11+
constraint_3,
12+
constraint_4,
13+
constraint_5
14+
)
415

516
__all__ = [
617
'BMR_algorithm',
718
'BWR_algorithm',
819
'run_optimization',
920
'save_convergence_curve',
1021
'objective_function',
22+
'rastrigin_function',
23+
'ackley_function',
24+
'rosenbrock_function',
1125
'constraint_1',
1226
'constraint_2',
27+
'nonlinear_objective_function',
28+
'constraint_3',
29+
'constraint_4',
30+
'constraint_5',
1331
]
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
import numpy as np
22

3-
# Sphere function (default objective)
3+
# Sphere function (from previous implementation)
44
def objective_function(x):
55
return np.sum(x**2)
66

7+
# Rastrigin function
8+
def rastrigin_function(x):
9+
return 10 * len(x) + np.sum(x**2 - 10 * np.cos(2 * np.pi * x))
10+
11+
# Ackley function
12+
def ackley_function(x):
13+
a = 20
14+
b = 0.2
15+
c = 2 * np.pi
16+
n = len(x)
17+
s1 = -a * np.exp(-b * np.sqrt(np.sum(x**2) / n))
18+
s2 = -np.exp(np.sum(np.cos(c * x)) / n)
19+
return s1 + s2 + a + np.exp(1)
20+
21+
# Rosenbrock function
22+
def rosenbrock_function(x):
23+
return np.sum(100 * (x[1:] - x[:-1]**2)**2 + (x[:-1] - 1)**2)
24+
725
# Example constraint: x[0] + x[1] <= 10
826
def constraint_1(x):
927
return x[0] + x[1] - 10 # Must be <= 0
1028

1129
# Example constraint: x[0] >= x[1]
1230
def constraint_2(x):
1331
return -(x[0] - x[1]) # Must be <= 0 (x[0] >= x[1])
32+
33+
# Example: A nonlinear constrained optimization problem
34+
def nonlinear_objective_function(x):
35+
return x[0]**2 + x[1]**2 + x[0]*x[1] - 10*x[0] - 15*x[1]
36+
37+
# Constraints
38+
def constraint_3(x):
39+
return 25 - (x[0]**2 + x[1]**2) # Circle constraint: x[0]^2 + x[1]^2 <= 25
40+
41+
def constraint_4(x):
42+
return x[0] - 5 # Must be <= 5
43+
44+
def constraint_5(x):
45+
return x[1] - 5 # Must be <= 5

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def read_file(filename):
88

99
setup(
1010
name='rao_algorithms',
11-
version='0.2.0',
11+
version='0.2.1',
1212
author='Samdeep Kunkunuru',
1313
author_email='[email protected]',
1414
description='BMR and BWR optimization algorithms with constraint handling',

tests/test_algorithms.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import unittest
22
import 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

55
class 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+
3158
if __name__ == '__main__':
32-
unittest.main()
59+
unittest.main()

0 commit comments

Comments
 (0)