question

Igor R3 avatar image
0 Likes"
Igor R3 asked tannerp commented

Creating a loop for model execution

I create some triggers with the goal of repeat the test 3 times, when the test rest a trigger start the test, when the test stop, an global table is altered, and a specific cell thats start with 0 is incremented in 1, than if the value of this cell is lower than 3, the trigger calls the reset.
All this triggers are working when I manually push the equivalent button (e.g. when I push the button "stop", the test reset and automatically reset) this works for any button ("reset" and "stop"). However, if I start from any of the buttons, only the first trigger is active, and i can complete the loop as i want (e.g. when I push the button "reset", the test starts, goes to the end, stop and reset, but not start again).
I believe that is some configuration from Flexsim 18, created probable to avoid start an infinite loop. So, to do my test as I want, I need to disable (if there is the case) this lock.
I'm using Flexsim version 18.0.8 build 268 built on 06-Set-2018.
My code from the triggers:

  //#################################################################//
  //#######################RESET TRIGGER#############################//
  //#################################################################//
OnModelReset{
  runspeed(1000);
  go();
}
  //#################################################################//
  //#######################STOP TRIGGER###############################//
  //#################################################################//
OnRunStop{
  //----------This first part is working fine------------------------//
  Variant tableID = /** \nTable: */ /***tag:table*//**/"execution"/**/;
  Variant row = /** \nRow: */ /***tag:row*//**/1/**/;
  Variant column = /** \nColumn: */ /***tag:col*//**/1/**/;
  Table table;
  switch (tableID.type) {
    case VAR_TYPE_NODE: table = tableID; break;
    case VAR_TYPE_STRING: table = Table(tableID.as(string)); break;
    default:
    table = reftable(tableID.as(int));
    break;
  }
  Variant value = table[row][column];
  value = value+1;
  table[row][column] = value;
  //-----------------------------------------------------------------//
  //the condition if also is working, just executing when the condition is true
	if(value < 3){reset();}
}

I'm also tried use "User Commands" to call reset and start, trying to tricking the hypothesis of be an system look to avoid infinite looping, but was not an successful attempt.

I'm a C++ developer, and only recently start using Flexsim, so maybe that is a very simple question, but I'm already read many parts of user guild and looking for this solution on this forum, both without success. Even find some topics about loops, none of than was about this, and i won't use the Experiment tool to do so, once i'm intent to use a variable instead the fixed 3 for the time series, which i'm not seen how to do on experiment configurations, and I'll not need the features extractions from Experiment tool, this way I'm prefer don't use the Experiment tool for solve my problem, if possible, my main goal is use the "model" view as an exhibition tool for my project.

P.S. As suggested in an answer below, I'm annexing an example of my model with the triggers in the code above.

question.fsm

Choose One
custom code
question.fsm (22.8 KiB)
· 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.

tannerp avatar image tannerp commented ·

Have you used the Experimenter? Using the Experimenter, you can specify how many replications you'd like for each scenario.

0 Likes 0 ·
Igor R3 avatar image Igor R3 commented ·

I'm aware, but I'm interested only on the visualization of the process, than I'm do not need any of features extractions from Experimenter, beside I'm not sure if i can have the productive chain visualization (the model animation) using the Experimenter. I'm trying find a solution to use the animation of my process.

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Igor R3 commented ·

If you want better help, perhaps you should post your sample model showing what you tried and what is not behaving how you want rather than a lengthy text description.

0 Likes 0 ·
Igor R3 avatar image Igor R3 Phil BoBo ♦♦ commented ·

The model dosen't matter, any model with the trigger as defined in the code, that was able to really create a cycle (reset,start,stop,reset,start,stop) of execution will attend my need. However, i'm need the animation, which is not possible in Experiments, then, i need the trigger of stop calls the reset, the trigger of reset calls the start, and this make many runs as i define in the global table, as showed on the code.
The lengthy text description is just to explain the whole situation.

0 Likes 0 ·
Show more comments

1 Answer

Phil BoBo avatar image
0 Likes"
Phil BoBo answered

The model's On Reset trigger fires more often than you want. For example, the model gets reset when you save it. You shouldn't tell the model to go during its On Reset. If you do that, then you can't actually reset the model without running it.

The model's On Run Stop trigger also fires more often than you want. For example, that trigger fires if you simply stop the model in the middle of a run.

What you want is a custom trigger that fires when a model replication is finished in order to stop the model and start the next replication. You can do this with a User Event. Simply set the time for the user event to be the amount of time that you want each replication to last. Put your replication looping code into that event:

stop();

Table table = Table("execution");
Variant value = table[1][1];

print("Replication", value, "complete.");

if(value < 10){
	table[1][1]++;
	treenode StartNextRun = model().find("Tools/StartNextRun");
	postwindowmessage(systemwindow(0), FLEXSIM_MESSAGE_USER_NODEFUNCTION, StartNextRun, 0);
} else {
	print("All replications complete.");
	table[1][1] = 1;
}

I've moved the code that starts the next replication into a node function in the Tools folder so that it can be executed after that final event is finished processing, using a FLEXSIM_MESSAGE_USER_NODEFUNCTION.

Attached is your model (16446-question-1.fsm), modified accordingly.


5 |100000

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