-
Notifications
You must be signed in to change notification settings - Fork 2
Centralize optimizer demo blocks to eliminate 800+ lines of duplication #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
007db12
Initial plan
Copilot 3063bb6
feat: add centralized demo module for optimizer demonstrations
Copilot b7a3742
refactor: update 109 optimizers to use centralized demo runner
Copilot 1bfd2c1
test: add comprehensive tests for centralized demo module
Copilot c9ea1c2
docs: add demo section to README with examples
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| """Centralized demo runner for optimizer demonstrations. | ||
|
|
||
| This module provides a standardized way to run demonstrations for any optimizer, | ||
| ensuring consistent output formatting and reducing code duplication across the | ||
| codebase. | ||
|
|
||
| Example: | ||
| >>> from opt.demo import run_demo | ||
| >>> from opt.swarm_intelligence.particle_swarm import ParticleSwarm | ||
| >>> solution, fitness = run_demo(ParticleSwarm) | ||
| Running ParticleSwarm demo... | ||
| Function: shifted_ackley | ||
| Dimensions: 2 | ||
| Bounds: [-2.768, 2.768] | ||
| Max iterations: 100 | ||
| <BLANKLINE> | ||
| Results: | ||
| Best solution found: [...] | ||
| Best fitness value: ... | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
|
|
||
| from opt.benchmark.functions import shifted_ackley | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| from opt.abstract_optimizer import AbstractOptimizer | ||
|
|
||
|
|
||
| def run_demo( | ||
| optimizer_class: type[AbstractOptimizer], | ||
| *, | ||
| func: Callable[[np.ndarray], float] = shifted_ackley, | ||
| dim: int = 2, | ||
| lower_bound: float = -2.768, | ||
| upper_bound: float = 2.768, | ||
| max_iter: int = 100, | ||
| **kwargs: Any, | ||
| ) -> tuple[np.ndarray, float]: | ||
| """Run a standardized demo for any optimizer. | ||
|
|
||
| Args: | ||
| optimizer_class: The optimizer class to demonstrate. | ||
| func: Benchmark function to optimize. Defaults to shifted_ackley. | ||
| dim: Dimensionality of the search space. Defaults to 2. | ||
| lower_bound: Lower bound of the search space. Defaults to -2.768. | ||
| upper_bound: Upper bound of the search space. Defaults to 2.768. | ||
| max_iter: Maximum iterations. Defaults to 100. | ||
| **kwargs: Additional optimizer-specific parameters. | ||
|
|
||
| Returns: | ||
| Tuple of (best_solution, best_fitness). | ||
|
|
||
| Example: | ||
| >>> from opt.demo import run_demo | ||
| >>> from opt.swarm_intelligence.particle_swarm import ParticleSwarm | ||
| >>> solution, fitness = run_demo(ParticleSwarm) | ||
| Running ParticleSwarm demo... | ||
| Function: shifted_ackley | ||
| Dimensions: 2 | ||
| Bounds: [-2.768, 2.768] | ||
| Max iterations: 100 | ||
| <BLANKLINE> | ||
| Results: | ||
| Best solution found: [...] | ||
| Best fitness value: ... | ||
| """ | ||
| print(f"Running {optimizer_class.__name__} demo...") | ||
| print(f" Function: {func.__name__}") | ||
| print(f" Dimensions: {dim}") | ||
| print(f" Bounds: [{lower_bound}, {upper_bound}]") | ||
| print(f" Max iterations: {max_iter}") | ||
|
|
||
| optimizer = optimizer_class( | ||
| func=func, | ||
| dim=dim, | ||
| lower_bound=lower_bound, | ||
| upper_bound=upper_bound, | ||
| max_iter=max_iter, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| best_solution, best_fitness = optimizer.search() | ||
|
|
||
| print("\nResults:") | ||
| print(f" Best solution found: {best_solution}") | ||
| print(f" Best fitness value: {best_fitness:.6f}") | ||
|
|
||
| return best_solution, best_fitness | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
run_demofunction does not handle cases where an optimizer might fail during instantiation or execution. Consider adding error handling to provide a more helpful error message when optimizers with incompatible signatures or parameters fail. For example, if an optimizer requires additional mandatory parameters beyond the standard set, the error message should guide users on how to provide them.