Skip to content

Commit be09019

Browse files
Merge pull request #162 from grid-parity-exchange/additional_callbacks
Additional callbacks
2 parents bc71466 + 8c54323 commit be09019

9 files changed

+49
-64
lines changed

prescient/data/simulation_state/mutable_simulation_state.py

-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from __future__ import annotations
1010
from typing import TYPE_CHECKING
1111
if TYPE_CHECKING:
12-
from types import Iterable, Tuple
1312
from prescient.engine.abstract_types import G, S
1413

1514
from collections import deque
@@ -70,26 +69,14 @@ def get_initial_generator_state(self, g:G) -> float:
7069
''' Get the generator's state in the previous time period '''
7170
return self._init_gen_state[g]
7271

73-
def get_all_initial_generator_state(self) -> Iterable[Tuple[G,float]]:
74-
''' Get the generator's state in the previous time period for each generator '''
75-
yield from self._init_gen_state.items()
76-
7772
def get_initial_power_generated(self, g:G) -> float:
7873
''' Get how much power was generated in the previous time period '''
7974
return self._init_power_gen[g]
8075

81-
def get_all_initial_power_generated(self) -> Iterable[Tuple[G,float]]:
82-
''' Get how much power was generated in the previous time period for each generator '''
83-
yield from self._init_power_gen.items()
84-
8576
def get_initial_state_of_charge(self, s:S) -> float:
8677
''' Get state of charge in the previous time period '''
8778
return self._init_soc[s]
8879

89-
def get_all_initial_state_of_charge(self) -> Iterable[Tuple[S,float]]:
90-
''' Get state of charge in the previous time period for each storage device '''
91-
yield from self._init_soc.items()
92-
9380
def get_current_actuals(self, forecastable:str) -> float:
9481
''' Get the current actual value for forecastable
9582

prescient/data/simulation_state/simulation_state.py

-15
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,16 @@ def get_initial_generator_state(self, g:G):
3636
''' Get the generator's state in the previous time period '''
3737
pass
3838

39-
@abstractmethod
40-
def get_all_initial_generator_state(self):
41-
''' Get the generator's state in the previous time period for each generator '''
42-
pass
43-
4439
@abstractmethod
4540
def get_initial_power_generated(self, g:G):
4641
''' Get how much power was generated in the previous time period '''
4742
pass
4843

49-
@abstractmethod
50-
def get_all_initial_power_generated(self):
51-
''' Get how much power was generated in the previous time period for each generator '''
52-
pass
53-
5444
@abstractmethod
5545
def get_initial_state_of_charge(self, s:S):
5646
''' Get state of charge in the previous time period '''
5747
pass
5848

59-
@abstractmethod
60-
def get_all_initial_state_of_charge(self):
61-
''' Get state of charge in the previous time period for each storage device '''
62-
pass
63-
6449
@abstractmethod
6550
def get_current_actuals(self, forecastable:str) -> float:
6651
''' Get the current actual value for a forecastable data item

prescient/data/simulation_state/state_with_offset.py

-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from __future__ import annotations
1010
from typing import TYPE_CHECKING
1111
if TYPE_CHECKING:
12-
from types import Iterable, Tuple
1312
from prescient.engine.abstract_types import G, S, EgretModel
1413
from prescient.data.simulation_state.simulation_state import SimulationState
1514

@@ -75,26 +74,14 @@ def get_initial_generator_state(self, g:G):
7574
''' Get the generator's state in the previous time period '''
7675
return self._init_gen_state[g]
7776

78-
def get_all_initial_generator_state(self) -> Iterable[Tuple[G,float]]:
79-
''' Get the generator's state in the previous time period for each generator '''
80-
yield from self._init_gen_state.items()
81-
8277
def get_initial_power_generated(self, g:G):
8378
''' Get how much power was generated in the previous time period '''
8479
return self._init_power_gen[g]
8580

86-
def get_all_initial_power_generated(self) -> Iterable[Tuple[G,float]]:
87-
''' Get how much power was generated in the previous time period for each generator '''
88-
yield from self._init_power_gen.items()
89-
9081
def get_initial_state_of_charge(self, s:S):
9182
''' Get state of charge in the previous time period '''
9283
return self._init_soc[s]
9384

