Example #1
0
        static Timeout <string> NewTarget(SimEnvironment env)
        {
            var delay  = env.Random.DiscreteUniform(1, 20);
            var target = env.Random.Choice(Targets);

            return(env.Timeout(delay, target));
        }
Example #2
0
        static IEnumerable <SimEvent> Worker(SimEnvironment env)
        {
            Console.WriteLine("{0}: Carico la macchina...", env.Now);
            yield return(env.Timeout(LoadTime));

            env.Exit(env.Random.Choice(Tasks));
        }
Example #3
0
        /// <summary>
        ///   Customer arrives, is served and leaves.
        /// </summary>
        private static SimEvents Customer(SimEnvironment env, string name, Resource counter, double timeInBank)
        {
            var arrive = env.Now;

            Console.WriteLine("{0:00.0000} {1}: Here I am", arrive, name);

            using (var req = counter.Request())
            {
                var patience = env.Random.NextDouble(MinPatience, MaxPatience);
                // Wait for the counter or abort at the end of our tether
                yield return(req.Or(env.Timeout(patience)));

                var wait = env.Now - arrive;
                if (req.Succeeded)
                {
                    // We got to the counter
                    Console.WriteLine("{0:00.0000} {1}: Waited {2:0.000}", env.Now, name, wait);

                    var tib = env.Random.Exponential(1.0 / timeInBank);
                    yield return(env.Timeout(tib));

                    Console.WriteLine("{0:00.0000} {1}: Finished", env.Now, name);
                }
                else
                {
                    // We reneged
                    Console.WriteLine("{0:00.0000} {1}: RENEGED after {2:0.000}", env.Now, name, wait);
                }
            }
        }
        /// <summary>
        ///   A process which consumes messages.
        /// </summary>
        static IEvents MessageConsumer(string name, SimEnvironment env, Store <Message> inPipe)
        {
            while (true)
            {
                // Gets event for message pipe.
                var getEv = inPipe.Get();
                yield return(getEv);

                var msg = getEv.Value;

                if (msg.Timestamp < env.Now)
                {
                    // If message was already put into pipe, then
                    // MessageConsumer was late getting to it.
                    // Depending on what is being modeled, this may, or may not,
                    // have some significance.
                    Console.WriteLine("LATE Getting Message: at time {0:.0}, \"{1}\" received message \"{2}\"", env.Now,
                                      name, msg.Content);
                }
                else
                {
                    // MessageConsumer is synchronized with MessageGenerator.
                    Console.WriteLine("At time {0:.0}, \"{1}\" received message \"{2}\"", env.Now, name, msg.Content);
                }

                // Process does some other work, which may result in missing messages.
                yield return(env.Timeout(Rand.Next(4, 8)));
            }
        }
