CallbackAssembly: IMetabaseObjectDescriptor;
The CallbackAssembly property determines an assembly that contains a class for executing a custom algorithm.
Executing the example requires that the repository contains a modeling container with the MS identifier containing a folder with the USEROPTIMIZATION identifier, a metamodel with the USEROPTIMIZATION_METAMODEL identifier, ad a model with the USEROPTIMIZATION_MODEL identifier. The repository should also contain a unit with the USEROPTIMIZATIONCALLBACK identifier that implements a custom algorithm using the IMsUserOptimizationCallback interface.
Add links to the Cp, Dimensions, Metabase, Ms system assemblies.
Sub UserProc;
Var
MB: IMetabase;
MsObj, ProblemObj: IMetabaseObject;
CrInfo: IMetabaseObjectCreateInfo;
Problem: IMsProblem;
MetaModel: IMsMetaModel;
Period: IMsModelPeriod;
CalcChain: IMsCalculationChainEntries;
CalcChainModel: IMsCalculationChainModel;
Model: IMsModel;
Trans, UserOptTrans: IMsFormulaTransform;
TransformVar, UserOptTransformVar: IMsFormulaTransformVariable;
Slice, UserOptSlice, ConstrSlice: IMsFormulaTransformSlice;
Operands, ControlVariables, ConstrOperands: IMsFormulaTermList;
OperTermX1, ConstrTermX1: IMsFormulaTerm;
ControlTermX1: IMsOptimizationFormulaTerm;
Tree: IMsFormulaTransformSlicesTree;
InitApprox: Array Of double;
i: Integer;
NlConstraints: IMsNonLinearConstraints;
NlConstr: IMsNonLinearConstraint;
UserOptProblem: IMsUserOptimizationProblem;
PTermInfo: IMsFormulaTermInfo;
PTerm: IMsFormulaTerm;
Begin
MB := MetabaseClass.Active;
// Get modeling container
MsObj := MB.ItemById("MS").Bind;
// Create a custom optimization problem
CrInfo := MB.CreateCreateInfo;
CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSPROBLEM;
CrInfo.Id := MB.GenerateId("USEROPTIMIZATIONPROBLEM", MsObj.Key);
CrInfo.Name := "Custom optimization problem";
CrInfo.Parent := MB.ItemByIdNamespace("USEROPTIMIZATION", MsObj.key);
ProblemObj := MB.CreateObject(CrInfo).Edit;
Problem := ProblemObj As IMsProblem;
UserOptProblem := New MsUserOptimizationProblem.Create;
Problem.Details := UserOptProblem;
// Set calculation periods
Period := UserOptProblem.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");
// Set calculated metamodel
MetaModel := MB.ItemByIdNamespace("USEROPTIMIZATION_METAMODEL", MsObj.Key).Edit As IMsMetaModel;
Problem.MetaModel := MetaModel;
// Get model calculated by problem
CalcChain := MetaModel.CalculationChain;
CalcChainModel := CalcChain.FindById("USEROPTIMIZATION_MODEL") As IMsCalculationChainModel;
Model := CalcChainModel.Model;
// Set criterion function calculation options
UserOptProblem.Level := DimCalendarLevel.Year;
// Set criterion function
PTermInfo := UserOptProblem.ControlTransform.CreateTermInfo;
PTermInfo.ParseTerm("{Inflation, %|London[t]}");
PTerm := UserOptProblem.Operands.Add(pTermInfo.Slice);
PTerm.TermInfo := PTermInfo;
UserOptProblem.CriterionFunction.AsString := PTerm.TermInfo.TermInnerText;
// Add phase variables
Trans := Model.Transform;
TransformVar := Trans.Outputs.Item(0);
Slice := TransformVar.Slices.Item(0);
Operands := UserOptProblem.Operands;
OperTermX1 := Operands.Add(Slice);
Debug.WriteLine(OperTermX1.TermToInnerText);
// Set controlling variables
ControlVariables := UserOptProblem.ControlVariables;
UserOptTrans := UserOptProblem.ControlTransform;
UserOptTransformVar := UserOptTrans.Inputs.Add(TransformVar.VariableStub);
Tree := UserOptTransformVar.SlicesTree(UserOptTransformVar);
UserOptSlice := Tree.CreateSlice(2);
ControlTermX1 := ControlVariables.Add(UserOptSlice) As IMsOptimizationFormulaTerm;
Debug.WriteLine(ControlTermX1.TermToInnerText);
// 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 criterion function constraints
NlConstraints := UserOptProblem.Constraints;
NlConstr := NlConstraints.Add;
// Add a new operand to a collection of criterion function operands
ConstrOperands := NlConstr.Operands;
UserOptTransformVar := UserOptTrans.Inputs.Add(transformVar.VariableStub);
Tree := UserOptTransformVar.SlicesTree(UserOptTransformVar);
ConstrSlice := Tree.CreateSlice(3);
ConstrTermX1 := ConstrOperands.Add(ConstrSlice);
// Set constraint expression
NlConstr.Expression.AsString := ConstrTermX1.TermToInnerText;
NlConstr.LowerBound.AsString := "-10";
NlConstr.UpperBound.AsString := "10";
// Set custom algorithm
UserOptProblem.CallbackAsseMBly := MetabaseClass.Active.ItemById("USEROPTIMIZATIONCALLBACK");
UserOptProblem.CallbackClass := "UserClass";
// Save problem
ProblemObj.Save;
End Sub UserProc;
After executing the example, a custom optimization problem with specified parameters will be created in the modeling container.
See also: