1 | # --------------------------------------------------------------------- |
---|
2 | # Diana process modelling, simulation and analysis software |
---|
3 | # Copyright (c) 2006, Sergiy Gogolenko |
---|
4 | # e-mail: gogolenk@mpi-magdeburg.mpg.de |
---|
5 | # All rights reserved. |
---|
6 | # --------------------------------------------------------------------- |
---|
7 | # $Id: HafkeReactor.py Fri Aug 11 13:22:08 2006 gogolenk $ |
---|
8 | # --------------------------------------------------------------------- |
---|
9 | # Written under: i586-suse-linux |
---|
10 | # --------------------------------------------------------------------- |
---|
11 | # Last update: Fri Aug 11 13:22:08 2006 |
---|
12 | # --------------------------------------------------------------------- |
---|
13 | # Description: |
---|
14 | # Simple example of using DIANA for solving parmeter fitting problems |
---|
15 | # with deterministic optimizer (in this case LBFGS-solver). |
---|
16 | |
---|
17 | import diana, sys, os |
---|
18 | |
---|
19 | modelPath = "Substrateuptake.so" |
---|
20 | observedParams = ["c_x", "c_s"] |
---|
21 | fileMesData = "./data/observations.dat" |
---|
22 | integratorName = "ida" # "odessa" |
---|
23 | optimizerName = "lbfgsb" # "ipopt" |
---|
24 | |
---|
25 | # initialize Diana main class |
---|
26 | dmain=diana.GetDianaMain(sys.argv); |
---|
27 | |
---|
28 | # create NMSimplex-optimizer |
---|
29 | sfactory = dmain.GetSolverFactory(); |
---|
30 | solver = sfactory.CreateSolver(diana.CAPE_NLP, None, optimizerName); |
---|
31 | |
---|
32 | # set solver's parameters L-BFGS-B |
---|
33 | pars_PEsolver = solver.GetParameters() |
---|
34 | pars_PEsolver["StopCondition"].SetValue(1+8) |
---|
35 | pars_PEsolver["MaxTimeOfWork"].SetValue(200) |
---|
36 | pars_PEsolver["MaxCountOfEvals"].SetValue(1000) |
---|
37 | pars_PEsolver["MaxCorrections"].SetValue(100) |
---|
38 | pars_PEsolver["VerboseLevel"].SetValue(0) |
---|
39 | pars_PEsolver["Factr"].SetValue(1e+7) |
---|
40 | pars_PEsolver["PGradTol"].SetValue(1e-5) |
---|
41 | |
---|
42 | # create model |
---|
43 | mmanager=dmain.GetModelManager() |
---|
44 | model = mmanager.CreateModel(diana.CAPE_CONTINUOUS, modelPath) |
---|
45 | model.Initialize() |
---|
46 | |
---|
47 | # load measured data (state values and time points) from specified file |
---|
48 | md = diana.DianaMeasuredData(model, observedParams) |
---|
49 | md.load(fileMesData) |
---|
50 | |
---|
51 | # create paramerter fitting task |
---|
52 | taskPE = diana.SensParameterFittingTask(dmain, integratorName, model, md) |
---|
53 | |
---|
54 | # describe estimated parameters |
---|
55 | taskPE.AddEstimatedParameterBySpec( |
---|
56 | diana.DianaNLPRealParameterSpec("mu_max", "", 4.0, 4.0, 6.0, 0.0001), |
---|
57 | diana.DIANA_PARAMETER) |
---|
58 | taskPE.AddEstimatedParameterBySpec( |
---|
59 | diana.DianaNLPRealParameterSpec("k_s", "", 3.0, 1.0, 3.0, 0.0001), |
---|
60 | diana.DIANA_PARAMETER) |
---|
61 | |
---|
62 | taskPE.Initialize() |
---|
63 | print "Parameter fitting task for %s is initialized" \ |
---|
64 | % model.GetComponentName() |
---|
65 | |
---|
66 | # create report |
---|
67 | report = dmain.CreateReportingInterface(optimizerName) |
---|
68 | # Set parameters for report class |
---|
69 | pars_rep = report.GetParameters() |
---|
70 | pars_rep["Author"].SetValue("SG") |
---|
71 | pars_rep["OutputFolder"].SetValue("./parfit") |
---|
72 | pars_rep["ConsoleOutput"].SetValue(True) |
---|
73 | pars_rep["Samples"].SetValue(True) |
---|
74 | |
---|
75 | report.Initialize() |
---|
76 | print "Report initialized" |
---|
77 | |
---|
78 | # set prepeared task and report to solver |
---|
79 | solver.SetReportingInterface(report); |
---|
80 | solver.SetNLPTask(taskPE); |
---|
81 | solver.Initialize(); |
---|
82 | print "Optimizer %s initialized" % solver.GetComponentName() |
---|
83 | |
---|
84 | # start optimization |
---|
85 | print "Parameter fitting for %s..." % model.GetComponentName() |
---|
86 | try: |
---|
87 | solver.Solve() |
---|
88 | except: |
---|
89 | print "Best solution: ", solver.GetSolution() |
---|
90 | raise |
---|
91 | |
---|
92 | print "Solution: ", solver.GetSolution() |
---|
93 | print "Parameter values:" |
---|
94 | print taskPE.GetSoughtParameters() |
---|