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

Refactor parameters into General, Simple and Constant #194

Merged
merged 8 commits into from
Jul 5, 2024

Conversation

jetuk
Copy link
Member

@jetuk jetuk commented Jun 5, 2024

This is designed as an alternative to #192.

The high level idea is the solve the circular parameter issue with initial volumes by recognising that some parameters
do not vary in time and/or depend no node state.

We introduce the General, Simple and Constant parameters as new traits:

  • GeneralParameter - rename of the current implementation.
  • SimpleParameter - Parameters that only depend on other Simple or Constant parameters, but do vary in time.
  • ConstantParameter - Parameters that only depend on other Constant parameters, and do not vary in time.

Simple parameters are evaluated before general and .before methods each time-step. This allows them to vary in time, but means that can't depend on network or general parameter values. This is most relevant for profiles which have no dependencies but do vary in time.

Constant parameters would be evaluated at the beginning of the simulation only. NB, this is not yet implemented.

Some parameters can potentially implement one or more of these traits. For example, the AggregatedParameter is implemented for General and Simple depending on whether it is aggregating over MetricF64 or SimpleMetricF64. This allows the user to use an aggregated parameter as a simple parameter provided it is only aggregating over simple parameters. From the schema point of view there is no difference. When added to the core model it attempts to add it as a simplified version if possible.

@Batch21 @s-simoncelli @laurenpetch any thoughts on this vs #192 ? Sorry, it's a big refactor / change this one.

jetuk added 3 commits May 21, 2024 14:37
GeneralParameter contains compute and after methods, Parameter
contains the others.
This is designed as an alternative to #192.

The high level idea is the solve the circular parameter
issue with initial volumes by recognising that some parameters
do not vary in time and/or depend no node state.

We introduce the General, Simple and Constant parameters as new
traits:

- `GeneralParameter` - rename of the current implementation.
- `SimpleParameter` - Parameters that only depend on other
Simple or Constant parameters, but do vary in time.
- `ConstantParameter` - Parameters that only depend on other
Constant parameters, and do *not* vary in time.

Simple parameters are evaluated before general and `.before`
methods each time-step. This allows them to vary in time, but
means that can't depend on network or general parameter values.
This is most relevant for profiles which have no dependencies
but do vary in time.

Constant parameters would be evaluated at the beginning of the
simulation only. NB, this is not yet implemented.

Some parameters can potentially implement one or more of these
traits. For example, the AggregatedParameter is implemented
for General and Simple depending on whether it is aggregating
over MetricF64 or SimpleMetricF64. This allows the user to use
an aggregated parameter as a simple parameter provided it is
only aggregating over simple parameters. From the schema
point of view there is no difference. When added to the core
model it attempts to add it as a simplified version if possible.
@jetuk jetuk requested a review from Batch21 June 5, 2024 09:08
Copy link
Contributor

@Batch21 Batch21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From an initial read through, this feels like a better solution than #192

One issue I did find when testing using the delay parameter as a simple parameter is that the after method is not currently called simple parameters.

));
}

match parameter.try_into_simple() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a bit of explanation here in a comment? Potentially the function name could be updated to reflect the fact that a simple parameter could be added instead of a general one.

MultiParameterValue((ParameterIndex<MultiValue>, String)),
ParameterValue(GeneralParameterIndex<f64>),
IndexParameterValue(GeneralParameterIndex<usize>),
MultiParameterValue((GeneralParameterIndex<MultiValue>, String)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point but should these 3 variants be put into a nest enum to be consistent with the simple params?

Final parameter variant that is computed only at the beginning
of a model.
@jetuk
Copy link
Member Author

jetuk commented Jul 1, 2024

One issue I did find when testing using the delay parameter as a simple parameter is that the after method is not currently called simple parameters.

This is now fixed. It's ready for a final review.

&mut self,
parameter: Box<dyn SimpleParameter<usize>>,
) -> Result<SimpleParameterIndex<usize>, PywrError> {
// TODO Fix this check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be fixed in this PR?

&mut self,
parameter: Box<dyn SimpleParameter<MultiValue>>,
) -> Result<SimpleParameterIndex<MultiValue>, PywrError> {
// TODO Fix this check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

jetuk added 2 commits July 4, 2024 13:29
Implements the same API for all 3 types. Adds checks to ensure
names are unique across all parameter variants.
@jetuk
Copy link
Member Author

jetuk commented Jul 4, 2024

This has been merged with master and the above issues addressed.

@jetuk jetuk merged commit 287cc67 into main Jul 5, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants