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

Monte carlo Improvement #930

Merged
merged 46 commits into from
Jan 25, 2024
Merged

Conversation

GbotemiB
Copy link
Contributor

@GbotemiB GbotemiB commented Dec 1, 2023

Closes # (if applicable).

Changes proposed in this Pull Request

This PR includes improvement on the Monte-Carlo simulation. The improvement includes the option to choose from different distributions for the simulation. The distribution includes normal, triangle, lognormal, beta, and gamma. While choosing the distribution, it also allows users to pass parameters for the distribution.

Checklist

  • I consent to the release of this PR's code under the AGPLv3 license and non-code contributions under CC0-1.0 and CC-BY-4.0.
  • I tested my contribution locally and it works fine.
  • Code and workflow changes are sufficiently documented.
  • Newly introduced dependencies are added to envs/environment.yaml and doc/requirements.txt.
  • Changes in configuration options are added in all of config.default.yaml and config.tutorial.yaml.
  • Add a test config or line additions to test/ (note tests are changing the config.tutorial.yaml)
  • Changes in configuration options are also documented in doc/configtables/*.csv and line references are adjusted in doc/configuration.rst and doc/tutorial.rst.
  • A note for the release notes doc/release_notes.rst is amended in the format of previous release notes, including reference to the requested PR.

@GbotemiB GbotemiB marked this pull request as draft December 1, 2023 16:52
Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

hello @GbotemiB :D
Great activity :) I've added some comments as discussed :)

scripts/monte_carlo.py Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Show resolved Hide resolved
@GbotemiB
Copy link
Contributor Author

GbotemiB commented Dec 4, 2023

Thanks @davide-f for the amazing review.
I will work on the implementation based on the reviews and get back to you.

config.tutorial.yaml Outdated Show resolved Hide resolved
@GbotemiB
Copy link
Contributor Author

Hello @davide-f, I have implemented all the requested changes. I think the PR is ready for another round of review.

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Great revision @GbotemiB :D
Few minor comments :)

As a comments, in debug, may you show a plot of input and outputs provided by the rescaling function for different values?
Have you done that to make sure the expected distributions are verified?

scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
config.tutorial.yaml Show resolved Hide resolved
@davide-f
Copy link
Member

@GbotemiB Emmanuel, please use precommit.
in github a ton of pre-commit issues are appearing, please check. It would be good to clean a bit the history by resetting to some past commit like this one and placing the changes.

To make sure you use the pre-commit, please check this that suggests you how to set it up

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Partial revision for few comments :D

scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
scripts/monte_carlo.py Outdated Show resolved Hide resolved
@GbotemiB GbotemiB force-pushed the monte-carlo branch 5 times, most recently from 05c902d to 7c490ad Compare December 18, 2023 22:58
@GbotemiB
Copy link
Contributor Author

Hi @davide-f, I have successfully cleaned up the history. Thanks for the tip.

Also, I have made changes to the part of the code you reviewed. Please feel free to review the changes. Thanks.

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Nice @GbotemiB !
I was curious in validating the extraction of the coefficients by the monte-carlo.
The plots show the distribution of the run of selected montecarlos, but do not show the post-processed coefficients that modify the runs.
I think it should be good to verify that the coefficients that are extracted across the 59 simulations do have the shape we expect.

If you have the network basic elec network for example, you could re-obtain the coefficient for example doing:
coeff = sum(n_montecarlo.generators_t.p_max_pu[:, ... == solar ...]) / sum(n_elecnetwork.generators_t.p_max_pu[:, ... == solar ...])

the example above is for solar, but you could do the same for the other carriers and try get the curves.
I'd like to validate that what we expect being a gaussian is a gaussian for example.

I believe we can soon merge the PR :)

config.default.yaml Outdated Show resolved Hide resolved
@GbotemiB
Copy link
Contributor Author

GbotemiB commented Jan 19, 2024

Hi @davide-f, I can confirm that the post-processed coefficients follow the pre-processed coefficients.
image
image

These are some of the plots generated. I will include the plots in the notebook I am working on for the case study analysis and share the notebook with you.

@davide-f
Copy link
Member

Hello @GbotemiB ,
Thanks for the images, but few comments:

  • I expected a uniform distribution for the load, why does it have a different shape instead?
  • Similarly, for the onwind I expected a lognormal shape but this seems to be normal instead, while for sigma=1.5 I expected a different shape.

Could you crosscheck?

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Hello, closing to finalize, I added a comment on the shapes of the distribution that do not look like expected.
Could you clarify?

config.default.yaml Outdated Show resolved Hide resolved
@GbotemiB
Copy link
Contributor Author

Hello @GbotemiB , Thanks for the images, but few comments:

  • I expected a uniform distribution for the load, why does it have a different shape instead?
  • Similarly, for the onwind I expected a lognormal shape but this seems to be normal instead, while for sigma=1.5 I expected a different shape.

Could you crosscheck?

Regarding the distribution of the parameters, it follows this config. But I could reconfigure the parameters.

uncertainties:
    loads_t.p_set:
      type: beta
      args: [2, 0.5]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]:
      type: beta
      args: [4, 1]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "onwind"]:
      type: beta
      args: [4, 1]
    generators.capital_cost.loc[n.generators.carrier == "solar"]:
      type: normal
      args: [0, 1]
    generators.capital_cost.loc[n.generators.carrier == "onwind"]:
      type: normal
      args: [0, 1]
    generators.weight.loc[n.generators.carrier == "onwind"]:
      type: beta
      args: [2, 0.5]
    generators.weight.loc[n.generators.carrier == "solar"]:
      type: beta
      args: [2, 0.5]

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Final rounds!
Great you added the reference to the api :) close to done! [I know it's the third time, but we are really there :)]
Happy to jump in if necessary.

Regarding the distribution plots, have you tested all distributions?

scripts/monte_carlo.py Show resolved Hide resolved
config.default.yaml Outdated Show resolved Hide resolved
Co-authored-by: Davide Fioriti <[email protected]>
@GbotemiB
Copy link
Contributor Author

Final rounds! Great you added the reference to the api :) close to done! [I know it's the third time, but we are there :)] Happy to jump in if necessary.

Regarding the distribution plots, have you tested all distributions?

@davide-f, No problem about continuing the review if necessary.
Regarding the distribution, I will reconfirm all distribution again. I made a few changes to the triangle distribution.

Feel free to jump in.

@GbotemiB
Copy link
Contributor Author

GbotemiB commented Jan 25, 2024

@davide-f, Regarding the distribution, I have tested them. Here is a config for Monte-Carlo and the results.

monte_carlo:
  options:
    add_to_snakefile: true
    samples: 961 # number of optimizations. Note that number of samples when using scipy has to be the square of a prime number
    sampling_strategy: "pydoe2"  # "pydoe2", "chaospy", "scipy", packages that are supported
    seed: 42 # set seedling for reproducibilty
  uncertainties:
    loads_t.p_set:
      type: normal
      args: [1, 100]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "solar"]:
      type: uniform
      args: [0.1, 1]
    generators_t.p_max_pu.loc[:, n.generators.carrier == "onwind"]:
      type: lognormal
      args: [1.5]
    generators.capital_cost.loc[n.generators.carrier == "solar"]:
      type: triangle
      args: [0.7]
    generators.capital_cost.loc[n.generators.carrier == "onwind"]:
      type: beta
      args: [0.5, 1]
    generators.weight.loc[n.generators.carrier == "onwind"]:
      type: beta
      args: [2, 0.5]
    generators.weight.loc[n.generators.carrier == "solar"]:
      type: gamma
      args: [2, 0.5]

Here are the plots.
image
image
image
image
image
image
image

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

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

Great @GbotemiB :D
The PR is ready to merge, really great great job :D

@davide-f davide-f merged commit d912d0d into pypsa-meets-earth:main Jan 25, 2024
4 checks passed
@GbotemiB GbotemiB deleted the monte-carlo branch January 25, 2024 23:40
@GbotemiB
Copy link
Contributor Author

Thank you for the support @davide-f. Special shoutout to @ekatef 🎉 🥳

@ekatef
Copy link
Member

ekatef commented Jan 26, 2024

Fantastic news, @GbotemiB and @davide-f! A really cool feature to have 😄
Congratulations with an amazing result! 🎉 🎉 🎉

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.

3 participants