A Python Library to run solar and storage optimization. This uses mixed integer linear programming and maximises revenue made by charging and discharging the battery. The model uses variable prices and a solar generation profile.
pip install solar-and-storage
This repository uses uv for local development and CI (pyproject.toml + uv.lock).
uv sync
uv run pytest src/tests
uv run ruff check .
Import the packages
import numpy as np
from solar_and_storage import SolarAndStorageMake the fake price and solar data
# make prices
prices = np.zeros(24) + 30
prices[6:19] = 40
prices[9] = 50
prices[12:14] = 30
prices[16:18] = 50
prices[17] = 60
# make solar profile
solar = np.zeros(24)
solar[8:16] = 2.0
solar[10:14] = 4.0Then run optimization
solar_and_storage = SolarAndStorage(prices=prices, solar_generation=list(solar))
solar_and_storage.run_optimization()
result_df = solar_and_storage.get_results()Now plot the data
fig = solar_and_storage.get_figure()
fig.show(rendered="browser")The first plot shows the solar profile, the second shows the prices that day. The third shows the battery profile. Finally the fourth shows profit. You can see that the battery charged from the solar site at the end of the solar maximum
Use current_soc to set the starting battery state of charge as a fraction of
the battery capacity. For example, a half-full battery can discharge during an
initial high-price period:
prices = np.zeros(24)
prices[0] = 100
solar = np.zeros(24)
solar_and_storage = SolarAndStorage(
prices=prices,
solar_generation=list(solar),
current_soc=0.5,
battery_eta_charge=1,
battery_eta_discharge=1,
)
result_df = solar_and_storage.get_results()Thanks you to the follow repos for inspiration
- https://github.com/ADGEfficiency/energy-py-linear
- https://github.com/wzyfrank/battery/
- https://github.com/greysonchung/Battery-Optimisation/
- https://github.com/edu230991/battery-optimization/ sdk-python-ci.yml
Thanks goes to these wonderful people (emoji key):
Peter Dudfield 💻 |
gilbertgong 💻 |
davidhuanggg 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!