Example #5
0
        static IEnumerable <SimEvent> CondTester(SimEnvironment env)
        {
            var aProc = env.Process(AProcess(env));
            var cond  = env.AllOf(env.Timeout(5, "VAL_T"), aProc);

            yield return(cond);

            Console.WriteLine("ALL: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2));

            aProc = env.Process(AProcess(env));
            cond  = env.AnyOf(env.Timeout(5, "VAL_T"), aProc);
            yield return(cond);

            Console.WriteLine("ANY: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2));

            aProc = env.Process(AProcess(env));
            var aTime = env.Timeout(5, "VAL_T");
            ConditionEval <Timeout <string>, SimProcess> pred =
                c => c.Ev1.Succeeded && c.Ev2.Succeeded && c.Ev1.Value.Equals("VAL_T") && c.Ev2.Value.Equals("VAL_P");

            cond = env.Condition(aTime, aProc, pred);
            yield return(cond);

            Console.WriteLine("CUSTOM: {0}", cond.Value.Select(x => x.Value).Aggregate((s1, s2) => s1 + ", " + s2));
        }
Example #6
0
        static SimEvents Go(SimEnvironment env, int id)
        {
            Console.WriteLine("{0} {1} Starting", env.Now, id);
            yield return(env.Timeout(100));

            Console.WriteLine("{0} {1} Arrived", env.Now, id);
        }
Example #7
0
        public static void Run()
        {
            // Sets up and starts simulation
            Console.WriteLine("Movie renege");
            _env = Sim.Environment(RandomSeed);

            // Creates movie theater
            var counter = Sim.Resource(_env, 1);
            var titles  = new[] { ".NET Unchained", "Kill Process", "Pulp Implementation" };
            var movies  = new List <MovieInfo>(titles.Length);

            movies.AddRange(titles.Select(t => new MovieInfo(t, _env.Event())));
            _theater = new Theater(counter, movies);

            // Starts process and simulates
            _env.Process(CustomerArrivals());
            _env.Run(until: SimTime);

            // Analysis and results
            foreach (var movie in movies.Where(m => m.SoldOut.Succeeded))
            {
                Console.WriteLine("Movie \"{0}\" sold out {1:.0} minutes after ticket counter opening.", movie.Title,
                                  movie.WhenSoldOut);
                Console.WriteLine("  Number of people leaving queue when film sold out: {0}", movie.RenegerCount);
            }
        }
Example #8
0
 public SimEvents Fight(SimEnvironment env, Player target)
 {
     Console.WriteLine("Three {0} attempting to escape!", target.Name);
     while (true)
     {
         if (_random.Next(0, 10) < 2)
         {
             // Checks for hit on player
             target.Damage += 1;     // Hit! Increment damage to player
             if (target.Damage <= 5) // Target survives...
             {
                 Console.WriteLine("Ha! {0} hit! Damage = {1}", target.Name, target.Damage);
             }
             else
             {
                 target.LifeEvent.Succeed();
                 if (target.Lives - 1 == 0)
                 {
                     Console.WriteLine("No more {0} left!", target.Name);
                 }
                 else
                 {
                     Console.WriteLine("Now only {0} {1} left!", target.Lives - 1, target.Name);
                 }
             }
         }
         yield return(env.Timeout(1));
     }
 }
Example #9
0
 static IEnumerable <SimEvent> Clock(SimEnvironment env, string name, double tick)
 {
     while (true)
     {
         Console.WriteLine("{0} {1:0.0}", name, env.Now);
         yield return(env.Timeout(tick));
     }
 }
Example #10
0
 protected EscaperBase(SimEnvironment env, Binder binder)
 {
     _env = env;
     _binder = binder;
     _name = GetType().Name;
     // Speed is set to its default value.
     Speed = double.Parse(AppSettings.DefaultEscaperSpeed);
 }
Example #11
0
        static SimEvents Person(SimEnvironment env, string name)
        {
            Console.WriteLine("Hi, I'm {0} and I will wait for {1} minutes!", name, WaitTime);
            // The process will stop its execution for WaitTime time units.
            yield return(env.Timeout(WaitTime));

            Console.WriteLine("Ok, {0}'s wait has finished at {1}. Bye :)", name, env.Now);
        }
Example #12
0
 internal FilterStore(SimEnvironment env, int capacity, WaitPolicy getPolicy, WaitPolicy putPolicy,
                      WaitPolicy itemPolicy) : base(env)
 {
     _capacity  = capacity;
     _getQueue  = WaitQueue.New <GetEvent>(getPolicy, Env);
     _putQueue  = WaitQueue.New <PutEvent>(putPolicy, Env);
     _itemQueue = WaitQueue.New <T>(itemPolicy, Env);
 }
        static IEnumerable <SimEvent> Producer(SimEnvironment env, Store <int> store, int n)
        {
            var call = env.Call(FibonacciFunc(env, n));

            yield return(call);

            store.Put((int)call.Value);
        }
Example #14
0
 internal Container(SimEnvironment env, double capacity, double level, WaitPolicy getPolicy, WaitPolicy putPolicy)
     : base(env)
 {
     _capacity = capacity;
     Level     = level;
     _getQueue = WaitQueue.New <GetEvent>(getPolicy, Env);
     _putQueue = WaitQueue.New <PutEvent>(putPolicy, Env);
 }
Example #15
0
        static IEvents SayHello(SimEnvironment env)
        {
            while (true)
            {
                yield return(env.Timeout(2.1));

                Console.WriteLine("Hello World at {0}!", env.Now);
            }
        }
Example #16
0
        /// <summary>
        ///   Generates new cars that arrive at the gas station.
        /// </summary>
        private static SimEvents CarGenerator(SimEnvironment env, Resource gasStation, Container fuelPump)
        {
            foreach (var i in Enumerable.Range(0, int.MaxValue))
            {
                yield return(env.Timeout(env.Random.Next(MinInter, MaxInter)));

                env.Process(Car("Car " + i, env, gasStation, fuelPump));
            }
        }
Example #17
0
 static SimEvents Execute(SimEnvironment env, int finish)
 {
     while (env.Now < finish)
     {
         // Creates a new Customer and activates it (with default parameters).
         env.Process(Customer.Buy(env, "Marta"));
         Console.WriteLine("{0} {1}", env.Now, "Marta");
         yield return(env.Timeout(10));
     }
 }
Example #18
0
        static IEnumerable <SimEvent> Spawner(SimEnvironment env, Tally rec)
        {
            var queue = Sim.Resource(env, 2);

            while (true)
            {
                env.Process(Person(env, queue, rec));
                yield return(env.Timeout(env.Random.Next(2, 5)));
            }
        }
        private static IEvents SayHello(SimEnvironment env, char id)
        {
            while (true)
            {
                Console.WriteLine("{0} - Sleeping at {1}, real {2}...", id, env.Now, env.RealTime.WallClock.UtcNow);
                yield return(env.Timeout(3));

                Console.WriteLine("{0} - Awake at {1}, real {2}", id, env.Now, env.RealTime.WallClock.UtcNow);
            }
        }
Example #20
0
        static IEnumerable <SimEvent> Person(SimEnvironment env, Resource queue, Tally rec)
        {
            using (var req = queue.Request()) {
                var startTime = env.Now;
                yield return(req);

                rec.Observe(env.Now - startTime);
                var workTime = env.Random.Next(3, 10);
                yield return(env.Timeout(workTime));
            }
        }
Example #21
0
        private const int MaxPatience       = 3;  // Max. customer patience

        /// <summary>
        ///   Source generates customers randomly.
        /// </summary>
        private static SimEvents Source(SimEnvironment env, int number, double interval, Resource counter)
        {
            foreach (var i in Enumerable.Range(0, number))
            {
                var n = string.Format("Customer{0:00}", i);
                var c = Customer(env, n, counter, timeInBank: 12.0);
                env.Process(c);
                var t = env.Random.Exponential(1.0 / interval);
                yield return(env.Timeout(t));
            }
        }
Example #22
0
 static SimEvents PersonSpawner(SimEnvironment env, Resource postOffice)
 {
     while (true)
     {
         // We first start a new "Person" process...
         env.Process(Person(env, postOffice, env.Random.Choice(Names)));
         // And then we sleep for nearly SpawnFrequency minutes.
         var waitTime = env.Random.Exponential(1.0 / SpawnFrequency);
         yield return(env.Timeout(waitTime));
     }
 }
Example #23
0
        static IEnumerable <SimEvent> Machine(SimEnvironment env)
        {
            while (true)
            {
                var worker = env.Process(Worker(env));
                yield return(worker);

                Console.WriteLine("{0}: Eseguo il comando {1}", env.Now, worker.Value);
                yield return(env.Timeout(WorkTime));
            }
        }
Example #24
0
        public Client(SimEnvironment env, G g, int id) : base(env, g)
        {
            _id = id;
            for (var i = 1; i < G.MachineCount; ++i)
            {
                var index = (i + id) % G.MachineCount;
                _peers[i - 1] = new ServerInfo(index);
            }

            _packetConstructed = env.Event();
        }
Example #25
0
        /// <summary>
        ///   Arrives at the gas station after a certain delay and refuels it.
        /// </summary>
        private static SimEvents TankTruck(SimEnvironment env, Container fuelPump)
        {
            yield return(env.Timeout(TankTruckTime));

            Console.WriteLine("Tank truck arriving at time {0}", env.Now);

            var amount = fuelPump.Capacity - fuelPump.Level;

            Console.WriteLine("Tank truck refueling {0:.0} liters.", amount);
            yield return(fuelPump.Put(amount));
        }
Example #26
0
        static IEnumerable <SimEvent> Person(SimEnvironment env, string gender, Resource toilet)
        {
            using (var req = toilet.Request()) {
                yield return(req);

                Console.WriteLine("{0:0.00}: {1} --> Bagno", env.Now, gender);
                yield return(env.Timeout(env.Random.Exponential(1.0 / AvgTimeInToilet)));

                Console.WriteLine("{0:0.00}: {1} <-- Bagno", env.Now, gender);
            }
        }
Example #27
0
        static IEnumerable <SimEvent> Process(SimEnvironment env)
        {
            var ev = env.Event <string>();

            env.Process(EventFailer(ev));
            yield return(ev);

            if (ev.Failed)
            {
                Console.WriteLine(ev.Value);
            }
        }
Example #28
0
        public static SimEvents Buy(SimEnvironment env, string name, double budget = 40)
        {
            Console.WriteLine("Here I am at the shop {0}", name);
            for (var i = 0; i < 4; ++i)
            {
                yield return(env.Timeout(5)); // Executed 4 times at intervals of 5 time units

                Console.WriteLine("I just bought something {0}", name);
                budget -= 10;
            }
            Console.WriteLine("All I have left is {0} I am going home {1}", budget, name);
        }
        static IEnumerable <SimEvent> PressureSensor(SimEnvironment env, char tag, IRecorder pressureRecorder)
        {
            while (true)
            {
                yield return(env.Timeout(MonitoringTime));

                // Read the pressure value and record it.
                var pressure = env.Random.Normal(1000, 50);
                pressureRecorder.Observe(pressure);
                Console.WriteLine($"Pressure sensor for machine {tag} has recorded a pressure of {pressure:.00} bar");
            }
        }
        static IEnumerable <SimEvent> TemperatureSensor(SimEnvironment env, char tag, IRecorder temperatureRecorder)
        {
            while (true)
            {
                yield return(env.Timeout(MonitoringTime));

                // Read the temperature value and record it.
                var temperature = env.Random.Poisson(100);
                temperatureRecorder.Observe(temperature);
                Console.WriteLine($"Temperature sensor for machine {tag} has recorded a temperature of {temperature} °F");
            }
        }
        /// <summary>
        ///   A process which randomly generates messages.
        /// </summary>
        static IEvents MessageGenerator(string name, SimEnvironment env, BroadcastPipe <Message> outPipe)
        {
            while (true)
            {
                // Waits for next transmission.
                yield return(env.Timeout(Rand.Next(6, 10)));

                // Messages are time stamped to later check
                // if the consumer was late getting them.
                var content = string.Format("{0} says hello at {1:.0}", name, env.Now);
                yield return(outPipe.Put(new Message(env.Now, content)));
            }
        }
Example #32
0
 public Pledge(SimEnvironment env, Binder binder)
     : base(env, binder)
 {
 }
Example #33
0
 public Trémaux(SimEnvironment env, Binder binder)
     : base(env, binder)
 {
 }
Example #34
0
 public RandomMouse(SimEnvironment env, Binder binder)
     : base(env, binder)
 {
 }
 public WallFollower(SimEnvironment env, Binder binder)
     : base(env, binder)
 {
 }