question

Yifei Wang avatar image
1 Like"
Yifei Wang asked Brandon Peterson edited

How to represent "or" in By Case pickoption

Hi,

Please see the attached picture. "||" does not work in my code. I would like the source to randomly assign itemtype2 to Port 3,5, or 6. It seems duniform only assigns a random number between min and max. Is there a way to represent "or" in FlexSim?

FlexSim 16.0.1
flexscriptcodeportsby case
picture.png (159.6 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.

Matthew Gillespie avatar image
1 Like"
Matthew Gillespie answered Yifei Wang commented

Take a look at this By Percentage pickoption. You could use the code it uses in your break statement. It generates a number between 1 and 100 and, depending on what number it is, chooses a return value. For example, if the random number is a 10 it will return 1 since 10 < 33.

Here's the code behind it:

int randomnum = trunc(uniform(0.0, 100.0, 0));
double total = 0.0;

total += 33; 
if (randomnum <= total)
	return 1;
total += 33; 
if (randomnum <= total)
	return 2;
total += 34; 
if (randomnum <= total)
	return 3;

bypercent.png (26.9 KiB)
· 1
5 |100000

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

Yifei Wang avatar image Yifei Wang commented ·

Thank you, Regan!

Thanks, Matthew

1 Like 1 ·
Regan Blackett avatar image
2 Likes"
Regan Blackett answered Yifei Wang commented

Just like in your other question you can't use the logical relation operator for OR or AND like that. It's the right symbol but incorrectly used. The result of an OR or AND expression is always true or false represented as zero (false) and one (true). So the logical result of 3|| 5 || 6 is 'true' not one of the three values. In order to chose one of the values to assign to your variable you need a method that assigns it based on some kind of condition or algorithm. How is value 3, 5, or 6 selected? Under what conditions in the model?

What your describing, having the source send things that are type 2 to either ports 3, 5, 6, you either need to tell the queues connected to that port to pull item type 2, or write condition statements that says why you would select ports 3 vs 5 vs 6.

· 4
5 |100000

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

Yifei Wang avatar image Yifei Wang commented ·

ok. It makes sense. But, is there another code similar to duniform but can include multiple discrete numbers?

0 Likes 0 ·
Regan Blackett avatar image Regan Blackett ♦ Yifei Wang commented ·

If @Matthew Gillespie's approach using the random number code doesn't do it for you, you could try dempirical("tablename") to chose your values by percentage. To use it, you would need to set up a table of values in a Global Table that has two columns and as many rows as possible values you want to chose from, set up like this:

Percentage Value
33 3
33 5
34 6

and call demprical like this:

int myvalue = demprical("GlobalTable1") 

The parmaeter passed must be the name of your table data. Fill in whatever percentage values you want to weight each value according to your needs.

1 Like 1 ·
Yifei Wang avatar image Yifei Wang Regan Blackett ♦ commented ·

thanks regan!

1 Like 1 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Yifei Wang commented ·

The bernoulli() distribution will return one of two values based off a probability.

The dempirical() command does basically what the By Percentage pickoption code does, except you provide it a table of values and probabilities.

0 Likes 0 ·
Mischa Spelt avatar image
0 Likes"
Mischa Spelt answered Yifei Wang commented

If you want to draw 3, 5 or 6 with equal probabilities, you can put them in an array and use duniform to draw the position (index) in the array:

intarray ports = makearray(3);
fillarray(ports, 3, 5, 6);
int randomPort = ports[duniform(1, arraysize(ports))];

Or, in this particular case, you can use that you have almost-contiguous values:

int port = duniform(4, 6);
if(port == 4) { port = 3; }

Or even in a single statement

int port = bernoulli(100/3, 3, duniform(5, 6)) 
// or bernoulli(100/3, 3, bernoulli(50, 5, 6))
· 1
5 |100000

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

Yifei Wang avatar image Yifei Wang commented ·

thanks

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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