Market Clearing Problem - Power Network Simulation
A professional research and engineering tool for electricity market optimization coupled with power network analysis
Features • Installation • Quick Start • Documentation • Contributing
- Overview
- Features
- Architecture
- Installation
- Quick Start
- Usage
- Project Structure
- Configuration
- Troubleshooting
- Documentation
- Contributing
- License
MCP-NetSim is a comprehensive simulation platform that solves the Market Clearing Problem (MCP) for electricity markets while ensuring physical feasibility of power network operations. The project implements an iterative coupling between economic optimization (PyPSA) and physical network analysis (pandapower) to deliver optimal and feasible solutions.
Electricity market operators must:
- ✅ Minimize generation costs (economic optimization)
- ✅ Ensure physical feasibility (network constraints)
- ✅ Handle congestion (line capacity limits)
- ✅ Maintain voltage stability (voltage limits)
- ✅ Integrate renewables (variable generation)
MCP-NetSim solves this complex optimization problem automatically through an iterative algorithm that converges to an economically optimal and physically feasible solution.
- 🔬 Academic Research: Market design, congestion management, renewable integration
- 🏭 Industrial Applications: Production planning, network analysis, contingency studies
- 📚 Education: Teaching electricity markets and power system optimization
- 🔍 Analysis: Scenario evaluation, sensitivity analysis, policy impact studies
- 🔌 Network Modeling: AC power flow analysis with pandapower
- 💰 Market Optimization: Economic dispatch with PyPSA (Market Clearing Problem)
- 🔄 Iterative Coupling: Automatic convergence to optimal and feasible solutions
- 🌱 Renewable Integration: Wind and solar generation with variable profiles
- 🔋 Storage Modeling: Battery storage with charge/discharge constraints
- 📊 Multiple Scenarios: Normal, congestion, renewables, N-1 contingency
- 📈 Interactive Visualization: Real-time plots with Plotly
- 🌐 Web Interface: Modern Streamlit dashboard
- 💾 Results Export: CSV and Excel export with structured data
- Multi-timestep Simulation: Hourly or 15-minute resolution
- LMP Calculation: Locational Marginal Prices per bus
- Congestion Detection: Automatic identification of overloaded lines
- Voltage Monitoring: Real-time voltage violation detection
- Scenario Management: Easy switching between test cases
- Configuration Management: YAML-based centralized configuration
- Structured Logging: JSON-formatted logs for analysis
- Unit Testing: Comprehensive test suite with pytest
┌───────────────────────────────────────────────────┐
│ MCP-NetSim Platform │
├───────────────────────────────────────────────────│
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Economic │ │ Physical │ │
│ │ Optimization │◄───────►│ Network │ │
│ │ (PyPSA) │Iterative│ Analysis │ │
│ │ │Coupling │ (pandapower) │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ └──────────┬─────────────┘ │
│ │ │
│ ┌─────▼─────┐ │
│ │ Results │ │
│ │ Export │ │
│ └───────────┘ │
│ │
└───────────────────────────────────────────────────┘
| Component | Technology | Purpose |
|---|---|---|
| Optimization | PyPSA | Market clearing problem solver |
| Network Analysis | pandapower | AC power flow calculations |
| Visualization | Plotly | Interactive charts and graphs |
| Web Interface | Streamlit | Modern dashboard UI |
| Data Processing | Pandas, NumPy | Data manipulation and analysis |
| Configuration | YAML | Centralized settings |
| Testing | pytest | Unit and integration tests |
| Logging | Python logging | Structured JSON logs |
- Python: 3.8 or higher
- Operating System: macOS, Linux, or Windows (with WSL)
- Package Manager: pip (Python package installer)
git clone https://github.com/yourusername/MCP-NetSim.git
cd MCP-NetSim# Install all required packages
pip install -r requirements.txt
# Or with trusted hosts (if SSL issues)
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org -r requirements.txtMCP-NetSim requires an optimization solver for PyPSA to work.
brew install cbcsudo apt-get update
sudo apt-get install coinor-cbcsudo dnf install coin-or-Cbc# In WSL Ubuntu
sudo apt-get update
sudo apt-get install coinor-cbc# Test Python imports
python3 -c "import pypsa, pandapower; print('✓ All imports successful')"
# Test solver availability
python3 -c "
import pypsa
n = pypsa.Network()
n.set_snapshots([0])
n.add('Bus', 'bus1', v_nom=110)
n.add('Generator', 'gen1', bus='bus1', p_nom=100, marginal_cost=30)
n.add('Load', 'load1', bus='bus1', p_set=50)
n.optimize()
print('✓ Solver works!')
"-
Launch the web interface:
python3 -m streamlit run app.py
-
Open your browser:
- The interface will automatically open at
http://localhost:8501 - If not, navigate manually to the URL shown in the terminal
- The interface will automatically open at
-
Run a simulation:
- Select a scenario from the sidebar (e.g., "Normal")
- Click "🚀 Run Simulation"
- View results in the interactive tabs
# Run normal scenario
python3 main.py --scenario normal
# Run with verbose output
python3 main.py --scenario normal --verbose
# Run multi-timestep simulation (24 hours)
python3 main.py --scenario normal --multi-timestep --hours 24from src.data.network_data import NetworkDataGenerator
from src.network.network_model import NetworkModel
from src.market.market_model import MarketModel
from src.coupling.coupling_logic import CouplingLogic
# Create network data
generator = NetworkDataGenerator()
network_data = generator.create_ieee_14_bus_network()
# Initialize models
network_model = NetworkModel(network_data)
market_model = MarketModel(network_data)
coupling_logic = CouplingLogic(network_model, market_model)
# Solve coupled problem
results = coupling_logic.solve_coupled_problem(verbose=True)
# Access results
print(f"Converged: {results['converged']}")
print(f"Total Cost: {results['total_cost']:.2f} €")
print(f"Generation: {results['generation_schedule']}")
print(f"LMPs: {results['lmps']}")| Scenario | Description | Use Case |
|---|---|---|
| Normal | Standard network operation | Baseline analysis |
| Congestion | High demand scenario | Congestion management |
| Renewables | High renewable penetration | Renewable integration |
| N-1 Outage | Line contingency analysis | Security assessment |
- 📊 Real-time Visualization: Interactive charts for prices, generation, loading, and voltage
- 💾 Export Results: Download results as CSV or Excel files
- ⚙️ Configuration: Adjust simulation parameters via sidebar
- 📈 Multiple Views: Switch between different visualization tabs
python3 main.py [OPTIONS]
Options:
--scenario SCENARIO Scenario to run (normal, congestion, renewable_heavy, n1_outage)
--verbose Enable verbose output
--multi-timestep Enable multi-timestep simulation
--hours HOURS Number of hours for multi-timestep (default: 24)
--output-dir DIR Output directory for results (default: results/)MCP-NetSim/
├── src/ # Source code
│ ├── data/ # Network data generation
│ │ └── network_data.py # IEEE 14-bus and custom networks
│ ├── network/ # Network modeling
│ │ └── network_model.py # pandapower integration
│ ├── market/ # Market optimization
│ │ └── market_model.py # PyPSA MCP solver
│ ├── coupling/ # Coupling logic
│ │ └── coupling_logic.py # Iterative algorithm
│ ├── renewables/ # Renewable integration
│ │ ├── renewable_integration.py
│ │ └── storage_model.py
│ ├── scenarios/ # Scenario management
│ │ └── scenario_manager.py
│ ├── visualization/ # Plotting functions
│ │ └── visualizer.py
│ ├── results/ # Results export
│ │ └── results_export.py
│ └── utils/ # Utilities
│ ├── config_loader.py # Configuration management
│ ├── logger.py # Structured logging
│ └── exceptions.py # Custom exceptions
├── config/ # Configuration files
│ └── config.yaml # Centralized configuration
├── tests/ # Test suite
│ ├── test_network_model.py
│ ├── test_market_model.py
│ └── conftest.py
├── results/ # Output directory
│ └── data/ # Exported results
├── logs/ # Log files
├── app.py # Streamlit web interface
├── main.py # CLI entry point
├── requirements.txt # Python dependencies
├── pytest.ini # pytest configuration
├── presentation.html # Project presentation
└── README.md # This file
Configuration is managed through config/config.yaml. Key sections:
network:
base_voltage_kv: 110.0
voltage_min_pu: 0.95
voltage_max_pu: 1.05market:
solver: cbc # or glpk
tolerance: 1e-6coupling:
max_iterations: 20
convergence_tolerance: 1e-3
congestion_threshold: 90.0 # Percentagelogging:
level: INFO
format: json
file: logs/mcpsim.logSymptoms:
- Convergence: ✗ No
- Total Generation: 0.00 MW
- Total Cost: 0.00 €
Solution: This usually means PyPSA solver is not installed. Install CBC:
# macOS
brew install cbc
# Linux
sudo apt-get install coinor-cbcThen restart the application.
Solution:
pip install pypsaSolution:
pip install pandapowerPossible Causes:
- Network data issues
- Voltage setpoints out of range
- Line parameters invalid
Solution:
- Check network data validity
- Verify voltages are between 0.9-1.1 pu
- Review line resistance/reactance values
Possible Causes:
- Solver not installed
- Generation capacity < Load
- Invalid generator costs
Solution:
- Verify solver installation (see above)
- Check that total generation capacity ≥ total load
- Ensure all generator costs are positive
- Check the error message in the web interface (red box)
- Enable verbose mode:
python3 main.py --scenario normal --verbose - Review logs in
logs/mcpsim.log - See
presentation.htmlfor detailed project explanation
presentation.html: Complete project presentation (open in browser)README.md: This file - main documentationconfig/config.yaml: Configuration reference- Code comments: Inline documentation in source files
The MCP minimizes total generation cost subject to:
- Power balance: Σ P_gen = Σ P_load
- Generator limits: P_min ≤ P ≤ P_max
- Network constraints: Line limits, voltage bounds
LMPs represent the marginal cost of electricity at each bus, accounting for:
- Generation costs
- Transmission congestion
- Losses
The algorithm iteratively:
- Optimizes economically (PyPSA)
- Verifies physically (pandapower)
- Adjusts constraints if violations detected
- Repeats until convergence
# Run all tests
pytest
# Run with coverage
pytest --cov=src --cov-report=html
# Run specific test file
pytest tests/test_network_model.py -vCurrent test coverage targets:
- Network model: AC power flow calculations
- Market model: Optimization solver integration
- Coupling logic: Convergence algorithm
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes with tests
- Run tests:
pytest - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow PEP 8 Python style guide
- Use type hints for function signatures
- Add docstrings to all functions and classes
- Write unit tests for new features
| Metric | Description | Normal Range |
|---|---|---|
| Convergence | Algorithm converged successfully | ✓ Yes |
| Iterations | Number of iterations needed | 1-5 |
| Total Cost | Total generation cost | Scenario-dependent |
| LMPs | Locational Marginal Prices | 20-50 €/MWh |
| Congested Lines | Lines loaded >90% | 0 (ideal) |
| Voltage Violations | Buses outside limits | 0 (ideal) |
- LMP Maps: Show price differences due to congestion
- Generation Schedules: Optimal dispatch per generator
- Line Loading: Identify bottlenecks in the network
- Voltage Profiles: Ensure system stability
This project is licensed under the MIT License - see the LICENSE file for details.
- PyPSA: Python for Power System Analysis
- pandapower: An easy-to-use power system analysis tool
- Streamlit: For the web interface framework
- Plotly: For interactive visualizations
- Email: zaksab98@gmail.com
Made with ⚡ for power system research and engineering