Commit 0e680866 authored by Anjula Antonis's avatar Anjula Antonis
Browse files

Added old Anjula examples.

parent ec43e098
Pipeline #321026 passed with stages
in 1 minute and 52 seconds
import numpy as np
from pycity_scheduling.classes import *
from pycity_scheduling.algorithms import algorithms
t = Timer(op_horizon=2)
p = Prices(t)
w = Weather(t)
e = Environment(t, w, p)
cd = CityDistrict(e, objective='peak-shaving')
bd1 = Building(e, objective='peak-shaving')
cd.addEntity(bd1, [0, 0])
bes = BuildingEnergySystem(e)
bd1.addEntity(bes)
tes = ThermalEnergyStorage(e, 20, 0.5, 0.5)
bes.addDevice(tes)
bat=Battery(e,2,2)
bes.addDevice(bat)
pv=Photovoltaic(e,10,70)
bes.addDevice(pv)
eh = ElectricalHeater(e, 20)
bes.addDevice(eh)
ap = Apartment(e)
bd1.addEntity(ap)
load = np.array([10, 10])
fi = FixedLoad(e, method=0, demand=load)
ap.addEntity(fi)
sh = SpaceHeating(e, method=0, loadcurve=load)
ap.addEntity(sh)
bd2 = Building(e, objective='price')
cd.addEntity(bd2, [0, 0])
bes = BuildingEnergySystem(e)
bd2.addEntity(bes)
tes = ThermalEnergyStorage(e, 20, 0.5, 0.5)
bes.addDevice(tes)
eh = ElectricalHeater(e, 20)
bes.addDevice(eh)
ap = Apartment(e)
bd2.addEntity(ap)
load = np.array([10, 10])
fi = FixedLoad(e, method=0, demand=load)
ap.addEntity(fi)
sh = SpaceHeating(e, method=0, loadcurve=load)
ap.addEntity(sh)
bd3 = Building(e, objective='peak-shaving')
cd.addEntity(bd3, [0, 0])
bes = BuildingEnergySystem(e)
bd3.addEntity(bes)
tes = ThermalEnergyStorage(e, 20, 1, 1)
bes.addDevice(tes)
eh = ElectricalHeater(e, 30)
bes.addDevice(eh)
ap = Apartment(e)
bd3.addEntity(ap)
load = np.array([10, 10])
fi = FixedLoad(e, method=0, demand=load)
ap.addEntity(fi)
sh = SpaceHeating(e, method=0, loadcurve=load)
ap.addEntity(sh)
f = algorithms['exchange-admm']
r = f(cd, rho=2, eps_primal=0.001)
print(r[0])
print(r[1])
print(len(r[1]))
print(r[2])
print(len(r[2]))
print(r[3])
import numpy as np
from pycity_scheduling.classes import *
from pycity_scheduling.algorithms import algorithms
t = Timer(op_horizon=14, step_size=3600, initial_time=(0, 0, 0))
p = Prices(t)
w = Weather(t)
e = Environment(t, w, p)
cd = CityDistrict(e, objective='peak-shaving')
bd1 = Building(e, objective='peak-shaving')
cd.addEntity(bd1, [0,0])
bes1 = BuildingEnergySystem(e)
bd1.addEntity(bes1)
tes1 = ThermalEnergyStorage(e, 40, 0.5, 0.5)
bes1.addDevice(tes1)
eh1 = ElectricalHeater(e, 20)
bes1.addDevice(eh1)
bat = Battery(e,50,10,5)
bes1.addDevice(bat)
pv1 = Photovoltaic(e,1000,0.12)
bes1.addDevice(pv1)
ap1 = Apartment(e)
bd1.addEntity(ap1)
#load = np.array([10, 10, 15, 12, 9, 8, 11, 13, 17, 10, 14, 16, 12, 14, 16, 10, 11, 9, 14, 15, 17, 18, 9, 10])
load = np.array([10, 10, 15, 12, 9, 8, 11, 13, 17, 10, 14, 16, 12, 14])
#fi = FixedLoad(environment=e, method=1, annualDemand=3000.0, profileType="H0")
fi1 = FixedLoad(e, method=0, demand=load)
ap1.addEntity(fi1)
sh1 = SpaceHeating(e, method=0, loadcurve=load)
ap1.addEntity(sh1)
bd2 = Building(e, objective='price')
cd.addEntity(bd2, [0, 0])
bes2 = BuildingEnergySystem(e)
bd2.addEntity(bes2)
tes2 = ThermalEnergyStorage(e, 40, 0.5, 0.5)
bes2.addDevice(tes2)
eh2 = ElectricalHeater(e, 20)
bes2.addDevice(eh2)
ap2 = Apartment(e)
bd2.addEntity(ap2)
load2 = np.array([20, 20,10,15])
fi2 = FixedLoad(e, method=0, demand=load)
ap2.addEntity(fi2)
sh2 = SpaceHeating(e, method=0, loadcurve=load)
ap2.addEntity(sh2)
f = algorithms['exchange-admm']
r = f(cd, rho=2, eps_primal=0.001)
"""
print("\nTotal amount of buildings in city district: " + str(len(cd.nodes.items())) + "\n")
print("" + str(cd) + ":")
for building in cd.get_lower_entities():
print("\t--> " + str(building) + ":")
for bes in building.get_lower_entities():
print("\t\t--> " + str(bes) + ":")
for entity in bes.get_lower_entities():
print("\t\t\t--> " + str(entity) + "")
print("")
"""
# Algorithm specific outputs:
#print(r[0])
#print(r[1])
#print(r[2])
#print(r[3])
# Scheduling outputs:
print(list(cd.P_El_Schedule))
print(list(pv1.P_El_Schedule))
print(list(bat.P_El_Schedule))
print(list(eh1.P_El_Schedule))
print(list(fi1.P_El_Schedule))
print(list(eh1.P_El_Schedule+fi1.P_El_Schedule+bat.P_El_Schedule+pv1.P_El_Schedule+eh2.P_El_Schedule+fi2.P_El_Schedule))
#print(tes.E_Th_Schedule) #added by me
#print(tes.P_Th_Schedule)
#print(tes.P_Th_Schedule+eh.P_El_Schedule)
#print(sh.P_Th_Demand) #added by me
#print(pv.P_El_Schedule)
#for t in range(1,4):
# print(tes1.E_Th_Schedule(t+1) )
# print(tes1.E_Th_Schedule(t) + tes1.P_Th_Schedule(t) - tes1.kLosses(t)
\ No newline at end of file
import copy
import matplotlib.pyplot as plt
from matplotlib import gridspec
import pycity_scheduling.util.factory as factory
from pycity_scheduling.classes import *
from pycity_scheduling.algorithms import *
import numpy as np
env = factory.generate_standard_environment(step_size=3600, op_horizon=24, mpc_horizon=None, mpc_step_width=None,
initial_date=(2018, 2, 10), initial_time=(0, 0, 0))
cd = CityDistrict(environment=env, objective='peak-shaving')
bd1 = Building(environment=env, objective='peak-shaving')
cd.addEntity(entity=bd1, position=[0, 0])
ap1 = Apartment(environment=env)
bd1.addEntity(ap1)
sh1 = SpaceHeating(environment=env, method=1, livingArea=160, specificDemand=95.9, profile_type='HEF')
#load = np.array([10, 10, 15, 12, 9, 8, 11, 13, 17, 10, 14, 16, 12, 14, 16, 10, 11, 9, 14, 15, 17, 18, 9, 10])
#sh1 = SpaceHeating(env, method=0, loadcurve=load)
ap1.addEntity(sh1)
bes1 = BuildingEnergySystem(environment=env)
bd1.addEntity(bes1)
tes1 = ThermalEnergyStorage(environment=env, E_Th_max=40.0, soc_init=0.5)
bes1.addDevice(tes1)
hp1 = HeatPump(environment=env, P_Th_nom=8.0)
bes1.addDevice(hp1)
pv1 = Photovoltaic(environment=env, area=60.0, eta=0.18)
bes1.addDevice(pv1)
fi = FixedLoad(environment=env, method=1, annualDemand=3000.0, profileType="H0")
#fi = FixedLoad(env, method=0, demand=load)
ap1.addEntity(fi)
eh1 = ElectricalHeater(environment=env, P_Th_nom=2.0)
bes1.addDevice(eh1)
#bd2 = copy.deepcopy(bd1)
#cd.addEntity(entity=bd2, position=[0, 1])
central_optimization(cd)
# Plot PV power
plot_time = list(range(env.timer.timestepsUsedHorizon))
figure = plt.figure(figsize=(6, 6))
gs = gridspec.GridSpec(5, 1)
ax0 = plt.subplot(gs[0])
ax0.plot(plot_time, cd.P_El_Schedule)
plt.grid()
plt.ylabel("City District [kW]")
plt.xlim((0, env.timer.timestepsUsedHorizon - 1))
plt.ylim((-3, 3))
plt.title("Schedules")
ax1 = plt.subplot(gs[1], sharex=ax0)
ax1.plot(plot_time, pv1.P_El_Schedule)
plt.grid()
plt.ylabel("PV [kW]")
ax2 = plt.subplot(gs[2], sharex=ax0)
ax2.plot(plot_time, hp1.P_El_Schedule)
plt.grid()
plt.ylabel("Heat Pump [kW]")
ax2 = plt.subplot(gs[3], sharex=ax0)
ax2.plot(plot_time, fi.P_El_Schedule)
plt.grid()
plt.ylabel("fixed load [kW]")
ax3 = plt.subplot(gs[4], sharex=ax0)
ax3.plot(plot_time, tes1.P_Th_Schedule)
plt.grid()
plt.xlabel("Time", fontsize=12)
plt.ylabel("Thermal Energy Storage [kWh]")
plt.ylim((0, 40))
plt.show()
import matplotlib.pyplot as plt
from matplotlib import gridspec
import pycity_scheduling.util.factory as factory
from pycity_scheduling.classes import *
from pycity_scheduling.algorithms import *
# Variable whether to have a backup heater or not:
BACKUP_HEATER = True
env = factory.generate_standard_environment(step_size=900, op_horizon=96, mpc_horizon=None, mpc_step_width=None,
initial_date=(2010, 2, 10), initial_time=(0, 0, 0))
cd = CityDistrict(environment=env, objective='peak-shaving')
bd1 = Building(environment=env, objective='peak-shaving')
cd.addEntity(entity=bd1, position=[0, 0])
ap1 = Apartment(environment=env)
bd1.addEntity(ap1)
sh1 = SpaceHeating(environment=env, method=1, livingArea=160, specificDemand=95.9, profile_type='HEF')
ap1.addEntity(sh1)
bes1 = BuildingEnergySystem(environment=env)
bd1.addEntity(bes1)
tes1 = ThermalEnergyStorage(environment=env, E_Th_max=10.0, soc_init=0.5)
bes1.addDevice(tes1)
hp1 = HeatPump(environment=env, P_Th_nom=8.0)
bes1.addDevice(hp1)
pv1 = Photovoltaic(environment=env, area=60.0, eta=0.18)
bes1.addDevice(pv1)
# New battery:
bat1 = Battery(environment=env, E_El_max=2.0, P_El_max_charge=10.0)
bes1.addDevice(bat1)
if BACKUP_HEATER:
eh1 = ElectricalHeater(environment=env, P_Th_nom=2.0)
bes1.addDevice(eh1)
#central_optimization(cd, mode='convex') # Default!
central_optimization(cd, mode='integer')
# Plot PV power
plot_time = list(range(env.timer.timestepsUsedHorizon))
figure = plt.figure(figsize=(6, 6))
gs = gridspec.GridSpec(6, 1)
ax0 = plt.subplot(gs[0])
ax0.plot(plot_time, cd.P_El_Schedule)
plt.grid()
plt.ylabel("City District [kW]")
plt.xlim((0, env.timer.timestepsUsedHorizon - 1))
plt.ylim((-3, 3))
plt.title("Schedules")
ax1 = plt.subplot(gs[1], sharex=ax0)
ax1.plot(plot_time, pv1.P_El_Schedule)
plt.grid()
plt.ylabel("PV [kW]")
ax2 = plt.subplot(gs[2], sharex=ax0)
ax2.plot(plot_time, hp1.P_El_Schedule)
plt.grid()
plt.ylabel("Heat Pump [kW]")
"""
ax2 = plt.subplot(gs[3], sharex=ax0)
ax2.plot(plot_time, bat1.P_El_Schedule)
plt.grid()
plt.ylabel("Battery [kW]")
"""
ax2 = plt.subplot(gs[4], sharex=ax0)
ax2.plot(plot_time, eh1.P_El_Schedule)
plt.grid()
plt.ylabel("electric heater [kW]")
ax3 = plt.subplot(gs[5], sharex=ax0)
ax3.plot(plot_time, tes1.E_Th_Schedule)
plt.grid()
plt.xlabel("Time", fontsize=12)
plt.ylabel("Thermal Energy Storage [kWh]")
plt.ylim((0, 40))
plt.show()
\ No newline at end of file
import pycity_scheduling.util.factory as factory
env = factory.generate_standard_environment()
num_sfh = 20
sfh_distribution = {
'SFH.2002': 0.5,
'SFH.2010': 0.3,
'SFH.2016': 0.2,
}
sfh_heating_distribution = {
'HP': 0.5,
'BL': 0.1,
'EH': 0.4,
}
sfh_device_probs = {
'FL': 1,
'DL': 0.2,
'EV': 0.3,
'BAT': 0.5,
'PV': 0.8,
}
num_mfh = 5
mfh_distribution = {
'MFH.2002': 0.6,
'MFH.2010': 0.2,
'MFH.2016': 0.2,
}
mfh_heating_distribution = {
'HP': 0.4,
'BL': 0.2,
'EH': 0.4,
}
mfh_device_probs = {
'FL': 1,
'DL': 0.2,
'EV': 0.2,
'BAT': 0.4,
'PV': 0.8,
}
district = factory.generate_tabula_district(env, num_sfh, num_mfh,
sfh_distribution,
sfh_heating_distribution,
sfh_device_probs,
mfh_distribution,
mfh_heating_distribution,
mfh_device_probs)
#for nid, node in district.nodes.items():
# print(nid, node)
print("\nTotal amount of buildings in city district: " + str(len(district.nodes.items())) + "\n")
print("" + str(district) + ":")
for building in district.get_lower_entities():
print("\t--> " + str(building) + ":")
for bes in building.get_lower_entities():
print("\t\t--> " + str(bes) + ":")
for entity in bes.get_lower_entities():
print("\t\t\t--> " + str(entity) + "")
import pycity_scheduling.util.factory as factory
env = factory.generate_standard_environment()
num_sfh = 30
sfh_distribution = {
'SFH.1995': 0.2,
'SFH.2002': 0.3,
'SFH.2010': 0.3,
'SFH.2016': 0.2,
}
sfh_heating_distribution = {
'HP': 0.5,
'BL': 0.1,
'EH': 0.4,
}
sfh_device_probs = {
'FL': 1,
'DL': 0.2,
'EV': 0.3,
'BAT': 0.5,
'PV': 0.8,
}
num_mfh = 20
mfh_distribution = {
'MFH.2002': 0.5,
'MFH.2010': 0.3,
'MFH.2016': 0.2,
}
mfh_heating_distribution = {
'HP': 0.4,
'BL': 0.2,
'EH': 0.4,
}
mfh_device_probs = {
'FL': 1,
'DL': 0.2,
'EV': 0.2,
'BAT': 0.4,
'PV': 0.8,
}
district = factory.generate_tabula_district(env, num_sfh, num_mfh,
sfh_distribution,
sfh_heating_distribution,
sfh_device_probs,
mfh_distribution,
mfh_heating_distribution,
mfh_device_probs)
#for nid, node in district.nodes.items():
# print(nid, node)
print("\nTotal amount of buildings in city district: " + str(len(district.nodes.items())) + "\n")
print("" + str(district) + ":")
for building in district.get_lower_entities():
print("\t--> " + str(building) + ":")
for bes in building.get_lower_entities():
print("\t\t--> " + str(bes) + ":")
for entity in bes.get_lower_entities():
print("\t\t\t--> " + str(entity) + "")
# Robust Example No. 1
In contrast to the unoptimized case (stand-alone) both the price as well as
the peaks in the schedule are reduced in the optimized case. This is due to the
price and peak-shaving objectives of the building and the city district.
import numpy as np
import pycity_scheduling.classes as classes
import pycity_scheduling.algorithms as algs
from pycity_scheduling.util import factory
from pycity_scheduling.util.metric import calculate_costs, peak_to_average_ratio, peak_reduction_ratio
env = factory.generate_standard_environment(step_size=900, op_horizon=6)
# Make it attractive for the client to shift demand into first half of
# scheduling period
env.prices.tou_prices = np.array([10]*3 + [20]*3)
# Aggregator objective is peak-shaving
district = classes.CityDistrict(env, objective='peak-shaving')
# Fixed load of constant 10 kW, space heating with constant 10 kW load, thermal
# storage with 20 kWh, electric heater with 20 kW
# The building gets assigned a price objective automatically
bd = factory.generate_simple_building(env, fl=10, sh=10, tes=20, eh=20)
district.addEntity(bd, (0, 0))
# Pseudo scheduling where each device is scheduled on its own
algs.stand_alone_optimization(district)
# Results are now in the _Ref schedules
bd.save_ref_schedule()
# Normal scheduling with aggregator and client objectives
algs.central_optimization(district)
np.set_printoptions(formatter={'float': '{: >8.3f}'.format})
print('Comparing stand-alone with optimized case:')
print('Building P_El:')
print(bd.P_El_Ref_Schedule)
print(bd.P_El_Schedule)
print('Optimized costs: {: >8.3f}'.format(calculate_costs(bd)))
bd.load_schedule("Ref")
print('Stand-alone costs: {: >8.3f}'
.format(calculate_costs(bd)))
bd.load_schedule("default")
print('Optimized PAR: {: >8.3f}'.format(peak_to_average_ratio(bd)))
bd.load_schedule("Ref")
print('Stand-alone PAR: {: >8.3f}'
.format(peak_to_average_ratio(bd)))
bd.load_schedule("default")
print('PRR: {: >8.3f}'.format(peak_reduction_ratio(bd, "Ref")))
# Robust Example No. 1
When robust optimization (RO) is used (lower rows) the storage cannot fully be
utilized. This is best seen in time step 3 where the SoC is lower than without
robust scheduling.
Naturally, the schedule becomes less optimal with RO. This is shown by the
higher costs for the robust schedule. You can also see how the building can
completely satisfy its demand during the times of low prices, when no RO is
used. With RO however, the building has to shift some demand into the times of
high prices.
**Note:** We compare two schedules from different schedulings with each other.
import numpy as np
import pycity_scheduling.classes as classes
from pycity_scheduling.algorithms import central_optimization
from pycity_scheduling.util import factory
from pycity_scheduling.util.metric import calculate_costs
env = factory.generate_standard_environment(step_size=900, op_horizon=6)
# Make it attractive to shift demand into first half of scheduling period
env.prices.tou_prices = np.array([6]*3 + [10]*3)
district = classes.CityDistrict(env)
# Space heating with constant 10 kW load, thermal storage with 20 kWh,
# electric heater with 20 kW
bd = factory.generate_simple_building(env, sh=10, tes=20, eh=20)
district.addEntity(bd, (0, 0))
# Scheduling without RO
central_optimization(district)
# Results without RO are now in the _Ref schedules
bd.save_ref_schedule()
# Protect 6 time steps and assume a deviation of 50% in each time step
# Such a high deviation is unrealistic but makes for a good example.
central_optimization(district, robustness=(6, 0.5))
np.set_printoptions(formatter={'float': '{: >8.3f}'.format})
print('Building P_El:')
print(bd.P_El_Ref_Schedule)
print(bd.P_El_Schedule)
print('ThermalEnergyStorage E_Th:')
print(bd.bes.tes.E_Th_Ref_Schedule)
print(bd.bes.tes.E_Th_Schedule)
print('ThermalEnergyStorage Limits:')
print(list(bd.model.lower_robustness_bounds[:].value))
print(list(bd.model.upper_robustness_bounds[:].value))
print('ElectricHeater P_Th:')
print(bd.bes.electricalHeater.P_Th_Ref_Schedule)
print(bd.bes.electricalHeater.P_Th_Schedule)
print('SpaceHeating P_Th:')
print(bd.apartments[0].Th_Demand_list[0].P_Th_Ref_Schedule)
print(bd.apartments[0].Th_Demand_list[0].P_Th_Schedule)
print('Costs:')
bd.load_schedule("Ref")
print('{:.2f}'.format(calculate_costs(bd)))
bd.load_schedule("default")
print('{:.2f}'.format(calculate_costs(bd)))
......@@ -103,6 +103,10 @@ class Building(EntityContainer, bd.Building):
return 0 == model.P_Th_vars[t]
m.P_equality_constr = pyomo.Constraint(m.t, rule=p_equality_rule)
if mode == "integer":
pass
# Your code here!
if robustness is not None and self.bes.has_tes:
self._create_robust_constraints()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment