Skip to content

Commit

Permalink
added module reduceDynamicOptimization (Thanks, Willi)
Browse files Browse the repository at this point in the history
 - remove eqs and vars, which are not part of the optimization problem

open issues: overestimation for states



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25262 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Mar 25, 2015
1 parent 74781e3 commit 450ee5e
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 25 deletions.
75 changes: 74 additions & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -1882,6 +1882,7 @@ algorithm
end matchcontinue;
end markStateEquationsWork;


protected function consNotMarked
"author Frenkel TUD 2012-12
if mark[e]=0 then e::iQueue else iQueue"
Expand Down Expand Up @@ -2119,6 +2120,78 @@ algorithm
BackendDAE.SHARED(info=einfo) := shared;
end getExtraInfo;

public function reduceEqSystem
"
reduce EqSystem for iVarlst

Author: wbraun
"
input BackendDAE.EqSystem iSyst;
input BackendDAE.Shared shared;
input list<BackendDAE.Var> iVarlst;
output BackendDAE.EqSystem oSyst;

protected
BackendDAE.BaseClockPartitionKind partitionKind;
array<Integer> ass1, ass2;
BackendDAE.Variables v;
BackendDAE.Variables vars;
BackendDAE.Variables iVars = BackendVariable.listVar(iVarlst);
BackendDAE.EquationArray ordererdEqs, arrEqs;
list<Integer> indx_lst_v, indx_lst_e, ind_mark, statevarindx_lst;
Integer ind;
array<Integer> indx_arr;
array<Integer> mapIncRowEqn;
list<BackendDAE.Equation> el;
list<BackendDAE.Var> vl;

DAE.FunctionTree funcs;
BackendDAE.IncidenceMatrix m;
BackendDAE.BackendDAE backendDAE2;
BackendDAE.EqSystem syst;
algorithm

BackendDAE.EQSYSTEM(orderedEqs = ordererdEqs, orderedVars = v, matching = BackendDAE.MATCHING(ass1=ass1, ass2=ass2), partitionKind = partitionKind) := iSyst;

(_,statevarindx_lst) := BackendVariable.getAllStateVarIndexFromVariables(v);
indx_lst_v := BackendVariable.getVarIndexFromVariables(iVars, v);

indx_lst_v := List.appendNoCopy(indx_lst_v, statevarindx_lst) "overestimate";
indx_lst_e := List.map1r(indx_lst_v,arrayGet,ass1);


indx_arr := arrayCreate(equationArraySizeDAE(iSyst), 0);
funcs := getFunctions(shared);
(_, m, _, _, mapIncRowEqn) := getIncidenceMatrixScalar(iSyst, BackendDAE.SPARSE(), SOME(funcs));


indx_arr := markStateEquationsWork(indx_lst_e, {}, m, ass1, indx_arr);
ind_mark := arrayList(indx_arr);


indx_lst_e := {};
ind := 1;

for mark in ind_mark loop
if mark == 1 then
indx_lst_e := ind :: indx_lst_e;
end if;
ind := ind + 1;
end for;

indx_lst_e := List.map1r(indx_lst_e,arrayGet,mapIncRowEqn);

el := BackendEquation.getEqns(indx_lst_e, ordererdEqs);
arrEqs := BackendEquation.listEquation(el);

vl := BackendEquation.equationsVars(arrEqs, v);
vars := BackendVariable.listVar1(vl);

oSyst := BackendDAE.EQSYSTEM(vars, arrEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), {}, partitionKind);

end reduceEqSystem;


public function removeDiscreteAssignments "
Author: wbraun
Function tarverse Statements and remove discrete one"
Expand Down Expand Up @@ -7548,7 +7621,6 @@ algorithm
(BackendDAEOptimize.replaceEdgeChange, "replaceEdgeChange", false),
(BackendDAEOptimize.residualForm, "residualForm", false),
(ResolveLoops.resolveLoops, "resolveLoops", false),
(ResolveLoops.resolveLoops, "resolveLoops", false),
(DynamicOptimization.inputDerivativesForDynOpt, "inputDerivativesForDynOpt", false),
(EvaluateFunctions.evalFunctions, "evalFunc", false),
(UnitCheck.unitChecking, "unitChecking", true),
Expand Down Expand Up @@ -7605,6 +7677,7 @@ algorithm
(SymbolicJacobian.detectSparsePatternODE, "detectJacobianSparsePattern", false),
(Tearing.tearingSystem, "tearingSystem", false),
(DynamicOptimization.removeLoops, "extendDynamicOptimization", false),
(DynamicOptimization.reduceDynamicOptimization, "reduceDynamicOptimization", false),
(HpcOmEqSystems.partitionLinearTornSystem, "partlintornsystem", false),
(BackendDAEOptimize.addInitialStmtsToAlgorithms, "addInitialStmtsToAlgorithms", false),
(SymbolicJacobian.calculateStrongComponentJacobians, "calculateStrongComponentJacobians", false),
Expand Down
32 changes: 32 additions & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -259,6 +259,7 @@ algorithm
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsLstVars;


public function equationsVars
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
Expand Down Expand Up @@ -1336,6 +1337,37 @@ algorithm
end match;
end requationsAddDAE;

public function removeRemovedEqs
"
remove removedEqs
"
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
protected
BackendDAE.Variables knvars, exobj, aliasVars;
BackendDAE.EquationArray remeqns, inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.BackendDAEType btp;
BackendDAE.ExtraInfo ei;
Integer n;
algorithm
(BackendDAE.SHARED(knvars, exobj, aliasVars, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs, ei)) := inShared;
BackendDAE.EQUATION_ARRAY(numberOfElement=n) := remeqns;
for ind in 1:n loop
remeqns := equationRemove(ind, remeqns);
end for;

outShared := BackendDAE.SHARED(knvars, exobj, aliasVars, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs, ei);

end removeRemovedEqs;

public function setAtIndex "author: lochel
Sets the n-th array element of an EquationArray.
Please note: one-based indexing"
Expand Down
26 changes: 26 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2774,6 +2774,32 @@ algorithm
end match;
end removeVarDAE;

public function removeAliasVars
"
remove alias Vars
"
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
protected
BackendDAE.Variables knvars, exobj, aliasVars;
BackendDAE.EquationArray remeqns, inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.BackendDAEType btp;
BackendDAE.ExtraInfo ei;
algorithm
(BackendDAE.SHARED(knvars, exobj, aliasVars, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs, ei)) := inShared;
aliasVars := emptyVars();
outShared := BackendDAE.SHARED(knvars, exobj, aliasVars, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs, ei);

end removeAliasVars;

public function removeVar
"Removes a var from the vararray but does not scaling down the array"
input Integer inIndex;
Expand Down
83 changes: 83 additions & 0 deletions Compiler/BackEnd/DynamicOptimization.mo
Expand Up @@ -670,5 +670,88 @@ algorithm

end res2Con;


// =============================================================================
// section for postOptModule >>reduceDynamicOptimization<<
//
// remove eqs which not need for the calculations of cost and constraints
// =============================================================================


public function reduceDynamicOptimization
"
author: vitalij
"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
list<BackendDAE.Var> varlst, opt_varlst, conVarsList, fconVarsList, objMayer = {}, objLagrange = {};
list<BackendDAE.EqSystem> systlst, newsyst = {};
BackendDAE.EqSystem tmpsyst;
BackendDAE.Variables v;
BackendDAE.Shared shared;
algorithm
if Flags.isSet(Flags.REDUCE_DYN_OPT) and Flags.getConfigBool(Flags.GENERATE_DYN_OPTIMIZATION_PROBLEM) then

//BackendDump.bltdump("START:reduceDynamicOptimization", inDAE);
BackendDAE.DAE(systlst, shared) := inDAE;
// ToDo
shared := BackendEquation.removeRemovedEqs(shared);
shared := BackendVariable.removeAliasVars(shared);

for syst in systlst loop

BackendDAE.EQSYSTEM(orderedVars = v) := syst;
varlst := BackendVariable.varList(v);

opt_varlst := {};

conVarsList := List.select(varlst, BackendVariable.isRealOptimizeConstraintsVars);
fconVarsList := List.select(varlst, BackendVariable.isRealOptimizeFinalConstraintsVars);
objMayer := checkObjectIsSet(v,"$OMC$objectMayerTerm");
objLagrange := checkObjectIsSet(v,"$OMC$objectLagrangeTerm");

opt_varlst := List.appendNoCopy(opt_varlst, conVarsList);
opt_varlst := List.appendNoCopy(fconVarsList, conVarsList);
opt_varlst := List.appendNoCopy(objMayer, conVarsList);
opt_varlst := List.appendNoCopy(objLagrange, conVarsList);

if not List.isEmpty(opt_varlst) then
try
tmpsyst := BackendDAEUtil.reduceEqSystem(syst, shared, opt_varlst);
//BackendDump.dumpEqSystem(syst,"IN_TMP:reduceDynamicOptimization");
//BackendDump.dumpEqSystem(tmpsyst,"OUT_TMP:reduceDynamicOptimization");
newsyst := tmpsyst :: newsyst;
else
//BackendDump.dumpEqSystem(syst,"TMP:reduceDynamicOptimization fail!");
newsyst := syst :: newsyst;
end try;
end if;
end for;

outDAE := BackendDAE.DAE(newsyst, shared);
//BackendDump.bltdump("END:reduceDynamicOptimization", outDAE);
else
outDAE := inDAE;
end if;
end reduceDynamicOptimization;

public function checkObjectIsSet
"check: mayer or lagrange term are set"
input BackendDAE.Variables inVars;
input String CrefName;
output list<BackendDAE.Var> outVars;
protected
DAE.ComponentRef leftcref;
algorithm
leftcref := ComponentReference.makeCrefIdent(CrefName, DAE.T_REAL_DEFAULT, {});

try
outVars := BackendVariable.getVar(leftcref, inVars);
else
outVars := {};
end try;
end checkObjectIsSet;

annotation(__OpenModelica_Interface="backend");
end DynamicOptimization;
26 changes: 3 additions & 23 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -58,6 +58,7 @@ protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Differentiate;
protected import DynamicOptimization;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
Expand Down Expand Up @@ -1633,7 +1634,7 @@ algorithm
// Differentiate the System w.r.t states&inputs for matrices B

optimizer_vars = BackendVariable.addVariables(statesarr, BackendVariable.copyVariables(conVars));
object = checkObjectIsSet(outputvarsarr,"$OMC$objectLagrangeTerm");
object = DynamicOptimization.checkObjectIsSet(outputvarsarr,"$OMC$objectLagrangeTerm");
optimizer_vars = BackendVariable.addVars(object, optimizer_vars);
//BackendDump.printVariables(optimizer_vars);
(linearModelMatrix, sparsePattern, sparseColoring, funcs) = createJacobian(backendDAE2,states_inputs,statesarr,inputvarsarr,paramvarsarr,optimizer_vars,varlst,"B");
Expand All @@ -1645,7 +1646,7 @@ algorithm
end if;

// Differentiate the System w.r.t states for matrices C
object = checkObjectIsSet(outputvarsarr,"$OMC$objectMayerTerm");
object = DynamicOptimization.checkObjectIsSet(outputvarsarr,"$OMC$objectMayerTerm");
optimizer_vars = BackendVariable.addVars(object, optimizer_vars);
//BackendDump.printVariables(optimizer_vars);
(linearModelMatrix, sparsePattern, sparseColoring, funcs) = createJacobian(backendDAE2,states_inputs,statesarr,inputvarsarr,paramvarsarr,optimizer_vars,varlst,"C");
Expand Down Expand Up @@ -2152,27 +2153,6 @@ algorithm
end match;
end deriveAllHelper;

protected function checkObjectIsSet
"check: mayer or lagrange term are set"
input BackendDAE.Variables inVars;
input String CrefName;
output list<BackendDAE.Var> outVars;
protected
DAE.ComponentRef leftcref;
BackendDAE.Var dummy_var;
algorithm
leftcref := ComponentReference.makeCrefIdent(CrefName, DAE.T_REAL_DEFAULT, {});

try
BackendVariable.getVar(leftcref, inVars);
outVars := {BackendDAE.VAR(leftcref, BackendDAE.VARIABLE(), DAE.OUTPUT(),
DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {},
DAE.emptyElementSource, NONE(), NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER())};
else
outVars := {};
end try;
end checkObjectIsSet;

public function getJacobianMatrixbyName
input BackendDAE.SymbolicJacobians injacobianMatrixes;
input String inJacobianName;
Expand Down
6 changes: 5 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -432,6 +432,8 @@ constant DebugFlag CONSTJAC = DEBUG_FLAG(131, "constjac", false,
Util.gettext("solves linear systems with const jacobian and variable b-Vector symbolically"));
constant DebugFlag EXTENDS_DYN_OPT = DEBUG_FLAG(132, "extendsDynOpt", false,
Util.gettext("generat extends NLP, move loops in the optimization problem as constraints. hint: using intial guess from file!"));
constant DebugFlag REDUCE_DYN_OPT = DEBUG_FLAG(133, "reduceDynOpt", false,
Util.gettext("remove eqs which not need for the calculations of cost and constraints"));


// This is a list of all debug flags, to keep track of which flags are used. A
Expand Down Expand Up @@ -571,7 +573,8 @@ constant list<DebugFlag> allDebugFlags = {
NO_PARTITIONING,
ADVANCE_TEARING,
CONSTJAC,
EXTENDS_DYN_OPT
EXTENDS_DYN_OPT,
REDUCE_DYN_OPT
};

public
Expand Down Expand Up @@ -735,6 +738,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"countOperations",
"inputDerivativesUsed",
"extendDynamicOptimization",
"reduceDynamicOptimization",
"calculateStrongComponentJacobians",
"calculateStateSetsJacobians",
"detectJacobianSparsePattern",
Expand Down

0 comments on commit 450ee5e

Please sign in to comment.