Skip to content

Commit

Permalink
Merge pull request #9 from vruge/master
Browse files Browse the repository at this point in the history
update dyn. optmization examples
  • Loading branch information
adrpo committed May 19, 2015
2 parents b07755b + 5bc2cb9 commit 028b425
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 17 deletions.
2 changes: 2 additions & 0 deletions openmodelica/cruntime/optimization/basic/Makefile
Expand Up @@ -47,6 +47,8 @@ TFC7.mos \
TFC8.mos \
TFCtestFlag.mos \
TT.mos \
TT2.mos \
TT3.mos \


# test that currently fail. Move up when fixed.
Expand Down
62 changes: 62 additions & 0 deletions openmodelica/cruntime/optimization/basic/TT2.mos
@@ -0,0 +1,62 @@
// name: CombiTimeTable
// status: correct

loadModel(Modelica);
getErrorString();

setCommandLineOptions("+g=Optimica");
getErrorString();

loadString("
optimization TimeTableTest(objectiveIntegrand = (x - timeTable.y[1])^2)
input Real u(min = -10, max = 10);
Real x(min = -5, max = 10);
Modelica.Blocks.Sources.CombiTimeTable timeTable(table = [0,1; 1,3; 2,-3; 3,5; 4,7; 5, 0], extrapolation = Modelica.Blocks.Types.Extrapolation.HoldLastPoint);
initial equation
x = timeTable.y[1];
equation
der(x) = u;
end TimeTableTest;
");
getErrorString();

optimize(TimeTableTest, numberOfIntervals=20, tolerance = 1e-8, stopTime = 5);
getErrorString();

res := OpenModelica.Scripting.compareSimulationResults("TimeTableTest_res.mat","ReferenceFiles/TimeTableTest_ref.mat","TimeTableTest_res.csv",0.01,0.0001,
{"u",
"x"});
getErrorString();

// Result:
// true
// ""
// true
// ""
// true
// ""
// record SimulationResult
// resultFile = "TimeTableTest_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 5.0, numberOfIntervals = 20, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'TimeTableTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "
// Optimizer Variables
// ========================================================
// State[0]:x(start = 0, nominal = 10, min = -5, max = 10, init = 1)
// Input[1]:u(start = 0, nominal = 10, min = -10, max = 10)
// --------------------------------------------------------
// number of nonlinear constraints: 0
// ========================================================
// stdout | info | Jacobian or SparsePattern is not generated or failed to initialize! Switch back to normal.
//
// ******************************************************************************
// This program contains Ipopt, a library for large-scale nonlinear optimization.
// Ipopt is released as open source code under the Eclipse Public License (EPL).
// For more information visit http://projects.coin-or.org/Ipopt
// ******************************************************************************
//
// "
// end SimulationResult;
// ""
// {"Files Equal!"}
// ""
// endResult
62 changes: 62 additions & 0 deletions openmodelica/cruntime/optimization/basic/TT3.mos
@@ -0,0 +1,62 @@
// name: CombiTimeTablePeriodic
// status: correct

loadModel(Modelica);
getErrorString();

setCommandLineOptions("+g=Optimica");
getErrorString();

loadString("
optimization TimeTableTest(objectiveIntegrand = (x - timeTable.y[1])^2)
input Real u(min = -10, max = 10);
Real x(min = -5, max = 10);
Modelica.Blocks.Sources.CombiTimeTable timeTable(table = [0,1; 1,3; 2,-3; 3,5; 4,7; 5, 0], extrapolation = Modelica.Blocks.Types.Extrapolation.Periodic );
initial equation
x = timeTable.y[1];
equation
der(x) = u;
end TimeTableTest;
");
getErrorString();

optimize(TimeTableTest, numberOfIntervals=20, tolerance = 1e-8, stopTime = 5);
getErrorString();

res := OpenModelica.Scripting.compareSimulationResults("TimeTableTest_res.mat","ReferenceFiles/TimeTableTest_ref.mat","TimeTableTest_res.csv",0.01,0.0001,
{"u",
"x"});
getErrorString();

// Result:
// true
// ""
// true
// ""
// true
// ""
// record SimulationResult
// resultFile = "TimeTableTest_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 5.0, numberOfIntervals = 20, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'TimeTableTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "
// Optimizer Variables
// ========================================================
// State[0]:x(start = 0, nominal = 10, min = -5, max = 10, init = 1)
// Input[1]:u(start = 0, nominal = 10, min = -10, max = 10)
// --------------------------------------------------------
// number of nonlinear constraints: 0
// ========================================================
// stdout | info | Jacobian or SparsePattern is not generated or failed to initialize! Switch back to normal.
//
// ******************************************************************************
// This program contains Ipopt, a library for large-scale nonlinear optimization.
// Ipopt is released as open source code under the Eclipse Public License (EPL).
// For more information visit http://projects.coin-or.org/Ipopt
// ******************************************************************************
//
// "
// end SimulationResult;
// ""
// {"Files Equal!"}
// ""
// endResult
28 changes: 14 additions & 14 deletions openmodelica/cruntime/optimization/benchmark/DrumBoiler.mo
Expand Up @@ -154,7 +154,7 @@ package drumBoiler
model Band
parameter Real MaxValue(start = 1e120);
parameter Real MinValue(start = -1e120);
parameter Real gain(start = 1);
parameter Real gain=1;
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Real con(min = MinValue, max = MaxValue) = gain * u annotation(isConstraint = true);
Expand All @@ -164,15 +164,15 @@ package drumBoiler
model BandFinal
parameter Real MaxValue(start = 1e120);
parameter Real MinValue(start = -1e120);
parameter Real gain(start = 1);
parameter Real gain=1;
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Real con(min = MinValue, max = MaxValue) = gain * u annotation(isFinalConstraint = true);
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Text(origin = {-25, -2}, extent = {{-57, 28}, {57, -28}}, textString = "u_min <= u(stopTime) <= u_max")}));
end BandFinal;

model GreaterEqual
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0)=1;
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {116, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
protected
Expand All @@ -181,7 +181,7 @@ package drumBoiler
end GreaterEqual;

model LessEqual
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0)=1;
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {112, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Expand All @@ -190,7 +190,7 @@ package drumBoiler
end LessEqual;

model Equal
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0) = 1;
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {116, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Expand All @@ -199,7 +199,7 @@ package drumBoiler
end Equal;

model GreaterEqualFinal
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0) = 1;
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {112, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Expand All @@ -208,7 +208,7 @@ package drumBoiler
end GreaterEqualFinal;

model LessEqualFinal
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0) = 1;
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {112, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Expand All @@ -217,7 +217,7 @@ package drumBoiler
end LessEqualFinal;

model EqualFinal
parameter Real gain(start = 1, min = 0);
parameter Real gain(min = 0)=1;
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 180), iconTransformation(origin = {112, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 180)));
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-114, 4}, extent = {{-14, -18}, {14, 10}}, rotation = 0)));
protected
Expand All @@ -230,38 +230,38 @@ package drumBoiler
package ObjectFunction
model MinimizeFinal
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 1}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -12}, {18, 24}}, rotation = 0)));
parameter Real gain(start = 1);
parameter Real gain=1;
protected
Real signal = gain * u annotation(isMayer = true);
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Text(origin = {-25, 38}, extent = {{-35, 18}, {105, -70}}, textString = "minimize signal"), Text(origin = {-23, 14}, extent = {{-35, 18}, {105, -70}}, textString = "at the end time point")}));
end MinimizeFinal;

