Skip to content

feat: Add configuration management system #471

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

aries043
Copy link
Contributor

@aries043 aries043 commented May 29, 2025

Summary

This PR introduces a comprehensive configuration management system to replace hardcoded values throughout the benchmarking script. The change enables flexible experiment customization through external configuration files while maintaining full backward compatibility.
The previous refactoring is verified in test-refactorizing-1.yml, and the problem is verified in test-refactorizing-2.yml.

Problem

The current benchmarking script has several configuration management issues:

  • Hardcoded values scattered throughout code: Runtime limits, boundary values, evaluation counts, and other parameters are embedded directly in the code
  • Inflexible experiment setup: Changing experiment parameters requires code modification and recompilation
  • Poor reusability: Different experiment configurations cannot be easily saved and reused
  • Maintenance overhead: Parameter adjustments require touching multiple code locations
# Current hardcoded approach
options = {
    'max_function_evaluations': 100000 * self.ndim_problem,  # Fixed multiplier
    'max_runtime': 3600 * 3,  # Fixed 3 hours
    'fitness_threshold': 1e-10,  # Fixed threshold
    'boundary_range': 10.0,  # Fixed boundaries
    # ... more hardcoded values
}

Solution

1. Centralized Configuration Class

Introduced ExperimentConfig dataclass to consolidate all configurable parameters:

@dataclass
class ExperimentConfig:
    max_function_evaluations_multiplier: int = 100000
    max_runtime_hours: float = 3.0
    fitness_threshold: float = 1e-10
    saving_fitness: int = 2000
    boundary_range: float = 10.0
    sigma_value: float = 20.0 / 3.0
    random_seed: int = 2022
    verbose_level: int = 0
    results_folder: str = "pypop7_benchmarks_lso"

2. Configuration File Support

Added support for both JSON and YAML configuration files:

def load_config(config_file: Optional[str] = None) -> ExperimentConfig:
    # Loads from JSON/YAML files with graceful fallback to defaults

3. Template Generation

Added configuration template generation for easy customization:

# Generate configuration template
python benchmarking_lsbbo_2.py --save-config-template

# Use custom configuration
python benchmarking_lsbbo_2.py --config my_config.yaml --start 0 --end 0 --optimizer CMAES

4. Enhanced CLI Interface

Extended command-line interface with configuration options:

  • --config: Specify custom configuration file
  • --save-config-template: Generate configuration template

Key Benefits

  • ✅ Flexibility: Experiment parameters can be changed without code modification
  • ✅ Reusability: Configuration files can be saved, shared, and version-controlled
  • ✅ Maintainability: All configuration logic centralized in one location
  • ✅ Format Support: Both JSON and YAML formats supported with automatic detection
  • ✅ Template System: Easy configuration file creation with --save-config-template
  • ✅ Graceful Fallback: Falls back to defaults if configuration file is invalid

Backward Compatibility

  • ✅ Default behavior unchanged: All existing commands work exactly the same
  • ✅ Same output format: Results are saved in identical locations with same naming
  • ✅ No breaking changes: All existing scripts continue to function
  • ✅ Progressive enhancement: New features are opt-in only

Configuration Options

Parameter Description Default
max_function_evaluations_multiplier Multiplier for max evaluations (× dimension) 100000
max_runtime_hours Maximum runtime in hours 3.0
fitness_threshold Target fitness threshold for early stopping 1e-10
saving_fitness Interval for saving fitness data 2000
boundary_range Problem boundary range (-range to +range) 10.0
sigma_value Sigma parameter for compatible optimizers 6.67
random_seed Random seed for reproducibility 2022
verbose_level Output verbosity (0=quiet, 1=normal, 2=verbose) 0
results_folder Custom results storage folder "pypop7_benchmarks_lso"

Example Configuration File

# config.yaml
max_function_evaluations_multiplier: 50000
max_runtime_hours: 1.5
fitness_threshold: 1e-8
boundary_range: 5.0
verbose_level: 1
results_folder: "custom_results"

Testing

Enhanced GitHub Actions workflow tests:

  • ✅ Configuration file loading (JSON/YAML)
  • ✅ Template generation functionality
  • ✅ Parameter validation and error handling
  • ✅ Backward compatibility verification
  • ✅ Multi-format configuration support

Files Changed

  • tutorials/benchmarking_lsbbo_2.py - Added comprehensive configuration management
  • .github/workflows/test-refactoring-2.yml - Configuration-focused test suite

Usage Examples

# Generate configuration template
python benchmarking_lsbbo_2.py --save-config-template

# Use default configuration (existing behavior)
python benchmarking_lsbbo_2.py --start 0 --end 0 --optimizer CMAES --ndim_problem 10

# Use custom YAML configuration
python benchmarking_lsbbo_2.py --config custom.yaml --start 0 --end 0 --optimizer CMAES --ndim_problem 10

# Use custom JSON configuration
python benchmarking_lsbbo_2.py --config custom.json --start 0 --end 0 --optimizer JADE --ndim_problem 50

Future Enhancements

This configuration system enables future improvements:

  • Environment-specific configuration profiles
  • Advanced parameter validation and constraints
  • Configuration inheritance and composition
  • Integration with experiment management tools

However, this PR focuses on establishing the core configuration infrastructure while maintaining simplicity and backward compatibility.


Testing Instructions:

# Test template generation
python tutorials/benchmarking_lsbbo_2.py --save-config-template

# Test with custom configuration
echo "max_runtime_hours: 0.5" > test_config.yaml
python tutorials/benchmarking_lsbbo_2.py --config test_config.yaml --start 0 --end 0 --optimizer CMAES --ndim_problem 2

# Verify backward compatibility
python tutorials/benchmarking_lsbbo_2.py --start 0 --end 0 --optimizer PRS --ndim_problem 2

aries043 added 14 commits May 28, 2025 15:19
…stry

- Replace 80+ line if-elif chain with centralized OPTIMIZER_CONFIGS dictionary
- Add type hints and proper error handling
- Improve code maintainability and readability
- Add comprehensive GitHub Actions testing workflow
- Include argument validation and better error messages

Fixes: Long if-elif chain making code hard to maintain
- Add ExperimentConfig dataclass for centralized settings
- Support JSON and YAML configuration files
- Add --config and --save-config-template CLI options
- Replace hardcoded values with configurable parameters
- Add separate GitHub Actions workflows for each improvement
- Improve code maintainability and flexibility
- Remove trailing whitespace and blank line whitespace (W291, W293)
- Remove unused variable config_with_comments (F841)
- Add newline at end of file (W292)
- Remove docstring comments as requested
- Clean up code formatting for better maintainability
- Remove all explanatory comments as requested
- Remove docstrings from functions and classes
- Keep only essential file header docstring
- Clean up code to focus on functionality only
- Remove remaining blank line whitespace (W293)
- Add proper newline at end of file (W292)
- Ensure clean code formatting
- Remove blank line whitespace (W293)
- Add proper newline at end of file (W292)
- Clean code formatting complete
- Remove blank line whitespace (W293)
- Add proper newline at end of file (W292)
- Clean code formatting complete
@Evolutionary-Intelligence
Copy link
Owner

@aries043 Thanks very much again for your wonderful configuration management system. I will integrate it after I check it. As I am busy, this integration may be relatively slow (sorry).

@aries043
Copy link
Contributor Author

That's fine! Thank you for carefully considering my suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants