question

Leo L6 avatar image
0 Likes"
Leo L6 asked IEThai commented

Create Model Parameters by Code

Hello,

I'm looking for a way to create model parameters by code. One way I see this working is a script that will take in a global table with lower bound and upper bound information and generate a corresponding parameter table that can be used in the experimenter.

The first thing I thought of was to create a table class with the Parameter table and use the table methods to set parameters/parameter table size like so:1671764164030.png

However, this does not seem to work. Is the table class able to be used here or am I approaching this the wrong way?

Thank you for any help!!

FlexSim 22.0.0
flexscriptparametersflexscript codingparameters tabletable class
1671764164030.png (11.1 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered IEThai commented

Here's the working syntax for 22.0 - since you're accessing the tree structure, note that it may change in a future version:

string pTableName="Parameters";
Object paramT=Model.find("Tools/ParameterTables/"+pTableName);
Table params=paramT.find(">variables/parameters");
treenode newParam=function_s(paramT,"addParameter");
int row=newParam.rank;
params[row]["Name"]="MyNewParameter";
params[row]["Value"]=5;
params[row]["Display Units"]="cms";
params[row]["Description"]="test parameter";
treenode attrHolder=params.cell(row,"Value");
attrHolder.subnodes["type"].value=CV_TYPE_CONTINUOUS;  //also CV_TYPE_DISCRETE, INTEGER, BINARY, EXPRESSION, OPTION, PASSTHROUGH
attrHolder.subnodes["warningLevel"].value=2;
attrHolder.subnodes["lowerBound"].value=1;
attrHolder.subnodes["upperBound"].value=10;
attrHolder.subnodes["stepSize"].value=0.5;
attrHolder.subnodes["sequenceLength"].value=10;
attrHolder.subnodes["reference"].value=Model.find("Queue1");
attrHolder.subnodes["isProxy"].value=0;
// optionally add onSet
treenode onSet=function_s(attrHolder,"assertEvent","OnSet");
onSet=function_s(attrHolder,"assertEventWithCode","OnSet");
onSet.value+="// Your own set code here";
buildnodeflexscript(onSet);
· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Leo L6 avatar image Leo L6 commented ·
Thank you!, worked perfectly
0 Likes 0 ·
Hoang Nk avatar image Hoang Nk commented ·
Hi,

I tried this code and it works superbly, however, what kind of function_s can I use to remove some/all of the existing parameters? For context, I would like to clear the model parameter everytime the model is On Reset, and then a new parameter table would be created On Simulation Start.

Many thanks :)

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Hoang Nk commented ·
The parameter tables are designed to persist between resets. For automating parameter value changes you use the experimenter to define scenario comparison or optimization jobs.
0 Likes 0 ·
IEThai avatar image IEThai commented ·

Hi Mr.Josn @Jason Lightfoot
I've been able to do it as you mentioned, but if I want to remove it, what should I do using code commands? Please help me.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ IEThai commented ·

To remove row 2 for example:

string pTableName="Parameters";
Object paramT=Model.find("Tools/ParameterTables/"+pTableName);
Table params=paramT.find(">variables/parameters");
params.deleteRow(2);
0 Likes 0 ·
IEThai avatar image IEThai Jason Lightfoot ♦♦ commented ·

Thank you so much! Your code really helped me.

0 Likes 0 ·