Nested conditional mystery

26 views
Skip to first unread message

H J

unread,
Dec 4, 2025, 7:54:50 PMDec 4
to JaamSim Users Discussion Group
I'm mystified by how conditional logic in Jaamsim works. I'm using the UniformDistribution to generate distinct cases and then Branch depending on the case. See two examples attached: with 2 branches the logic works, with 3 branches it messes up ... (I expect 20%, 30% and 50% of the entities send over respective branches; instead I receive 20%, 40% and 40% ...). What am I missing? Thanks!
MultiBranchTest.-3.cfg
MultiBranchTest.-2.cfg

Harry King

unread,
Dec 4, 2025, 9:45:32 PMDec 4
to JaamSim Users Discussion Group
The problem is in your CustomOutput C1, C2, and C3 for UniformDistribution1:

{ C1  'this.Value < 0.2  # 20% expected #' }
{ C2  'this.Value < 0.5# 30% expected #' }
{ C3  'this.Value <= 1.0 # 50% expected #' }
{ Case  'this.C1? 1 : (this.C2? 2 : ( this.C3? 3 : 4)) ' }


Each time this.Value is evaluated, it returns a new random sample from the distribution! This means that C1, C2, and C3 are completely unrelated to each other, which invalidates the Case output. Note that the random distribution functions for all simulation software work this way -- it is not just a quirk of JaamSim.

The correct way to calculate the Case output is to save the random sample as a local variable, i.e.:

{ Case 'rand = this.Value; rand < 0.2 ? 1 : (rand < 0.5 ? 2 : 3)' }

Note that C3 is always 1 in your original calculation so this portion of the expression for Case can be ignored.

An easier way to make this random selection is to use the DiscreteDistribution object instead of the UniformDistribution. Set its ValueLIst input to 1 2 3 and its ProbabilityList input to 0.2  0.3 0.5.

Or, you can do everything in an expression using the 'discrete' function:  discrete( { 0.2, 0.3. 0.5 }, { 1, 2, 3 } )

Harry

H J

unread,
Dec 5, 2025, 6:01:26 AMDec 5
to Harry King, JaamSim Users Discussion Group
Thanks Harry, greatly appreciated!

I am aware of the DiscreteDistribution but it's limited to "hardwired" probability values rather than those generated by an expression that can take values from a FileToMatrix entity - this has been discussed in this community before. Having looked in the Java code, generalizing the DiscreteFunction for this is indeed not trivial. With the UniformDistribution I trust I will be able to achieve this (will have a look if the `discrete` function offers the same flexibility).

Thanks, Henk

--
You received this message because you are subscribed to the Google Groups "JaamSim Users Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jaamsim-user...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/jaamsim-users/2742c3d6-92cd-4593-95b4-ed5863592c17n%40googlegroups.com.

Harry King

unread,
Dec 5, 2025, 12:13:57 PMDec 5
to JaamSim Users Discussion Group
If you want to use values from a FileToMatrix object, then the 'discrete' function would be your best choice. You can use it directly in the input to the Choice keyword for the Branch object, without defining a separate probability distribution object.

Harry

H J

unread,
Dec 5, 2025, 12:19:28 PMDec 5
to Harry King, JaamSim Users Discussion Group
Indeed, that's what I'm doing now; everything now works as expected! Thanks a lot!
-- Henk

Reply all
Reply to author
Forward
0 new messages