Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Time-varying environment variable for the builtin dynamic optimization

Time-varying environment variable for the builtin dynamic optimization

I meet a problem when using the builtin dynamic optimization. How can I add a time-varying environment variable for dynamic optimization?
Here is my situation:
    The dynamic programming optimization runs successfully if the environment variable is given in the equation part, e.g. env_var = 1;
    But how can I add a time-varying environment variable?
e.g. env_var =
  0   0
  1   0
  1   1
  2   4
  3   9
  4  16
    I use Modelica.Blocks.Sources.CombiTimeTable to import the environment variable. The simulation runs successfully in OMEdit, but the optimization shows messages: "stdout            | info    | ... loading \"
   I think it may because CombiTimeTable can only be used for the simulation but not for the optimization.

    To simplify and varify this situation the test code is attached in The code and ElectriPrice.mat file or


1. Opt with env_var = 1 successfully

Code:


model env_var_1
  Real x(start =0, fixed=true, min=0)"state";
  input Real u(min=1, max=2)"control";
  Real e "environment variable";
  // Obj is -x (maximum x)
equation
  e = 1;
  der(x) = u + e;
end env_var_1;

setCommandLineOptions("-g=Optimica")

optimization OptClassTemplate(objective=-x) 
  extends env_var_1;   
end OptClassTemplate;

optimize(OptClassTemplate, numberOfIntervals=100, stopTime=24, tolerance=1e-8)

2.Opt with env_var = CombiTimeTable messages: stdout  (Remember to change the file path of ElectriPrice7.mat in CombiTimeTable )

Code:


model env_var_TimeTable
  Real x(start =0, fixed=true, min=0, max=100)"state";
  input Real u(min=1, max=2)"control";
  Real e "environment variable";
      Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(fileName = "C:/Onedrive/OneDrive - Servers-30/Modelica_n/OMMatlab/ModelicaReadMat/ElectriPrice7.mat", tableName = "env_var", tableOnFile = true)  annotation(
    Placement(visible = true, transformation(origin = {-28, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Interaction.Show.RealValue realValue annotation(
    Placement(visible = true, transformation(origin = {56, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  // Obj is -x (maximum x)
equation
  e = combiTimeTable.y[1];
  der(x) = u + e;
  connect(combiTimeTable.y[1], realValue.numberPort) annotation(
    Line(points = {{-16, 30}, {44, 30}, {44, 30}, {44, 30}}, color = {0, 0, 127}));
annotation(
    uses(Modelica(version = "3.2.3")));
end env_var_TimeTable;



loadModel(Modelica)

setCommandLineOptions("-g=Optimica")

optimization OptClassTemplate(objective=-x) 
  extends env_var_TimeTable;   
end OptClassTemplate;
optimize(OptClassTemplate, numberOfIntervals=100, stopTime=24, tolerance=1e-8)

In the second Opt with env_var = CombiTimeTable case, the optimization result shows as below. It seems that the CombiTimeTable can only import time-varying variables for the simulation but not for the optimization.
I have checked the _res.mat result file. The environment variable e always equals to the initial value (0.3648) rather than a time-varying variable.
Is there a way for importing time-varying environment variables for dynamic optimization?

Thanks for your attention.
                                                                                             Shawnli
                                                                                             shawnli89@163.com

Edited by: shawnli89 - Oct-05-19 09:47:03

Re: Time-varying environment variable for the builtin dynamic optimization

Here is the solution. When using CombiTimeTable to import environment variables in dynamic programming optimization, the timeEvents in the CombiTimeTable dialog should choose NoTimeEvents.

There are 0 guests and 0 other users also viewing this topic
You are here: