- Index
- » Users
- » shawnli89
- » Profile
Posts
Posts
If we change the input Real u into the input Integer u, there will be an error shows that messages: Failed to build model: nmpcBatchReactor.
Does Dynamic Optimization with OM and IpOpt support Integer Variable?
Great Thanks.
Code:
model BatchReactor
Real x1(start =1, fixed=true, min=0, max=1);
Real x2(start =0, fixed=true, min=0, max=1);
input Real u(min=0, max=5);
equation
der(x1) = -(u+u^2/2)*x1;
der(x2) = u*x1;
end BatchReactor;
optimization nmpcBatchReactor(objective=-x2)
extends BatchReactor;
end nmpcBatchReactor;
Here is the solution. When using CombiTimeTable to import environment variables in dynamic programming optimization, the timeEvents in the CombiTimeTable dialog should choose NoTimeEvents.
For the RealNetwork1, I didn't change anything and just press the simulation.
All the RealValue Component of RealNetwork1 is still zero, both in plotting and modeling perspective. Did I miss something?
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
While, I finally got the solution. Here is the modified code, which can be used for the simulation results file and optimization result file. An extra bug is also fixed.
Note that the absolute address is used for the input "resultFile", which can be acquired by:
%% for simulation
sim_str = ['simulate(' simModleName ', startTime=0.0, stopTime=5.0)']
sim_res = omc.sendExpression(sim_str)
omimport(sim_res.resultFile)
%% or for optimization
opt_str = [' optimize(OptClassTemplate, numberOfIntervals=16, stopTime=1, tolerance=1e-8) ']
opt_res = omc.sendExpression(opt_str) % optimization
omimport(opt_res.resultFile);
-------------------------------------------------------------------------------------------------------
Code:
function omimport(resultFile)
% a siltly change including load ([modelname,'_res.mat']) -> load(resultFile)
% and -> for i=1:size(name,1)
% and add if name(i,1)~="$"
%
% Read OpenModelica Result File into Workspace
%
% SYNTAX: omimport(modelname)
% z.B. omimport('package.model')
%
% Feedback/problems: Christian Schaad, ingenieurbuero@christian-schaad.de
%load ([modelname,'_res.mat']);
load(resultFile);
%Sort out double times
deltat0=find(diff(data_2(1,:))==0);
disp(['Removed same time values: ',num2str(length(deltat0)),'/',num2str(length(data_2(1,:)))])
assignin('base','data_2',data_2);
assignin('base','dataInfo',dataInfo);
assignin('base','name',name);
assignin('base','deltat0',deltat0);
name=name';
for i=1:size(name,1)
name(i,:)=strrep(name(i,:),'[','_');
name(i,:)=strrep(name(i,:),']','_');
name(i,:)=strrep(name(i,:),'(','_');
name(i,:)=strrep(name(i,:),')','_');
name(i,:)=strrep(name(i,:),'$','D');
end
for i=1:size(name,1)
if (isempty(strfind(name(i,:),'der(')))
nonchars=strfind(name(i,:),char(0));
if dataInfo(2,i)<0
try
assignin('base','temp',-data_2(-dataInfo(2,i),:));
catch
end
else
try
assignin('base','temp',data_2(dataInfo(2,i),:));
catch
end
end
try
evalin('base',(['temp(deltat0)=[];']));
evalin('base',([num2str(name(i,1:nonchars(1)-1)),'=temp;']));
catch
end
end
end
clear data_1 data_2 Aclass description modelname i dataInfo temp deltat0;
evalin('base',(['clear name data_2 dataInfo nonchars temp']));
Hi cschaad
Thanks for your amazing solution. It really helps.
Is there an idea to read the optimization _res.mat result? Since the structure between optimization _res.mat and simulation _res.mat are nearly the same.
I tried to read optimization _res.mat file and it comes an error on line “ if (isempty(strfind(name(i,,'der('))) ”
Really appreciate for your kind help.
Thanks
- Index
- » Users
- » shawnli89
- » Profile