# Systems Engineering Wiki (http://seweb.se.wtb.tue.nl)

## CookBook

cookbook:nondeterminism

# Non-determinism

The word non-determinism means that something cannot be determined. In process algebra this means that the sequence of some event cannot be decided if both events take place at the same time instant. Non-determinism is different from randomness. Randomness implies that it is equal likely that events take place. We assume that in an alternative statement with two alternatives both alternatives can be selected. The following specification fragment makes this clear.

```proc T(chan a?,b?,c!: lot) =
|[ var x: lot
:: *( ( a?x | b?x )
; delay ...
; c!x
)
)
]|```

So, if a lot can be received from `a` or from `b` it is possible that channel `a` is selected. It also is possible that channel `b` is selected. In the next cycle if a lot can be received via channel `a` or channel `b` it is again possible that channel `a` is selected. Non-determinism means that for example in an alternative statement always the first alternative will be selected and never the second one. Or always the second alternative and never the first alternative. Randomness means that in 50 percent of the choices the first alternative is selected and 50 percent the second alternative. In other words non-determinism implies that the systems engineer has no control over the sequence of events.

## An example

The following example illustrates the effect of non-determinism.

We are interested in the flow time of lots in a system. Lots are defined by the following definition:

`type lot = (nat, real)`

A lot contains a sequence number and the time of generation.

The system is described by the following specification:

```proc S() =
|[ chan a,b,c: lot
:: M(a,true) || M(b,false)|| T(a,b,c) || E(c)
]|```

One process generates odd numbers 1,3, …, while the other process generates even numbers 2,4, …. Machine `M` is specified by:

```proc M(chan a!: lot, val odd: bool) =
|[ id: nat = initId(odd), u: -> real = exponential(2.0), t: real
:: *( a!(id,time); t:= sample u; delay t; id:= id + 2 )
]|```

with function `initId`:

```func initId(odd: bool) -> nat =
|[ ( not even -> ret 1 | even -> ret 2 ) ]|```

Transformer process `T` is specified by:

```proc T(chan a?,b?,c!: lot) =
|[ var u: -> real = exponential(0.8), t: real
:: *( ( a?x | b?x )
; t:= sample u; delay t
; c!x
)
)
]|```

The flow time of every lot, the mean flow time and the variance of the flow time are calculated in exit process `E`:

```proc E(chan a?: lot) =
|[ i: nat = 0, x: lot, phi: real
, s1: real = 0.0, s2,t: real
:: i < 100000
*>( a?x
; phi:= time - x.1
; ( i <= 1 -> s2:= 0.0
| i >  1 -> s2:= (i - 2) / (i - 1) * s2 + (phi - s1) ^ 2 / i
)
; s1:= (i - 1 / i) * s1 + phi / i
)
; !!time, "\tE\t"
, "\tMean flow time=\t", s1
, "\tVariance =\t", s2
, "\tTroughput=\t", i / time, "\n"
]|```

In case the engineer likes to have some form of fairness in his specification, he has to specify this behavior.

## An example with a dispatcher

In this example we demand that process `T` starts processing on the lot which waits the longest time for processing in a process `M`.

We modify process `M`:

```proc M(chan a!: lot, val odd: bool, p!,q?: void) =
|[ var id: nat = initId(odd), u: -> real = exponential(1.0), t: real
:: *( p!; q?; a!(id,time); t:= sample u; delay t; id:= id + 2 )
]|```

Machine `M` sends a trigger to a dispatcher to denote that the process wants to send a lot to process `T`.

We modify process `T` in the following way. Before process `T` asks for a lot, the process sends a trigger to the dispatcher:

```proc T(chan a?,b?,c!: lot, r: !void) =
|[ var u: -> real = exponential(0.8), t: real
:: *( r!
; ( a?x | b?x )
; t:= sample u; delay t
; c!x
)
)
]|```

Dispatcher `D` determines which machine may continue by sending a trigger `q` back. The dispatcher is specified by:

```proc D(chan p0?,p1?,r?,q0!,q1!: void) =
|[ var js: [nat], send: bool =false, j: nat
:: *( p0?; js:= js ++ 
| p1?; js:= js ++ 
| r?; send:= true
| send and len(js) > 0 -> j:= hd(js)
; ( j = 0 -> q0! | j = 1 -> q1! )
; js:= tl(js); send:= false
)
]|```

We obtain the following process `S`:

```proc S(chan c!: lot) =
|[ chan a,b,c: lot, p0,p1,q0,q1,r: void
:: M(a,true,p0,q0) || M(b,false,p1,q1)|| T(a,b,c,r) || E(c)
|| D(p0,p1,r,q0,q1)
]|```

## An example with a dispatcher and bundles

By using the full power of the channel concept (one channel can have two senders and one receiver) and by introducing bundles we can make this specification more elegant.

We present process `T`:

```proc T(chan a?,b!: lot, r: !void) =
|[ var u: -> real = exponential(0.8), t: real
:: *( r!
; a?x
; t:= sample u; delay t
; b!x
)
)
]|```

We present dispatcher `D`:

```proc D(chan p?: nat, r?: void, q!: 2 # void) =
|[ var js: [nat], send: bool =false, j: nat
:: *( p?j; js:= js ++ [j]
| r?; send:= true
| send and len(js) > 0 -> q.hd(js)!
; js:= tl(js); send:= false
)
]|```

And we present process `S`:

```proc S() =
|[ chan a,b: lot, p: nat, q: 2 # void ,r: void
:: M(a,true,p,q.0) || M(a,false,p,q.1)|| T(a,b,r) || E(c)
|| D(p,r,q)
]|```

Channel `a` is connected to the two machines `M` and process `T`.

cookbook/nondeterminism.txt · Last modified: Thursday, 12 February 2009 : 09:49:30 by hvrooy

### Page Tools
{"ft_lang_label":"__label__en","ft_lang_prob":0.77532417,"math_prob":0.9879095,"size":4811,"snap":"2020-45-2020-50","text_gpt3_token_len":1496,"char_repetition_ratio":0.11712086,"word_repetition_ratio":0.15706806,"special_character_ratio":0.3614633,"punctuation_ratio":0.2698283,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9959923,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T05:52:55Z\",\"WARC-Record-ID\":\"<urn:uuid:9262a0ce-bd09-4729-a859-3fe4a79511ae>\",\"Content-Length\":\"19001\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:447371b3-79ba-463d-b917-111ffa59b737>\",\"WARC-Concurrent-To\":\"<urn:uuid:9cc6b1c7-1602-4971-bcc8-b556460e6029>\",\"WARC-IP-Address\":\"131.155.21.192\",\"WARC-Target-URI\":\"http://se.wpa.wtb.tue.nl/cookbook/nondeterminism\",\"WARC-Payload-Digest\":\"sha1:GGV32YSOVIVYAEZODHYEJR3IPV6NFTF4\",\"WARC-Block-Digest\":\"sha1:CLHKCMRCI344IKW7MIMG5SP74J3BRHBT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107896778.71_warc_CC-MAIN-20201028044037-20201028074037-00117.warc.gz\"}"}