Skip to content

Commit

Permalink
Merge pull request #98 from lbl-srg/issue97_sepMeaOpt
Browse files Browse the repository at this point in the history
Issue97 sep mea opt
  • Loading branch information
dhblum authored Dec 9, 2017
2 parents 052ecbc + 9dd4659 commit 33993d2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 155 deletions.
30 changes: 24 additions & 6 deletions mpcpy/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from pymodelica import compile_fmu
from pyjmi import transfer_optimization_problem;
from pyjmi.optimization.casadi_collocation import ExternalData
import copy

#%% Optimization Class
class Optimization(utility._mpcpyPandas):
Expand Down Expand Up @@ -398,7 +399,12 @@ class JModelica(_Package, utility._FMU):
This package is compatible with ``models.Modelica`` objects. Please
consult the JModelica user guide for more information regarding
optimization options and solver statistics.
optimization options and solver statistics.
The option 'n_e' is overwritten by default to equal the number of
points as calculated using the model measurements sample rate and
length of optimization horizon (same as if model is simulated).
However, editing this option will overwrite this default.
'''

Expand Down Expand Up @@ -621,7 +627,8 @@ def _solve(self, Optimization):
self.opt_options['external_data'] = self.external_data;
self.opt_options['init_traj'] = self.res_init;
self.opt_options['nominal_traj'] = self.res_init;
self.opt_options['n_e'] = self._sim_opts['ncp'];
if self._step_from_meas:
self.opt_options['n_e'] = self._sim_opts['ncp'];
# Set parameters if they exist
if hasattr(self, 'parameter_data'):
for key in self.parameter_data.keys():
Expand Down Expand Up @@ -734,18 +741,29 @@ def _get_optimization_options(self):
'''

return self.opt_options;
return copy.deepcopy(self.opt_options);

def _set_optimization_options(self, opt_options, init = False):
'''Set the JModelica optimization options using a dictionary.
'''
# Check that automatically set options are not being changed
if not init:

