For modeling guidelines that require you to use a subset of block or parameter values, you can create Model Advisor checks in which you specify these constraints:
Supported or unsupported block parameter values
Supported or unsupported model parameter values
Supported or unsupported blocks
Check for whether blocks or parameters meet a combination of constraints
You can also create constraints that check for prerequisite constraints before checking the actual constraint. You can check your model against these constraints as you edit or run the checks interactively after you complete your model design.
The sldemo_bounce
model simulates a ball bouncing on Earth. In
this example, you create two Model Advisor checks consisting of constraints. Then,
check your model against those constraints.
Create these block parameter constraints:
c1=Advisor.authoring.PositiveBlockParameterConstraint; c1.ID='ID_1'; c1.BlockType='Gain'; c1.ParameterName='Gain'; c1.SupportedParameterValues={'-.7'}; c1.ValueOperator='eq';
c2=Advisor.authoring.NegativeBlockParameterConstraint; c2.ID='ID_2'; c2.BlockType='InitialCondition'; c2.ParameterName='Value'; c2.UnsupportedParameterValues={'0'}; c2.ValueOperator='le';
Constraint c1
specifies that a Gain
block must have a value equal to -.7
. Constraint
c2
specifies that the Initial
Condition block must have a value less than or equal to
zero.
Create this positive model parameter constraint.
c3=Advisor.authoring.PositiveModelParameterConstraint; c3.ID='ID_3'; c3.ParameterName='SolverType'; c3.SupportedParameterValues={'Variable-step'};
Constraint c3
specifies that the
Solver parameter must be equal to
Variable-step
.
Create this positive block type constraint:
c4=Advisor.authoring.PositiveBlockTypeConstraint; c4.ID='ID_5'; s1=struct('BlockType','Constant','MaskType',''); s2=struct('BlockType','Subsystem','MaskType',''); s3=struct('BlockType','InitialCondition','MaskType',''); s4=struct('BlockType','Gain','MaskType',''); s5=struct('BlockType','Memory','MaskType',''); s6=struct('BlockType','SecondOrderIntegrator','MaskType',''); s7=struct('BlockType','Terminator','MaskType',''); c4.SupportedBlockTypes={s1;s2;s3;s4;s5;s6;s7;}; c4.addPreRequisiteConstraintID('ID_3');
Constraint c4
specifies the supported blocks.
Constraint c3
is a prerequisite to c4
meaning that the Model Advisor only checks c4
if
c3
passes.
Create a data file that contains these constraints. This data file corresponds to one Model Advisor check.
Advisor.authoring.generateBlockConstraintsDataFile( ... 'sldemo_constraints_1.xml','constraints',{c1,c2,c3,c4});
The data file contains tagging specifically for constraints.
<?xml version="1.0" encoding="utf-8"?> <customcheck> <checkdata> <PositiveBlockParameterConstraint BlockType="Gain" id="ID_1"> <parameter type="string">Gain</parameter> <value>-.7</value> <operator>eq</operator> </PositiveBlockParameterConstraint> <NegativeBlockParameterConstraint BlockType="InitialCondition" id="ID_2"> <parameter type="string">Value</parameter> <value>0</value> <operator>le</operator> </NegativeBlockParameterConstraint> <PositiveModelParameterConstraint id="ID_3"> <parameter type="enum">SolverType</parameter> <value>Variable-step</value> </PositiveModelParameterConstraint> <PositiveBlockTypeConstraint id="ID_5"> <BlockType MaskType="">Constant</BlockType> <BlockType MaskType="">Subsystem</BlockType> <BlockType MaskType="">InitialCondition</BlockType> <BlockType MaskType="">Gain</BlockType> <BlockType MaskType="">Memory</BlockType> <BlockType MaskType="">SecondOrderIntegrator</BlockType> <BlockType MaskType="">Terminator</BlockType> <dependson>ID_3</dependson> </PositiveBlockTypeConstraint> <CompositeConstraint> <ID>ID_1</ID> <ID>ID_2</ID> <ID>ID_5</ID> <operator>and</operator> </CompositeConstraint> </checkdata> </customcheck>
Note
For model configuration parameter constraints, use the
Advisor.authoring.generateBlockConstraintsDataFile
method only when specifying model configuration parameter constraints as
prerequisites to block constraints or as part of a composite constraint
consisting of block and model configuration parameter constraints. For
other cases, use the
Advisor_authoring.generateConfigurationParameterDatafile
method.
Create two block parameter constraints and a composite constraint.
cc1=Advisor.authoring.PositiveBlockParameterConstraint; cc1.ID='ID_cc1'; cc1.BlockType='SecondOrderIntegrator'; cc1.ParameterName='UpperLimitX'; cc1.SupportedParameterValues={'inf'}; cc1.ValueOperator='eq'; cc2=Advisor.authoring.PositiveBlockParameterConstraint; cc2.ID='ID_cc2'; cc2.BlockType='SecondOrderIntegrator'; cc2.ParameterName='LowerLimitX'; cc2.SupportedParameterValues={'0.0'}; cc2.ValueOperator='eq'; cc=Advisor.authoring.CompositeConstraint; cc.addConstraintID('ID_cc1'); cc.addConstraintID('ID_cc2'); cc.CompositeOperator='and';
Constraint cc1
specifies that for a Second-Order
Integrator block, the Upper limit x
parameter must have a value equal to inf
. Constraint
cc2
specifies that for a Second-Order
Integrator block, the Lower limit x
parameter must have a value equal to zero. Constraint cc
specifies that for this check to pass, both cc1
and
cc2
have to pass.
Create a data file that contains these constraints. This data file corresponds to a second Model Advisor check.
Advisor.authoring.generateBlockConstraintsDataFile( ... 'sldemo_constraints_2.xml','constraints',{cc1,cc2,cc});
To specify and register these checks, use this
sl_customization.m
file.
function sl_customization(cm) % register custom checks. cm.addModelAdvisorCheckFcn(@defineModelAdvisorChecks); % register items to factory group. cm.addModelAdvisorTaskFcn(@defineModelAdvisorGroups); % defineModelAdvisorChecks function defineModelAdvisorChecks % check1 rec = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0001'); rec.Title = 'Example1: Check block parameter constraints'; rec.setCallbackFcn(@(system)(Advisor.authoring.CustomCheck.checkCallback... (system)), 'None', 'StyleOne'); rec.TitleTips = 'Example check block parameter constraints'; % --- data file input parameters rec.setInputParametersLayoutGrid([1 1]); inputParam1 = ModelAdvisor.InputParameter; inputParam1.Name = 'Data File'; inputParam1.Value = 'sldemo_constraints_1.xml'; inputParam1.Type = 'String'; inputParam1.Description = 'Name or full path of XML data file.'; inputParam1.setRowSpan([1 1]); inputParam1.setColSpan([1 1]); rec.setInputParameters({inputParam1}); rec.SupportExclusion = false; rec.SupportLibrary = true; % check2 rec1 = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0002'); rec1.Title = 'Example2: Check block parameter constraints'; rec1.setCallbackFcn(@(system)(Advisor.authoring.CustomCheck.checkCallback... (system)), 'None', 'StyleOne'); rec1.TitleTips = 'Example check block parameter constraints'; % --- data file input parameters rec1.setInputParametersLayoutGrid([1 1]); inputParam1 = ModelAdvisor.InputParameter; inputParam1.Name = 'Data File'; inputParam1.Value = 'sldemo_constraints_2.xml'; inputParam1.Type = 'String'; inputParam1.Description = 'Name or full path of XML data file.'; inputParam1.setRowSpan([1 1]); inputParam1.setColSpan([1 1]); rec1.setInputParameters({inputParam1}); rec1.SupportExclusion = false; rec1.SupportLibrary = true; mdladvRoot = ModelAdvisor.Root; mdladvRoot.register(rec); mdladvRoot.register(rec1); %% defineModelAdvisorGroups function defineModelAdvisorGroups mdladvRoot = ModelAdvisor.Root; % --- sample factory group 1 rec = ModelAdvisor.FactoryGroup('com.mathworks.Test.factoryGroup'); rec.DisplayName='Example: My Group'; rec.addCheck('mathworks.check_0001'); rec.addCheck('mathworks.check_0002'); mdladvRoot.publish(rec);
Advisor.authoring.createBlockConstraintCheck
function to create the ModelAdvisor.Check
object and
specify the constraint data file as an input parameter to this
object.Note that block parameter and model parameter constraint checks must use
the setCallbackFcn
type of
StyleOne
.
At the command prompt, type create the Example1: Check block parameter constraints and Example2: Check block parameter constraints checks by typing this command:
Advisor.Manager.refresh_customizations
At the command prompt, type sldemo_bounce
.
In the Modeling tab, select Model Advisor to open the Model Advisor.
In the left pane, select By Task > Example: My Group. For each check, in the right pane, the Data File parameters are set to the data files that you previously created.
Click Run Selected Checks.
The Example1: Check block parameter constraints check
produces a warning because the Gain block has a value of
-0.8
not -0.7
. The
Example2: Check block parameter constraints check
passes because the Second-Order Integrator block meets both
constraints.
You can use edit-time checking for custom checks that define block and parameter constraints. To enable edit-time checking, in the Model Advisor Configuration Editor, select the checks that contain the constraints. For more information on edit-time checking, see Check Your Model by Using Edit Time Checks.
Advisor.authoring.generateBlockConstraintsDataFile
| NegativeBlockParameterConstraint
| NegativeBlockTypeConstraint
| NegativeModelParameterConstraint
| PositiveBlockParameterConstraint
| PositiveBlockTypeConstraint
| PositiveModelParameterConstraint