Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Driver fix for Opt mode compatibility with CSV and debug #383

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions templates/template_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ def _modify_outer_models(self, template, case, components, sources):
text = 'Samplers|CustomSampler@name:mc_arma_dispatch|constant@name:{}'
# Remove anything having to do with 'denoises', it's no longer needed.
raven.remove(raven.find(".//alias[@variable='denoises']"))
denoises_parent = template.find(".//constant[@name='denoises']/..")
denoises_parent.remove(denoises_parent.find(".//constant[@name='denoises']"))
for denoises_parent in template.findall(".//constant[@name='denoises']/.."):
denoises_parent.remove(denoises_parent.find(".//constant[@name='denoises']"))
# Remove any GRO_final_return vars that compute Sigma or Var (e.g. var_NPV)
final_return_vars = template.find('.//VariableGroups/Group[@name="GRO_outer_results"]')
new_final_return_vars = [var for var in final_return_vars.text.split(", ") if "std" not in var and "var" not in var]
Expand All @@ -521,7 +521,7 @@ def _modify_outer_models(self, template, case, components, sources):
if cap.is_parametric() and isinstance(cap.get_value(debug=case.debug['enabled']) , list):
feature_list += name + '_capacity' + ','
feature_list = feature_list[0:-1]
if case.get_mode() == 'opt':
if case.get_mode() == 'opt' and (not case.debug['enabled']):
gpr = template.find('Models').find('ROM')
gpr.find('Features').text = feature_list
new_opt_metric = self._build_opt_metric_out_name(case)
Expand Down Expand Up @@ -682,7 +682,7 @@ def _modify_outer_samplers(self, template, case, components):
dist, xml = self._create_new_sweep_capacity(name, var_name, vals, sampler)
dists_node.append(dist)
# Bayesian Optimizer requires additional modification
if case.get_opt_strategy() == 'BayesianOpt' and case.get_mode() == 'opt':
if case.get_opt_strategy() == 'BayesianOpt' and case.get_mode() == 'opt' and (not case.debug['enabled']):
xml.remove(xml.find('initial'))
samps_node.append(xml)
grid_node = xmlUtils.newNode('grid', text='0 1',
Expand Down Expand Up @@ -719,7 +719,7 @@ def _modify_outer_optimizers(self, template, case):
"""
# Setting base outer for opt based on optimizer used
strategy = case.get_opt_strategy()
if case.get_mode() == 'opt':
if case.get_mode() == 'opt'and (not case.debug['enabled']):
# Strategy tells us which optimizer to use
if strategy == 'BayesianOpt':
opt_node = template.find('Optimizers').find(".//BayesianOptimizer[@name='cap_opt']")
Expand All @@ -730,8 +730,11 @@ def _modify_outer_optimizers(self, template, case):
template.remove(template.find('Samplers'))
template.find('Models').remove(template.find(".//ROM[@name='gpROM']"))
template.find('Optimizers').remove(template.find(".//BayesianOptimizer[@name='cap_opt']"))
# if running in debug, none of these nodes should be here, skip
if case.debug['enabled']:
return
# only modify if optimization_settings is in Case
if (case.get_mode() == 'opt') and (case.get_optimization_settings() is not None) and (not case.debug['enabled']): # TODO there should be a better way to handle the debug case
if (case.get_mode() == 'opt') and (case.get_optimization_settings() is not None): # TODO there should be a better way to handle the debug case
optimization_settings = case.get_optimization_settings()
# Strategy tells us which optimizer to use
if strategy == 'BayesianOpt':
Expand Down
113 changes: 113 additions & 0 deletions tests/integration_tests/mechanics/debug_mode/opt/heron_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<HERON>
<TestInfo>
<name>DebugMode</name>
<author>talbpaul</author>
<created>2021-02-22</created>
<description>
Tests a debug mode of operation, where a single outer runs a single sample of inner
and outputs the optimized dispatch.
</description>
<classesTested>HERON</classesTested>
</TestInfo>

<Case name="Debug_Run">
<mode>opt</mode>
<debug>
<inner_samples>2</inner_samples>
<macro_steps>2</macro_steps>
<dispatch_plot>True</dispatch_plot>
</debug>
<num_arma_samples>1</num_arma_samples>
<time_discretization>
<time_variable>Time</time_variable>
<end_time>2</end_time>
<num_steps>21</num_steps>
</time_discretization>
<economics>
<ProjectTime>3</ProjectTime>
<DiscountRate>0.08</DiscountRate>
<tax>0.3</tax>
<inflation>0.02</inflation>
<verbosity>50</verbosity>
</economics>
<dispatcher>
<pyomo/>
</dispatcher>
</Case>

<Components>
<Component name="steamer">
<produces resource="steam" dispatch="fixed">
<capacity resource="steam">
<opt_bounds debug_value="3.14">1, 100</opt_bounds>
</capacity>
</produces>
<economics>
<lifetime>5</lifetime>
</economics>
</Component>

<Component name="generator">
<produces resource="electricity" dispatch="independent">
<consumes>steam</consumes>
<capacity resource="steam">
<fixed_value>-100</fixed_value>
</capacity>
<transfer>
<linear>
<rate resource="steam">-1</rate>
<rate resource="electricity">0.5</rate>
</linear>
</transfer>
</produces>
<economics>
<lifetime>5</lifetime>
</economics>
</Component>

<Component name="electr_market">
<demands resource="electricity" dispatch="dependent">
<capacity>
<fixed_value>-2</fixed_value>
</capacity>
</demands>
<economics>
<lifetime>30</lifetime>
<CashFlow name="e_sales" type="repeating" taxable='True' inflation='none' >
<driver>
<activity>electricity</activity>
</driver>
<reference_price>
<fixed_value>0.5</fixed_value>
</reference_price>
</CashFlow>
</economics>
</Component>

<Component name="electr_flex">
<demands resource="electricity" dispatch="dependent">
<capacity>
<fixed_value>-1e200</fixed_value>
</capacity>
</demands>
<economics>
<lifetime>30</lifetime>
<CashFlow name="e_sales" type="repeating" taxable='True' inflation='none' >
<driver>
<activity>electricity</activity>
</driver>
<reference_price>
<Function method="flex_price">transfers</Function>
</reference_price>
</CashFlow>
</economics>
</Component>

</Components>

<DataGenerators>
<ARMA name='Price' variable="Signal">%HERON_DATA%/TSA/Sine/arma.pk</ARMA>
<Function name="transfers">../transfers.py</Function>
</DataGenerators>

</HERON>
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

<DataGenerators>
<ARMA name='Price' variable="Signal">%HERON_DATA%/TSA/Sine/arma.pk</ARMA>
<Function name="transfers">transfers.py</Function>
<Function name="transfers">../transfers.py</Function>
</DataGenerators>

</HERON>
35 changes: 29 additions & 6 deletions tests/integration_tests/mechanics/debug_mode/tests
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
[Tests]
[./DebugMode]
[./DebugModeWithSweep]
type = 'HeronIntegration'
input = heron_input.xml
input = 'sweep/heron_input.xml'
[./dispatch_db]
type = NetCDF
output = 'Debug_Run_o/dispatch.nc'
output = 'sweep/Debug_Run_o/dispatch.nc'
gold_files = 'dispatch.nc'
[../]
[./dispatch_csv]
type = UnorderedCSV
output = 'Debug_Run_o/dispatch_print.csv'
output = 'sweep/Debug_Run_o/dispatch_print.csv'
gold_files = 'dispatch_print.csv'
rel_err = 1e-8
[../]
[./debug_plot]
type = Exists
output = 'Debug_Run_o/dispatch_id0_y10_c0_f1.png Debug_Run_o/dispatch_id0_y11_c0_f1.png Debug_Run_o/dispatch_id1_y10_c0_f1.png Debug_Run_o/dispatch_id1_y11_c0_f1.png'
output = 'sweep/Debug_Run_o/dispatch_id0_y10_c0_f1.png sweep/Debug_Run_o/dispatch_id0_y11_c0_f1.png sweep/Debug_Run_o/dispatch_id1_y10_c0_f1.png sweep/Debug_Run_o/dispatch_id1_y11_c0_f1.png'
[../]
[./debug_plot]
type = Exists
output = 'network.png'
output = 'sweep/network.png'
[../]
[../]
[./DebugModeWithOpt]
type = 'HeronIntegration'
input = 'opt/heron_input.xml'
[./dispatch_db]
type = NetCDF
output = 'opt/Debug_Run_o/dispatch.nc'
gold_files = 'dispatch.nc'
[../]
[./dispatch_csv]
type = UnorderedCSV
output = 'opt/Debug_Run_o/dispatch_print.csv'
gold_files = 'dispatch_print.csv'
rel_err = 1e-8
[../]
[./debug_plot]
type = Exists
output = 'opt/Debug_Run_o/dispatch_id0_y10_c0_f1.png opt/Debug_Run_o/dispatch_id0_y11_c0_f1.png opt/Debug_Run_o/dispatch_id1_y10_c0_f1.png opt/Debug_Run_o/dispatch_id1_y11_c0_f1.png'
[../]
[./debug_plot]
type = Exists
output = 'opt/network.png'
[../]
[../]
[]
Expand Down
Loading