Training Data Generation
To generate training data for the slowest non-linear equations found during Profiling Modelica Models we now simulate the equations multiple time and save in- and outputs.
We will use the Functional Mock-up Interface (FMI) standard to generate FMU that we extend with some function to evaluate single equations without the need to simulate the rest of the model.
Functions
NonLinearSystemNeuralNetworkFMU.generateTrainingData — FunctiongenerateTrainingData(fmuPath, workDir, fname, eqId, inputVars, inputBounds, outputVars;
options=DataGenOptions())Generate training data for given equation of FMU.
Generate random inputs between min and max, evalaute equation and compute output. All input-output pairs are saved in CSV file fname.
Arguments
fmuPath::String: Path to FMU.workDir::String: Working directory for generateTrainingData.fname::String: File name to save training data to.eqId::Int64: Index of equation to generate training data for.inputVars::Array{String}: Array with names of input variables.inputBounds::MinMaxBoundaryValues{T}: Boundary values for input space.outputVars::Array{String}: Array with names of output variables.
Keywords
options::DataGenOptions: Settings for data generation.
See also generateFMU, DataGenOptions..
NonLinearSystemNeuralNetworkFMU.addEqInterface2FMU — FunctionaddEqInterface2FMU(modelName, pathToFmu, eqIndices; workingDir=pwd())Create extendedFMU with special_interface to evalaute single equations.
Arguments
modelName::String: Name of Modelica model to export as FMU.pathToFmu::String: Path to FMU to extend.eqIndices::Array{Int64}: Array with equation indices to add equiation interface for.
Keywords
workingDir::String=pwd(): Working directory. Defaults to current working directory.
Returns
- Path to generated FMU
workingDir/<modelName>.interface.fmu.
See also profiling, generateFMU, generateTrainingData.
NonLinearSystemNeuralNetworkFMU.generateFMU — FunctiongenerateFMU(modelName, moFiles; options)Generate 2.0 Model Exchange FMU for Modelica model using OMJulia.
Arguments
modelName::String: Name of the Modelica model.moFiles::Array{String}: Path to the *.mo file(s) containing the model.
Keywords
options::OMOptions: Options for OpenModelica compiler.
Returns
- Path to generated FMU
workingDir/<modelName>.fmu.
See also OMOptions, addEqInterface2FMU, generateTrainingData.
Structures
NonLinearSystemNeuralNetworkFMU.DataGenOptions — TypeDataGenOptions <: AnySettings for data generation.
method::NonLinearSystemNeuralNetworkFMU.DataGenerationMethod: Method to generate data points. Allowed values:RandomMethod,RandomWalkMethodn::Integer: Number of data points to generate.nBatches::Integer: Number of batches to divide N into.nThreads::Integer: Number of threads to use in parallelappend::Bool: Append to already existing dataclean::Bool: Clean up temp CSV files
See als RandomMethod, RandomWalkMethod.
NonLinearSystemNeuralNetworkFMU.RandomMethod — TypeRandomMethod <: DataGenerationMethodRandom data generation using rand.
NonLinearSystemNeuralNetworkFMU.RandomWalkMethod — TypeRandomWalkMethod <: DataGenerationMethodRandomized brownian-like motion data generation. Tries to stay within one solution in case the non-linear system is not unique solveable. Uses previous data point to generate close data point with previous solution as input to NLS solver.
delta::Float64: Step size of random walk.
Examples
First we need to create a Model-Exchange 2.0 FMU with OpenModelica.
This can be done directly from OpenModelica or with generateFMU:
moFiles = ["test/simpleLoop.mo"]
options = OMOptions(workingDir = "tempDir")
fmu = generateFMU("simpleLoop",
moFiles;
options = options)"tempDir/simpleLoop.fmu"Next we need to add non-standard FMI function
fmi2Status myfmi2EvaluateEq(fmi2Component c, const size_t eqNumber)that will call <modelname>_eqFunction_<eqIndex>(DATA* data, threadData_t *threadData) for all non-linear equations we want to generate data for.
Using addEqInterface2FMU this C code will be generated and added to the FMU.
interfaceFmu = addEqInterface2FMU("simpleLoop",
fmu,
[14],
workingDir = "tempDir")"tempDir/simpleLoop.interface.fmu"Now we can create evaluate equation 14 for random values and save the outputs to generate training data.
using CSV
using DataFrames
options = DataGenOptions(n=10, nThreads=1)
boundary = MinMaxBoundaryValues([0.0, 0.95], [1.5, 3.15])
generateTrainingData(interfaceFmu,
"tempDir",
"simpleLoop_data.csv",
14,
["s", "r"],
boundary,
["y"];
options = options)
df = DataFrame(CSV.File("simpleLoop_data.csv"))| Row | s | r | y | Trace |
|---|---|---|---|---|
| Float64 | Float64 | Float64 | Int64 | |
| 1 | 1.06469 | 2.59406 | 0.173633 | 1 |
| 2 | 1.06454 | 2.59372 | 0.173197 | 1 |
| 3 | 1.06559 | 2.59368 | 0.176094 | 1 |
| 4 | 1.06644 | 2.59202 | 0.178346 | 1 |
| 5 | 1.06546 | 2.59073 | 0.175532 | 1 |
| 6 | 1.06522 | 2.5908 | 0.174886 | 1 |
| 7 | 1.06553 | 2.58917 | 0.175632 | 1 |
| 8 | 1.0652 | 2.5888 | 0.174683 | 1 |
| 9 | 1.06393 | 2.589 | 0.171174 | 1 |
| 10 | 1.06384 | 2.59008 | 0.171006 | 1 |