def test_estimation_rmse(self): ga_opts = {'maxiter': 3, 'pop_size': 8, 'trm_size': 3} ps_opts = {'maxiter': 3} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('GA', 'PS'), ga_opts=ga_opts, ps_opts=ps_opts, seed=1, ftype='RMSE') estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertLess(err['tot'], 1.48)
def test_estimation_basic(self): """Will use default methods ('MODESTGA', 'PS')""" modestga_opts = { 'generations': 2, 'workers': 1, 'pop_size': 8, 'trm_size': 3 } ps_opts = {'maxiter': 2} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, modestga_opts=modestga_opts, ps_opts=ps_opts, default_log=False) estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0)
def test_estimation_rmse(self): modestga_opts = {"generations": 8} ps_opts = {"maxiter": 16} session = Estimation( self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={"Tstart": "T"}, methods=("MODESTGA", "PS"), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype="RMSE", default_log=False, ) estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertLess(err["tot"], 1.48)
def test_estimation_rmse(self): modestga_opts = {'generations': 8} ps_opts = {'maxiter': 16} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('MODESTGA', 'PS'), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype='RMSE', default_log=False) estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertLess(err['tot'], 1.48)
def test_estimation_basic_parallel(self): """Will use default methods ('MODESTGA', 'PS')""" modestga_opts = { "generations": 2, "workers": 2, "pop_size": 16, "trm_size": 3 } ps_opts = {"maxiter": 2} session = Estimation( self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, modestga_opts=modestga_opts, ps_opts=ps_opts, default_log=False, ) estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0)
def test_estimation_all_args(self): ga_opts = {'maxiter': 3, 'pop_size': 10, 'trm_size': 5, 'lhs': True} ps_opts = {'maxiter': 3} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=2, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('GA', 'PS'), ga_opts=ga_opts, ps_opts=ps_opts, seed=1, ftype='NRMSE') estimates = session.estimate() err, res = session.validate() # Standard validation period err2, res2 = session.validate(vp=(25000, 28600)) self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertIsNotNone(err2) self.assertIsNotNone(res2) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertGreater(len(res2.index), 1) self.assertGreater(len(res2.columns), 0) self.assertEqual(session.lp[0][0], 0) self.assertEqual(session.lp[0][1], 3600) # Enabling next line triggers the Matplotlib error (issue #20) # raw_input('Continue...') self.assertLess(err['tot'], 1.7) # NRMSE
def test_ps_only(self): ga_opts = {'maxiter': 0} ps_opts = {'maxiter': 1} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('PS', ), ga_opts=ga_opts, ps_opts=ps_opts, seed=1, ftype='RMSE') session.estimate()
def test_estimation_basic(self): ga_opts = {'maxiter': 3} ps_opts = {'maxiter': 3} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, ga_opts=ga_opts, ps_opts=ps_opts) estimates = session.estimate() err, res = session.validate() self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0)
def test_opts(self): modestga_opts = { 'workers': 2, # CPU cores to use 'generations': 10, # Max. number of generations 'pop_size': 40, # Population size 'mut_rate': 0.05, # Mutation rate 'trm_size': 10, # Tournament size 'tol': 1e-4, # Solution tolerance 'inertia': 20 # Max. number of non-improving generations } ps_opts = {'maxiter': 10, 'rel_step': 0.1, 'tol': 0.001, 'try_lim': 10} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, methods=('MODESTGA', 'PS'), modestga_opts=modestga_opts, ps_opts=ps_opts, default_log=False) modestga_return = session.MODESTGA_OPTS ps_return = session.PS_OPTS def extractDictAFromB(A, B): return dict([(k, B[k]) for k in A.keys() if k in B.keys()]) self.assertEqual(modestga_opts, extractDictAFromB(modestga_opts, modestga_return)) self.assertEqual(ps_opts, extractDictAFromB(ps_opts, ps_return))
def test_opts(self): modestga_opts = { "workers": 2, # CPU cores to use "generations": 10, # Max. number of generations "pop_size": 40, # Population size "mut_rate": 0.05, # Mutation rate "trm_size": 10, # Tournament size "tol": 1e-4, # Solution tolerance "inertia": 20, # Max. number of non-improving generations } ps_opts = {"maxiter": 10, "rel_step": 0.1, "tol": 0.001, "try_lim": 10} session = Estimation( self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, methods=("MODESTGA", "PS"), modestga_opts=modestga_opts, ps_opts=ps_opts, default_log=False, ) modestga_return = session.MODESTGA_OPTS ps_return = session.PS_OPTS def extractDictAFromB(A, B): return dict([(k, B[k]) for k in A.keys() if k in B.keys()]) self.assertEqual(modestga_opts, extractDictAFromB(modestga_opts, modestga_return)) self.assertEqual(ps_opts, extractDictAFromB(ps_opts, ps_return))
def test_estimation_all_args(self): modestga_opts = { "generations": 2, "workers": 2, "pop_size": 16, "trm_size": 3 } ps_opts = {"maxiter": 3} session = Estimation( self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=2, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={"Tstart": "T"}, methods=("MODESTGA", "PS"), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype="NRMSE", default_log=False, ) estimates = session.estimate() err, res = session.validate() # Standard validation period err2, res2 = session.validate(vp=(25000, 28600)) self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertIsNotNone(err2) self.assertIsNotNone(res2) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertGreater(len(res2.index), 1) self.assertGreater(len(res2.columns), 0) self.assertEqual(session.lp[0][0], 0) self.assertEqual(session.lp[0][1], 3600) self.assertLess(err["tot"], 1.7) # NRMSE
def test_estimation_all_args(self): modestga_opts = { 'generations': 2, 'workers': 2, 'pop_size': 16, 'trm_size': 3 } ps_opts = {'maxiter': 3} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=2, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('MODESTGA', 'PS'), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype='NRMSE', default_log=False) estimates = session.estimate() err, res = session.validate() # Standard validation period err2, res2 = session.validate(vp=(25000, 28600)) self.assertIsNotNone(estimates) self.assertGreater(len(estimates), 0) self.assertIsNotNone(err) self.assertIsNotNone(res) self.assertIsNotNone(err2) self.assertIsNotNone(res2) self.assertGreater(len(res.index), 1) self.assertGreater(len(res.columns), 0) self.assertGreater(len(res2.index), 1) self.assertGreater(len(res2.columns), 0) self.assertEqual(session.lp[0][0], 0) self.assertEqual(session.lp[0][1], 3600) self.assertLess(err['tot'], 1.7) # NRMSE
def test_ps_only(self): modestga_opts = {'generations': 0} ps_opts = {'maxiter': 1} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('PS', ), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype='RMSE', default_log=False) session.estimate()
def test_opts(self): ga_opts = {'maxiter': 10, 'pop_size': 10, 'look_back': 10, 'tol': 0.001, 'mut': 0.02, 'mut_inc': 0.3, 'trm_size': 3} ps_opts = {'maxiter': 10, 'rel_step': 0.1, 'tol': 0.001, 'try_lim': 10} session = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, ga_opts=ga_opts, ps_opts=ps_opts) ga_return = session.GA_OPTS ps_return = session.PS_OPTS self.assertDictContainsSubset(ga_opts, ga_return) self.assertDictContainsSubset(ps_opts, ps_return)
def test_seed(self): ga_opts = {'maxiter': 10} ps_opts = {'maxiter': 5} # Run 1 session1 = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('GA', ), ga_opts=ga_opts, ps_opts=ps_opts, seed=1, ftype='RMSE') estimates1 = session1.estimate() # Run 2 session2 = Estimation(self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={'Tstart': 'T'}, methods=('GA', ), ga_opts=ga_opts, ps_opts=ps_opts, seed=1, ftype='RMSE') estimates2 = session2.estimate() # Check if estimates are the same same = True for key in estimates1: if estimates1[key] != estimates2[key]: same = False self.assertTrue( same, "Different estimates obtained despite the same seed" )
def test_ps_only(self): modestga_opts = {"generations": 0} ps_opts = {"maxiter": 1} session = Estimation( self.tmpdir, self.fmu_path, self.inp, self.known, self.est, self.ideal, lp_n=1, lp_len=3600, lp_frame=(0, 3600), vp=(20000, 40000), ic_param={"Tstart": "T"}, methods=("PS", ), modestga_opts=modestga_opts, ps_opts=ps_opts, ftype="RMSE", default_log=False, ) session.estimate()
# Working directory workdir = os.path.join('examples', 'sin', 'workdir') if not os.path.exists(workdir): os.mkdir(workdir) assert os.path.exists(workdir), "Work directory does not exist" # Estimated and known parameters known = {} est = {'a': (7., 0., 8.), 'b': (2.0, 1., 4.)} # Session session = Estimation(workdir, fmu_path, inp, known, est, ideal, methods=('GA', 'PS'), ga_opts={'maxiter': 20, 'tol': 1e-6, 'lhs': False, 'pop_size': 8, 'trm_size': 3}, ps_opts={'maxiter': 500, 'tol': 1e-8}, scipy_opts={'scipy_opts': {'tol': 1e-6}}, ftype='RMSE', seed=1) t0 = time.time() estimates = session.estimate() t1 = time.time() err, res = session.validate() print("ELAPSED TIME: {}".format(t1 - t0)) # Check estimates ========================================= epsilon = 1e-2 a_err = abs(estimates['a'] - a) b_err = abs(estimates['b'] - b)
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 ========================================== session = Estimation( workdir, fmu_path, inp, known, est, ideal, lp_n=2, lp_len=25000, lp_frame=(0, 25000), vp=(150000, 215940), ic_param={"Tstart": "T"}, methods=("PS",), ps_opts={"maxiter": 300, "tol": 1e-6}, ftype="RMSE", ) estimates = session.estimate() err, res = session.validate()
known["R1"] = 0.1 known["R2"] = 0.25 # MODEL IDENTIFICATION ========================================== session = Estimation( workdir, fmu_path, inp, known, est, ideal, lp_n=2, lp_len=50000, lp_frame=(0, 50000), vp=(0, 50000), ic_param={"Tstart": "T"}, methods=("MODESTGA", "PS"), ps_opts={ "maxiter": 500, "tol": 1e-6 }, scipy_opts={}, ftype="RMSE", default_log=True, logfile="simple.log", ) estimates = session.estimate() err, res = session.validate()
# Working directory workdir = os.path.join('examples', 'lin', 'workdir') if not os.path.exists(workdir): os.mkdir(workdir) assert os.path.exists(workdir), "Work directory does not exist" # Estimated and known parameters known = {} est = {'a': (5., 0., 8.), 'b': (5., -4., 8.)} # Session session = Estimation(workdir, fmu_path, inp, known, est, ideal, lp_n=1, lp_len=86400/2, lp_frame=(0, 86400/2), vp=(86400/2, 86400), methods=('GA', 'SCIPY'), ga_opts={'maxiter': 10, 'tol': 1e-8, 'lhs': True}, ps_opts={'maxiter': 1000, 'tol': 1e-12}, scipy_opts={'solver': 'L-BFGS-B', 'options': {'eps': 1e-12}}, ftype='RMSE', seed=1) t0 = time.time() estimates = session.estimate() t1 = time.time() err, res = session.validate() print("ELAPSED TIME: {}".format(t1 - t0)) # Check estimates ========================================= epsilon = 1e-3 a_err = abs(estimates['a'] - a)
# Comparing parallel GA against GA using different population sizes case_workdir = os.path.join(workdir, "modestga") 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=('MODESTGA', ), modestga_opts={ 'generations': 20, # Max. number of generations 'pop_size': 60, # Population size 'trm_size': 7, # Tournament size 'tol': 1e-3, # Absolute tolerance 'workers': 3 # Number of CPUs to use }, ftype='RMSE', default_log=True, logfile='simple.log') estimates = session.estimate() err, res = session.validate()
# 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 ========================================== session = Estimation(workdir, fmu_path, inp, known, est, ideal, lp_n=2, lp_len=25000, lp_frame=(0, 25000), vp=(150000, 215940), ic_param={'Tstart': 'T'}, methods=('PS', ), ps_opts={ 'maxiter': 300, 'tol': 1e-6 }, ftype='RMSE', seed=1) estimates = session.estimate() err, res = session.validate()
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", )
known = json.load(f) # MODEL IDENTIFICATION ========================================== session = Estimation(workdir, fmu_path, inp, known, est, ideal, lp_n=2, lp_len=50000, lp_frame=(0, 50000), vp=(0, 50000), ic_param={'Tstart': 'T'}, methods=('GA', 'PS'), ga_opts={ 'maxiter': 5, 'tol': 0.001, 'lhs': True }, ps_opts={ 'maxiter': 500, 'tol': 1e-6 }, scipy_opts={}, ftype='RMSE', seed=1, default_log=True, logfile='simple.log') estimates = session.estimate() err, res = session.validate()
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()