femiguez / apsimx

R package for APSIM-X
https://femiguez.github.io/apsimx-docs/
52 stars 20 forks source link

Unable to perform parameter optimization for sugarcane #179

Open Clzuy opened 5 days ago

Clzuy commented 5 days ago

Dear Author,

I followed the parameter optimization example provided at https://femiguez.github.io/apsimx-docs/optim-apsim.html#optim_apsim to optimize parameters for the sugarcane crop, but I encountered some issues during the optimization process.

Initially, when using the default sugarcane type q138 provided by the system, I encountered the following error:

Error in inspect_apsimx_replacement("sugarRuan1004Opt.apsimx", src.dir = extd.dir, : More than one subchild found. Make it unique (see regular expressions) Therefore, I created a new cultivar named clzuy and included it. This resulted in the following message: > inspect_apsimx_replacement("sugarRuan1004Opt.apsimx", src.dir = extd.dir, + node = "Sugarcane", + node.child = "Cultivars", + node.subchild = "clzuy", + verbose = FALSE) These positions matched Models.Core.Folder 1 3 Error: Multiple root nodes found. Please provide a position I specified a position root = list("Models.Core.Folder", 1) and then it ran successfully: $type : Models.PMF.Cultivar, Models : [Sugarcane].cult.leaf_size = 1500,55000,55000 : [Sugarcane].cult.leaf_size_no = 1,14,20 : [Sugarcane].cult.cane_fraction =0.7 : [Sugarcane].cult.sucrose_fraction_stalk = 1,0.61 : [Sugarcane].cult.stress_factor_stalk = 0.2,1 : [Sugarcane].cult.sucrose_delay = 600 : [Sugarcane].cult.min_sstem_sucrose = 1500 : [Sugarcane].cult.min_sstem_sucrose_redn = 10 : [Sugarcane].cult.tt_emerg_to_begcane = 1900 : [Sugarcane].cult.tt_begcane_to_flowering = 6000 : [Sugarcane].cult.tt_flowering_to_crop_end = 2000 : [Sugarcane].cult.green_leaf_no = 13 : [Sugarcane].cult.tillerf_leaf_size = 1,1,1.5,1,1 : [Sugarcane].cult.tillerf_leaf_size_no = 1,4,10,16,26 Command Name : clzuy Enabled : TRUE ReadOnly : FALSE I then wrote the parameter paths as follows, based on the example: pp1 <- "Sugarcane.Cultivars.clzuy.cane_fraction" pp2 <- "Sugarcane.Cultivars.clzuy.sucrose_fraction_stalk" pp3 <- "Sugarcane.Cultivars.clzuy.min_sstem_sucrose" pp4 <- "Sugarcane.Cultivars.clzuy.tt_emerg_to_begcane" pp5 <- "Sugarcane.Cultivars.clzuy.green_leaf_no" However, when calling optim_apsimx, I encountered the following error: I am unable to resolve this error. From my comparison, I found that the sugarcane module is directly migrated from APSIM 7.10 to APSIMX, and its parameters differ from other crops in that they do not have the .FixedValue suffix. If I try to add .FixedValue to the parameters, it results in an immediate error. APSIMX VERSION-2024.9.7589.0 (genotype parameters for Sugarcane update on version-2024.07.7562) I have compressed and attached the relevant files. Could you please suggest any solutions? Thank you! [exam.zip](https://github.com/user-attachments/files/17265006/exam.zip) Best regards, Zongheng Li
femiguez commented 3 days ago

@Clzuy I'm able to run the optimization. Some important points:

1) get the latest version of the package from github "remotes::install_github("femiguez/apsimx")" 2) Make sure that you use relative paths for your met file (there is code you do not need to run) 3) Do not try to optimize too many parameters. I do not know which parameters are the best to optimize in your case. I do not use the sugarcane model. This is not even a "rule-of-thumb", but optimize one parameter per variable. In your case, you measured three variables and I selected three parameters from your list. Again, I do not know if these are "good" parameters to try to optimize

Clzuy commented 3 days ago

@femiguez Thank you for your answer. I have tried, and it's not an issue with other modules. For example, I can successfully modify parameters for wheat or maize, but the sugarcane parameters cannot be modified. These parameters were determined after reviewing literature and conducting sensitivity analysis. I performed the sensitivity analysis by creating cultivars using the rapsiming package, but the parameter optimization is not implemented through creating cultivars. The definition of sugarcane parameters differs from other crops in that they don't have the '.FixedValue' suffix, as shown in the image. I have seen that people have optimized these parameters based on APSIM classic. image image

Thank you for taking the time to answer despite your busy schedule. I appreciate it. Best regards, Zongheng Li

femiguez commented 3 days ago

Which version of the apsimx package are you running?

packageVersion("apsimx")

Clzuy commented 3 days ago

Which version of the apsimx package are you running?您运行的是哪个版本的 apsimx 软件包?

packageVersion("apsimx") packageVersion(“apsimx”)

package ‘apsimx’ version 2.8.206

femiguez commented 3 days ago

I'm attaching your script with minor edits. The code runs, but it's possible that it might need a minor tweak to give best results. optSugar_fem.R.zip

femiguez commented 3 days ago

In R you need to set your current directory maybe?

setwd("H:/Drought/lzhAPSIMfile")

place that line at the top of your script. All the relevant files should be in that folder. I'm making some changes to the script. I'll send you another version.

Clzuy commented 3 days ago

In R you need to set your current directory maybe?在 R 中,您需要设置当前目录,也许?

setwd("H:/Drought/lzhAPSIMfile")setwd(“H:/Drought/lzhAPSIMfile”)

place that line at the top of your script. All the relevant files should be in that folder. I'm making some changes to the script. I'll send you another version.将该行放在脚本的顶部。所有相关文件都应位于该文件夹中。我正在对脚本进行一些更改。我会给你发另一个版本。 Yes, I just discovered this issue, so I set the src.dir. You are really patient! Thank you, I might have to debug this tomorrow, it's already midnight. Thank you very much, have a wonderful day!

Clzuy commented 2 days ago

@femiguez Thank you very much for the code you provided. I tried running it today, and it worked very smoothly. The reason it didn't run properly before was probably because I passed in pp2 <- "Sugarcane.Cultivars.clzuy.sucrose_fraction_stalk", which is a vector. image

But I still want to modify this parameter, so I modified the code as follows:

pp1 <- "Sugarcane.Cultivars.clzuy.cane_fraction" pp2 <- "Sugarcane.Cultivars.clzuy.sucrose_fraction_stalk" wop.h <- optim_apsimx("sugarRuan1004Opt.apsimx", parm.paths = c(pp1, pp2), data = obsSugar, weights = "mean", replacement = c(TRUE, TRUE), initial.values = c(0.7, 0.61), root = list("Models.Core.Folder", 1), parm.vector.index = c(0, 2), hessian = TRUE, control = list(trace = 2L)) I found an error in the code. System.Exception: Error in file:H:\Drought\APSIM\Optimi Analysis\FemR\sugarRuan1004Opt.apsimx ---> System.Exception: Error reading file H:\Drought\APSIM\Optimi Analysis\FemR\sugarRuan1004Opt.apsimx ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path 'Children[0].Children[0].Children[0].Children[36].Command[2]', line 896, position 21. I opened the apsimx file and saw that it became like the picture below.

image

Is there a problem with the vector position I specified? Best regards, Zongheng Li