Constraints: IMsNonLinearConstraints;
Constraints: Prognoz.Platform.Interop.Ms.IMsNonLinearConstraints;
The Constraints property returns the collection of constraints for criterion function.
To set the object, to which optimal values will be loaded, use the IMsControlProblem.OptimalVariable property.
Executing the example requires that the repository contains a modeling container with the MS identifier. This container must contain a folder with the CONTROLPROBLEM_FOLDER identifier and a metamodel with the CONTROL_METAMODEL identifier. The specified metamodel must contain a model with the CONTROL_MODEL identifier containing several factors.
Add links to the Cp, Dimensions, Metabase, Ms system assemblies.
Sub UserProc;
Var
MB: IMetabase;
MsObj: IMetabaseObjectDescriptor;
CrInf: IMetabaseObjectCreateInfo;
MObj: IMetabaseObject;
Problem: IMsProblem;
MetaModel: IMsMetaModel;
ScenarioTree: IMsScenarioTreeEntries;
Scenario: IMsScenario;
ControlProblem: IMsControlProblem;
CPModel, Model: IMsModel;
CPTransform: IMsFormulaTransform;
CPOutputs, Inputs, CPInputs: IMsFormulaTransformVariables;
ControlsList, CFList: IMsFormulaTermList;
ConstrTerm, CFTerm: IMsFormulaTerm;
FactorVar, ControlVar, CFVar: IMsFormulaTransformVariable;
Slice: IMsFormulaTransformSlice;
ControlTerm: IMsNonLinearControlFormulaTerm;
Constrs: IMsNonLinearConstraints;
Constr: IMsNonLinearConstraint;
MsKey: Integer;
Period: IMsModelPeriod;
s: String;
i: Integer;
Begin
// Get current repository
MB := MetabaseClass.Active;
// Get modelling container
MsObj := Mb.ItemById("MS").Edit;
MsKey := MsObj.Key;
// Create the Modeling Problem object
CrInf := Mb.CreateCreateInfo;
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSPROBLEM;
CrInf.Id := MB.GenerateId("CONTROL_PROBLEM", MsKey);
CrInf.Name := "Optimal control problem";
CrInf.Parent := MB.ItemByIdNamespace("CONTROLPROBLEM_FOLDER", MsKey);
MObj := Mb.CreateObject(CrInf).Edit;
Problem := MObj As IMsProblem;
// Determine metamodel calculated by problem
MetaModel := MB.ItemByIdNamespace("CONTROL_METAMODEL", MsKey).Bind As IMsMetaModel;
Problem.MetaModel := MetaModel;
// Get modeling container scenarios
ScenarioTree := (MsObj As IMsModelSpace).ScenarioTree;
// Add a scenario
Scenario := (ScenarioTree.AddScenario).Scenario;
// Add created scenario to modeling problem
Problem.Scenarios.AddScenario(Scenario);
// Create calculation parameters of optimal control problem
ControlProblem := New MsControlProblem.Create;
// Determine that created modeling problem has the Optimal Control Problem type
Problem.Details := ControlProblem;
// Set basic parameters of optimal control problem
ControlProblem.Extremum := ExtremumType.Minimum;
ControlProblem.MaxIterationsCount := 100000;
ControlProblem.MethodType := CpNonLinearMethodType.SequentialQP;
ControlProblem.Level := DimCalendarLevel.Year;
ControlProblem.Tolerance := 0.005;
// Get model containing criterion function parameters
CPModel := ControlProblem.Model;
// Get output model
CPTransform := CPModel.Transform;
CPOutputs := CPTransform.Outputs;
// Determine that optimal values of criterion function will be loaded to output variable
ControlProblem.OptimalVariable := CPOutputs.Item(0).VariableStub As IMsVariable;
// Get collection of controlling variables
ControlsList := ControlProblem.ControlVariables;
// Get model calculated by modeling problem
Model := (MetaModel.CalculationChain.FindById("CONTROL_MODEL") As IMsCalculationChainModel).Model;
// Get collection of model factors
Inputs := Model.Transform.Inputs;
// Add all model factors to controlling variables collection
For i := 0 To Inputs.Count - 1 Do
FactorVar := Inputs.Item(i);
CPInputs := CPTransform.Inputs;
ControlVar := CPInputs.Add(FactorVar.VariableStub);
Slice := ControlVar.Slices.Add(FactorVar.Slices.Item(0).Selection);
// Determine limits of controlling values
ControlTerm := ControlsList.Add(Slice) As IMsNonLinearControlFormulaTerm;
ControlTerm.UseLowerBound := True;
ControlTerm.LowerBound := 0 - i * 0.1;
ControlTerm.UseUpperBound := True;
ControlTerm.UpperBound := 1 + i * 0.1;
End For;
// Get collection of variables to compose criterion function
CFList := ControlProblem.Operands;
// Add variable to collection
CFVar := CPInputs.Item(0);
Slice := CFVar.Slices.Item(0);
CFTerm := CFList.Add(Slice);
// Compose criterion function
s := CFTerm.TermToInnerText + " + 0.5";
ControlProblem.CriterionFunction.AsString := s;
// Get collection of criterion function constraints
Constrs := ControlProblem.Constraints;
// Add a constraint
Constr := Constrs.Add;
ConstrTerm := Constr.Operands.Add(Slice);
Constr.Expression.AsString := ConstrTerm.TermToInnerText;
Constr.LowerBound.AsString := "-100";
Constr.UpperBound.AsString := "100";
// Set calculation period
Period := ControlProblem.Period;
Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
Period.IdentificationEndDate := DateTime.Parse("31.12.2011");
Period.ForecastStartDate := DateTime.Parse("01.01.2012");
Period.ForecastEndDate := DateTime.Parse("31.12.2018");
// Save changes
MObj.Save;
(MsObj As IMetabaseObject).Save;
End Sub UserProc;
After executing the example a new optimal control problem is created in the CONTROLPROBLEM_FOLDER folder of modeling container. The metamodel, scenario, variable are added to the problem for making of criterion function. The criterion function, for which the minimum value is determined during problem calculation. The quadratic programming method is used for calculation. The controlling variable and one constraint for criterion function is added. The minimum number of iterations and accuracy of solution search are set.
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.Dimensions;
Imports Prognoz.Platform.Interop.Ms;
…
Public Shared Sub Main(Params: StartParams);
Var
MB: IMetabase;
MsObj: IMetabaseObjectDescriptor;
CrInf: IMetabaseObjectCreateInfo;
MObj: IMetabaseObject;
Problem: IMsProblem;
MetaModel: IMsMetaModel;
ScenarioTree: IMsScenarioTreeEntries;
Scenario: IMsScenario;
ControlProblem: IMsControlProblem;
CPModel, Model: IMsModel;
CPTransform: IMsFormulaTransform;
CPOutputs, Inputs, CPInputs: IMsFormulaTransformVariables;
ControlsList, CFList: IMsFormulaTermList;
ConstrTerm, CFTerm: IMsFormulaTerm;
FactorVar, ControlVar, CFVar: IMsFormulaTransformVariable;
Slice: IMsFormulaTransformSlice;
ControlTerm: IMsNonLinearControlFormulaTerm;
Constrs: IMsNonLinearConstraints;
Constr: IMsNonLinearConstraint;
MsKey: uinteger;
Period: IMsModelPeriod;
s: String;
i: Integer;
Begin
// Get current repository
MB := Params.Metabase;
// Get modeling container
MsObj := Mb.ItemById["MS"].Edit();
MsKey := MsObj.Key;
// Create the Modeling Problem object
CrInf := Mb.CreateCreateInfo();
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSPROBLEM As integer;
CrInf.Id := MB.GenerateId("CONTROL_PROBLEM", MsKey);
CrInf.Name := "Optimal control problem";
CrInf.Parent := MB.ItemByIdNamespace["CONTROLPROBLEM_FOLDER", MsKey];
MObj := Mb.CreateObject(CrInf).Edit();
Problem := MObj As IMsProblem;
// Determine metamodel calculated by problem
MetaModel := MB.ItemByIdNamespace["CONTROL_METAMODEL", MsKey].Bind() As IMsMetaModel;
Problem.MetaModel := MetaModel;
// Get modeling container scenario
ScenarioTree := (MsObj As IMsModelSpace).ScenarioTree;
// Add a scenario
Scenario := (ScenarioTree.AddScenario(False, True)).Scenario;
// Add created scenario to modeling problem
Problem.Scenarios.AddScenario(Scenario);
// Create calculation parameters of optimal control problem
ControlProblem := New MsControlProblem.Create();
// Determine that created modeling problem has the Optimal Control Problem type
Problem.Details := ControlProblem;
// Set basic parameters of optimal control problem
ControlProblem.Extremum := ExtremumType.tetMinimum;
ControlProblem.MaxIterationsCount := 100000;
ControlProblem.MethodType := CpNonLinearMethodType.cnlmtSequentialQP;
ControlProblem.Level := DimCalendarLevel.dclYear;
ControlProblem.Tolerance := 0.005;
// Get a model containing criterion function parameters
CPModel := ControlProblem.Model;
// Get output variable
CPTransform := CPModel.Transform;
CPOutputs := CPTransform.Outputs;
// Determine that optimal values of criterion function will be unloaded to modeling variable
ControlProblem.OptimalVariable := CPOutputs.Item[0].VariableStub As IMsVariable;
// Get collection of controlling variables
ControlsList := ControlProblem.ControlVariables;
// Get model calculated by modeling problem
Model := (MetaModel.CalculationChain.FindById("CONTROL_MODEL") As IMsCalculationChainModel).Model;
// Get collection of model factors
Inputs := Model.Transform.Inputs;
// Add all model factors to collection of controlling variables
For i := 0 To Inputs.Count - 1 Do
FactorVar := Inputs.Item[i];
CPInputs := CPTransform.Inputs;
ControlVar := CPInputs.Add(FactorVar.VariableStub);
Slice := ControlVar.Slices.Add(FactorVar.Slices.Item[0].Selection);
// Set limits of controlling variables
ControlTerm := ControlsList.Add(Slice) As IMsNonLinearControlFormulaTerm;
ControlTerm.UseLowerBound := True;
ControlTerm.LowerBound := 0 - i * 0.1;
ControlTerm.UseUpperBound := True;
ControlTerm.UpperBound := 1 + i * 0.1;
End For;
// Get collection of variables to compose criterion function
CFList := ControlProblem.Operands;
// Add a variable to collection
CFVar := CPInputs.Item[0];
Slice := CFVar.Slices.Item[0];
CFTerm := CFList.Add(Slice);
// Compose criterion function
s := CFTerm.TermToInnerText() + " + 0.5";
ControlProblem.CriterionFunction.AsString := s;
// Get collection of criterion function constraints
Constrs := ControlProblem.Constraints;
// Add a constraint
Constr := Constrs.Add();
ConstrTerm := Constr.Operands.Add(Slice);
Constr.Expression.AsString := ConstrTerm.TermToInnerText();
Constr.LowerBound.AsString := "-100";
Constr.UpperBound.AsString := "100";
// Set calculation period
Period := ControlProblem.Period;
Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
Period.IdentificationEndDate := DateTime.Parse("31.12.2011");
Period.ForecastStartDate := DateTime.Parse("01.01.2012");
Period.ForecastEndDate := DateTime.Parse("31.12.2018");
// Save changes
MObj.Save();
(MsObj As IMetabaseObject).Save();
End Sub;
See also: