Skip to content

Commit

Permalink
changed max of grid size to 300000 (from 200000)
Browse files Browse the repository at this point in the history
changed producer expenses to include land and fixed cost (from lcoe calculations)
added license file for open code
added link to open source in navbar
reduced trails for choosing candidate in optimization
removed nevergrad derivative optimizer from code
added test for block size input in bess form
added queues for celery
removed all executable portions from open source code
  • Loading branch information
pvstorageoptimization committed Dec 6, 2023
1 parent affc6ff commit 06d5a83
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 1,220 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 El-mor Renewable Energies Dev Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ You can run an optimization on an example system with:
documentation
=============

Check out our documentation<ADD link>. There are example of how to use the different modules for simulation and
Check out our `documentation <ADD link>`_. There are example of how to use the different modules for simulation and
optimization

License
=======

ADD LINK
`MIT <https://github.com/pvstorageoptimization/Optibess_algorithm/LICENSE>`_

36 changes: 2 additions & 34 deletions optibess_algorithm/financial_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,9 @@ def get_producer_expenses(self, cpi: float | None = None):
yearly_expenses = 0
# calculate first year capex
if year == 0:
yearly_expenses = self._total_producer_capex
yearly_expenses = self._total_producer_capex + self._land_capex + self._fixed_capex
# calculate opex
yearly_expenses += self._total_producer_opex
yearly_expenses += self._total_producer_opex + self._land_opex + self._fixed_opex

# multiple expenses by cpi multiplier
expenses.append(yearly_expenses * cpi_multi)
Expand Down Expand Up @@ -828,35 +828,3 @@ def plot_cash_flow(self, power_output=None, purchased_from_grid=None):
_, _, revenues = self.get_cash_flow(power_output, purchased_from_grid)
plt.plot(revenues)
plt.show()


if __name__ == '__main__':
# ((0, 1660), (84, 1001), (120, 949), (168, 377), (216, 232), (252, 119))
storage = LithiumPowerStorage(25, 100000, aug_table=((288, 1),))
producer = PvProducer("../../../test docs/Sushevo_Project.CSV", pv_peak_power=150000)

# pvgis
# producer = PvProducer(latitude=30.60187, longitude=34.97361, tech=Tech.EAST_WEST, pv_peak_power=9821)

# pvlib
# module = MODULE_DEFAULT
# inverter = INVERTER_DEFAULT
# producer = PvProducer(latitude=31.10062, longitude=34.83988, modules_per_string=28, strings_per_inverter=22,
# number_of_inverters=850, module=module, inverter=inverter,
# tech=Tech.FIXED)

output = OutputCalculator(25, 100000, producer, storage, save_all_results=True, fill_battery_from_grid=False,
bess_discharge_start_hour=17, producer_factor=1)
test = FinancialCalculator(output, 1000, capex_per_land_unit=215000, capex_per_kwp=370, opex_per_kwp=5,
battery_capex_per_kwh=170, battery_opex_per_kwh=5, battery_connection_capex_per_kw=50,
battery_connection_opex_per_kw=0.5, fixed_capex=15000000, fixed_opex=1000000,
interest_rate=0.04, cpi=0.02)
start_time = time.time()
print("irr: ", test.get_irr())
print("npv: ", test.get_npv(5))
print("lcoe: ", test.get_lcoe())
print("lcos: ", test.get_lcos())
print("total bess to grid: ", sum([output.results[i]['grid_from_bess'].sum() for i in range(len(output.results))]))
print("total pv output: ", output.results[1]['pv_output'].sum() * 25)
print("lcoe no grid power:", test.get_lcoe_no_power_costs())
print(f"calculation took: {(time.time() - start_time)} seconds")
34 changes: 0 additions & 34 deletions optibess_algorithm/output_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,37 +1054,3 @@ def plot_stat(self, years: Iterable[int] | None = None, stat: str = "output"):
all_data = pd.concat(stat_data)
all_data.plot()
plt.show()


if __name__ == '__main__':
year_num = 25
connection = 180000
storage = LithiumPowerStorage(year_num, connection, aug_table=((0, 846), (72, 1093), (140, 179), (200, 200)))

# file
import os
root_folder = os.path.dirname(os.path.abspath(__file__))
prod = PvProducer(os.path.join(root_folder, "test.csv"), pv_peak_power=13000)

# pvgis
# prod = PvProducer(latitude=30.60187, longitude=34.97361, tech=Tech.EAST_WEST, pv_peak_power=9821)

# pvlib
# module = MODULE_DEFAULT
# inverter = INVERTER_DEFAULT
# prod = PvProducer(latitude=30.92196, longitude=34.85602, modules_per_string=10, strings_per_inverter=2,
# number_of_inverters=2000, module=module, inverter=inverter,
# tech=Tech.EAST_WEST)

test = OutputCalculator(year_num, connection, prod, storage, producer_factor=1, save_all_results=True)

start_time = time.time()
test.run()
np.set_printoptions(linewidth=1000)
print(f"calculation took: {time.time() - start_time} seconds")
print(test.monthly_averages(stat="bess_from_pv"))
print("-----------------------------------------------------------------------------------")
print(test.monthly_averages(stat="bess_from_grid"))
print("-----------------------------------------------------------------------------------")
print(test.monthly_averages(stat="grid_from_bess"))
# print(test.monthly_averages(stat="pv_output"))
166 changes: 2 additions & 164 deletions optibess_algorithm/power_system_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ def _create_optimizer(self):
inopts={"integer_variables": range(13)}),
ng.optimizers.Powell], [self._budget // 2, self._budget // 4])(
parametrization=self._instru, budget=self._budget)
# reduce number of tries for finding candidates
opt._constraints_manager.max_trials = 10
return opt

def _register_callbacks(self, opt, progress_recorder=None):
Expand Down Expand Up @@ -298,167 +300,3 @@ def _optimize(self, progress_recorder=None):
except ng.errors.NevergradEarlyStopping:
recommendation = opt.provide_recommendation()
return recommendation.value, recommendation.loss


class NevergradDerivativeOptimizer(NevergradOptimizer):

def _set_variables(self):
temp_aug_table = self._output.aug_table.copy()
params = [ng.p.Scalar(init=min(i * self._def_aug_diff, self._output.num_of_years - 1), lower=0,
upper=self._month_bound // 12).set_integer_casting()
for i in range(self._max_aug_num)] + \
[ng.p.Scalar(init=temp_aug_table[0, 1] // 2, lower=0, upper=self._first_entry_bound).
set_integer_casting()] + \
[ng.p.Scalar(init=temp_aug_table[1, 1], lower=0, upper=self._first_entry_bound // 2).
set_integer_casting() for _ in range(self._initial_aug_num - 1)] + \
[ng.p.Scalar(init=0, lower=0, upper=self._first_entry_bound // 2).set_integer_casting()
for _ in range(self._max_aug_num - self._initial_aug_num)] + \
[ng.p.Scalar(init=100, lower=1, upper=100).set_integer_casting()]
self._instru = ng.p.Tuple(*params)

def _create_optimizer(self):
opt = ng.optimizers.CMAsmall(parametrization=self._instru, budget=self._budget)
self._step = 0
self._derivatives_calcs = 0
self._derivatives = np.zeros(len(self._instru))
self._last_parameters = None
self._last_result = None
return opt

def _check_param_diff(self, arr, tol):
"""
check if the difference between the given solution and the saved parameters is in the acceptable tolerance
:param arr: the given solution
:param tol: the tolerance
"""
for i in (list(range(self._max_aug_num)) + ([len(arr) - 1])):
# for augmentation years and producer factor check difference of each one
if not (self._last_parameters[i] - tol * 10) < arr[i] < (self._last_parameters[i] + tol * 10):
return False
# check the total blocks in the solution is in the tolerance range compared to the total block in the saved
# parameters
temp = sum(self._last_parameters[self._max_aug_num: -1])
if not temp * (1 - tol) < sum(arr[self._max_aug_num: -1]) < temp * (1 + tol):
return False
return True

def maximize_objective(self, arr):
"""
an objective function that returns the irr given an augmentation table (gives negative value for invalid
augmentation table)
:param arr: a collection (array-like, dict, tensor, etc.) containing info for a candid
"""
candid = self.get_candid(arr)
logging.info(candid)
aug_table = candid[0]
if not self._first_aug_of_size[len(aug_table)]:
logging.info('\x1b[6;30;42m' + f'first aug of size {len(aug_table)}' + '\x1b[0m')
self._first_aug_of_size[len(aug_table)] = True
if self._use_memory and self._step % 100 != 0:
if candid in self._memory:
self._step += 1
self._results.append(self._memory[candid])
return self._memory[candid]
# if the optimization step is not a multiple of 100, try using derivatives instead of full simulation
# if the parameters are not within 10% of the parameters use for derivatives calculation, use the full
# simulation to calculate the value
if self._step % 100 != 0 and self._check_param_diff(arr, 0.15):
self._step += 1
self._derivatives_calcs += 1
result = self._last_result + np.dot(np.array(arr) - self._last_parameters, self._derivatives)
# logging.info(np.array(arr) - self._last_parameters, self._derivatives,
# np.dot(np.array(arr) - self._last_parameters, self._derivatives), result)
if self._use_memory:
self._results.append(result)
return result
try:
self._output.aug_table = aug_table
self._output.producer_factor = candid[-1] / 100
except ValueError:
self._step += 1
result = -100
else:
self._output.run()
self._financial_calculator.output_calculator = self._output
result = self._financial_calculator.get_irr()
if self._step % 100 == 0:
self._last_result = result
self._last_parameters = np.array(arr)
# calculate derivatives for each parameter
for i in range(len(arr)):
temp = np.array(arr)
# change the ith value slightly
if i < self._max_aug_num:
if i < self._output.num_of_years - 1:
temp[i] += 1
else:
temp[i] -= 1
elif i == len(arr) - 1:
if temp[i] == 0:
temp[i] = 5
else:
temp[i] *= 0.95
else:
temp[i] = max(temp[i] * 1.1, temp[i] + 10)
temp_candid = self.get_candid(temp)
# use memory if result is available
if self._use_memory and temp_candid in self._memory:
temp_result = self._memory[temp_candid]
else:
# calculate the irr with the changed parameters
self._output.set_aug_table(temp_candid[0], False)
self._output.producer_factor = temp_candid[-1] / 100
self._output.run()
self._financial_calculator.output_calculator = self._output
temp_result = self._financial_calculator.get_irr()
if self._use_memory:
self._memory[temp_candid] = temp_result
# calculate the directional derivatives of the function
self._derivatives[i] = (temp_result - result) / (temp[i] - arr[i])
if np.isnan(self._derivatives[i]):
self._derivatives[i] = 0

if self._use_memory:
self._results.append(result)
self._memory[candid] = result
self._step += 1
return result


def flatten_tuples(t):
for x in t:
if isinstance(x, tuple):
yield from flatten_tuples(x)
else:
yield x


if __name__ == '__main__':
# make info logging show
logging.getLogger().setLevel(logging.INFO)
# setup power system
storage = LithiumPowerStorage(25, 5000, use_default_aug=True)
producer = PvProducer(pv_output_file="test.csv", pv_peak_power=15000)
output = OutputCalculator(25, 5000, producer, storage, save_all_results=False)
test = FinancialCalculator(output, 100, transition_low_factor=1.04, transition_high_factor=3.91,
summer_low_factor=1.04, summer_high_factor=3.91)

# start optimization
start_time = time.time()
optimizer = NevergradOptimizer(test, budget=2000)
opt_output, res = optimizer.run()
# print results
print(optimizer.get_candid(opt_output), res)
print(optimizer._first_aug_of_size)
print(f"Optimization took {time.time() - start_time} seconds")
# print(f"Used {optimizer._derivatives_calcs} calculations with derivatives")
# # write results to file
# with open('c:/Users/user/Documents/solar optimization project/poc docs/optimization_results1.csv', 'w',
# newline='') \
# as csv_file:
# writer = csv.writer(csv_file)
# writer.writerow(["parameters", "result"])
# for k, v in optimizer._memory.items():
# writer.writerow([tuple(flatten_tuples(k)), v])
42 changes: 1 addition & 41 deletions optibess_algorithm/pv_output_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def get_pvgis_hourly(latitude: float, longitude: float, tilt: float = constants.
else:
raw_data = pvlib.iotools.get_pvgis_hourly(latitude, longitude, pvcalculation=True,
surface_tilt=0 if tech == Tech.TRACKER else tilt,
surface_azimuth=azimuth, peakpower=pv_peak, outputformat='csv',
surface_azimuth=azimuth + 180, peakpower=pv_peak, outputformat='csv',
trackingtype=1 if tech == Tech.TRACKER else 0, loss=losses,
url=PVGIS_URL)[0]
# convert data from W to kW
Expand All @@ -163,43 +163,3 @@ def get_pvgis_hourly(latitude: float, longitude: float, tilt: float = constants.
filtered_data = [raw_data[(raw_data.index.month == i + 1) & (raw_data.index.year == year)] for i, year in
enumerate(years)]
return pd.concat(filtered_data)


if __name__ == '__main__':
# start_time = time.time()
# raw_data1 = get_pvgis_hourly(30, 34, pv_peak=10000)
# print(raw_data1[raw_data1.index.month == 5])
raw_data1 = get_pvlib_output(latitude=31, longitude=35, number_of_inverters=1000, modules_per_string=29,
strings_per_inverter=20, use_bifacial=True,
module=pd.Series({
"Adjust": 16.05712,
"BIPV": 0,
"Bifacial": 1,
"I_L_ref": 5.175703,
"I_mp_ref": 16.87,
"I_o_ref": 1.15e-09,
"I_sc_ref": 17.68,
"PTC": 640.0,
"R_s": 0.316688,
"R_sh_ref": 287.1022,
"STC": 720.0,
"T_NOCT": 44.0,
"Technology": 2,
"V_mp_ref": 42.68,
"V_oc_ref": 50.74,
"a_ref": 1.981696,
"alpha_sc": 0.0004,
"area": 3.1,
"beta_oc": -0.0024,
"cell_number": 132,
"gamma_ref": -0.5072,
"id": 1,
"length": 2.384,
"manufacturer": "Huasun",
"name": "Huasun 720",
"owner_id": None,
"width": 1.303
}))
raw_data2 = get_pvlib_output(latitude=31, longitude=35, number_of_inverters=1000, modules_per_string=29,
strings_per_inverter=20)
print(sum(raw_data1) / 417600, sum(raw_data2) / 417600)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
name = "optibess_algorithm"
version = "0.1.0"
description = "Optibess Algorithm is a python 3.10+ library for simulating and optimizing a photovoltaic system with power storage."
authors = [{ name = "Elmor Renewable Energies Dev Team" }]
authors = [{ name = "El-mor Renewable Energies Dev Team" }]
readme = "README.rst"
license = {file = "LICENSE"}
keywords = ["solar", "PV", "optimization", "power storage", "BESS", ]
Expand Down
Loading

0 comments on commit 06d5a83

Please sign in to comment.