Skip to content

Commit

Permalink
Adding reference implementation and saving convergence scores
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepkunkunuru committed Oct 2, 2024
1 parent b942a20 commit e133bb5
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.oaenv/
.pytest_cache/
tests/__pycache__/
rao_algorithms/__pycache__/
build/
dist/
results/
*.egg-info/
.pypirc
example.py
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,32 @@ pip install .

## How to Use

### Example: Constrained BMR Algorithm

```python
import numpy as np
from rao_algorithms import run_optimization, BMR_algorithm, objective_function, constraint_1, constraint_2

# Constrained BMR
# ---------------
bounds = np.array([[-100, 100]] * 2)
num_iterations = 100
population_size = 50
constraints = [constraint_1, constraint_2]

best_solution, best_scores = run_optimization(BMR_algorithm, bounds, num_iterations, population_size, 2, objective_function, constraints)
print(f"Constrained BMR Best solution: {best_solution}")

```

### Example: Unconstrained BMR Algorithm

```python
import numpy as np
from rao_algorithms import BMR_algorithm, objective_function

# Unconstrained BMR
# ---------------
# Define the bounds for a 2D problem
bounds = np.array([[-100, 100]] * 2)

Expand All @@ -47,8 +67,8 @@ population_size = 50
num_variables = 2

# Run the BMR algorithm
best_fitness = BMR_algorithm(bounds, num_iterations, population_size, num_variables, objective_function)
print(f"Best fitness found: {best_fitness}")
best_solution, best_scores = BMR_algorithm(bounds, num_iterations, population_size, num_variables, objective_function)
print(f"Unconstrained BMR Best solution found: {best_solution}")
```

### Example: Constrained BWR Algorithm
Expand All @@ -57,6 +77,8 @@ print(f"Best fitness found: {best_fitness}")
import numpy as np
from rao_algorithms import BWR_algorithm, objective_function, constraint_1, constraint_2

# Constrained BWR
# ---------------
# Define the bounds for a 2D problem
bounds = np.array([[-100, 100]] * 2)

Expand All @@ -67,8 +89,8 @@ num_variables = 2
constraints = [constraint_1, constraint_2]

# Run the BWR algorithm with constraints
best_fitness = BWR_algorithm(bounds, num_iterations, population_size, num_variables, objective_function, constraints)
print(f"Best fitness found: {best_fitness}")
best_solution, best_scores = BWR_algorithm(bounds, num_iterations, population_size, num_variables, objective_function, constraints)
print(f"Constrained BWR Best solution found: {best_solution}")
```

### Unit Testing
Expand Down
16 changes: 8 additions & 8 deletions rao_algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .algorithms import BMR_algorithm, BWR_algorithm
from .penalty import penalty_function, constrained_objective_function
from .optimization import run_optimization, save_convergence_curve
from .objective_functions import objective_function, constraint_1, constraint_2

__all__ = [
"BMR_algorithm",
"BWR_algorithm",
"penalty_function",
"constrained_objective_function",
"objective_function",
"constraint_1",
"constraint_2"
'BMR_algorithm',
'BWR_algorithm',
'run_optimization',
'save_convergence_curve',
'objective_function',
'constraint_1',
'constraint_2',
]
18 changes: 12 additions & 6 deletions rao_algorithms/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
def BMR_algorithm(bounds, num_iterations, population_size, num_variables, objective_func, constraints=None):
population = np.random.uniform(low=bounds[:, 0], high=bounds[:, 1], size=(population_size, num_variables))

best_scores = []

for iteration in range(num_iterations):
if constraints:
fitness = [constrained_objective_function(ind, objective_func, constraints) for ind in population]
Expand All @@ -12,6 +14,8 @@ def BMR_algorithm(bounds, num_iterations, population_size, num_variables, object

best_solution = population[np.argmin(fitness)]
mean_solution = np.mean(population, axis=0)
best_score = np.min(fitness)
best_scores.append(best_score)

for i in range(population_size):
r1, r2, r3, r4 = np.random.rand(4)
Expand All @@ -23,15 +27,16 @@ def BMR_algorithm(bounds, num_iterations, population_size, num_variables, object
else:
population[i] = bounds[:, 1] - (bounds[:, 1] - bounds[:, 0]) * r3

print(f"Iteration {iteration+1}, Best Fitness: {np.min(fitness)}")
population = np.clip(population, bounds[:, 0], bounds[:, 1])

best_fitness = np.min(fitness)
return best_fitness
return best_solution, best_scores


def BWR_algorithm(bounds, num_iterations, population_size, num_variables, objective_func, constraints=None):
population = np.random.uniform(low=bounds[:, 0], high=bounds[:, 1], size=(population_size, num_variables))

best_scores = []

for iteration in range(num_iterations):
if constraints:
fitness = [constrained_objective_function(ind, objective_func, constraints) for ind in population]
Expand All @@ -40,6 +45,8 @@ def BWR_algorithm(bounds, num_iterations, population_size, num_variables, object

best_solution = population[np.argmin(fitness)]
worst_solution = population[np.argmax(fitness)]
best_score = np.min(fitness)
best_scores.append(best_score)

for i in range(population_size):
r1, r2, r3, r4 = np.random.rand(4)
Expand All @@ -51,7 +58,6 @@ def BWR_algorithm(bounds, num_iterations, population_size, num_variables, object
else:
population[i] = bounds[:, 1] - (bounds[:, 1] - bounds[:, 0]) * r3

print(f"Iteration {iteration+1}, Best Fitness: {np.min(fitness)}")
population = np.clip(population, bounds[:, 0], bounds[:, 1])

best_fitness = np.min(fitness)
return best_fitness
return best_solution, best_scores
2 changes: 1 addition & 1 deletion rao_algorithms/objective_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

# Example of an objective function (Sphere function)
# Sphere function (default objective)
def objective_function(x):
return np.sum(x**2)

Expand Down
38 changes: 38 additions & 0 deletions rao_algorithms/optimization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np
import os
import csv

def initialize_population(bounds, population_size, num_variables):
"""Initialize population with random values within bounds."""
return np.random.uniform(low=bounds[:, 0], high=bounds[:, 1], size=(population_size, num_variables))

def clip_position(position, bounds):
"""Clip the position to make sure it stays within bounds."""
return np.clip(position, bounds[:, 0], bounds[:, 1])

def run_optimization(algorithm, bounds, num_iterations, population_size, num_variables, objective_function, constraints=None):
"""Run the selected algorithm and handle logging, saving results, etc."""

# Initialize population and variables
population = initialize_population(bounds, population_size, num_variables)
best_scores = []

# Prepare directory for saving results
if not os.path.exists('results'):
os.makedirs('results')

# Run the algorithm
best_solution, best_scores = algorithm(bounds, num_iterations, population_size, num_variables, objective_function, constraints)

# Save results
save_convergence_curve(best_scores)

return best_solution, best_scores

def save_convergence_curve(best_scores):
"""Save the convergence curve as a CSV."""
with open(f'results/convergence_curve.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Iteration', 'Best Score'])
for i, score in enumerate(best_scores):
writer.writerow([i, score])
Loading

0 comments on commit e133bb5

Please sign in to comment.