SAMA is a free, open-source Python platform for the design, sizing, and operation optimization of hybrid renewable energy microgrids. It performs location-specific techno-economic assessment using site-dependent load profiles, meteorological data, component degradation models, and utility billing structures, with no license fees and no black-box code.
Developed by the FAST Research Group at Western University, London, Ontario, Canada.
- Why SAMA?
- What SAMA Optimizes
- Key Capabilities
- Getting Started
- Quick Start
- Optimization Modes
- Optimization Algorithms
- Output Examples
- Documentation
- Running the Tests
- Publications
- Citation
- License
- Contact
Proprietary tools like HOMER Pro and HOMER Grid are closed-source and license-restricted. SAMA was built to remove that barrier:
- Free and open-source: AGPL-3.0
- Transparent algorithms: read, modify, and extend every line
- Validated: the 2023 version was compared with HOMER Pro for residential systems in Sacramento, California and New Bern, North Carolina (Sadat et al., 2023); that comparison has not been repeated for the current version
- No cost barriers: designed for researchers, students, and resource-constrained institutions worldwide
SAMA finds the optimal combination of five system design variables by minimizing Net Present Cost (NPC) over a 25-year project lifetime:
| Variable | Description |
|---|---|
Npv |
Number of PV modules (kW of rated capacity) |
Nwt |
Number of wind turbines |
Nbat |
Number of battery packs |
N_DG |
Number / size of diesel generator units |
Cn_I |
Inverter capacity (kW) |
Constraints include reliability (LPSP), renewable energy fraction, capital budget, and rooftop area limits. SAMA can also return the trade-off between objectives such as cost, emissions and renewable fraction as a Pareto front (see Optimization Modes).
| Category | Details |
|---|---|
| System Components | Solar PV (mono- or bi-facial modules, one or more sub-arrays), wind turbines, battery storage (Li-ion or lead-acid), diesel generator, utility grid, air-source heat pump (Bosch or Goodman), electric vehicle with V2X |
| Optimization Algorithms | Particle Swarm Optimization (PSO), Advanced Differential Evolution (ADE), Artificial Bee Colony (ABC), Grey Wolf Optimizer (GWO) |
| Energy Simulation | 8,760-hour hourly dispatch with marginal-cost-based EMS, EV smart charging and V2X lookahead arbitrage, heat pump thermal-electric coupling. The EV and heat pump are simulated within every design the optimizer evaluates, so the system is sized with them integrated |
| Electricity Tariffs | 8 structures: flat, seasonal, monthly, tiered, seasonal tiered, monthly tiered, Time-of-Use (TOU), Ultra-Low TOU (ULO); commercial and industrial demand charges (flat, seasonal, monthly or TOU, with optional ratchet) and a minimum charge rule |
| Natural Gas Tariffs | 8 structures including PG&E G-1 therms-based and Enbridge EGD 4-tier m³-based |
| Financial Analysis | 25-year NPC, LCOE, levelized emissions, price escalation, RE incentives (30% ITC), net metering reconciliation |
| Load Inputs | 10 electrical load input modes (CSV, monthly/annual averages, generic profiles), thermal load from Excel |
| Weather Inputs | SAM NSRDB API integration, user CSV files, monthly/annual averages |
| Outputs | Convergence plots, 25-year cash flow charts, energy distribution, battery SOC, grid interconnection, EV and heat pump performance charts, hourly CSV data |
SAMA can be used in three ways:
For researchers and developers who want full access to source code and algorithms.
git clone https://github.com/Sas1997/SAMA.git
cd SAMA
pip install -e .This installs SAMA in editable mode, so you can edit any file under src/samapy/ directly and see your changes immediately. Configure your system with the wizard or by editing src/samapy/core/Input_Data.py, then run:
samapy-runAvailable algorithms: pso, ade, abc, gwo
The recommended method for most users. No source editing required.
pip install samapyNote: The PyPI distribution name is
samapy. Once installed, import and CLI commands usesamapy. Step 1: Configure your project:
samapy-configAn interactive multi section wizard guides you through all parameters and saves samapy_config_COMPLETE.yaml.
Step 2: Run the optimization:
samapy-runStep 3: View results in samapy_outputs/
Additional run options:
samapy-run --algorithm ade # override algorithm
samapy-run --output results/run1 # override output directory
samapy-run --dry-run --verbose # validate config without running
samapy-run --no-gui # headless / server mode
samapy-run --seed 1 # reproducible run (fixed random seed)
samapy-run --mode multi # multi-objective mode (Pareto front)Reference example. examples/README.md maps every way of running SAMA to its inputs, configuration and outputs, and examples/reference_config.yaml is the reference case whose full results are in Section 18 of the documentation:
samapy-run -c examples/reference_config.yaml --seed 1 --no-gui --output reference_runA point-and-click GUI for non-technical users. No Python installation required. Currently in alpha.
Download:
SAMA-Setup.exe, then run the installer and follow the steps.
Run SAMAPY in five steps using bundled sample data (Boston, MA):
# 1. Install
pip install samapy
# 2. Create a project folder and navigate into it
mkdir my_samapy_project && cd my_samapy_project
# 3. Launch the configuration wizard (accept defaults for a quick test)
samapy-config
# 4. Run the optimization
samapy-run
# 5. View your results
# samapy_outputs/figs/ → all charts (PNG/SVG)
# samapy_outputs/data/ → Outputforplotting.csv (8,760-hour time series)Expected runtime: 2–10 minutes for
MaxIt=200,nPop=50depending on hardware. The first run is slower due to one-time Numba JIT compilation of the EMS engine.
SAMA can be run in two modes:
Single-objective mode (default, optimization_mode: single). One of the four algorithms below minimizes a scalar fitness:
fitness = NPC_weight * NPC + EM_weight * EM * LEM + constraint penalties
With EM = 0 (default) this is net present cost alone. With EM = 1 it is a weighted sum of cost and levelized emissions: EM_weight / NPC_weight is the NPC in dollars treated as equivalent to 1 kg/kWh of emissions. The defaults (1e-5 and 1e2) only put the two quantities on comparable numerical scales; set them in the configuration file, or in the wizard, which asks for them when emissions are included. Every run prints the function it minimizes with its weights. A weighted sum returns one design per run, and even repeated for many weights it only reaches designs on the convex hull of the trade-off (supported designs).
Multi-objective mode (optimization_mode: multi, or samapy-run --mode multi). NSGA-II from the pymoo library searches for the Pareto front: the set of designs for which no objective can be improved without worsening another. Choose two or more objectives in MOO_objectives from npc, lcoe, lem (emissions), lpsp (unmet demand) and re (renewable fraction, maximized). Reliability, minimum renewable fraction, budget and the capacity limits are handled as constraints, so the trade-off is explored within those limits.
optimization_mode: multi
MOO_objectives: [npc, re] # cost versus renewable fraction
MOO_selection: compromise # or min_npc, min_re, ..., or weights such as [2, 1]
MOO_n_pop: 50 # optional, default nPop
MOO_n_gen: 200 # optional, default MaxItThe run writes pareto_front.csv (every Pareto-optimal design with its NPC, LCOE, LEM, LPSP, renewable fraction and capital cost, and whether some weighted sum of the objectives would select it) and pareto_front.png, where unsupported designs, which no weighted sum can return, are shown as open circles. One design is then selected for the full SAMA results: compromise takes the design closest to the ideal point with equal weight on each normalized objective, min_<objective> takes the best design for one objective, and a list of weights favours the objectives with larger weights. Grid emission factors (E_CO2, E_SO2, E_NOx) are zero in the example configuration, so set them before using lem as an objective.
Two example configurations show the mode on the reference house: examples/moo_grid_no_export.yaml (cost against renewable fraction, grid-connected) and examples/moo_offgrid.yaml (cost against reliability, off-grid, with a non-convex front). examples/compare_weighted_sum.py compares each front with a weighted-sum sweep; the results are in Section 7.6.3 of the documentation.
In single-objective mode, all four algorithms optimize the same 5-variable design space. Choose based on your preference for convergence speed vs. exploration depth:
| Algorithm | Key Behavior | Best For |
|---|---|---|
| PSO: Particle Swarm Optimization | Velocity-based swarm convergence | Fast convergence |
| ADE: Advanced Differential Evolution | Multi-strategy mutation with adaptive F and CR | Thorough exploration |
| ABC: Artificial Bee Colony | Foraging phases with guided reinitialization | Balanced exploration |
| GWO: Grey Wolf Optimizer | Pack hierarchy convergence; parallel variant available | Fast convergence, parallel runs |
For rigorous comparison, set Run_Time > 1 to average across multiple independent runs.
After samapy-run completes, results are organized in samapy_outputs/:
samapy_outputs/
├── Optimization.png ← convergence curve
├── pareto_front.csv, pareto_front.png ← Pareto front (multi-objective mode)
├── figs/
│ ├── Cash_Flow.svg ← 25-year lifecycle cost breakdown
│ ├── Energy Distribution.png ← hourly served load and supply by source
│ ├── Energy Balance.png ← hourly dumped energy and shortages
│ ├── Grid Interconnection.png ← hourly grid import/export
│ ├── electricity_comparison_annual.png ← system vs. grid-only cost, by year
│ ├── electricity_comparison_monthly.png ← system vs. grid-only cost, by month
│ ├── Battery State of Charge.png ← hourly SOC (if a battery is selected)
│ ├── hp_monthly_summary.png ← heat pump COP and energy (if HP=1)
│ └── EV Energy.png ← EV charge/discharge (if EV=1)
└── data/
└── Outputforplotting.csv ← full 8,760-hour time series
Full documentation is available in this repository:
- SAMA_Documentation.md: Complete user guide covering all parameters, algorithms, financial models, input formats, troubleshooting, and mathematical formulations
Key documentation sections:
| Topic | Location |
|---|---|
| All configuration parameters | Section 6: Configuration File |
| Optimization algorithms (detail) | Section 7 |
| Energy management system | Section 8 |
| Component models | Section 9 |
| Financial model | Section 10 |
| Troubleshooting | Section 13 |
| Mathematical formulations | Section 19 |
| Where to find input data | Appendix A |
SAMA includes an automated test suite that runs through GitHub Actions on every push to main and on every pull request.
pip install -e . pytest
pytest # default suite, about one minute
pytest -m slow # full SAMA runs with result generation, a few minutesThe default suite checks installation, imports, bundled data, input data, the command-line runner, and the convergence of all four optimizers on benchmark functions with known optima (shifted sphere and Rosenbrock), using fixed random seeds. The test settings and pass criteria are documented in tests/test_optimizer_convergence.py.
SAMA has been peer-reviewed and published across multiple journals. If you use SAMA in your research, please cite [A1]:
| # | Reference |
|---|---|
| A1 | Sadat, S.A., Takahashi, J. and Pearce, J.M. (2023). A Free and open-source microgrid optimization tool: SAMA. Energy Conversion and Management, 298, 117686. DOI |
| A2 | Sadat, S.A. and Pearce, J.M. (2024). The threat of economic grid defection in the US with solar PV, battery and generator hybrid systems. Solar Energy, 282, 112910. DOI |
| A3 | Sadat, S.A. and Pearce, J.M. (2025). Techno-economic evaluation of electricity pricing structures on PV and PV-battery hybrid systems in Canada. Renewable Energy, 242, 122456. DOI |
| A4 | Sadat, S.A., Mittal, K. and Pearce, J.M. (2025). Using investments in solar photovoltaics as inflation hedges. Energies, 18(4), 890. DOI |
| A5 | Groza, J.M., Sadat, S.A., Hayibo, K.S. and Pearce, J.M. (2024). Autonomous peer-to-peer virtual net metering of solar PV distributed generation. Solar Energy Advances, 4, 100064. DOI |
| A6 | Sadat, S.A., Lemieux, J.E.B. and Pearce, J.M. (2026). Shifting Subsidies: Redirecting Alberta's Oil and Gas Support to Solar Power. Energies, 19(4), 972. DOI |
| A7 | Sadat, S.A., Hoex, B. and Pearce, J.M. (2022). A review of the effects of haze on solar PV performance. Renewable and Sustainable Energy Reviews, 167, 112796. DOI |
If you use SAMA in academic work, please cite the primary paper:
@article{sadat2023sama,
title = {A Free and open source microgrid optimization tool: {SAMA} the solar alone Multi-Objective Advisor},
author = {Sadat, Seyyed Ali and Takahashi, J. and Pearce, Joshua M.},
journal = {Energy Conversion and Management},
volume = {298},
pages = {117686},
year = {2023},
doi = {10.1016/j.enconman.2023.117686}
}SAMA is released under the GNU Affero General Public License v3.0 (AGPL-3.0). You are free to use, copy, modify, and distribute SAMA. Any derivative works must also be released under AGPL-3.0, including modified versions that are offered to users over a network.
FAST Research Group, Western University, London, Ontario, Canada
- GitHub: https://github.com/Sas1997/SAMA
- Issues and bug reports: Open an issue
- Contributing, questions and feature requests: see CONTRIBUTING.md
- For reprint requests, contact the developers through the GitHub repository. Problems with the Windows .exe alpha version can be reported as issues.
Developed by the FAST Research Group · Western University · AGPL-3.0
