From 37cefd45c369ffaa8b15c1ebbc2b9df5337094a7 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 29 Jun 2018 11:02:17 -0700 Subject: [PATCH 1/4] Reproduce error. --- unittests/resources/model/SimpleRC_Input.csv | 2 + .../optimization/SimpleRC_Constraints.csv | 2 + unittests/test_optimization.py | 70 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/unittests/resources/model/SimpleRC_Input.csv b/unittests/resources/model/SimpleRC_Input.csv index d27ad8b..6c38e70 100644 --- a/unittests/resources/model/SimpleRC_Input.csv +++ b/unittests/resources/model/SimpleRC_Input.csv @@ -11,3 +11,5 @@ Time,q_flow_csv 01/08/17,100 01/09/17,100 01/10/17,100 +01/11/17,100 +01/12/17,100 diff --git a/unittests/resources/optimization/SimpleRC_Constraints.csv b/unittests/resources/optimization/SimpleRC_Constraints.csv index 2e65666..c3fe81d 100644 --- a/unittests/resources/optimization/SimpleRC_Constraints.csv +++ b/unittests/resources/optimization/SimpleRC_Constraints.csv @@ -11,3 +11,5 @@ Time,q_flow_min,T_db_min,T_db_max 01/08/17,0,293,298 01/09/17,0,293,298 01/10/17,0,293,298 +01/11/17,0,290,300 +01/12/17,0,290,300 diff --git a/unittests/test_optimization.py b/unittests/test_optimization.py index 3a45294..611e7e7 100755 --- a/unittests/test_optimization.py +++ b/unittests/test_optimization.py @@ -18,6 +18,76 @@ from testing import TestCaseMPCPy #%% +class UpdateConstraintData(TestCaseMPCPy): + '''Test simple model tests the updating of constraints. + + ''' + + def setUp(self): + self.start_time = '1/1/2017'; + self.final_time = '1/2/2017'; + # Set .mo path + self.mopath = os.path.join(self.get_unittest_path(), 'resources', 'model', 'Simple.mo'); + # Gather inputs + start_time_exo = '1/1/2017'; + final_time_exo = '1/10/2017'; + control_csv_filepath = os.path.join(self.get_unittest_path(), 'resources', 'model', 'SimpleRC_Input.csv'); + control_variable_map = {'q_flow_csv' : ('q_flow', units.W)}; + self.controls = exodata.ControlFromCSV(control_csv_filepath, control_variable_map); + self.controls.collect_data(start_time_exo, final_time_exo); + # Set measurements + self.measurements = {}; + self.measurements['T_db'] = {'Sample' : variables.Static('T_db_sample', 1800, units.s)}; + self.measurements['q_flow'] = {'Sample' : variables.Static('q_flow_sample', 1800, units.s)}; + # Gather constraints + constraint_csv_filepath = os.path.join(self.get_unittest_path(), 'resources', 'optimization', 'SimpleRC_Constraints.csv'); + constraint_variable_map = {'q_flow_min' : ('q_flow', 'GTE', units.W), \ + 'T_db_min' : ('T_db', 'GTE', units.K), \ + 'T_db_max' : ('T_db', 'LTE', units.K)}; + self.constraints = exodata.ConstraintFromCSV(constraint_csv_filepath, constraint_variable_map); + self.constraints.collect_data(start_time_exo, final_time_exo); + + def test_update_constraints(self): + '''Test the updating of constraints in the optimization. + + ''' + + modelpath = 'Simple.RC'; + # Instantiate model + model = models.Modelica(models.JModelica, \ + models.RMSE, \ + self.measurements, \ + moinfo = (self.mopath, modelpath, {}), \ + control_data = self.controls.data); + # Instantiate optimization problem + opt_problem = optimization.Optimization(model, \ + optimization.EnergyMin, \ + optimization.JModelica, \ + 'q_flow', \ + constraint_data = self.constraints.data); + # Solve optimization problem with default constraints + opt_problem.optimize(self.start_time, self.final_time); + # Check references + df_test = opt_problem.display_measurements('Simulated'); + self.check_df(df_test, 'optimize_measurements.csv'); + df_test = model.control_data['q_flow'].display_data().to_frame(); + df_test.index.name = 'Time' + self.check_df(df_test, 'optimize_control_default.csv'); + # Update constraints and control data + self.constraints.collect_data('1/11/2017', '1/12/2017'); + opt_problem.constraint_data = self.constraints.data; + self.controls.collect_data('1/11/2017', '1/12/2017'); + model.control_data = self.controls.data + # Solve optimization problem with updated constraints + opt_problem.optimize('1/11/2017', '1/12/2017'); + # Check references + df_test = opt_problem.display_measurements('Simulated'); + self.check_df(df_test, 'optimize_measurements_updated constraints.csv'); + df_test = model.control_data['q_flow'].display_data().to_frame(); + df_test.index.name = 'Time' + self.check_df(df_test, 'optimize_control_default_updated_constraints.csv'); + + class OptimizeSimpleFromJModelica(TestCaseMPCPy): '''Test simple model optimization functions. From 0117a4c76d06d051ee10cbef2640bf93a2f43a34 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 29 Jun 2018 12:26:33 -0700 Subject: [PATCH 2/4] Implement fix. --- mpcpy/optimization.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mpcpy/optimization.py b/mpcpy/optimization.py index 503c5c4..56fa70f 100755 --- a/mpcpy/optimization.py +++ b/mpcpy/optimization.py @@ -602,14 +602,23 @@ def _simulate_initial(self, Optimization): ''' - # Set Exogenous + # Update exogenous except constraints self.weather_data = self.Model.weather_data; self.internal_data = self.Model.internal_data; self.control_data = self.Model.control_data; + # Update constraints if type(Optimization._problem_type) is _ParameterEstimate: self.other_inputs = self.Model.other_inputs; self.opt_input_names = self._init_input_names; - # Otherwise inputs set by write control mop + else: + for key in Optimization.constraint_data.keys(): + for field in Optimization.constraint_data[key]: + if field != 'Cyclic' and field != 'Final' and field != 'Initial': + key_new = key.replace('.', '_') + '_' + field; + if key_new not in self.other_inputs.keys(): + raise ValueError ('New constraint {0} found. The optimization problem needs to be re-instantiated to use this constraint.'.format(key_new)) + else: + self.other_inputs[key_new] = Optimization.constraint_data[key][field]; # Set parameters self.parameter_data = {}; for key in self.Model.parameter_data.keys(): From 9ecae22fb044372d377ce747172e7ffae6217b72 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 29 Jun 2018 12:27:17 -0700 Subject: [PATCH 3/4] Pass buggy unittest. --- unittests/test_optimization.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/unittests/test_optimization.py b/unittests/test_optimization.py index 611e7e7..496aded 100755 --- a/unittests/test_optimization.py +++ b/unittests/test_optimization.py @@ -77,7 +77,6 @@ def test_update_constraints(self): self.constraints.collect_data('1/11/2017', '1/12/2017'); opt_problem.constraint_data = self.constraints.data; self.controls.collect_data('1/11/2017', '1/12/2017'); - model.control_data = self.controls.data # Solve optimization problem with updated constraints opt_problem.optimize('1/11/2017', '1/12/2017'); # Check references @@ -86,6 +85,11 @@ def test_update_constraints(self): df_test = model.control_data['q_flow'].display_data().to_frame(); df_test.index.name = 'Time' self.check_df(df_test, 'optimize_control_default_updated_constraints.csv'); + # Check new constraint key raises error + self.constraints.data['New_Constraint'] = opt_problem.constraint_data['q_flow'] + opt_problem.constraint_data = self.constraints.data + with self.assertRaises(ValueError): + opt_problem.optimize('1/11/2017', '1/12/2017'); class OptimizeSimpleFromJModelica(TestCaseMPCPy): From 5232ccf89521fb71c76ab5e181edadb9f5cc7aad Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 29 Jun 2018 13:48:10 -0700 Subject: [PATCH 4/4] Edit and pass unittests. --- ...ze_control_default_updated_constraints.csv | 146 ++++++++++++++++++ ...imize_measurements_updated_constraints.csv | 146 ++++++++++++++++++ unittests/test_optimization.py | 118 ++++++-------- 3 files changed, 336 insertions(+), 74 deletions(-) create mode 100644 unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_control_default_updated_constraints.csv create mode 100644 unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_measurements_updated_constraints.csv diff --git a/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_control_default_updated_constraints.csv b/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_control_default_updated_constraints.csv new file mode 100644 index 0000000..15da7b7 --- /dev/null +++ b/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_control_default_updated_constraints.csv @@ -0,0 +1,146 @@ +Time,q_flow +2017-01-11 00:00:00+00:00,2.2672949739899195e-06 +2017-01-11 00:04:39.091846+00:00,528.9321462294566 +2017-01-11 00:19:20.908154+00:00,1218.83179834123 +2017-01-11 00:30:00+00:00,787.089244327407 +2017-01-11 00:34:39.091846+00:00,1034.3795733756233 +2017-01-11 00:49:20.908154+00:00,971.336710293556 +2017-01-11 01:00:00+00:00,926.1809555935372 +2017-01-11 01:04:39.091846+00:00,906.6310465663571 +2017-01-11 01:19:20.908154+00:00,845.6526306144593 +2017-01-11 01:30:00+00:00,802.3165689185737 +2017-01-11 01:34:39.091846+00:00,783.645488873043 +2017-01-11 01:49:20.908154+00:00,725.7748788578792 +2017-01-11 02:00:00+00:00,685.000001283175 +2017-01-11 02:04:39.091846+00:00,667.5272180971956 +2017-01-11 02:19:20.908154+00:00,613.7545962518052 +2017-01-11 02:30:00+00:00,576.238572273803 +2017-01-11 02:34:39.091846+00:00,560.2630500458715 +2017-01-11 02:49:20.908154+00:00,511.5084808895263 +2017-01-11 03:00:00+00:00,477.8932200951066 +2017-01-11 03:04:39.091846+00:00,463.68830437371804 +2017-01-11 03:19:20.908154+00:00,420.7859921528428 +2017-01-11 03:30:00+00:00,391.6466609892368 +2017-01-11 03:34:39.091846+00:00,379.4554017548232 +2017-01-11 03:49:20.908154+00:00,343.13941697700585 +2017-01-11 04:00:00+00:00,318.97459749442044 +2017-01-11 04:04:39.091846+00:00,309.00559050691913 +2017-01-11 04:19:20.908154+00:00,279.8973097908893 +2017-01-11 04:30:00+00:00,261.12046876538443 +2017-01-11 04:34:39.091846+00:00,253.54428643295108 +2017-01-11 04:49:20.908154+00:00,232.14176058242015 +2017-01-11 05:00:00+00:00,219.0741749850757 +2017-01-11 05:04:39.091846+00:00,214.02044782242513 +2017-01-11 05:19:20.908154+00:00,200.689880038918 +2017-01-11 05:30:00+00:00,193.55513989835978 +2017-01-11 05:34:39.091846+00:00,191.11033851168736 +2017-01-11 05:49:20.908154+00:00,186.0798185567228 +2017-01-11 06:00:00+00:00,185.00000127065962 +2017-01-11 06:04:39.091846+00:00,185.20595682295175 +2017-01-11 06:19:20.908154+00:00,188.56155833849738 +2017-01-11 06:30:00+00:00,193.55513989647406 +2017-01-11 06:34:39.091846+00:00,196.408328363582 +2017-01-11 06:49:20.908154+00:00,208.09263612888364 +2017-01-11 07:00:00+00:00,219.0741749835098 +2017-01-11 07:04:39.091846+00:00,224.52577744945555 +2017-01-11 07:19:20.908154+00:00,244.3388697723479 +2017-01-11 07:30:00+00:00,261.1204687646099 +2017-01-11 07:34:39.091846+00:00,269.07720673136623 +2017-01-11 07:49:20.908154+00:00,296.6800761615569 +2017-01-11 08:00:00+00:00,318.97459749375764 +2017-01-11 08:04:39.091846+00:00,329.3003289035728 +2017-01-11 08:19:20.908154+00:00,364.2206827428595 +2017-01-11 08:30:00+00:00,391.6466609886128 +2017-01-11 08:34:39.091846+00:00,404.1647096490039 +2017-01-11 08:49:20.908154+00:00,445.805051011758 +2017-01-11 09:00:00+00:00,477.89322009454924 +2017-01-11 09:04:39.091846+00:00,492.3893986566727 +2017-01-11 09:19:20.908154+00:00,540.037249807757 +2017-01-11 09:30:00+00:00,576.2385722733634 +2017-01-11 09:34:39.091846+00:00,592.46484703711 +2017-01-11 09:49:20.908154+00:00,645.3049400833756 +2017-01-11 10:00:00+00:00,685.0000012828376 +2017-01-11 10:04:39.091846+00:00,702.6787361223064 +2017-01-11 10:19:20.908154+00:00,759.8069624723402 +2017-01-11 10:30:00+00:00,802.3165689183063 +2017-01-11 10:34:39.091846+00:00,821.1452757128878 +2017-01-11 10:49:20.908154+00:00,881.5841556256353 +2017-01-11 11:00:00+00:00,926.1809561813218 +2017-01-11 11:04:39.091846+00:00,945.8374704712721 +2017-01-11 11:19:20.908154+00:00,1008.5528780054237 +2017-01-11 11:30:00+00:00,1054.4738090641144 +2017-01-11 11:34:39.091846+00:00,1074.6218023739304 +2017-01-11 11:49:20.908154+00:00,1138.5406595694026 +2017-01-11 12:00:00+00:00,1185.000001284471 +2017-01-11 12:04:39.091846+00:00,1205.294735796204 +2017-01-11 12:19:20.908154+00:00,1269.3233733356346 +2017-01-11 12:30:00+00:00,1315.5261935047113 +2017-01-11 12:34:39.091846+00:00,1335.6204206178325 +2017-01-11 12:49:20.908154+00:00,1398.663290811746 +2017-01-11 13:00:00+00:00,1443.8190463873736 +2017-01-11 13:04:39.091846+00:00,1463.3689482384236 +2017-01-11 13:19:20.908154+00:00,1524.3473701499258 +2017-01-11 13:30:00+00:00,1567.6834336500885 +2017-01-11 13:34:39.091846+00:00,1586.3545059317687 +2017-01-11 13:49:20.908154+00:00,1644.225121906511 +2017-01-11 14:00:00+00:00,1685.0000012851226 +2017-01-11 14:04:39.091846+00:00,1702.4727767077131 +2017-01-11 14:19:20.908154+00:00,1756.245404512554 +2017-01-11 14:30:00+00:00,1793.7614302939385 +2017-01-11 14:34:39.091846+00:00,1809.7369447591823 +2017-01-11 14:49:20.908154+00:00,1858.4915198747503 +2017-01-11 15:00:00+00:00,1892.106782471849 +2017-01-11 15:04:39.091846+00:00,1906.3116904315882 +2017-01-11 15:19:20.908154+00:00,1949.2140086113234 +2017-01-11 15:30:00+00:00,1978.353341576598 +2017-01-11 15:34:39.091846+00:00,1990.544593050896 +2017-01-11 15:49:20.908154+00:00,2026.860583786921 +2017-01-11 16:00:00+00:00,2051.025405069849 +2017-01-11 16:04:39.091846+00:00,2060.994404299487 +2017-01-11 16:19:20.908154+00:00,2090.1026909726183 +2017-01-11 16:30:00+00:00,2108.879533796716 +2017-01-11 16:34:39.091846+00:00,2116.455708374542 +2017-01-11 16:49:20.908154+00:00,2137.8582401804474 +2017-01-11 17:00:00+00:00,2150.9258275745424 +2017-01-11 17:04:39.091846+00:00,2155.9795469865644 +2017-01-11 17:19:20.908154+00:00,2169.310120723107 +2017-01-11 17:30:00+00:00,2176.444862659307 +2017-01-11 17:34:39.091846+00:00,2178.8896562987966 +2017-01-11 17:49:20.908154+00:00,2183.9201822044165 +2017-01-11 18:00:00+00:00,2185.000001285495 +2017-01-11 18:04:39.091846+00:00,2184.7940379890397 +2017-01-11 18:19:20.908154+00:00,2181.4384424217446 +2017-01-11 18:30:00+00:00,2176.4448626592975 +2017-01-11 18:34:39.091846+00:00,2173.591666449565 +2017-01-11 18:49:20.908154+00:00,2161.9073646308516 +2017-01-11 19:00:00+00:00,2150.9258275745424 +2017-01-11 19:04:39.091846+00:00,2145.4742173633003 +2017-01-11 19:19:20.908154+00:00,2125.661130987778 +2017-01-11 19:30:00+00:00,2108.8795337967704 +2017-01-11 19:34:39.091846+00:00,2100.9227880797284 +2017-01-11 19:49:20.908154+00:00,2073.319924599509 +2017-01-11 20:00:00+00:00,2051.02540506988 +2017-01-11 20:04:39.091846+00:00,2040.6996659058989 +2017-01-11 20:19:20.908154+00:00,2005.7793180190579 +2017-01-11 20:30:00+00:00,1978.3533415766303 +2017-01-11 20:34:39.091846+00:00,1965.835285159194 +2017-01-11 20:49:20.908154+00:00,1924.1949497508404 +2017-01-11 21:00:00+00:00,1892.1067824718823 +2017-01-11 21:04:39.091846+00:00,1877.6105961505305 +2017-01-11 21:19:20.908154+00:00,1829.9627509553584 +2017-01-11 21:30:00+00:00,1793.7614302940062 +2017-01-11 21:34:39.091846+00:00,1777.5351477693569 +2017-01-11 21:49:20.908154+00:00,1724.695060680112 +2017-01-11 22:00:00+00:00,1685.000001285173 +2017-01-11 22:04:39.091846+00:00,1667.321258683616 +2017-01-11 22:19:20.908154+00:00,1610.1930382914277 +2017-01-11 22:30:00+00:00,1567.6834336501797 +2017-01-11 22:34:39.091846+00:00,1548.8547190926342 +2017-01-11 22:49:20.908154+00:00,1488.4158451383455 +2017-01-11 23:00:00+00:00,1443.819046387484 +2017-01-11 23:04:39.091846+00:00,1424.162524333952 +2017-01-11 23:19:20.908154+00:00,1361.4471227587085 +2017-01-11 23:30:00+00:00,1315.5261935048027 +2017-01-11 23:34:39.091846+00:00,1295.3781925580317 +2017-01-11 23:49:20.908154+00:00,1231.4593406669574 +2017-01-12 00:00:00+00:00,1184.9999978763547 diff --git a/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_measurements_updated_constraints.csv b/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_measurements_updated_constraints.csv new file mode 100644 index 0000000..6edf626 --- /dev/null +++ b/unittests/references/test_optimization/OptimizeSimpleFromJModelica/optimize_measurements_updated_constraints.csv @@ -0,0 +1,146 @@ +Time,T_db,q_flow +2017-01-11 00:00:00+00:00,295.0,2.26729472454515e-06 +2017-01-11 00:04:39.091846+00:00,291.841490986544,528.932146229457 +2017-01-11 00:19:20.908154+00:00,289.999999992005,1218.83179834123 +2017-01-11 00:30:00+00:00,290.000000004353,787.089244327407 +2017-01-11 00:34:39.091846+00:00,289.999999993194,1034.37957337562 +2017-01-11 00:49:20.908154+00:00,289.999999992348,971.336710293556 +2017-01-11 01:00:00+00:00,290.000000000824,926.180955593537 +2017-01-11 01:04:39.091846+00:00,289.999999993194,906.631046566357 +2017-01-11 01:19:20.908154+00:00,289.999999992348,845.652630614459 +2017-01-11 01:30:00+00:00,290.000000000823,802.316568918574 +2017-01-11 01:34:39.091846+00:00,289.999999993193,783.645488873043 +2017-01-11 01:49:20.908154+00:00,289.999999992348,725.774878857879 +2017-01-11 02:00:00+00:00,290.000000000822,685.000001283175 +2017-01-11 02:04:39.091846+00:00,289.999999993193,667.527218097196 +2017-01-11 02:19:20.908154+00:00,289.999999992349,613.754596251805 +2017-01-11 02:30:00+00:00,290.000000000821,576.238572273803 +2017-01-11 02:34:39.091846+00:00,289.999999993192,560.263050045872 +2017-01-11 02:49:20.908154+00:00,289.999999992349,511.508480889528 +2017-01-11 03:00:00+00:00,290.000000000818,477.893220095107 +2017-01-11 03:04:39.091846+00:00,289.999999993191,463.688304373717 +2017-01-11 03:19:20.908154+00:00,289.999999992349,420.785992152844 +2017-01-11 03:30:00+00:00,290.000000000815,391.646660989237 +2017-01-11 03:34:39.091846+00:00,289.99999999319,379.455401754822 +2017-01-11 03:49:20.908154+00:00,289.99999999235,343.139416977007 +2017-01-11 04:00:00+00:00,290.000000000811,318.97459749442 +2017-01-11 04:04:39.091846+00:00,289.999999993189,309.005590506918 +2017-01-11 04:19:20.908154+00:00,289.99999999235,279.89730979089 +2017-01-11 04:30:00+00:00,290.000000000805,261.120468765384 +2017-01-11 04:34:39.091846+00:00,289.999999993187,253.54428643295 +2017-01-11 04:49:20.908154+00:00,289.999999992351,232.141760582421 +2017-01-11 05:00:00+00:00,290.000000000798,219.074174985076 +2017-01-11 05:04:39.091846+00:00,289.999999993186,214.020447822425 +2017-01-11 05:19:20.908154+00:00,289.999999992351,200.689880038918 +2017-01-11 05:30:00+00:00,290.00000000079,193.55513989836 +2017-01-11 05:34:39.091846+00:00,289.999999993185,191.110338511687 +2017-01-11 05:49:20.908154+00:00,289.999999992351,186.079818556723 +2017-01-11 06:00:00+00:00,290.000000000784,185.00000127066 +2017-01-11 06:04:39.091846+00:00,289.999999993185,185.205956822952 +2017-01-11 06:19:20.908154+00:00,289.999999992351,188.561558338497 +2017-01-11 06:30:00+00:00,290.00000000078,193.555139896474 +2017-01-11 06:34:39.091846+00:00,289.999999993186,196.408328363582 +2017-01-11 06:49:20.908154+00:00,289.99999999235,208.092636128883 +2017-01-11 07:00:00+00:00,290.000000000783,219.07417498351 +2017-01-11 07:04:39.091846+00:00,289.999999993188,224.525777449456 +2017-01-11 07:19:20.908154+00:00,289.99999999235,244.338869772347 +2017-01-11 07:30:00+00:00,290.000000000791,261.12046876461 +2017-01-11 07:34:39.091846+00:00,289.999999993189,269.077206731367 +2017-01-11 07:49:20.908154+00:00,289.999999992349,296.680076161556 +2017-01-11 08:00:00+00:00,290.000000000799,318.974597493758 +2017-01-11 08:04:39.091846+00:00,289.99999999319,329.300328903574 +2017-01-11 08:19:20.908154+00:00,289.999999992349,364.220682742858 +2017-01-11 08:30:00+00:00,290.000000000806,391.646660988613 +2017-01-11 08:34:39.091846+00:00,289.999999993191,404.164709649005 +2017-01-11 08:49:20.908154+00:00,289.999999992349,445.805051011757 +2017-01-11 09:00:00+00:00,290.000000000811,477.893220094549 +2017-01-11 09:04:39.091846+00:00,289.999999993192,492.389398656674 +2017-01-11 09:19:20.908154+00:00,289.999999992348,540.037249807756 +2017-01-11 09:30:00+00:00,290.000000000814,576.238572273363 +2017-01-11 09:34:39.091846+00:00,289.999999993193,592.464847037112 +2017-01-11 09:49:20.908154+00:00,289.999999992348,645.304940083374 +2017-01-11 10:00:00+00:00,290.000000000817,685.000001282838 +2017-01-11 10:04:39.091846+00:00,289.999999993193,702.678736122308 +2017-01-11 10:19:20.908154+00:00,289.999999992348,759.806962472338 +2017-01-11 10:30:00+00:00,290.00000000082,802.316568918306 +2017-01-11 10:34:39.091846+00:00,289.999999993194,821.14527571289 +2017-01-11 10:49:20.908154+00:00,289.999999992348,881.584155625633 +2017-01-11 11:00:00+00:00,290.000000000821,926.180956181322 +2017-01-11 11:04:39.091846+00:00,289.999999993194,945.837470471274 +2017-01-11 11:19:20.908154+00:00,289.999999992348,1008.55287800542 +2017-01-11 11:30:00+00:00,290.000000000823,1054.47380906411 +2017-01-11 11:34:39.091846+00:00,289.999999993194,1074.62180237393 +2017-01-11 11:49:20.908154+00:00,289.999999992348,1138.5406595694 +2017-01-11 12:00:00+00:00,290.000000000824,1185.00000128447 +2017-01-11 12:04:39.091846+00:00,289.999999993194,1205.29473579621 +2017-01-11 12:19:20.908154+00:00,289.999999992348,1269.32337333563 +2017-01-11 12:30:00+00:00,290.000000000825,1315.52619350471 +2017-01-11 12:34:39.091846+00:00,289.999999993195,1335.62042061783 +2017-01-11 12:49:20.908154+00:00,289.999999992348,1398.66329081174 +2017-01-11 13:00:00+00:00,290.000000000825,1443.81904638737 +2017-01-11 13:04:39.091846+00:00,289.999999993195,1463.36894823843 +2017-01-11 13:19:20.908154+00:00,289.999999992348,1524.34737014992 +2017-01-11 13:30:00+00:00,290.000000000826,1567.68343365009 +2017-01-11 13:34:39.091846+00:00,289.999999993195,1586.35450593177 +2017-01-11 13:49:20.908154+00:00,289.999999992348,1644.22512190651 +2017-01-11 14:00:00+00:00,290.000000000826,1685.00000128512 +2017-01-11 14:04:39.091846+00:00,289.999999993195,1702.47277670771 +2017-01-11 14:19:20.908154+00:00,289.999999992348,1756.24540451255 +2017-01-11 14:30:00+00:00,290.000000000827,1793.76143029394 +2017-01-11 14:34:39.091846+00:00,289.999999993195,1809.73694475918 +2017-01-11 14:49:20.908154+00:00,289.999999992348,1858.49151987475 +2017-01-11 15:00:00+00:00,290.000000000827,1892.10678247185 +2017-01-11 15:04:39.091846+00:00,289.999999993195,1906.31169043159 +2017-01-11 15:19:20.908154+00:00,289.999999992348,1949.21400861132 +2017-01-11 15:30:00+00:00,290.000000000827,1978.3533415766 +2017-01-11 15:34:39.091846+00:00,289.999999993195,1990.5445930509 +2017-01-11 15:49:20.908154+00:00,289.999999992348,2026.86058378692 +2017-01-11 16:00:00+00:00,290.000000000827,2051.02540506985 +2017-01-11 16:04:39.091846+00:00,289.999999993195,2060.99440429949 +2017-01-11 16:19:20.908154+00:00,289.999999992348,2090.10269097262 +2017-01-11 16:30:00+00:00,290.000000000828,2108.87953379672 +2017-01-11 16:34:39.091846+00:00,289.999999993195,2116.45570837454 +2017-01-11 16:49:20.908154+00:00,289.999999992348,2137.85824018045 +2017-01-11 17:00:00+00:00,290.000000000828,2150.92582757454 +2017-01-11 17:04:39.091846+00:00,289.999999993195,2155.97954698657 +2017-01-11 17:19:20.908154+00:00,289.999999992348,2169.31012072311 +2017-01-11 17:30:00+00:00,290.000000000828,2176.44486265931 +2017-01-11 17:34:39.091846+00:00,289.999999993195,2178.8896562988 +2017-01-11 17:49:20.908154+00:00,289.999999992348,2183.92018220442 +2017-01-11 18:00:00+00:00,290.000000000828,2185.00000128549 +2017-01-11 18:04:39.091846+00:00,289.999999993195,2184.79403798904 +2017-01-11 18:19:20.908154+00:00,289.999999992348,2181.43844242174 +2017-01-11 18:30:00+00:00,290.000000000828,2176.4448626593 +2017-01-11 18:34:39.091846+00:00,289.999999993195,2173.59166644956 +2017-01-11 18:49:20.908154+00:00,289.999999992348,2161.90736463085 +2017-01-11 19:00:00+00:00,290.000000000828,2150.92582757454 +2017-01-11 19:04:39.091846+00:00,289.999999993195,2145.4742173633 +2017-01-11 19:19:20.908154+00:00,289.999999992348,2125.66113098778 +2017-01-11 19:30:00+00:00,290.000000000828,2108.87953379677 +2017-01-11 19:34:39.091846+00:00,289.999999993195,2100.92278807973 +2017-01-11 19:49:20.908154+00:00,289.999999992348,2073.31992459951 +2017-01-11 20:00:00+00:00,290.000000000828,2051.02540506988 +2017-01-11 20:04:39.091846+00:00,289.999999993195,2040.6996659059 +2017-01-11 20:19:20.908154+00:00,289.999999992348,2005.77931801906 +2017-01-11 20:30:00+00:00,290.000000000828,1978.35334157663 +2017-01-11 20:34:39.091846+00:00,289.999999993195,1965.83528515919 +2017-01-11 20:49:20.908154+00:00,289.999999992348,1924.19494975084 +2017-01-11 21:00:00+00:00,290.000000000828,1892.10678247188 +2017-01-11 21:04:39.091846+00:00,289.999999993195,1877.61059615053 +2017-01-11 21:19:20.908154+00:00,289.999999992348,1829.96275095536 +2017-01-11 21:30:00+00:00,290.000000000827,1793.76143029401 +2017-01-11 21:34:39.091846+00:00,289.999999993195,1777.53514776935 +2017-01-11 21:49:20.908154+00:00,289.999999992348,1724.69506068011 +2017-01-11 22:00:00+00:00,290.000000000827,1685.00000128517 +2017-01-11 22:04:39.091846+00:00,289.999999993195,1667.32125868361 +2017-01-11 22:19:20.908154+00:00,289.999999992348,1610.19303829143 +2017-01-11 22:30:00+00:00,290.000000000827,1567.68343365018 +2017-01-11 22:34:39.091846+00:00,289.999999993195,1548.85471909263 +2017-01-11 22:49:20.908154+00:00,289.999999992348,1488.41584513835 +2017-01-11 23:00:00+00:00,290.000000000827,1443.81904638748 +2017-01-11 23:04:39.091846+00:00,289.999999993195,1424.16252433395 +2017-01-11 23:19:20.908154+00:00,289.999999992348,1361.44712275871 +2017-01-11 23:30:00+00:00,290.000000000826,1315.5261935048 +2017-01-11 23:34:39.091846+00:00,289.999999993194,1295.37819255803 +2017-01-11 23:49:20.908154+00:00,289.999999992348,1231.45934066696 +2017-01-12 00:00:00+00:00,289.999999991804,1184.99999787635 diff --git a/unittests/test_optimization.py b/unittests/test_optimization.py index 496aded..93544de 100755 --- a/unittests/test_optimization.py +++ b/unittests/test_optimization.py @@ -18,80 +18,6 @@ from testing import TestCaseMPCPy #%% -class UpdateConstraintData(TestCaseMPCPy): - '''Test simple model tests the updating of constraints. - - ''' - - def setUp(self): - self.start_time = '1/1/2017'; - self.final_time = '1/2/2017'; - # Set .mo path - self.mopath = os.path.join(self.get_unittest_path(), 'resources', 'model', 'Simple.mo'); - # Gather inputs - start_time_exo = '1/1/2017'; - final_time_exo = '1/10/2017'; - control_csv_filepath = os.path.join(self.get_unittest_path(), 'resources', 'model', 'SimpleRC_Input.csv'); - control_variable_map = {'q_flow_csv' : ('q_flow', units.W)}; - self.controls = exodata.ControlFromCSV(control_csv_filepath, control_variable_map); - self.controls.collect_data(start_time_exo, final_time_exo); - # Set measurements - self.measurements = {}; - self.measurements['T_db'] = {'Sample' : variables.Static('T_db_sample', 1800, units.s)}; - self.measurements['q_flow'] = {'Sample' : variables.Static('q_flow_sample', 1800, units.s)}; - # Gather constraints - constraint_csv_filepath = os.path.join(self.get_unittest_path(), 'resources', 'optimization', 'SimpleRC_Constraints.csv'); - constraint_variable_map = {'q_flow_min' : ('q_flow', 'GTE', units.W), \ - 'T_db_min' : ('T_db', 'GTE', units.K), \ - 'T_db_max' : ('T_db', 'LTE', units.K)}; - self.constraints = exodata.ConstraintFromCSV(constraint_csv_filepath, constraint_variable_map); - self.constraints.collect_data(start_time_exo, final_time_exo); - - def test_update_constraints(self): - '''Test the updating of constraints in the optimization. - - ''' - - modelpath = 'Simple.RC'; - # Instantiate model - model = models.Modelica(models.JModelica, \ - models.RMSE, \ - self.measurements, \ - moinfo = (self.mopath, modelpath, {}), \ - control_data = self.controls.data); - # Instantiate optimization problem - opt_problem = optimization.Optimization(model, \ - optimization.EnergyMin, \ - optimization.JModelica, \ - 'q_flow', \ - constraint_data = self.constraints.data); - # Solve optimization problem with default constraints - opt_problem.optimize(self.start_time, self.final_time); - # Check references - df_test = opt_problem.display_measurements('Simulated'); - self.check_df(df_test, 'optimize_measurements.csv'); - df_test = model.control_data['q_flow'].display_data().to_frame(); - df_test.index.name = 'Time' - self.check_df(df_test, 'optimize_control_default.csv'); - # Update constraints and control data - self.constraints.collect_data('1/11/2017', '1/12/2017'); - opt_problem.constraint_data = self.constraints.data; - self.controls.collect_data('1/11/2017', '1/12/2017'); - # Solve optimization problem with updated constraints - opt_problem.optimize('1/11/2017', '1/12/2017'); - # Check references - df_test = opt_problem.display_measurements('Simulated'); - self.check_df(df_test, 'optimize_measurements_updated constraints.csv'); - df_test = model.control_data['q_flow'].display_data().to_frame(); - df_test.index.name = 'Time' - self.check_df(df_test, 'optimize_control_default_updated_constraints.csv'); - # Check new constraint key raises error - self.constraints.data['New_Constraint'] = opt_problem.constraint_data['q_flow'] - opt_problem.constraint_data = self.constraints.data - with self.assertRaises(ValueError): - opt_problem.optimize('1/11/2017', '1/12/2017'); - - class OptimizeSimpleFromJModelica(TestCaseMPCPy): '''Test simple model optimization functions. @@ -476,6 +402,50 @@ def test_optimize_long(self): df_test.index.name = 'Time' self.check_df(df_test, 'optimize_long_control.csv'); + def test_update_constraints(self): + '''Test the updating of constraints in the optimization. + + ''' + + modelpath = 'Simple.RC'; + # Instantiate model + model = models.Modelica(models.JModelica, \ + models.RMSE, \ + self.measurements, \ + moinfo = (self.mopath, modelpath, {}), \ + control_data = self.controls.data); + # Instantiate optimization problem + opt_problem = optimization.Optimization(model, \ + optimization.EnergyMin, \ + optimization.JModelica, \ + 'q_flow', \ + constraint_data = self.constraints.data); + # Solve optimization problem with default constraints + opt_problem.optimize(self.start_time, self.final_time); + # Check references + df_test = opt_problem.display_measurements('Simulated'); + self.check_df(df_test, 'optimize_measurements.csv'); + df_test = model.control_data['q_flow'].display_data().to_frame(); + df_test.index.name = 'Time' + self.check_df(df_test, 'optimize_control_default.csv'); + # Update constraints and control data + self.constraints.collect_data('1/11/2017', '1/12/2017'); + opt_problem.constraint_data = self.constraints.data; + self.controls.collect_data('1/11/2017', '1/12/2017'); + # Solve optimization problem with updated constraints + opt_problem.optimize('1/11/2017', '1/12/2017'); + # Check references + df_test = opt_problem.display_measurements('Simulated'); + self.check_df(df_test, 'optimize_measurements_updated_constraints.csv'); + df_test = model.control_data['q_flow'].display_data().to_frame(); + df_test.index.name = 'Time' + self.check_df(df_test, 'optimize_control_default_updated_constraints.csv'); + # Check new constraint key raises error + self.constraints.data['New_Constraint'] = opt_problem.constraint_data['q_flow'] + opt_problem.constraint_data = self.constraints.data + with self.assertRaises(ValueError): + opt_problem.optimize('1/11/2017', '1/12/2017'); + #%% Temperature tests class OptimizeAdvancedFromJModelica(TestCaseMPCPy): '''Tests for the optimization of a model using JModelica.