-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding reference implementation and saving convergence scores
- Loading branch information
1 parent
b942a20
commit e133bb5
Showing
9 changed files
with
309 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
Oops, something went wrong.