model MaximizeFinal
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 1}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -12}, {18, 24}}, rotation = 0)));
parameter Real gain(start = 1);
parameter Real gain=1;
protected
Real signal = -gain * u annotation(isMayer = true);
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Text(origin = {-43, 38}, extent = {{-35, 18}, {105, -70}}, textString = "maximize signal"), Text(origin = {-43, 18}, extent = {{-35, 18}, {105, -70}}, textString = "at the end time point")}));
end MaximizeFinal;

model Maximize
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 1}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -12}, {18, 24}}, rotation = 0)));
parameter Real gain(start = 1);
parameter Real gain=1;
protected
Real signal = -gain * u annotation(isLagrange = true);
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Text(origin = {-29, 38}, extent = {{-35, 18}, {105, -70}}, textString = "maximize signal"), Text(origin = {-29, 14}, extent = {{-35, 18}, {105, -70}}, textString = "over the time")}));
end Maximize;

model Minimize
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 1}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -12}, {18, 24}}, rotation = 0)));
parameter Real gain(start = 1);
parameter Real gain=1;
protected
Real signal = gain * u annotation(isLagrange = true);
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Text(origin = {-35, 38}, extent = {{-35, 18}, {105, -70}}, textString = "minimize signal"), Text(origin = {-35, 12}, extent = {{-35, 18}, {105, -70}}, textString = "over the time")}));
end Minimize;

model MinimizeDifferenc
parameter Real gain(start = 1);
parameter Real gain=1;
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 81}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -72}, {18, -36}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {-118, -60}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-116, 80}, extent = {{-17, -17}, {17, 17}}, rotation = 0)));
protected
Expand All @@ -270,7 +270,7 @@ package drumBoiler
end MinimizeDifferenc;

model MinimizeDifferencFinal
parameter Real gain(start = 1);
parameter Real gain=1;
Modelica.Blocks.Interfaces.RealInput u annotation(Placement(visible = true, transformation(origin = {-117, 81}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-118, -6}, extent = {{-18, -72}, {18, -36}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealInput u1 annotation(Placement(visible = true, transformation(origin = {-118, -60}, extent = {{-17, -17}, {17, 17}}, rotation = 0), iconTransformation(origin = {-116, 80}, extent = {{-17, -17}, {17, 17}}, rotation = 0)));
protected
Expand Down
Expand Up @@ -103,7 +103,6 @@ getErrorString();
// * candidate: evaporator.T(nominal = 500.0)
// * candidate: evaporator.T_D(nominal = 500.0)
// => select value from evaporator.T_D(nominal = 500.0) for variable: evaporator.T_D
// [openmodelica/cruntime/optimization/benchmark/DrumBoiler.mo:157:9-157:39:writable] Warning: Parameter conSigma.gain has no value, and is fixed during initialization (fixed=true), using available start value (start=1.0) as default value.
// "
// {"Files Equal!"}
// ""
Expand Down
Expand Up @@ -114,7 +114,6 @@ getErrorString();
// * candidate: evaporator.T(nominal = 500.0)
// * candidate: evaporator.T_D(nominal = 500.0)
// => select value from evaporator.T_D(nominal = 500.0) for variable: evaporator.T_D
// [openmodelica/cruntime/optimization/benchmark/DrumBoiler.mo:157:9-157:39:writable] Warning: Parameter conSigma.gain has no value, and is fixed during initialization (fixed=true), using available start value (start=1.0) as default value.
// "
// {"Files Equal!"}
// ""
Expand Down
Expand Up @@ -107,7 +107,6 @@ getErrorString();
// * candidate: evaporator.T(nominal = 500.0)
// * candidate: evaporator.T_D(nominal = 500.0)
// => select value from evaporator.T_D(nominal = 500.0) for variable: evaporator.T_D
// [openmodelica/cruntime/optimization/benchmark/DrumBoiler.mo:157:9-157:39:writable] Warning: Parameter conSigma.gain has no value, and is fixed during initialization (fixed=true), using available start value (start=1.0) as default value.
// "
// {"Files Equal!"}
// ""
Expand Down

0 comments on commit 028b425

Please sign in to comment.