Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FMU Exception handling and unit test #69

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions modestpy/estim/ga_parallel/ga_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"""
import logging
import os
import pandas as pd
import numpy as np
from random import random
from multiprocessing import Manager
from multiprocessing.managers import BaseManager

import numpy as np
import pandas as pd

from modestga import minimize
from modestpy.fmi.model import Model
from modestpy.estim.estpar import EstPar
Expand All @@ -21,9 +21,20 @@


class ObjectiveFun:
"""Objective function for paralell GA.

This class is callable and it is a wrapper
containing FMU initialization and simulation.

Note:
If FMU simulation fails, the returned error is 1e8
(attribute `err_sim_failed`) and the FMU is
reinitialized before next simulation.
"""
def __init__(self, fmu_path, inp, known, est, ideal, ftype='RMSE'):
self.logger = logging.getLogger(type(self).__name__)
self.model = None
self.sim_failed = False
self.fmu_path = fmu_path
self.inp = inp

Expand All @@ -41,6 +52,7 @@ def __init__(self, fmu_path, inp, known, est, ideal, ftype='RMSE'):
self.output_names = [var for var in ideal]
self.ftype = ftype
self.best_err = 1e7
self.err_sim_failed = 1e8
self.res = pd.DataFrame()

self.logger.debug(f"fmu_path = {fmu_path}")
Expand All @@ -54,12 +66,17 @@ def rescale(self, v, lo, hi):
return lo + v * (hi - lo)

def __call__(self, x, *args):
# Instantiate the model

self.logger.debug(f"x = {x}")
if self.model is None:

# Instantiate the model if
# - there's no model yet
# - previous simulation failed
if (self.model is None) or (self.sim_failed is True):
self.model = self._get_model_instance(
self.fmu_path, self.inp, self.known, self.est, self.output_names
)
self.sim_failed = False
logging.debug(f"Model instance returned: {self.model}")

# Updated parameters are stored in x. Need to update the model.
Expand All @@ -69,15 +86,25 @@ def __call__(self, x, *args):
parameters[ep.name] = self.rescale(v, ep.lo, ep.hi)
except TypeError as e:
raise e

self.logger.debug(f"parameters = {parameters}")

self.model.parameters_from_df(parameters)

self.logger.debug(f"est: {self.est}")
self.logger.debug(f"parameters: {parameters}")
self.logger.debug(f"model: {self.model}")
self.logger.debug("Calling simulation...")
result = self.model.simulate()
self.logger.debug(f"result: {result}")
err = calc_err(result, self.ideal, ftype=self.ftype)['tot']

try:
result = self.model.simulate()
self.logger.debug(f"result: {result}")
err = calc_err(result, self.ideal, ftype=self.ftype)['tot']
except Exception as e:
self.model_failed = True
err = self.err_sim_failed
self.logger.error(str(e))

# Update best error and result
if err < self.best_err:
self.best_err = err
Expand All @@ -92,19 +119,31 @@ def _get_model_instance(self, fmu_path, inputs, known_pars, est, output_names):
self.logger.debug(f"est = {est}")
self.logger.debug(f"estpars_2_df(est) = {estpars_2_df(est)}")
self.logger.debug(f"output_names = {output_names}")
model = Model(fmu_path)
model.inputs_from_df(inputs)
model.parameters_from_df(known_pars)
model.parameters_from_df(estpars_2_df(est))
model.specify_outputs(output_names)

try:
model = Model(fmu_path)
model.inputs_from_df(inputs)
model.parameters_from_df(known_pars)
model.parameters_from_df(estpars_2_df(est))
model.specify_outputs(output_names)
res = model.simulate()
except Exception as e:
self.logger.error(str(e))
msg = "Can't initialize FMU with given parameters. Will try with defaults."
self.logger.error(msg)
model = Model(fmu_path)
model.inputs_from_df(inputs)
model.specify_outputs(output_names)
res = model.simulate()

self.logger.debug(f"Model instance initialized: {model}")
self.logger.debug(f"Model instance initialized: {model.model}")
res = model.simulate()
self.logger.debug(f"test result: {res}")

return model


class MODESTGA(object):
class MODESTGA:
"""
Parallel Genetic Algorithm based on modestga
(https://github.com/krzysztofarendt/modestga).
Expand Down
1 change: 1 addition & 0 deletions modestpy/test/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
from modestpy.test import test_fmpy
from modestpy.test import test_ga
from modestpy.test import test_ga_parallel
from modestpy.test import test_ps
from modestpy.test import test_scipy
from modestpy.test import test_estimation
Expand Down
14 changes: 13 additions & 1 deletion modestpy/test/test_ga_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_modestga_default(self):
par_df = ga.estimate()
assert type(par_df) is pd.DataFrame

def test_modestga_simulation_fail(self):
est_fail = {
"R1": (-99, -100., 0.),
"R2": (-1e6, -1e7, -1e5),
"C": (0., -1e-10, 1e-10)
}
gen = 3
ga = MODESTGA(self.fmu_path, self.inp, self.known, est_fail, self.ideal,
generations=gen)
par_df = ga.estimate()
assert type(par_df) is pd.DataFrame

def test_modestga_1_worker(self):
ga = MODESTGA(self.fmu_path, self.inp, self.known, self.est, self.ideal,
generations=self.gen,
Expand Down Expand Up @@ -107,6 +119,7 @@ def test_modestga_2_workers_large_pop(self):
def suite():
suite = unittest.TestSuite()
suite.addTest(TestMODESTGA('test_modestga_default'))
suite.addTest(TestMODESTGA('test_modestga_simulation_fail'))
suite.addTest(TestMODESTGA('test_modestga_1_worker'))
suite.addTest(TestMODESTGA('test_modestga_2_workers_small_pop'))
suite.addTest(TestMODESTGA('test_modestga_2_workers_large_pop'))
Expand All @@ -116,4 +129,3 @@ def suite():
if __name__ == '__main__':
config_logger(filename='unit_tests.log', level='DEBUG')
unittest.main()

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='modestpy',
version='0.1',
version='0.1.1',
description='FMI-compliant model identification package',
url='https://github.com/sdu-cfei/modest-py',
keywords='fmi fmu optimization model identification estimation',
Expand Down