# Initialize with specific default options
if init:
# Optimization control step
self._step_from_meas = True;
opt_options['n_e'] = 0;
# Check on automatically set options
else:
for key in opt_options:
if key in ['external_data', 'init_traj', 'nominal_traj', 'n_e']:
if key in ['external_data', 'init_traj', 'nominal_traj']:
# These cannot be changed
if opt_options[key] != self.opt_options[key]:
raise KeyError('Key {} is set automatically upon solve.'.format(key));
if key is 'n_e':
# This can be changed but flag needs to be set
if opt_options[key] != self.opt_options[key]:
self._step_from_meas = False;
# Set options
self.opt_options = opt_options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"init_traj": null,
"mutable_external_data": true,
"n_cp": 3,
"n_e": 50,
"n_e": 0,
"n_eval_points": 20,
"named_vars": false,
"nominal_traj": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"init_traj": null,
"mutable_external_data": true,
"n_cp": 3,
"n_e": 50,
"n_e": 2,
"n_eval_points": 20,
"named_vars": false,
"nominal_traj": null,
Expand All @@ -40,7 +40,7 @@
"print_condition_numbers": false,
"quadrature_constraint": true,
"result_file_name": "",
"result_mode": "collocation_points",
"result_mode": "mesh_points",
"solver": "IPOPT",
"variable_scaling": true,
"variable_scaling_allow_update": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,146 +1,4 @@
Time,T_db,q_flow
2017-01-01 00:00:00+00:00,295.0,98.573360993
2017-01-01 00:04:39.091846+00:00,293.050567093,106.128587008
2017-01-01 00:19:20.908154+00:00,286.978874095,114.135775245
2017-01-01 00:30:00+00:00,282.574007604,104.876008907
2017-01-01 00:34:39.091846+00:00,282.422204426,108.742622415
2017-01-01 00:49:20.908154+00:00,281.978746596,112.029571618
2017-01-01 01:00:00+00:00,281.643803605,105.078757252
2017-01-01 01:04:39.091846+00:00,281.766607,108.101061156
2017-01-01 01:19:20.908154+00:00,282.182729286,110.574969297
2017-01-01 01:30:00+00:00,282.471404837,104.485561083
2017-01-01 01:34:39.091846+00:00,282.640541566,107.127729662
2017-01-01 01:49:20.908154+00:00,283.200440359,109.3612269
2017-01-01 02:00:00+00:00,283.594242222,103.761571492
2017-01-01 02:04:39.091846+00:00,283.761624506,106.16596464
2017-01-01 02:19:20.908154+00:00,284.313963054,108.225932294
2017-01-01 02:30:00+00:00,284.703408042,103.121826676
2017-01-01 02:34:39.091846+00:00,284.857560002,105.288590606
2017-01-01 02:49:20.908154+00:00,285.365981271,107.170317655
2017-01-01 03:00:00+00:00,285.724774434,102.588166061
2017-01-01 03:04:39.091846+00:00,285.862836414,104.514391342
2017-01-01 03:19:20.908154+00:00,286.318262114,106.219029817
2017-01-01 03:30:00+00:00,286.639778538,102.154049323
2017-01-01 03:34:39.091846+00:00,286.760153436,103.852375615
2017-01-01 03:49:20.908154+00:00,287.157648677,105.396914108
2017-01-01 04:00:00+00:00,287.43818777,101.810393558
2017-01-01 04:04:39.091846+00:00,287.537774072,103.30855352
2017-01-01 04:19:20.908154+00:00,287.867798633,104.724928265
2017-01-01 04:30:00+00:00,288.100261062,101.54925537
2017-01-01 04:34:39.091846+00:00,288.175999779,102.886787628
2017-01-01 04:49:20.908154+00:00,288.429270927,104.21909871
2017-01-01 05:00:00+00:00,288.606700287,101.364315777
2017-01-01 05:04:39.091846+00:00,288.657238165,102.588882408
2017-01-01 05:19:20.908154+00:00,288.829958046,103.89044725
2017-01-01 05:30:00+00:00,288.949345756,101.250866556
2017-01-01 05:34:39.091846+00:00,288.974135297,102.414822847
2017-01-01 05:49:20.908154+00:00,289.065122032,103.745673405
2017-01-01 06:00:00+00:00,289.125342008,101.20578677
2017-01-01 06:04:39.091846+00:00,289.124290316,102.363316175
2017-01-01 06:19:20.908154+00:00,289.13384704,103.788249639
2017-01-01 06:30:00+00:00,289.134846753,101.227574117
2017-01-01 06:34:39.091846+00:00,289.107540987,102.43240921
2017-01-01 06:49:20.908154+00:00,289.034989259,104.019183718
2017-01-01 07:00:00+00:00,288.976010669,101.316313898
2017-01-01 07:04:39.091846+00:00,288.92270514,102.619798088
2017-01-01 07:19:20.908154+00:00,288.769482274,104.436796364
2017-01-01 07:30:00+00:00,288.651315495,101.473485194
2017-01-01 07:34:39.091846+00:00,288.572560127,102.922710912
2017-01-01 07:49:20.908154+00:00,288.34101744,105.035587186
2017-01-01 08:00:00+00:00,288.16514082,101.701679032
2017-01-01 08:04:39.091846+00:00,288.062245048,103.337672739
2017-01-01 08:19:20.908154+00:00,287.757077047,105.804922823
2017-01-01 08:30:00+00:00,287.52670603,102.004412134
2017-01-01 08:34:39.091846+00:00,287.401227259,103.860556206
2017-01-01 08:49:20.908154+00:00,287.02781038,106.728232442
2017-01-01 09:00:00+00:00,286.746706889,102.386131699
2017-01-01 09:04:39.091846+00:00,286.600664056,104.48700777
2017-01-01 09:19:20.908154+00:00,286.165680446,107.782930423
2017-01-01 09:30:00+00:00,285.838616935,102.852338069
2017-01-01 09:34:39.091846+00:00,285.674417553,105.21298098
2017-01-01 09:49:20.908154+00:00,285.185572641,108.940975662
2017-01-01 10:00:00+00:00,284.818136894,103.409653336
2017-01-01 10:04:39.091846+00:00,284.638577387,106.034973881
2017-01-01 10:19:20.908154+00:00,284.104555823,110.169966604
2017-01-01 10:30:00+00:00,283.703125482,104.065649735
2017-01-01 10:34:39.091846+00:00,283.511037048,106.949654495
2017-01-01 10:49:20.908154+00:00,282.940355489,111.434742644
2017-01-01 11:00:00+00:00,282.511269493,104.828268618
2017-01-01 11:04:39.091846+00:00,282.309944411,107.952772034
2017-01-01 11:19:20.908154+00:00,281.712297385,112.699449025
2017-01-01 11:30:00+00:00,281.262829873,105.70468969
2017-01-01 11:34:39.091846+00:00,281.055867104,109.037521594
2017-01-01 11:49:20.908154+00:00,280.441673062,113.929902946
2017-01-01 12:00:00+00:00,279.979697289,106.699582044
2017-01-01 12:04:39.091846+00:00,279.77089009,110.192782907
2017-01-01 12:19:20.908154+00:00,279.151006725,115.095950367
2017-01-01 12:30:00+00:00,278.684774769,107.812819727
2017-01-01 12:34:39.091846+00:00,278.478101793,111.401790782
2017-01-01 12:49:20.908154+00:00,277.863898315,116.173409436
2017-01-01 13:00:00+00:00,277.402071678,109.036974258
2017-01-01 13:04:39.091846+00:00,277.20088943,112.641735068
2017-01-01 13:19:20.908154+00:00,276.601810995,117.145223884
2017-01-01 13:30:00+00:00,276.151666142,110.355130304
2017-01-01 13:34:39.091846+00:00,275.958212013,113.884528266
2017-01-01 13:49:20.908154+00:00,275.380322068,118.001610158
2017-01-01 14:00:00+00:00,274.946659317,111.73967856
2017-01-01 14:04:39.091846+00:00,274.765417095,115.098615707
2017-01-01 14:19:20.908154+00:00,274.222121974,118.739225244
2017-01-01 14:30:00+00:00,273.815002016,113.152613367
2017-01-01 14:34:39.091846+00:00,273.649404043,116.25138908
2017-01-01 14:49:20.908154+00:00,273.151089891,119.359617871
2017-01-01 15:00:00+00:00,272.778290792,114.547502514
2017-01-01 15:04:39.091846+00:00,272.631636923,117.311631753
2017-01-01 15:19:20.908154+00:00,272.188591644,119.867374776
2017-01-01 15:30:00+00:00,271.85770254,115.872832449
2017-01-01 15:34:39.091846+00:00,271.73314006,118.251503668
2017-01-01 15:49:20.908154+00:00,271.355473995,120.268373285
2017-01-01 16:00:00+00:00,271.073825227,117.076077039
2017-01-01 16:04:39.091846+00:00,270.971275226,119.04780219
2017-01-01 16:19:20.908154+00:00,270.659168962,120.568432672
2017-01-01 16:30:00+00:00,270.42676438,118.107736066
2017-01-01 16:34:39.091846+00:00,270.348177868,119.682491237
2017-01-01 16:49:20.908154+00:00,270.108434743,120.772481694
2017-01-01 17:00:00+00:00,269.929959546,118.924743757
2017-01-01 17:04:39.091846+00:00,269.876396426,120.142670097
2017-01-01 17:19:20.908154+00:00,269.713154098,120.884197131
2017-01-01 17:30:00+00:00,269.59126349,119.492948462
2017-01-01 17:34:39.091846+00:00,269.562804525,120.420226454
2017-01-01 17:49:20.908154+00:00,269.477199409,120.905974704
2017-01-01 18:00:00+00:00,269.41230187,119.788657685
2017-01-01 18:04:39.091846+00:00,269.410934164,120.511392571
2017-01-01 18:19:20.908154+00:00,269.410165501,120.839070515
2017-01-01 18:30:00+00:00,269.407021927,119.799423689
2017-01-01 18:34:39.091846+00:00,269.431778542,120.416352594
2017-01-01 18:49:20.908154+00:00,269.513703588,120.683779833
2017-01-01 19:00:00+00:00,269.570294365,119.524298584
2017-01-01 19:04:39.091846+00:00,269.620131509,120.138971111
2017-01-01 19:19:20.908154+00:00,269.782312093,120.439581253
2017-01-01 19:30:00+00:00,269.896425359,118.973729278
2017-01-01 19:34:39.091846+00:00,269.969853416,119.686643608
2017-01-01 19:49:20.908154+00:00,270.208348576,120.105238566
2017-01-01 20:00:00+00:00,270.37673598,118.169148682
2017-01-01 20:04:39.091846+00:00,270.474099855,119.070224008
2017-01-01 20:19:20.908154+00:00,270.79063156,119.67890999
2017-01-01 20:30:00+00:00,271.014229911,117.142200041
2017-01-01 20:34:39.091846+00:00,271.133025852,118.303954243
2017-01-01 20:49:20.908154+00:00,271.520146224,119.158351829
2017-01-01 21:00:00+00:00,271.793351527,115.933443706
2017-01-01 21:04:39.091846+00:00,271.931367323,117.405311471
2017-01-01 21:19:20.908154+00:00,272.3823507,118.541309493
2017-01-01 21:30:00+00:00,272.70020467,114.590381819
2017-01-01 21:34:39.091846+00:00,272.854884274,116.394697512
2017-01-01 21:49:20.908154+00:00,273.361699743,117.826114248
2017-01-01 22:00:00+00:00,273.718428576,113.164732896
2017-01-01 22:04:39.091846+00:00,273.887774858,115.294974551
2017-01-01 22:19:20.908154+00:00,274.443928965,117.012082374
2017-01-01 22:30:00+00:00,274.835001491,111.70909253
2017-01-01 22:34:39.091846+00:00,275.015686342,114.13155683
2017-01-01 22:49:20.908154+00:00,275.610222823,116.096132815
2017-01-01 23:00:00+00:00,276.027993299,110.272374992
2017-01-01 23:04:39.091846+00:00,276.21678472,112.943013584
2017-01-01 23:19:20.908154+00:00,276.838417253,115.029274668
2017-01-01 23:30:00+00:00,277.275083456,108.864426164
2017-01-01 23:34:39.091846+00:00,277.469401551,111.962064593
2017-01-01 23:49:20.908154+00:00,278.103351204,113.133776833
2017-01-02 00:00:00+00:00,278.547277791,106.679200055
2017-01-01 00:00:00+00:00,295.0,117.641257296
2017-01-01 12:00:00+00:00,280.069052071,120.468887298
2017-01-02 00:00:00+00:00,278.653939071,121.337832533
4 changes: 3 additions & 1 deletion unittests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def test_set_options(self):
opt_options = opt_problem.get_optimization_options();
# Set new options
opt_options['IPOPT_options']['max_iter'] = 2;
opt_options['n_e'] = 2;
opt_options['result_mode'] = 'mesh_points';
opt_problem.set_optimization_options(opt_options)
# Get new options
opt_options = opt_problem.get_optimization_options();
Expand Down Expand Up @@ -331,7 +333,7 @@ def test_initial_constraint(self):
df_test = pd.DataFrame(columns=['message', 'iterations', 'objective'], index = [0])
df_test.loc[0] = opt_statistics[:-1]
self.check_df(df_test, 'statistics_initial_constraint.csv', timeseries=False);

#%% Temperature tests
class OptimizeAdvancedFromJModelica(TestCaseMPCPy):
'''Tests for the optimization of a model using JModelica.
Expand Down

0 comments on commit 33993d2

Please sign in to comment.