Skip to content
Snippets Groups Projects
Commit 141250aa authored by yifan.he's avatar yifan.he
Browse files

new components

parent 252b63ae
No related tags found
No related merge requests found
"""
MIT License
Copyright (c) 2023 RWTH Aachen University
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.
"""
from Model_Library.component.core import (
AbstractComponent,
ComponentCapacity,
ComponentKind,
ComponentCommodity,
ComponentLink,
ComponentPart,
)
from Model_Library.optimization_model import VariableKind
import pyomo.environ as pyo
class AssetLink(ComponentLink):
def __init__(self, asset, asset_var_name, start, var_name, end):
self.asset = asset
self.asset_var_name = asset_var_name
self.start = start
self.var_name = var_name
self.end = end
class MemberLink(ComponentLink):
def __init__(self, member, member_res_name, local_res_name, end):
self.member = member
self.member_res_name = member_res_name
self.local_res_name = local_res_name
self.end = end
class AssetAdapter(AbstractComponent):
def __init__(self, name, configuration, assets):
self.asset = assets[configuration["asset"]]
self.grid = self.asset._components[configuration["grid"]]
super().__init__(
name=name,
commodity_1=self.grid.commodity,
commodity_2=None,
commodity_3=self.grid.commodity,
commodity_4=None,
configuration=configuration,
capacity=ComponentCapacity.NONE,
)
self.commodity = self.grid.commodity
def match(self, kind=ComponentKind.ALL, commodity=ComponentCommodity.ALL):
match_kind = kind == ComponentKind.ALL or kind == ComponentKind.ASSET_ADAPTER
match_commodity = (
commodity == ComponentCommodity.ALL
or commodity == self.commodity
or (isinstance(commodity, list) and self.commodity in commodity)
)
return match_kind and match_commodity
def iter_component_parts(self):
yield ComponentPart.NONE_STATE
def iter_links(self):
yield AssetLink(
self.asset,
self.grid.name + ".output_1",
(self.grid.name, ComponentPart.NONE_STATE),
self.name + ".into_asset",
(self.name, ComponentPart.NONE_STATE),
)
yield AssetLink(
self.asset,
self.grid.name + ".input_1",
(self.grid.name, ComponentPart.NONE_STATE),
self.name + ".from_asset",
(self.name, ComponentPart.NONE_STATE),
)
def non_state_base_variable_names(self):
return [
(self.name + ".input_1", VariableKind.INDEXED),
(self.name + ".output_1", VariableKind.INDEXED),
]
def add_non_state_variables(self, o_block):
input = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".input_1", input)
output = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".output_1", output)
def add_non_state_model(self, d_block, o_block):
input = o_block.component_dict[self.name + ".input_1"]
output = o_block.component_dict[self.name + ".output_1"]
into_asset = o_block.component_dict[self.name + ".into_asset"]
from_asset = o_block.component_dict[self.name + ".from_asset"]
def rule(m, t):
return input[t] == into_asset[t]
o_block.add(self.name + ".input_cons", pyo.Constraint(o_block.T, rule=rule))
def rule(m, t):
return output[t] == from_asset[t]
o_block.add(self.name + ".output_cons", pyo.Constraint(o_block.T, rule=rule))
class MemberAdapter(AbstractComponent):
def __init__(self, name, configuration, members):
self.member = members[configuration["member"]]
self.grid = self.member._components[configuration["grid"]]
super().__init__(
name=name,
commodity_1=self.grid.commodity,
commodity_2=None,
commodity_3=self.grid.commodity,
commodity_4=None,
configuration=configuration,
capacity=ComponentCapacity.NONE,
)
self.commodity = self.grid.commodity
def match(self, kind=ComponentKind.ALL, commodity=ComponentCommodity.ALL):
match_kind = kind == ComponentKind.ALL or kind == ComponentKind.ASSET_ADAPTER
match_commodity = (
commodity == ComponentCommodity.ALL
or commodity == self.commodity
or (isinstance(commodity, list) and self.commodity in commodity)
)
return match_kind and match_commodity
def iter_component_parts(self):
yield ComponentPart.NONE_STATE
def iter_links(self):
yield MemberLink(
self.member,
self.grid.name + ".output_1",
"into_member",
(self.name, ComponentPart.NONE_STATE),
)
yield MemberLink(
self.member,
self.grid.name + ".input_1",
"from_member",
(self.name, ComponentPart.NONE_STATE),
)
def non_state_base_variable_names(self):
return [
(self.name + ".input_1", VariableKind.INDEXED),
(self.name + ".output_1", VariableKind.INDEXED),
]
def add_non_state_variables(self, o_block):
input = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".input_1", input)
output = pyo.Var(o_block.T, bounds=(0, None))
o_block.add(self.name + ".output_1", output)
def add_non_state_model(self, d_block, o_block):
input = o_block.component_dict[self.name + ".input_1"]
output = o_block.component_dict[self.name + ".output_1"]
into_member = self.into_member.resample(o_block.dynamic).values
from_member = self.from_member.resample(o_block.dynamic).values
def rule(m, t):
return input[t] == into_member[t]
o_block.add(self.name + ".input_cons", pyo.Constraint(o_block.T, rule=rule))
def rule(m, t):
return output[t] == from_member[t]
o_block.add(self.name + ".output_cons", pyo.Constraint(o_block.T, rule=rule))
This diff is collapsed.
from Model_Library.component.core import (
BaseBusBar,
BaseComponent,
BaseConsumption,
BaseGeneration,
BaseGrid,
BaseStorage,
ComponentCommodity,
)
from Model_Library.optimization_model import VariableKind
import pyomo.environ as pyo
class ElectricalBusBar(BaseBusBar):
def __init__(self, name, configuration):
super().__init__(name=name, commodity=ComponentCommodity.ELECTRICITY)
class BiPowerElectronic(BaseComponent):
def __init__(self, name, configuration):
super().__init__(
name=name,
commodity_1=ComponentCommodity.ELECTRICITY,
commodity_2=ComponentCommodity.ELECTRICITY,
commodity_3=ComponentCommodity.ELECTRICITY,
commodity_4=ComponentCommodity.ELECTRICITY,
configuration=configuration,
)
def _load_model(self, configuration, model):
configuration["efficiency_a"] = model["efficiency_a"]
configuration["efficiency_b"] = model["efficiency_b"]
configuration["rated_power_side_a"] = model["rated_power_side_a"]
configuration["rated_power_side_b"] = model["rated_power_side_b"]
def _load_conversions(self, configuration):
efficiency_a = configuration["efficiency_a"] # TODO move comment to docu 1 -> 2
efficiency_b = configuration["efficiency_b"] # TODO move comment to docu 2 -> 1
if configuration["rated_power_side_a"] == "output":
configuration["indep_var_1"] = "output_2"
configuration["conversion_1"] = ("output_2", "input_1", 1.0 / efficiency_a)
else:
configuration["indep_var_1"] = "input_1"
configuration["conversion_1"] = ("input_1", "output_2", efficiency_a)
if configuration["rated_power_side_b"] == "output":
configuration["indep_var_2"] = "output_1"
configuration["conversion_2"] = ("output_1", "input_2", 1.0 / efficiency_b)
else:
configuration["indep_var_2"] = "input_2"
configuration["conversion_2"] = ("input_2", "output_1", efficiency_b)
class ElectricalConsumption(BaseConsumption):
def __init__(self, name, configuration):
super().__init__(
name=name,
commodity=ComponentCommodity.ELECTRICITY,
configuration=configuration,
)
class ElectricalGrid(BaseGrid):
def __init__(self, name, configuration):
super().__init__(
name=name,
commodity=ComponentCommodity.ELECTRICITY,
configuration=configuration,
)
class Battery(BaseStorage):
def __init__(self, name, configuration):
super().__init__(
name=name,
commodity=ComponentCommodity.ELECTRICITY,
configuration=configuration,
)
...@@ -193,7 +193,12 @@ class Battery(BaseStorage): ...@@ -193,7 +193,12 @@ class Battery(BaseStorage):
class EVBattery(BaseStorage): class EVBattery(BaseStorage):
def __init__(self, name, configuration): def __init__(self, name, configuration):
super().__init__(name=name, commodity=ComponentCommodity.ELECTRICITY, configuration=configuration) super().__init__(
name=name,
commodity=ComponentCommodity.ELECTRICITY,
configuration=configuration
)
self.connected_profile = configuration.get("connected_profile", None)
def add_state_model(self, d_block, s_block): def add_state_model(self, d_block, s_block):
super().add_state_model(d_block, s_block) super().add_state_model(d_block, s_block)
...@@ -202,11 +207,12 @@ class EVBattery(BaseStorage): ...@@ -202,11 +207,12 @@ class EVBattery(BaseStorage):
energy = s_block.component_dict[self.name + ".energy"] energy = s_block.component_dict[self.name + ".energy"]
T = s_block.T T = s_block.T
# === SOC 惩罚项 ===
soc_ranges = [ soc_ranges = [
("low_heavy", 0.3, 0.1, "below"), # <30% ("low_heavy", 0.2, 0.005, "below"),
("low_light", 0.5, 0.03, "below"), # 30-50% ("low_light", 0.5, 0.001, "below"),
("high_light", 0.7, 0.03, "above"), # 70-80% ("high_light", 0.7, 0.001, "above"),
("high_heavy", 0.8, 0.1, "above"), # >80% ("high_heavy", 0.9, 0.005, "above"),
] ]
for name, threshold, factor, direction in soc_ranges: for name, threshold, factor, direction in soc_ranges:
...@@ -223,10 +229,27 @@ class EVBattery(BaseStorage): ...@@ -223,10 +229,27 @@ class EVBattery(BaseStorage):
return slack[t] >= energy[t] - thr * capacity return slack[t] >= energy[t] - thr * capacity
s_block.add(con_name, pyo.Constraint(T, rule=rule)) s_block.add(con_name, pyo.Constraint(T, rule=rule))
obj_expr = pyo.quicksum(factor * slack[t] * s_block.step_size(t) for t in T) obj_expr = pyo.quicksum(factor * slack[t] * s_block.step_size(t) for t in T)
s_block.add_general_scaled_objective(obj_expr) s_block.add_general_scaled_objective(obj_expr)
if self.connected_profile is not None:
connected = self.connected_profile.resample(s_block.dynamic).values
lookback_steps = 8
for t in T:
if t > 0 and connected[t - 1] == 1 and connected[t] == 0:
recently_drove = (connected[max(0, t - lookback_steps):t] == 0).any()
if not recently_drove:
s_block.add(
f"{self.name}.soc_departure_strict_{t}",
pyo.Constraint(expr=energy[t] >= 0.7 * capacity)
)
else:
print(f"⚠️ {self.name}: connected_profile NOT SET. No departure SOC constraints added.")
class ElectricalPassThrough(BaseComponent): class ElectricalPassThrough(BaseComponent):
def __init__(self, name, configuration): def __init__(self, name, configuration):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment