CriterionFunction: IExpression;
CriterionFunction: Prognoz.Platform.Interop.ForeSystem.IExpression;
The CriterionFunction property returns criterion function.
To create a criterion function, use phase variables. To get a set of phase variables, use the IMsOptimizationProblem.Operands property.
Executing the example requires that the repository contains a modeling container with the MS identifier containing a folder with the TARGET identifier and a metamodel with the TARGET_METAMODEL identifier. A calculation chain of this model must contain a model with the TARGET_MODEL identifier.
Add links to the Cp, Cubes, Dimensions, Metabase, Ms system assemblies.
Sub UserProc;
Var
mb: IMetabase;
msObj, problemObj: IMetabaseObject;
crInfo: IMetabaseObjectCreateInfo;
problem: IMsProblem;
metaModel: IMsMetaModel;
forecastProblem: IMsProblemDetails;
targetProblem: IMsTargetProblem;
period: IMsModelPeriod;
calcChain: IMsCalculationChainEntries;
calcChainModel: IMsCalculationChainModel;
model, targetModel: IMsModel;
trans, targetTrans: IMsFormulaTransform;
transformVar, targetTransformVar: IMsFormulaTransformVariable;
slice, targetSlice, constrSlice: IMsFormulaTransformSlice;
operands, controlVariables, constrOperands: IMsFormulaTermList;
operTermX1, constrTermX1: IMsFormulaTerm;
controlTermX1: IMsTargetFormulaTerm;
tree: IMsFormulaTransformSlicesTree;
initApprox: Array Of double;
i: Integer;
varConstraints: IMsTargetVarConstraints;
varConstr: IMsTargetVarConstraint;
targetConstraints: IMsTargetConstraints;
targetConstr: IMsTargetConstraint;
targetConstrArray: IMsTargetConstraintInfoArray;
targetConstrInfo: IMsTargetConstraintInfo;
Begin
mb := MetabaseClass.Active;
msObj := mb.ItemById("MS").Bind;
crInfo := MB.CreateCreateInfo;
crInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSPROBLEM;
crInfo.Id := mb.GenerateId("TARGETPROBLEM", msObj.Key);
crInfo.Name := "Criterion problem";
crInfo.Parent := mb.ItemByIdNamespace("TARGET", msObj.key);
problemObj := mb.CreateObject(crInfo).Edit;
problem := problemObj As IMsProblem;
// Set calculated metamodel
metaModel := Mb.ItemByIdNamespace("TARGET_METAMODEL", msObj.Key).Edit As IMsMetaModel;
problem.MetaModel := metaModel;
// Set forecasting parameters for problem
forecastProblem := New MsForecastingProblem.Create;
problem.Details := forecastProblem;
// Create criterion problem calculation parameters
targetProblem := New MsTargetProblem.Create;
// Set calculation periods
period := targetProblem.Period;
period.IdentificationStartDate := DateTime.Parse("01.01.2001");
period.IdentificationEndDate := DateTime.Parse("31.12.2014");
period.ForecastStartDate := DateTime.Parse("01.01.2015");
period.ForecastEndDate := DateTime.Parse("31.12.2019");
// Get a model calculated by problem
calcChain := metaModel.CalculationChain;
calcChainModel := calcChain.FindById("TARGET_MODEL") As IMsCalculationChainModel;
model := calcChainModel.Model;
// Get output variable slice
trans := model.Transform;
transformVar := trans.Outputs.Item(0);
slice := transformVar.Slices.Item(0);
// Set criterion problem parameters
problem.AdditionalDetails.AddDetails(targetProblem, slice);
// Add phase variables
operands := targetProblem.Operands;
operTermX1 := operands.Add(transformVar.Slices.Item(0));
// Set criterion function
targetProblem.CriterionFunction.AsString := operTermX1.TermToInnerText;
// Set criterion function calculation parameters
targetProblem.Extremum := ExtremumType.Maximum;
targetProblem.AutoSearchType := TargetAutoSearchType.ExactSol;
targetProblem.NodesCount := 4;
targetProblem.MethodType := CpNonLinearMethodType.GridSearch;
targetProblem.VariablesCalculationTechnique := StateVariablesCalculationTechniqueType.DirectEntirelySubstitution;
targetProblem.Level := DimCalendarLevel.Year;
// Set accuracy of solution and available number of iterations
targetProblem.Tolerance := 0.00001;
targetProblem.MaxIterationsCount := 150;
// Set controlling variables
controlVariables := targetProblem.ControlVariables;
targetModel := targetProblem.Model;
targetTrans := targetModel.Transform;
targetTransformVar := targetTrans.Inputs.Add(transformVar.VariableStub);
tree := targetTransformVar.SlicesTree(targetTransformVar);
targetSlice := tree.CreateSlice(2);
controlTermX1 := controlVariables.Add(targetSlice) As IMsTargetFormulaTerm;
// Set initial values of controlling variable
initApprox := New Double[5];
For i := 0 To 4 Do
initApprox[i] := i * 0.14;
End For;
controlTermX1.InitApproximation := initApprox;
// Set fixation mode for controlling variable limits
controlTermX1.UpperBoundFixed := MsBoundFixType.NotFixed;
controlTermX1.LowerBoundFixed := MsBoundFixType.NotFixed;
// Set controlling variable constraints
varConstraints := controlTermX1.VarConstraints;
For i := 0 To 4 Do
varConstr := varConstraints.Add;
varConstr.TimeMoment := i;
varConstr.UseLowerBound := True;
varConstr.LowerBoundFixed := False;
varConstr.LowerBound := 0.44 + i * 0.1;
varConstr.UseUpperBound := True;
varConstr.UpperBoundFixed := False;
varConstr.UpperBound := 0.64 + i * 0.1;
End For;
// Set criterion function constraints
targetConstraints := targetProblem.Constraints;
// Create constraint
targetConstr := targetConstraints.Add;
// Add a new operand into collection of criterion function operands
constrOperands := targetConstr.Operands;
targetTransformVar := targetTrans.Inputs.Add(transformVar.VariableStub);
tree := targetTransformVar.SlicesTree(targetTransformVar);
constrSlice := tree.CreateSlice(3);
constrTermX1 := constrOperands.Add(constrSlice);
// Set constraint expression
targetConstr.Expression.AsString := constrTermX1.TermToInnerText;
// Set fixation mode for constraint limits
targetConstr.LowerBoundFixed := MsBoundFixType.NotFixed;
targetConstr.UpperBoundFixed := MsBoundFixType.NotFixed;
// Set values of criterion function constraint
targetConstrArray := targetConstr.ConstraintInfoArray;
For i := 0 To 4 Do
targetConstrInfo := targetConstrArray.Add;
targetConstrInfo.TimeMoment := i;
targetConstrInfo.LowerBoundFixed := False;
targetConstrInfo.LowerBound := 0.74 + i * 0.1;
targetConstrInfo.UpperBoundFixed := False;
targetConstrInfo.UpperBound := 1.74 + i * 0.1;
End For;
// Save the model
problemObj.Save;
End Sub UserProc;
After executing the example a criterion problem is created and set up in the modeling container and the TARGET folder.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Cp;
Imports Prognoz.Platform.Interop.Cubes;
Imports Prognoz.Platform.Interop.Dimensions;
Imports Prognoz.Platform.Interop.ForeSystem;
Imports Prognoz.Platform.Interop.Ms;
…
Public Shared Sub Main(Params: StartParams);
Var
mb: IMetabase;
msObj, problemObj: IMetabaseObject;
crInfo: IMetabaseObjectCreateInfo;
problem: IMsProblem;
metaModel: IMsMetaModel;
forecastProblem: IMsProblemDetails;
targetProblem: IMsTargetProblem;
period: IMsModelPeriod;
calcChain: IMsCalculationChainEntries;
calcChainModel: IMsCalculationChainModel;
model, targetModel: IMsModel;
trans, targetTrans: IMsFormulaTransform;
transformVar, targetTransformVar: IMsFormulaTransformVariable;
slice, targetSlice, constrSlice: IMsFormulaTransformSlice;
operands, controlVariables, constrOperands: IMsFormulaTermList;
operTermX1, constrTermX1: IMsFormulaTerm;
controlTermX1: IMsTargetFormulaTerm;
tree: IMsFormulaTransformSlicesTree;
initApprox: Array Of double;
i: Integer;
varConstraints: IMsTargetVarConstraints;
varConstr: IMsTargetVarConstraint;
targetConstraints: IMsTargetConstraints;
targetConstr: IMsTargetConstraint;
targetConstrArray: IMsTargetConstraintInfoArray;
targetConstrInfo: IMsTargetConstraintInfo;
Begin
mb := Params.Metabase;
msObj := mb.ItemById["MS"].Bind();
crInfo := MB.CreateCreateInfo();
crInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSPROBLEM As integer;
crInfo.Id := mb.GenerateId("TARGETPROBLEM", msObj.Key);
crInfo.Name := "Criterion problem";
crInfo.Parent := mb.ItemByIdNamespace["TARGET", msObj.key];
problemObj := mb.CreateObject(crInfo).Edit();
problem := problemObj As IMsProblem;
// Set calculated metamodel
metaModel := Mb.ItemByIdNamespace["TARGET_METAMODEL", msObj.Key].Edit() As IMsMetaModel;
problem.MetaModel := metaModel;
// Set forecasting parameters for problem
forecastProblem := New MsForecastingProblem.Create();
problem.Details := forecastProblem;
// Create criterion problem calculation parameters
targetProblem := New MsTargetProblem.Create();
// Set calculation periods
period := targetProblem.Period;
period.IdentificationStartDate := DateTime.Parse("01.01.2001");
period.IdentificationEndDate := DateTime.Parse("31.12.2014");
period.ForecastStartDate := DateTime.Parse("01.01.2015");
period.ForecastEndDate := DateTime.Parse("31.12.2019");
// Get a model calculated by problem
calcChain := metaModel.CalculationChain;
calcChainModel := calcChain.FindById("TARGET_MODEL") As IMsCalculationChainModel;
model := calcChainModel.Model;
// Get output variable slice
trans := model.Transform;
transformVar := trans.Outputs.Item[0];
slice := transformVar.Slices.Item[0];
// Set criterion problem parameters
problem.AdditionalDetails.AddDetails(targetProblem, slice);
// Add phase variables
operands := targetProblem.Operands;
operTermX1 := operands.Add(transformVar.Slices.Item[0]);
// Set criterion function
targetProblem.CriterionFunction.AsString := operTermX1.TermToInnerText();
// Set criterion function calculation parameters
targetProblem.Extremum := ExtremumType.tetMaximum;
targetProblem.AutoSearchType := TargetAutoSearchType.tastExactSol;
targetProblem.NodesCount := 4;
targetProblem.MethodType := CpNonLinearMethodType.cnlmtGridSearch;
targetProblem.VariablesCalculationTechnique := StateVariablesCalculationTechniqueType.svctDirectEntirelySubstitution;
targetProblem.Level := DimCalendarLevel.dclYear;
// Set accuracy of solution and available number of iterations
targetProblem.Tolerance := 0.00001;
targetProblem.MaxIterationsCount := 150;
// Set controlling variables
controlVariables := targetProblem.ControlVariables;
targetModel := targetProblem.Model;
targetTrans := targetModel.Transform;
targetTransformVar := targetTrans.Inputs.Add(transformVar.VariableStub);
tree := targetTransformVar.SlicesTree[targetTransformVar];
targetSlice := tree.CreateSlice(2);
controlTermX1 := controlVariables.Add(targetSlice) As IMsTargetFormulaTerm;
// Set initial values of controlling variable
initApprox := New Double[5];
For i := 0 To 4 Do
initApprox[i] := i * 0.14;
End For;
controlTermX1.InitApproximation := initApprox;
// Set fixation mode for controlling variable limits
controlTermX1.UpperBoundFixed := MsBoundFixType.msbftNotFixed;
controlTermX1.LowerBoundFixed := MsBoundFixType.msbftNotFixed;
// Set controlling variable constraints
varConstraints := controlTermX1.VarConstraints;
For i := 0 To 4 Do
varConstr := varConstraints.Add();
varConstr.TimeMoment := i;
varConstr.UseLowerBound := True;
varConstr.LowerBoundFixed := False;
varConstr.LowerBound := 0.44 + i * 0.1;
varConstr.UseUpperBound := True;
varConstr.UpperBoundFixed := False;
varConstr.UpperBound := 0.64 + i * 0.1;
End For;
// Set criterion function constraints
targetConstraints := targetProblem.Constraints;
// Create constraint
targetConstr := targetConstraints.Add();
// Add a new operand into collection of criterion function operands
constrOperands := targetConstr.Operands;
targetTransformVar := targetTrans.Inputs.Add(transformVar.VariableStub);
tree := targetTransformVar.SlicesTree[targetTransformVar];
constrSlice := tree.CreateSlice(3);
constrTermX1 := constrOperands.Add(constrSlice);
// Set constraint expression
targetConstr.Expression.AsString := constrTermX1.TermToInnerText();
// Set fixation mode for constraint limits
targetConstr.LowerBoundFixed := MsBoundFixType.msbftNotFixed;
targetConstr.UpperBoundFixed := MsBoundFixType.msbftNotFixed;
// Set values of criterion function constraint
targetConstrArray := targetConstr.ConstraintInfoArray;
For i := 0 To 4 Do
targetConstrInfo := targetConstrArray.Add();
targetConstrInfo.TimeMoment := i;
targetConstrInfo.LowerBoundFixed := False;
targetConstrInfo.LowerBound := 0.74 + i * 0.1;
targetConstrInfo.UpperBoundFixed := False;
targetConstrInfo.UpperBound := 1.74 + i * 0.1;
End For;
// Save the model
problemObj.Save();
End Sub;
See also: