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 parameter 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 = "direct" # "nmsimplex" |
---|
24 | |
---|
25 | # initialize Diana main class |
---|
26 | dmain=diana.GetDianaMain(sys.argv) |
---|
27 | |
---|
28 | # create NMSimplex-optimizer |
---|
29 | sfactory = dmain.GetSolverFactory() |
---|
30 | solverPE = sfactory.CreateSolver(diana.CAPE_NLP, None, optimizerName) |
---|
31 | |
---|
32 | # create model |
---|
33 | mmanager=dmain.GetModelManager() |
---|
34 | model = mmanager.CreateModel(diana.CAPE_CONTINUOUS, modelPath) |
---|
35 | model.Initialize() |
---|
36 | |
---|
37 | # load measured data (state values and time points) from specified file |
---|
38 | md = diana.DianaMeasuredData(model, observedParams) |
---|
39 | md.load(fileMesData) |
---|
40 | |
---|
41 | # Describe estimated parameters |
---|
42 | sps = [ |
---|
43 | diana.DianaNLPRealParameterSpec("k_s", # name |
---|
44 | "", # description |
---|
45 | 1.0, # default value |
---|
46 | 1.0, # lower bound |
---|
47 | 3.0, # upper bound |
---|
48 | 0.0001),# precision |
---|
49 | diana.DianaNLPRealParameterSpec("mu_max", "", 4.0, 4.0, 6.0, 0.0001) |
---|
50 | ] |
---|
51 | |
---|
52 | # create paramerter fitting task |
---|
53 | taskPE = diana.ParameterFittingTask(dmain, integratorName, |
---|
54 | model, md, sps) |
---|
55 | taskPE.Initialize() |
---|
56 | |
---|
57 | print "Parameter fitting task for %s is initialized" \ |
---|
58 | % model.GetComponentName() |
---|
59 | |
---|
60 | # create report |
---|
61 | report = dmain.CreateReportingInterface(optimizerName) |
---|
62 | # set parameters for reporting |
---|
63 | pars_solverPErep = report.GetParameters() |
---|
64 | pars_solverPErep["Author"].SetValue("SG") |
---|
65 | pars_solverPErep["OutputFolder"].SetValue("./parfit") |
---|
66 | pars_solverPErep["ConsoleOutput"].SetValue(True) |
---|
67 | pars_solverPErep["Samples"].SetValue(True) |
---|
68 | report.Initialize() |
---|
69 | print "Report initialized" |
---|
70 | |
---|
71 | # set prepared task and report to solver |
---|
72 | solverPE.SetReportingInterface(report); |
---|
73 | solverPE.SetNLPTask(taskPE); |
---|
74 | solverPE.Initialize(); |
---|
75 | print "Optimizer %s initialized" % solverPE.GetComponentName() |
---|
76 | |
---|
77 | # start optimization |
---|
78 | print "Parameter fitting for %s..." % model.GetComponentName() |
---|
79 | try: |
---|
80 | solverPE.Solve() |
---|
81 | except: |
---|
82 | print "Best solution: ", solverPE.GetSolution() |
---|
83 | raise |
---|
84 | print "Solution: ", solverPE.GetSolution() |
---|
85 | print "Parameter values:" |
---|
86 | print taskPE.GetSoughtParameters() |
---|