A reusable parameter optimization package for systematic testing of parameter combinations
Project description
Parameter Optimizer
A reusable Python package that systematically tests different parameter combinations for a given class to find the optimal configuration based on a specified metric. Perfect for hyperparameter optimization, configuration tuning, and systematic testing.
Features
- 🔧 Flexible Parameter Configuration: Define parameter ranges in JSON format
- 💾 Intelligent Caching: Avoid duplicate tests with persistent result caching
- 📌 Fixed Parameter Support: Lock certain parameters while optimizing others
- 📊 Progress Tracking: Monitor optimization progress and view detailed results
- 🛡️ Error Handling: Graceful handling of test failures and invalid configurations
- 🔌 Universal Integration: Works with any class that accepts keyword arguments
- 📈 Resource Management: Built-in memory monitoring and batch processing
- 🎯 Multiple Metrics: Support for custom metric functions and optimization goals
Installation
From PyPI (Recommended)
pip install parameter-optimizer
For Development
git clone https://github.com/parameter-optimizer/parameter-optimizer.git
cd parameter-optimizer
pip install -e ".[dev]"
With Optional Dependencies
# For testing
pip install parameter-optimizer[test]
# For development
pip install parameter-optimizer[dev]
Quick Start
1. Basic Usage
from parameter_optimizer import ParameterOptimizer
# Define your target class
class MyModel:
def __init__(self, learning_rate, batch_size, epochs):
self.learning_rate = learning_rate
self.batch_size = batch_size
self.epochs = epochs
# Your model initialization here
def train_and_evaluate(self):
# Your training logic here
# Return a performance metric (higher = better)
return accuracy_score
# Define metric function
def metric_function(model_instance):
return model_instance.train_and_evaluate()
# Create optimizer
optimizer = ParameterOptimizer(
target_class=MyModel,
parameters_json_path="parameters.json",
metric_function=metric_function
)
# Run optimization
best_config = optimizer.optimize()
print(f"Best configuration: {best_config}")
# Get detailed results
results = optimizer.get_results()
summary = optimizer.get_optimization_summary()
2. Parameter Configuration
Create a JSON file (parameters.json) with parameter ranges:
{
"learning_rate": [0.001, 0.01, 0.1],
"batch_size": [16, 32, 64],
"epochs": [10, 20, 50]
}
3. Advanced Usage with Fixed Parameters
# Fix some parameters while optimizing others
optimizer = ParameterOptimizer(
target_class=MyModel,
parameters_json_path="parameters.json",
metric_function=metric_function,
fixed_parameters={"epochs": 20}, # Fix epochs to 20
cache_dir="./my_cache"
)
# Run optimization with progress tracking
def progress_callback(current, total, best_score, best_config):
print(f"Progress: {current}/{total} ({current/total*100:.1f}%)")
if best_config:
print(f"Current best: {best_config} (score: {best_score:.3f})")
best_config = optimizer.optimize(progress_callback=progress_callback)
Examples
The package includes comprehensive examples for different use cases:
- Machine Learning: Hyperparameter optimization for ML models
- Database Tuning: Connection pool and query optimization
- Web Server: Multi-objective optimization for performance
- Game AI: Player satisfaction optimization
- Cache Systems: Memory and performance tuning
Run examples:
python examples/usage_examples.py
python examples/integration_tests.py
API Reference
ParameterOptimizer
Main class for parameter optimization.
ParameterOptimizer(
target_class, # Class to optimize
parameters_json_path, # Path to parameter configuration
metric_function, # Function to evaluate performance
cache_dir="./optimization_cache", # Cache directory
fixed_parameters=None # Dict of fixed parameters
)
Methods:
optimize(max_combinations=None, progress_callback=None): Run optimizationget_results(sort_by_metric=True): Get all test resultsget_progress(): Get current progress informationget_optimization_summary(): Get comprehensive summary
Data Models
- TestResult: Individual test result with parameters, score, and metadata
- OptimizationSummary: Complete optimization summary with statistics
Requirements
- Python 3.8+
- psutil (for resource monitoring)
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Testing
# Run all tests
pytest
# Run with coverage
pytest --cov=parameter_optimizer
# Run specific test categories
pytest tests/ # Unit tests
pytest examples/integration_tests.py # Integration tests
License
MIT License - see LICENSE file for details.
Changelog
v1.0.0
- Initial stable release
- Complete parameter optimization functionality
- Comprehensive caching system
- Resource management and monitoring
- Full test coverage and examples
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parameter_optimizer-1.0.0.tar.gz.
File metadata
- Download URL: parameter_optimizer-1.0.0.tar.gz
- Upload date:
- Size: 79.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7427328f239e2bf0fa9b1adbe6e1315a4403e5fcc75e57e7d32eb7a870b2a2d
|
|
| MD5 |
161873177a8a6022258634b7523693ca
|
|
| BLAKE2b-256 |
489e6885218c369aab657591818b7193baab07090186010ce056fcd429c155e0
|
File details
Details for the file parameter_optimizer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: parameter_optimizer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3473bd4e097f22dcfaecff303badb09ed09ff7e88496a5c54e4da596f63dfffe
|
|
| MD5 |
c57ca45b8caa98baf9370dc5393a4476
|
|
| BLAKE2b-256 |
0cab4145893527198ccd17430a53c0fcbe2dd2609923b813d81949a7a257e7a1
|