question

C avatar image
0 Likes"
C asked C commented

Changing the value of a variable daily depending on the month

Hi all!

I want to increase the battery discharge rate by changing the value "1.0" (the underlined one in the code) depending on dates and probabilities. For example;

If it is January, there is a 60% chance that the value is 1.0 for that day, a 35% chance that the value is 1.1 for that day, and a 5% chance that the value is 1.2 for that day. A maximum of 5 days at most in January can have the value of 1.2, 15 days 1.1, and the rest of 11 days have the value of 1. I think it should use a statistical distribution for that.

If it is not possible, while is it not the way I intended (I want randomizing) but it can use a table with values defined for 365 days and change the value accordingly. Just reference a table, and I will fill it.

v4.fsm

The current code is given below:


  1. /**Custom Code*/
  2. Object current = param(1);
  3. treenode activity = param(2);
  4. Token token = param(3);
  5. Variant assignTo = param(4);
  6. string labelName = param(5);
  7. treenode processFlow = ownerobject(activity);
  8.  
  9. TrackedVariable battery = current.labels["Battery"];
  10. battery.rate = 1.0*current.dischargeRate;
  11.  
  12. return battery.value/current.dischargeRate;
FlexSim 19.0.0
statistical distributioncoding
v4.fsm (247.8 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

Felix Möhlmann avatar image
1 Like"
Felix Möhlmann answered C commented

I added some logic to your model that should do what you ask for.

A global variable "dailyChargeRate" was added and replaces the fixed value in line 10 in the code that sets the discharge rate.

A new process flow sets this value each day at midnight (first time directly on model start). Which number is chosen is based on the "ChargeRatesTable". Three columns form a group that specify the possible value, its chance to get picked and the maximum number of times it can occur in the month. Each row represents a month.

The code first checks if a new month has started and resets the array label of the token that counts how often each value was picked.

Then it goes through the table and checks which values are still eligible to be picked (below max). These get pushed to an array that holds all possible values. So far I copied the values you mentioned into the table. The number of three-column groups is practically unlimited (though there is a max size for a table in FlexSim).

Afterwards the chance of all eligible values is summed up, since it might be smaller than one, if one ore more values have reached their max already. A random number between 0 and that sum then determines which value gets picked: If the random number is smaller than the chance of a value plus the chances of all previous values, the number gets picked. For example, let's say there are three numbers with chances (0.1, 0.2, and 0.4), so the random number would between 0 and 0.7. For 0.28, the second value would get picked, because 0.1 < 0.28, but 0.1 + 0.2 > 0.28.

After a number is picked, the respective count in the token label is incremented and the global variable is set to the new value.

v4-fm.fsm

I'll also attach the model I used to test the logic. Each day the chosen value is written to a global table. Since it's otherwise it can quickly run many years to test the results.

Currently, since the chance of the value 1 is higher than its maximum allowed count, it mostly appears in the first 2/3 of a month. 1.1 then follows in bulk after 1 is not allowed anymore.

dailyRateTest-fm.fsm


v4-fm.fsm (251.2 KiB)
· 7
5 |100000

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