Skip to content

Commit

Permalink
Run Black
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusHolly committed Dec 17, 2024
1 parent fd3e227 commit 796134c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
12 changes: 6 additions & 6 deletions idaes/apps/grid_integration/multiperiod/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def generate_daily_data(raw_data: list, horizon_length: int):
)

daily_data = {
j: raw_data[((j - 1) * horizon_length):(j * horizon_length)]
j: raw_data[((j - 1) * horizon_length) : (j * horizon_length)]
for j in range(1, (len(raw_data) // horizon_length) + 1)
}

Expand All @@ -63,7 +63,9 @@ def generate_daily_data(raw_data: list, horizon_length: int):
return pd.DataFrame(daily_data).transpose()


def cluster_lmp_data(raw_data: list, n_clusters: int, horizon_length: int, seed: int = 42):
def cluster_lmp_data(
raw_data: list, n_clusters: int, horizon_length: int, seed: int = 42
):
"""
Clusters the given price signal into n_clusters using the k-means clustering
technique.
Expand All @@ -88,7 +90,7 @@ def cluster_lmp_data(raw_data: list, n_clusters: int, horizon_length: int, seed:
{1: {1: 4, 2: 3, 3: 5},
2: {1: 1, 2: 7, 3: 3}}
weights: dict
A dictionary of weights for each representative day, indexed the
Expand All @@ -111,9 +113,7 @@ def cluster_lmp_data(raw_data: list, n_clusters: int, horizon_length: int, seed:
d + 1: {t + 1: centroids[d, t] for t in range(num_time_periods)}
for d in range(num_days)
}
weights = {
d + 1: sum(labels == d) for d in range(num_days)
}
weights = {d + 1: sum(labels == d) for d in range(num_days)}

return lmp_data_clusters, weights

Expand Down
55 changes: 27 additions & 28 deletions idaes/apps/grid_integration/multiperiod/price_taker_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,11 @@ def _get_operation_blocks(
"""
Returns a dictionary of operational blocks named 'blk_name'.
In addition, it also checks the existence of the operational
blocks, and the existence of specified attributes.
blocks, and the existence of specified attributes.
"""
# pylint: disable=not-an-iterable
op_blocks = {
d: {
t: self.period[d, t].find_component(blk_name)
for t in self.set_time
}
d: {t: self.period[d, t].find_component(blk_name) for t in self.set_time}
for d in self.set_days
}

Expand All @@ -303,9 +300,7 @@ def _get_operation_blocks(

# First, check for the existence of the operational block
if blk is None:
raise AttributeError(
f"Operational block {blk_name} is not found."
)
raise AttributeError(f"Operational block {blk_name} is not found.")

# Next, check for the existence of attributes.
for attribute in attribute_list:
Expand All @@ -327,7 +322,7 @@ def add_capacity_limits(
"""
Adds capacity limit constraints of the form:
op_range_lb * capacity * op_mode(t) <= commodity(t) <= capacity * op_mode(t)
ex: 0.3 * P_max * op_mode(t) <= P(t) <= P_max * op_mode(t),
ex: 0.3 * P_max * op_mode(t) <= P(t) <= P_max * op_mode(t),
where P(t) is power at time t and op_mode(t) = 1 if the system is operating
at time t; and op_mode(t) = 0, otherwise.
Expand Down Expand Up @@ -395,9 +390,9 @@ def add_ramping_limits(
"""
Adds ramping constraints of the form:
ramping_var[t] - ramping_var[t-1] <=
ramping_var[t] - ramping_var[t-1] <=
startup_rate * capacity * startup[t] + rampup_rate * capacity * op_mode[t-1]
ramping_var[t-1] - ramping_var[t] <=
shutdown_rate * capacity * shutdown[t] + rampdown_rate * capacity * op_mode[t]
Expand Down Expand Up @@ -444,7 +439,7 @@ def add_ramping_limits(

op_blocks = self._get_operation_blocks(
blk_name=op_block_name,
attribute_list=["op_mode", "startup", "shutdown", ramping_var_name]
attribute_list=["op_mode", "startup", "shutdown", ramping_var_name],
)

# Creating the pyomo block
Expand Down Expand Up @@ -493,7 +488,7 @@ def add_startup_shutdown(
up_time: int, default=1,
Total uptime (must be >= 1), e.g., 4 time periods
Uptime must include the minimum uptime and the time required
for shutdown.
for shutdown.
down_time: int, default=1,
Total downtime (must be >= 1), e.g., 4 time periods
Expand All @@ -505,7 +500,8 @@ def add_startup_shutdown(
self.config.down_time = down_time

op_blocks = self._get_operation_blocks(
blk_name=op_block_name, attribute_list=["op_mode", "startup", "shutdown"],
blk_name=op_block_name,
attribute_list=["op_mode", "startup", "shutdown"],
)
if des_block_name is not None:
install_unit = self.find_component(des_block_name + ".install_unit")
Expand Down Expand Up @@ -565,8 +561,8 @@ def add_hourly_cashflows(
revenue_streams: List of strings representing the names of the
revenue streams, default: None
Example: ::
Example: ::
['elec_revenue', 'H2_revenue', ]
costs: List of strings representing the names of the
Expand Down Expand Up @@ -627,7 +623,8 @@ def add_hourly_cashflows(

# Set net cash inflow expression
self.period[p].net_hourly_cash_inflow = Expression(
expr=self.period[p].total_hourly_revenue - self.period[p].total_hourly_cost
expr=self.period[p].total_hourly_revenue
- self.period[p].total_hourly_cost
)

# Logger info for where constraint is located on the model
Expand Down Expand Up @@ -665,13 +662,13 @@ def add_overall_cashflows(
Annualization factor
cash_inflow_scale_factor: float, default=1.0,
Scaling factor for the sum of net hourly cashflows.
If the specified price signal is for the full year, then the
Scaling factor for the sum of net hourly cashflows.
If the specified price signal is for the full year, then the
value of this argument must be 1.0. However, if the specified
price signal is for a short period, say 1 month, then an
price signal is for a short period, say 1 month, then an
appropriate scaling factor can be specified to compute the value
for a year (12, for the case of 1 month's price signal).
"""

# Domain validation of input arguments
Expand Down Expand Up @@ -721,7 +718,8 @@ def add_overall_cashflows(
cf.net_cash_inflow = Var(doc="Net cash inflow")
cf.net_cash_inflow_calculation = Constraint(
expr=cf.net_cash_inflow
== cash_inflow_scale_factor * sum(
== cash_inflow_scale_factor
* sum(
self.WEIGHTS[d] * self.period[d, t].net_hourly_cash_inflow
for d, t in self.period
)
Expand All @@ -730,8 +728,7 @@ def add_overall_cashflows(
cf.corporate_tax = Var(within=NonNegativeReals, doc="Corporate tax")
cf.corporate_tax_calculation = Constraint(
expr=cf.corporate_tax
>= corporate_tax_rate
* (cf.net_cash_inflow - self.fom - self.depreciation)
>= corporate_tax_rate * (cf.net_cash_inflow - self.fom - self.depreciation)
)

cf.net_profit = Var(doc="Net profit after taxes")
Expand All @@ -741,7 +738,9 @@ def add_overall_cashflows(

if annualization_factor is None:
# If the annualization factor is specified
annualization_factor = (1 - (1 + discount_rate) ** (-lifetime)) / discount_rate
annualization_factor = (
1 - (1 + discount_rate) ** (-lifetime)
) / discount_rate

cf.lifetime_npv = Expression(
expr=annualization_factor * cf.net_profit - cf.capex
Expand All @@ -756,12 +755,12 @@ def add_overall_cashflows(
def append_objective(self, objective_type="npv"):
"""
Appends the objective function.
Args:
objective_type: str, default="npv",
Supported objective functions are
annualized net present value ("npv"),
net profit ("net_profit"), and
annualized net present value ("npv"),
net profit ("net_profit"), and
lifetime net present value ("lifetime_npv").
"""
# pylint: disable = attribute-defined-outside-init
Expand Down
5 changes: 2 additions & 3 deletions idaes/apps/grid_integration/multiperiod/unit_commitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def startup_shutdown_constraints(
"""
Appends startup and shutdown constraints for a given unit/process
"""

@blk.Constraint(set_time)
def binary_relationship_con(_, t):
if t == 1:
Expand Down Expand Up @@ -97,9 +98,7 @@ def ramping_limits(
"""
Appends ramping constraints
"""
ramping_var = {
t: getattr(blk, ramping_var) for t, blk in op_blocks.items()
}
ramping_var = {t: getattr(blk, ramping_var) for t, blk in op_blocks.items()}

@blk.Constraint(set_time)
def ramp_up_con(_, t):
Expand Down
2 changes: 1 addition & 1 deletion idaes/core/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,4 @@ def _in_range(val):
f"Value {val} lies outside the admissible range {[lb, ub]}"
)

return _in_range
return _in_range
5 changes: 2 additions & 3 deletions idaes/core/util/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ def test_is_in_range():
assert is_in_range(0, 1)(0.5) == 0.5
with pytest.raises(
ConfigurationError,
match="Value -1 lies outside the admissible range \\[0, 1\\]"
match="Value -1 lies outside the admissible range \\[0, 1\\]",
):
is_in_range(0, 1)(-1)

with pytest.raises(
ConfigurationError,
match="Value 2 lies outside the admissible range \\[0, 1\\]"
ConfigurationError, match="Value 2 lies outside the admissible range \\[0, 1\\]"
):
is_in_range(0, 1)(2)

0 comments on commit 796134c

Please sign in to comment.