Open Source Library to Build Discrete Event Simulation Models in Go/Golang (http://golang.org/)
Copyright (c) 2013-2015 Alex Goussiatiner agoussia@yahoo.com
Godes is the general-purpose simulation library which includes the simulation engine and building blocks for modeling a wide variety of systems at varying levels of details.
All active objects shall implement the RunnerInterface and have Run() method. For each active object Godes creates a goroutine - lightweight thread.
Godes contains set of built-in functions for generating random numbers for commonly used probability distributions. Each of the distrubutions in Godes has one or more parameter values associated with it: Uniform (Min, Max), Normal (Mean and Standard Deviation), Exponential (Lambda), Triangular(Min, Mode, Max)
Godes implements operations with FIFO and LIFO queues
Godes uses BooleanControl variable as a lock for synchronizing execution of multiple runners
The Object calculates and prints statistical parameters for set of samples collected during the simulation.
$ go get github.com/agoussia/godes
During the working day the visitors are entering the restaurant at random intervals and immediately get the table. The inter arrival interval is the random variable with uniform distribution from 0 to 70 minutes. The last visitor gets admitted not later than 8 hours after the opening. The simulation itself is terminated when the last visitors enters the restaurant.
package main
import (
"fmt"
"godes"
)
// the arrival and service are two random number generators for the uniform distribution
var arrival *godes.UniformDistr = godes.NewUniformDistr(true)
// the Visitor is a Runner
// any type of the Runner should be defined as struct
// with the *godes.Runner as anonimous field
type Visitor struct {
*godes.Runner
number int
}
var visitorsCount int = 0
func (vst *Visitor) Run() { // Any runner should have the Run method
fmt.Printf(" %-6.3f \t Visitor # %v arrives \n", godes.GetSystemTime(), vst.number)
}
func main() {
var shutdown_time float64 = 8 * 60
godes.Run()
for {
//godes.Stime is the current simulation time
if godes.GetSystemTime() < shutdown_time {
//the function acivates the Runner
godes.AddRunner(&Visitor{&godes.Runner{}, visitorsCount})
//this advance the system time
godes.Advance(arrival.Get(0, 70))
visitorsCount++
} else {
break
}
}
// waits for all the runners to finish the Run()
godes.WaitUntilDone()
}
/* OUTPUT:
0.000 Visitor # 0 arrives
37.486 Visitor # 1 arrives
98.737 Visitor # 2 arrives
107.468 Visitor # 3 arrives
149.471 Visitor # 4 arrives
207.523 Visitor # 5 arrives
230.922 Visitor # 6 arrives
261.770 Visitor # 7 arrives
269.668 Visitor # 8 arrives
310.261 Visitor # 9 arrives
338.323 Visitor # 10 arrives
397.720 Visitor # 11 arrives
409.123 Visitor # 12 arrives
436.817 Visitor # 13 arrives
447.731 Visitor # 14 arrives
*/
The restaurant has only one table to sit on. During the working day the visitors are entering the restaurant at random intervals and wait for the table to be available. The inter arrival interval is the random variable with uniform distribution from 0 to 70 minutes.The time spent in the restaurant is the random variable with uniform distribution from 10 to 60 minutes. The last visitor gets admitted not later than 8 hours after the opening. The simulation itself is terminated when the last visitors has left the restaurant.
package main
import (
"fmt"
"godes"
)
// the arrival and service are two random number generators for the uniform distribution
var arrival *godes.UniformDistr = godes.NewUniformDistr(true)
var service *godes.UniformDistr = godes.NewUniformDistr(true)
// tableBusy is the boolean control variable than can be accessed and changed by number of Runners
var tableBusy *godes.BooleanControl = godes.NewBooleanControl()
// the Visitor is a Runner
// any type of the Runner should be defined as struct // with the *godes.Runner as anonimous field
type Visitor struct {
*godes.Runner
number int
}
var visitorsCount int = 0
func (vst Visitor) Run() { // Any runner should have the Run method
fmt.Printf("%-6.3f \t Visitor %v arrives \n", godes.GetSystemTime(), vst.number)
tableBusy.Wait(false) // this will wait till the tableBusy control becomes false
tableBusy.Set(true) // sets the tableBusy control to true - the table is busy
fmt.Printf("%-6.3f \t Visitor %v gets the table \n", godes.GetSystemTime(), vst.number)
godes.Advance(service.Get(10, 60)) //the function advance the simulation time by the value in the argument
tableBusy.Set(false) // sets the tableBusy control to false - the table is idle
fmt.Printf("%-6.3f \t Visitor %v leaves \n", godes.GetSystemTime(), vst.number)
}
func main() {
var shutdown_time float64 = 8 * 60
godes.Run()
for {
//godes.GetSystemTime() is the current simulation time
if godes.GetSystemTime() < shutdown_time {
//the function acivates the Runner
godes.AddRunner(Visitor{&godes.Runner{}, visitorsCount})
godes.Advance(arrival.Get(0, 70))
visitorsCount++
} else {
break
}
}
godes.WaitUntilDone() // waits for all the runners to finish the Run()
}
/* OUTPUT
0.000 Visitor 0 arrives
0.000 Visitor 0 gets the table
13.374 Visitor 0 leaves
37.486 Visitor 1 arrives
37.486 Visitor 1 gets the table
60.558 Visitor 1 leaves
98.737 Visitor 2 arrives
98.737 Visitor 2 gets the table
107.468 Visitor 3 arrives
146.824 Visitor 2 leaves
146.824 Visitor 3 gets the table
149.471 Visitor 4 arrives
171.623 Visitor 3 leaves
171.623 Visitor 4 gets the table
187.234 Visitor 4 leaves
207.523 Visitor 5 arrives
207.523 Visitor 5 gets the table
230.922 Visitor 6 arrives
245.859 Visitor 5 leaves
245.859 Visitor 6 gets the table
261.770 Visitor 7 arrives
269.668 Visitor 8 arrives
272.368 Visitor 6 leaves
272.368 Visitor 7 gets the table
290.484 Visitor 7 leaves
290.484 Visitor 8 gets the table
310.261 Visitor 9 arrives
333.570 Visitor 8 leaves
333.570 Visitor 9 gets the table
338.323 Visitor 10 arrives
354.874 Visitor 9 leaves
354.874 Visitor 10 gets the table
393.826 Visitor 10 leaves
397.720 Visitor 11 arrives
397.720 Visitor 11 gets the table
409.123 Visitor 12 arrives
436.817 Visitor 13 arrives
447.731 Visitor 14 arrives
455.705 Visitor 11 leaves
455.705 Visitor 13 gets the table
482.955 Visitor 13 leaves
482.955 Visitor 12 gets the table
496.034 Visitor 12 leaves
496.034 Visitor 14 gets the table
555.822 Visitor 14 leaves
*/
During the four working hours the visitors are entering the restaurant at random intervals and form the arrival queue.
The inter arrival interval is the random variable with uniform distribution from 0 to 30 minutes. The restaurant employs two waiters who are servicing one visitor in a time. The service time is the random variable with uniform distribution from 10 to 60 minutes.
The simulation itself is terminated when
* Simulation time passes the four hours
* Both waiters have finished servicing
* There are no visitors in the arrival queue.
The model calculates the average (arithmetic mean) of the visitors waiting time
package main
import (
"fmt"
"godes"
)
// the arrival and service are two random number generators for the uniform distribution
var arrival *godes.UniformDistr = godes.NewUniformDistr(true)
var service *godes.UniformDistr = godes.NewUniformDistr(true)
// true when waiter should act
var waitersSwt *godes.BooleanControl = godes.NewBooleanControl()
// FIFO Queue for the arrived
var visitorArrivalQueue *godes.FIFOQueue = godes.NewFIFOQueue("arrivalQueue")
// the Visitor is a Passive Object
type Visitor struct {
id int
}
// the Waiter is a Runner
type Waiter struct {
*godes.Runner
id int
}
var visitorsCount int = 0
var shutdown_time float64 = 4 * 60
func (waiter *Waiter) Run() {
for {
waitersSwt.Wait(true)
if visitorArrivalQueue.Len() > 0 {
visitor := visitorArrivalQueue.Get()
if visitorArrivalQueue.Len() == 0 {
waitersSwt.Set(false)
}
fmt.Printf("%-6.3f \t Visitor %v is invited by waiter %v \n", godes.GetSystemTime(), visitor.(Visitor).id, waiter.id)
godes.Advance(service.Get(10, 60)) //advance the simulation time by the visitor service time
fmt.Printf("%-6.3f \t Visitor %v leaves \n", godes.GetSystemTime(), visitor.(Visitor).id)
}
if godes.GetSystemTime() > shutdown_time && visitorArrivalQueue.Len() == 0 {
fmt.Printf("%-6.3f \t Waiter %v ends the work \n", godes.GetSystemTime(), waiter.id)
break
}
}
}
func main() {
for i := 0; i < 2; i++ {
godes.AddRunner(&Waiter{&godes.Runner{}, i})
}
godes.Run()
for {
visitorArrivalQueue.Place(Visitor{visitorsCount})
fmt.Printf("%-6.3f \t Visitor %v arrives \n", godes.GetSystemTime(), visitorsCount)
waitersSwt.Set(true)
godes.Advance(arrival.Get(0, 30))
visitorsCount++
if godes.GetSystemTime() > shutdown_time {
break
}
}
waitersSwt.Set(true)
godes.WaitUntilDone() // waits for all the runners to finish the Run()
fmt.Printf("Average Waiting Time %6.3f \n", visitorArrivalQueue.GetAverageTime())
}
/* OUTPUT
0.000 Visitor 0 arrives
0.000 Visitor 0 is invited by waiter 0
13.374 Visitor 0 leaves
16.066 Visitor 1 arrives
16.066 Visitor 1 is invited by waiter 1
39.137 Visitor 1 leaves
42.316 Visitor 2 arrives
42.316 Visitor 2 is invited by waiter 1
46.058 Visitor 3 arrives
46.058 Visitor 3 is invited by waiter 0
64.059 Visitor 4 arrives
70.857 Visitor 3 leaves
70.857 Visitor 4 is invited by waiter 0
86.468 Visitor 4 leaves
88.938 Visitor 5 arrives
88.938 Visitor 5 is invited by waiter 0
90.403 Visitor 2 leaves
98.966 Visitor 6 arrives
98.966 Visitor 6 is invited by waiter 1
112.187 Visitor 7 arrives
115.572 Visitor 8 arrives
125.475 Visitor 6 leaves
125.475 Visitor 7 is invited by waiter 1
127.275 Visitor 5 leaves
127.275 Visitor 8 is invited by waiter 0
132.969 Visitor 9 arrives
143.591 Visitor 7 leaves
143.591 Visitor 9 is invited by waiter 1
144.995 Visitor 10 arrives
164.895 Visitor 9 leaves
164.895 Visitor 10 is invited by waiter 1
170.361 Visitor 8 leaves
170.451 Visitor 11 arrives
170.451 Visitor 11 is invited by waiter 0
175.338 Visitor 12 arrives
187.207 Visitor 13 arrives
191.885 Visitor 14 arrives
203.848 Visitor 10 leaves
203.848 Visitor 12 is invited by waiter 1
213.596 Visitor 15 arrives
228.436 Visitor 11 leaves
228.436 Visitor 13 is invited by waiter 0
231.098 Visitor 12 leaves
231.098 Visitor 14 is invited by waiter 1
231.769 Visitor 16 arrives
241.515 Visitor 13 leaves
241.515 Visitor 15 is invited by waiter 0
287.864 Visitor 15 leaves
287.864 Visitor 16 is invited by waiter 0
290.886 Visitor 14 leaves
290.886 Waiter 1 ends the work
330.903 Visitor 16 leaves
330.903 Waiter 0 ends the work
Average Waiting Time 15.016
*/
This is the same process as in Example 2. Simulation is repeated 5 times. ```go package main
import ( "fmt" "godes" )
// the arrival and service are two random number generators for the uniform distribution var arrival godes.UniformDistr = godes.NewUniformDistr(true) var service godes.UniformDistr = godes.NewUniformDistr(true)
// true when waiter should act var waitersSwt *godes.BooleanControl = godes.NewBooleanControl()
// FIFO Queue for the arrived var visitorArrivalQueue *godes.FIFOQueue = godes.NewFIFOQueue("0")
// the Visitor is a Passive Object type Visitor struct { id int }
// the Waiter is a Runner type Waiter struct { *godes.Runner id int }
var visitorsCount int = 0 var shutdown_time float64 = 4 * 60
func (waiter *Waiter) Run() { for { waitersSwt.Wait(true) if visitorArrivalQueue.Len() > 0 { visitorArrivalQueue.Get() if visitorArrivalQueue.Len() == 0 { waitersSwt.Set(false) } godes.Advance(service.Get(10, 60)) //advance the simulation time by the visitor service time
}
if godes.GetSystemTime() > shutdown_time && visitorArrivalQueue.Len() == 0 {
break
}
}
}
func main() { for runs := 0; runs < 5; runs++ { for i := 0; i < 2; i++ { godes.AddRunner(&Waiter{&godes.Runner{}, i}) } godes.Run() for { visitorArrivalQueue.Place(Visitor{visitorsCount}) waitersSwt.Set(true) godes.Advance(arrival.Get(0, 30)) visitorsCount++ if godes.GetSystemTime() > shutdown_time { break } } waitersSwt.Set(true) godes.WaitUntilDone() // waits for all the runne
$ claude mcp add godes \
-- python -m otcore.mcp_server <graph>