94-
def get_all_initial_state_of_charge(self) -> Iterable[Tuple[S,float]]:
95-
''' Get state of charge in the previous time period for each storage device '''
96-
yield from self._init_soc.items()
97-
9885
def get_current_actuals(self, forecastable:str) -> float:
9986
''' Get the current actual value for a forecastable data item
10087

prescient/data/simulation_state/time_interpolated_state.py

-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from __future__ import annotations
1010
from typing import TYPE_CHECKING
1111
if TYPE_CHECKING:
12-
from types import Iterable, Tuple
1312
from prescient.engine.abstract_types import G, S
1413

1514
import math
@@ -123,26 +122,14 @@ def get_initial_generator_state(self, g:G):
123122
''' Get the generator's state in the previous time period '''
124123
return self._inner_state.get_initial_generator_state(g)
125124

126-
def get_all_initial_generator_state(self) -> Iterable[Tuple[G,float]]:
127-
''' Get the generator's state in the previous time period for each generator '''
128-
yield from self._inner_state.get_all_initial_generator_state()
129-
130125
def get_initial_power_generated(self, g:G):
131126
''' Get how much power was generated in the previous time period '''
132127
return self._inner_state.get_initial_power_generated(g)
133128

134-
def get_all_initial_power_generated(self) -> Iterable[Tuple[G,float]]:
135-
''' Get how much power was generated in the previous time period for each generator '''
136-
yield from self._inner_state.get_all_initial_power_generated()
137-
138129
def get_initial_state_of_charge(self, s:S):
139130
''' Get state of charge in the previous time period '''
140131
return self._inner_state.get_initial_state_of_charge(s)
141132

142-
def get_all_initial_state_of_charge(self) -> Iterable[Tuple[S,float]]:
143-
''' Get state of charge in the previous time period for each storage device '''
144-
yield from self._inner_state.get_all_initial_state_of_charge()
145-
146133
def get_current_actuals(self, forecastable:str) -> float:
147134
''' Get the current actual value for a forecastable data item
148135

prescient/engine/egret/data_extractors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def get_generator_bus(sced: OperationsModel, g: G) -> B:
9595
def is_generator_on(sced: OperationsModel, g: G) -> bool:
9696
g_dict = sced.data['elements']['generator'][g]
9797
if 'fixed_commitment' in g_dict:
98-
return g_dict['fixed_commitment']['values'][0] > 0
98+
if isinstance(g_dict['fixed_commitment'], dict):
99+
return g_dict['fixed_commitment']['values'][0] > 0
100+
else:
101+
return g_dict['fixed_commitment'] > 0
99102
elif 'commitment' in g_dict:
100103
return g_dict['commitment']['values'][0] > 0
101104
else:

prescient/engine/egret/egret_plugin.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def create_sced_instance(data_provider:DataProvider,
115115
assert current_state is not None
116116

117117
sced_md = data_provider.get_initial_actuals_model(options, sced_horizon, current_state.minutes_per_step)
118+
options.plugin_context.callback_manager.invoke_after_get_initial_actuals_model_for_sced_callbacks(
119+
options, sced_md)
118120

119121
# Set initial state
120122
_copy_initial_state_into_model(options, current_state, sced_md)
@@ -382,6 +384,8 @@ def create_deterministic_ruc(options,
382384

383385
# Create a new model
384386
md = data_provider.get_initial_forecast_model(options, ruc_horizon, 60)
387+
options.plugin_context.callback_manager.invoke_after_get_initial_forecast_model_for_ruc_callbacks(
388+
options, md)
385389

386390
initial_ruc = current_state is None or current_state.timestep_count == 0
387391

@@ -627,6 +631,8 @@ def create_simulation_actuals(
627631
# Get a new model
628632
total_step_count = options.ruc_horizon * 60 // step_size_minutes
629633
md = data_provider.get_initial_actuals_model(options, total_step_count, step_size_minutes)
634+
options.plugin_context.callback_manager.invoke_after_get_initial_actuals_model_for_simulation_actuals_callbacks(
635+
options, md)
630636

631637
# Fill it in with data
632638
if this_hour == 0:
@@ -706,12 +712,11 @@ def _ensure_contingencies_monitored(options:Options, md:EgretModel, initial_ruc:
706712
def _copy_initial_state_into_model(options:Options,
707713
current_state:SimulationState,
708714
md:EgretModel):
709-
for g, initial_status in current_state.get_all_initial_generator_state():
710-
md.data['elements']['generator'][g]['initial_status'] = initial_status
711-
for g, initial_p_output in current_state.get_all_initial_power_generated():
712-
md.data['elements']['generator'][g]['initial_p_output'] = initial_p_output
713-
for s, initial_state_of_charge in current_state.get_all_initial_state_of_charge():
714-
md.data['elements']['storage'][s]['initial_state_of_charge'] = initial_state_of_charge
715+
for g, g_dict in md.elements('generator', generator_type='thermal'):
716+
g_dict['initial_status'] = current_state.get_initial_generator_state(g)
717+
g_dict['initial_p_output'] = current_state.get_initial_power_generated(g)
718+
for s,s_dict in md.elements('storage'):
719+
s_dict['initial_state_of_charge'] = current_state.get_initial_state_of_charge(s)
715720

716721
def get_attrs_to_price_option(options:Options):
717722
'''

