Issues using MPCPy with LBNL Modelica Buildings Library in Optimization

205 views
Skip to first unread message

Tom Stesco

unread,
May 14, 2018, 12:04:09 PM5/14/18
to MPCPy

Hello, I've recently been trying to use a reduced order thermal building model [1] as a model in MPCPy. I followed the tutorial code and it works for the Tutorial.mo modelica model. I want to now use the functionality of the Modelica Buildings library and any help is appreciated. 

I can load and simulate the Modelica Buildings model [1] fine.

moinfo = ('<modelicaPath>/ThermalZones/ReducedOrder/Examples/SimpleRoomFourElements.mo', 
          'ReducedOrder.SimpleRoomFourElements', 
          {});
model = models.Modelica(models.JModelica,
                        models.RMSE,
                        emulation.measurements,
                        moinfo = moinfo,
                        parameter_data = {},
                       weather_data = weather.data,
                       control_data = control.data,
                       tz_name = weather.tz_name);

model.simulate('1/1/2017', '1/3/2017');
 
I added a heat input from MPCPy control.data which works fine in simulation also. The optimzation problem fails however (with and without constraints).

opt_problem = optimization.Optimization(model, 
                                        optimization.EnergyMin,
                                        optimization.JModelica,
                                        'input_Q_flow',
                                        constraint_data = constraints.data);

Each time a java RuntimeError occurs without information aside from the stack trace to _modelicacasadi_wrapper._transferOptimizationProblem:

RuntimeErrorTraceback (most recent call last)
<ipython-input-241-d5f5a5cb9469> in <module>()
      2                                         optimization.EnergyMin,
      3                                         optimization.JModelica,
----> 4                                         'input_Q_flow'
      5                                         )

/opt/MPCPy/mpcpy/optimization.py in __init__(self, Model, problem_type, package_type, objective_variable, **kwargs)
     88         self.objective_variable = objective_variable;
     89         self._problem_type = problem_type();
---> 90         self._package_type = package_type(self);
     91         self.tz_name = Model.tz_name
     92 

/opt/MPCPy/mpcpy/optimization.py in __init__(self, Optimization)
    435 
    436         # Setup JModelica optimization problem
--> 437         Optimization._problem_type._setup_jmodelica(self, Optimization);
    438         # Set default optimization options
    439         self._set_optimization_options(self.opt_problem.optimize_options(), init = True)

/opt/MPCPy/mpcpy/optimization.py in _setup_jmodelica(self, JModelica, Optimization)
    343         JModelica._initalize_mop();
    344         JModelica._write_control_mop(Optimization);
--> 345         JModelica._compile_transfer_problem();
    346 
    347 class EnergyCostMin(_Problem):

/opt/MPCPy/mpcpy/optimization.py in _compile_transfer_problem(self)
    810         self.opt_problem = transfer_optimization_problem(self.mopmodelpath + '_optimize', \
    811                                                          self.moppath, \
--> 812                                                          compiler_options = {'extra_lib_dirs':self.Model.libraries});
    813 
    814     def _get_optimization_options(self):

/opt/JModelica/Python/pyjmi/casadi_interface.pyc in transfer_optimization_problem(class_name, file_name, compiler_options, compiler_log_level, accept_model)
    151                                    compiler_options=compiler_options,
    152                                    compiler_log_level=compiler_log_level,
--> 153                                    accept_model=accept_model)
    154     return op
    155 

