pypsa-meets-earth / pypsa-earth

PyPSA-Earth: A flexible Python-based open optimisation model to study energy system futures around the world.
https://pypsa-earth.readthedocs.io/en/latest/
225 stars 177 forks source link

Monte carlo Improvement #930

Closed GbotemiB closed 8 months ago

GbotemiB commented 10 months ago

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

GbotemiB commented 10 months ago

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

GbotemiB commented 9 months ago

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

davide-f commented 9 months ago

@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

GbotemiB commented 9 months ago

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.

GbotemiB commented 9 months ago

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?

I have tested that the rescaling works. Here is what the rescaled distribution looks like using 1369 samples

image
GbotemiB commented 9 months ago

Hello @davide-f, I added into the monte-carlo.py script a few lines to plot the selected distribution. I also added a rule to the snakemake file to separate the cleaning process for solve_all_networks_monte rule.

Happy to hear your reviews 🙃

GbotemiB commented 8 months ago

Great @GbotemiB :D Added few comments :) Can you also post here the images that you created with the monte carlo? very curious and useful to review!

This is another simulation with 49 samples.

Here is the plot for the objective values for the 49 networks. image

Here are the distribution plots for the parameters. image image image image image image image

GbotemiB commented 8 months ago

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 commented 8 months ago

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

Could you crosscheck?

GbotemiB commented 8 months ago

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]
GbotemiB commented 8 months ago

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 commented 8 months ago

@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

GbotemiB commented 8 months ago

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

ekatef commented 8 months ago

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