prescient/plugins/internal.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ class _StatisticsSubscribers(NamedTuple):
1515

1616
callbacks = ['options_preview',
1717
'update_operations_stats',
18+
'after_get_initial_actuals_model_for_sced',
19+
'after_get_initial_forecast_model_for_ruc',
20+
'after_get_initial_actuals_model_for_simulation_actuals',
1821
'after_ruc_generation',
1922
'after_ruc_activation',
2023
'before_operations_solve',
2124
'before_ruc_solve',
2225
'after_operations',
23-
'finalization',]
26+
'finalization',
27+
]
2428

2529
class PluginCallbackManager():
2630
'''

prescient/plugins/plugin_registration.py

+27
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ def register_initialization_callback(
106106
'''
107107
self.callback_manager.register_initialization_callback(callback)
108108

109+
def register_after_get_initial_actuals_model_for_sced_callback(
110+
self,
111+
callback: Callable[[Options, OperationsModel], None]
112+
) -> None:
113+
''' Request a method to be called immediately after an actuals model for the sced has
114+
been generated, but before any data is loaded into it.
115+
'''
116+
self.callback_manager.register_after_get_initial_actuals_model_for_sced_callback(callback)
117+
118+
def register_after_get_initial_forecast_model_for_ruc_callback(
119+
self,
120+
callback: Callable[[Options, RucModel], None]
121+
) -> None:
122+
''' Request a method to be called immediately after an forecast model for the ruc has
123+
been generated, but before any data is loaded into it.
124+
'''
125+
self.callback_manager.register_after_get_initial_forecast_model_for_ruc_callback(callback)
126+
127+
def register_after_get_initial_actuals_model_for_simulation_actuals_callback(
128+
self,
129+
callback: Callable[[Options, RucModel], None]
130+
) -> None:
131+
''' Request a method to be called immediately after an actuals model for the simulation_actuals has
132+
been generated, but before any data is loaded into it.
133+
'''
134+
self.callback_manager.register_after_get_initial_actuals_model_for_simulation_actuals_callback(callback)
135+
109136
def register_finalization_callback(
110137
self,
111138
callback: Callable[[Options, Simulator], None]

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
packages = find_namespace_packages(include=['prescient.*'])
2222

2323
setup(name='gridx-prescient',
24-
version='2.2.1.dev0',
24+
version='2.2.1',
2525
description='Power Generation Scenario creation and simulation utilities',
2626
url='https://github.com/grid-parity-exchange/Prescient',
2727
author='Jean-Paul Watson, David Woodruff, Andrea Staid, Dominic Yang',
@@ -40,6 +40,6 @@
4040
'prescient.simulator.tests':['regression_tests_data/**/*'],
4141
},
4242
install_requires=['numpy','matplotlib','pandas','scipy','pyomo>=6.1.2',
43-
'python-dateutil','networkx','jupyter', 'gridx-egret>=0.5.4.dev0',
43+
'python-dateutil','networkx','jupyter', 'gridx-egret==0.5.3',
4444
],
4545
)

0 commit comments

Comments
 (0)