/opt/JModelica/Python/modelicacasadi_transfer/modelica_casadi_transfer_wrapper.pyc in transfer_optimization_problem(ocp, class_name, file_name, compiler_options, compiler_log_level, accept_model)
    154         if not accept_model:
    155             return _transfer_optimica(ocp, class_name, files,
--> 156                                       _get_options(compiler_options), compiler_log_level)
    157         else:
    158             return modelicacasadi_wrapper.transferModelFromOptimicaCompiler(ocp, class_name, files,

/opt/JModelica/Python/modelicacasadi_transfer/modelica_casadi_transfer_wrapper.pyc in _transfer_optimica(ocp, class_name, files, options, log_level)
    177 
    178 def _transfer_optimica(ocp, class_name, files, options, log_level):
--> 179     return modelicacasadi_wrapper._transferOptimizationProblem(ocp, class_name, files, options, log_level)
    180 
    181 

/opt/JModelica/Python/modelicacasadi_wrapper/modelicacasadi_wrapper.pyc in _transferOptimizationProblem(*args)
   3440 
   3441     """
-> 3442     return _modelicacasadi_wrapper._transferOptimizationProblem(*args)
   3443 # This file is compatible with both classic and new-style classes.
   3444 

RuntimeError: a java error occurred; details were printed

All that I've been able to find thus far is that the MSL Media Library is not supported by optimica/JModelica. I've been looking at some work arounds (e.g. using Buildings.Media instead of MSL) and trying more slight changes in the model but nothing has helped thus far. Please let me know if there is anything I'm missing or if this is just unsupported as of yet.

Thanks!

[1]http://simulationresearch.lbl.gov/modelica/releases/v5.0.1/help/Buildings_ThermalZones_ReducedOrder_Examples.html#Buildings.ThermalZones.ReducedOrder.Examples.SimpleRoomFourElements

David Blum

unread,
May 17, 2018, 8:52:46 AM5/17/18
to MPCPy
Hey Tom, 

Thanks for posting the issue and sorry for my delayed response.

In general, the Modelica.Media and Buildings.Media package structures do work with optimization in JModelica.  However, you need to be sure 1) the implementations of the functions in the Media package you're using use constructs supported for optimization in JModelica.  And 2) you need to be sure the models using the Media package use constructs supported for optimization in JModelica (e.g. models from Buildings.Fluid).  A short list of those constructs that are not supported is:

- if/then else in the equation section where the logical expression depends on time
- for loops in the equation section where the iteration variable depends on time
- when/while statements
- discrete variables (integer, boolean, enumerate)
- external C functions (e.g. tables)

I have used the Buildings.Media.Air package with success before but it required some modification to component models to satisfy 2).  This could involve setting a parameter differently (e.g. often for fluid components, allowFlowReversal should = False), modifying how an expression is written, or reformulating the model.  You should be able to see the java error output in the terminal window above the python error trace output, and that could give you a clue to where the issue is.

In general, I would assume models in the Buildings Library are not supported for optimization in JModelica without attention to 2).  Unfortunately, solving an optimization problem is often a different animal than simulating a model.   Recognizing this is a challenge, we are currently working to develop a library of components that are known to be ready to use in optimization as part of the IBPSA Project 1 WP 1.2, though we are not far along enough yet to release any version.  Hopefully such a library would then be updated as new constructs get supported for optimization using Modelica (another challenge to overcome is adding such support for something like an MILP with discrete variables).  If you are interested in getting more involved in this effort, however, please let me know.

For your specific problem of the reduced order model in the Buildings Library, an initial issue probably comes from the MixingVolume model.  Be sure to set allowFlowReversal to False and if looking to mix air into the volume, keep the number of connected ports to two, so that there is essentially an inlet and an outlet port.  You may also try setting energyDynamics to SteadyState.  I can also try to implement the reduced order model in an optimization and see where other issues are, but I have not done this yet.

Hopefully this is helpful.  Please stay in touch with your progress.

Best Regards, 

Dave

Tom Stesco

unread,
May 21, 2018, 8:16:04 AM5/21/18
to MPCPy
Hello Dave,

Wonderful write up! Thank you for taking the time to flush out the constructs unsupported by JModelica and give some pointers. Really looking forward to the further JModelica integration/documentation within the IBPSA Project 1 WP 1.2. I don't know if I can help with adding support for MILP, do you know if this is a JModelica backend issue or if an interface is required? 

I'll try getting a workable optimization on the Modelica Buildings ReducedOrder example models using your tips or if need be replacing the MixingVolume itself with an RC model and report back here. Another method I'm working on with my supervisor presently is linearizing the modelica model to state space matrices for some formal validation. 

At any rate, I'm also working on some scripts and plotting for MPC iterations which I'll mention in another thread. 

I should also say fantastic work on MPCPy!

Cheers,
Tom

dhb...@gmail.com

unread,
May 21, 2018, 12:09:50 PM5/21/18
to MPCPy
Hey Tom, 

In terms of the MILP, its not really an issue more than an enhancement that needs to be done.  Right now, JModelica uses the Modelica/Optimica syntax to create an NLP and invoke IPOPT to solve it.  To support an MILP, the Modelica/Optimica syntax would have to be used to create an MILP, and then invoke a MILP solver to solve it.  Currently, such a workflow doesn't exist.

Glad to hear you are finding MPCPy useful!

Best Regards, 

Dave

Tom Stesco

unread,
May 21, 2018, 2:59:12 PM5/21/18
to MPCPy
Good to hear it's not a JModelica backend issue! I'm trying to work with linear models where possible for the Optimal Control Problem formulation but there is a chance we may need to look at discrete variables down the road, for window opening for example.

If I understand you correctly then this would be an enhancement on the MPCPy side to the Optimica .mop file generation along with making the correct calls to the PyJMI CasADi API wrapper. Also, changes to the initial Modelica models to setup the discrete variables.

I'll be sure to mention if I end up doing any testing here later on.

Thanks,
Tom

dhb...@gmail.com

unread,
May 29, 2018, 6:09:01 PM5/29/18
to MPCPy
Hey Tom, 

I'm afraid it's a bit more involved than that.  IPOPT is an NLP solver, not a MILP solver.  And so, even if discrete variables were included in the Optimica code, it would have to be compiled into an optimization problem defined in a way that can be solved by the appropriate solver.  Professor Pytlak has done some work to try to do something like this: https://www.tandfonline.com/doi/abs/10.1080/10556788.2013.799159.

Best Regards, 

Dave
Reply all
Reply to author
Forward
0 new messages