odow / SDDP.jl

Stochastic Dual Dynamic Programming in Julia
https://sddp.dev
Other
289 stars 60 forks source link

How to save a trained model? #757

Closed WuSiren closed 2 weeks ago

WuSiren commented 3 weeks ago

How to save a trained model? I've tried to save it with JLD2, but when I loaded the model, it told:

Error showing value of type JLD2.ReconstructedMutable{Symbol("PolicyGraph{Int64}"), (:objective_sense, :root_node, :root_children, :initial_root_state, :nodes, :belief_partition, :most_recent_training_results, :ext, :timer_output), Tuple{JLD2.ReconstructedPrimitive{:OptimizationSense, UInt32}, Int64, Vararg{Any, 7}}}:
ERROR: UndefRefError: access to undefined reference

So is there any proper method to saving a SDDP model?

WuSiren commented 3 weeks ago

I've tried to save a trained model as a StochOptFormat but I failed:

ERROR: StochOptFormat does not support writing after a call to `SDDP.train`.

But I need to save the trained model so that other people can reproduce the results I obtained easily. Could you please provide some advice? Thanks!

Thuener commented 2 weeks ago

You can try https://sddp.dev/stable/apireference/#SDDP.write_to_file, but it is still experimental.

What I have done was to use https://sddp.dev/stable/apireference/#SDDP.write_cuts_to_file and https://sddp.dev/stable/apireference/#SDDP.read_cuts_from_file.

I create a function to save all the parameters (save_parameters) that I use to create the subproblems into a .json file and then another load function to create the subproblems (load_parameters) using that .json file. Then, I create two functions: save_model, which calls save_parameters and write_cuts_to_file, and load_model, which calls load_parameters and read_cuts_from_file.

odow commented 2 weeks ago

You can not save a model to JLD2 because it will contain references to the underlying solver like Gurobi.

As @Thuener suggests, you should write cuts to a file.

Note that StochOptFormat is currently experimental.

My suggestion would be to share the script that rebuilds the model as Julia code, and also share the cuts .json file that you can read with read_cuts_from_file.

WuSiren commented 2 weeks ago

Thank you very much, @Thuener and @odow !

I think it may be necessary for me to get clear one thing that what essentially a trained decision rule is? Is it simply a parameterized function, or a new small optimization problem whose solution is the decision?

Please forgive me for not knowing it deeply enough! 🙏

odow commented 2 weeks ago

or a new small optimization problem whose solution is the decision

It's this

WuSiren commented 2 weeks ago

or a new small optimization problem whose solution is the decision

It's this

I see. Thanks. If I save the policy using SDDP.write_cuts_to_file(model, "myPolicy.json"), then how should someone else use it after he runs SDDP.read_cuts_from_file(model, "myPolicy.json"), for example, if he wants to perform in-sample or out-of-sample simulate, or just evaluate the decision rule for some given scenario? (I got it)

WuSiren commented 2 weeks ago

You can try https://sddp.dev/stable/apireference/#SDDP.write_to_file, but it is still experimental.

What I have done was to use https://sddp.dev/stable/apireference/#SDDP.write_cuts_to_file and https://sddp.dev/stable/apireference/#SDDP.read_cuts_from_file.

I create a function to save all the parameters (save_parameters) that I use to create the subproblems into a .json file and then another load function to create the subproblems (load_parameters) using that .json file. Then, I create two functions: save_model, which calls save_parameters and write_cuts_to_file, and load_model, which calls load_parameters and read_cuts_from_file.

Hi, @Thuener ! Is your method same with @odow 's suggestion?

My suggestion would be to share the script that rebuilds the model as Julia code, and also share the cuts .json file that you can read with read_cuts_from_file.

Could you please provide more details on how you save the parameters to a .json file? According to my understanding, your method can have reproducibility without providing the explicit model script to the user, is it?

odow commented 2 weeks ago

without providing the explicit model script to the user

No, you should still provide the script.

See, e.g., https://github.com/odow/SDDP.jl/blob/master/papers/policy_graph/paper.jl and https://github.com/odow/SDDP.jl/blob/master/papers/policy_graph/powder_data.json

WuSiren commented 2 weeks ago

Oh, I roughly understand how it works. Thank you very much Prof. @odow ! 🤝