forked from sdu-cfei/modest-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_ga_vs_ga_parallel.py
75 lines (64 loc) · 2.96 KB
/
simple_ga_vs_ga_parallel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Copyright (c) 2017, University of Southern Denmark
All rights reserved.
This code is licensed under BSD 2-clause license.
See LICENSE file in the project root for license terms.
"""
import logging
import json
import os
import pandas as pd
from modestpy import Estimation
from modestpy.utilities.sysarch import get_sys_arch
logging.basicConfig(level='INFO', filename='test.log', filemode='w')
if __name__ == "__main__":
"""
This file is supposed to be run from the root directory.
Otherwise the paths have to be corrected.
"""
# DATA PREPARATION ==============================================
# Resources
platform = get_sys_arch()
assert platform, 'Unsupported platform type!'
fmu_file = 'Simple2R1C_ic_' + platform + '.fmu'
fmu_path = os.path.join('examples', 'simple', 'resources', fmu_file)
inp_path = os.path.join('examples', 'simple', 'resources', 'inputs.csv')
ideal_path = os.path.join('examples', 'simple', 'resources', 'result.csv')
est_path = os.path.join('examples', 'simple', 'resources', 'est.json')
known_path = os.path.join('examples', 'simple', 'resources', 'known.json')
# Working directory
workdir = os.path.join('examples', 'simple', 'workdir')
if not os.path.exists(workdir):
os.mkdir(workdir)
assert os.path.exists(workdir), "Work directory does not exist"
# Load inputs
inp = pd.read_csv(inp_path).set_index('time')
# Load measurements (ideal results)
ideal = pd.read_csv(ideal_path).set_index('time')
# Load definition of estimated parameters (name, initial value, bounds)
with open(est_path) as f:
est = json.load(f)
# Load definition of known parameters (name, value)
with open(known_path) as f:
known = json.load(f)
# MODEL IDENTIFICATION ==========================================
# Comparing parallel GA against GA using different population sizes
for method in ['MODESTGA', 'GA_LEGACY']:
for pop in [40, 60, 80]:
case_workdir = os.path.join(workdir, f"{method}-{pop}")
if not os.path.exists(case_workdir):
os.mkdir(case_workdir)
session = Estimation(case_workdir, fmu_path, inp, known, est, ideal,
lp_n=1, lp_len=50000, lp_frame=(0, 50000),
vp=(0, 50000), ic_param={'Tstart': 'T'},
methods=((method,)),
ga_opts={
'maxiter': 20, 'pop_size': pop,
'trm_size': 7, 'tol': 1e-3, 'lhs': True},
modestga_opts={
'generations': 20, 'pop_size': pop, 'trm_size': 7,
'tol': 1e-3, 'workers': 2},
ftype='RMSE',
default_log=True, logfile='simple.log')
estimates = session.estimate()
err, res = session.validate()