Esempio n. 1
0
        private IEnumerable <Event> MultiThreadedRealtimeProcess(PseudoRealtimeSimulation env, AutoResetEvent wh)
        {
            Task.Run(() => MultiThreadInteractor(env, wh));

            var simulatedDelay = TimeSpan.FromSeconds(1);
            var wallClock      = Stopwatch.StartNew();

            yield return(env.Timeout(simulatedDelay)); // after 500ms, realtime scale is set to 0.5

            Assert.True(env.Now == env.StartDate + simulatedDelay);
            Assert.True(wallClock.Elapsed >= TimeSpan.FromMilliseconds(1400), $"b {wallClock.Elapsed} >= {TimeSpan.FromMilliseconds(1400)}");
            wallClock.Restart();
            yield return(env.Timeout(simulatedDelay)); // still runs at 0.5 scale

            Assert.True(env.Now == env.StartDate + 2 * simulatedDelay);
            Assert.True(wallClock.Elapsed >= TimeSpan.FromMilliseconds(1900), $"c {wallClock.Elapsed} >= {TimeSpan.FromMilliseconds(1900)}");
            wh.Set();                                  // SYNC1
            wallClock.Restart();
            yield return(env.Timeout(simulatedDelay)); // after the synchronization, realtime scale is set to 2

            Assert.True(env.Now == env.StartDate + 3 * simulatedDelay);
            Assert.True(wallClock.Elapsed >= TimeSpan.FromMilliseconds(400), $"d {wallClock.Elapsed} >= {TimeSpan.FromMilliseconds(400)}");
            wh.Set();                                  // SYNC2
            wallClock.Restart();
            yield return(env.Timeout(simulatedDelay)); // after the syncrhonization, virtual time is used

            Assert.True(env.Now == env.StartDate + 4 * simulatedDelay);
            Assert.True(wallClock.Elapsed <= TimeSpan.FromMilliseconds(100), $"e {wallClock.Elapsed} <= {TimeSpan.FromMilliseconds(100)}");
        }
Esempio n. 2
0
 private void MultiThreadInteractor(PseudoRealtimeSimulation env, AutoResetEvent wh)
 {
     Task.Delay(500).Wait();
     env.SetRealtime(0.5);
     wh.WaitOne(); // SYNC1
     env.SetRealtime(2);
     wh.WaitOne(); // SYNC2
     env.SetVirtualtime();
 }
Esempio n. 3
0
        public async void PseudoRealtimeMultiThreadedTest2()
        {
            var env = new PseudoRealtimeSimulation();

            env.PseudoRealtimeProcess(AnotherMultiThreadedRealtimeProcess(env));
            var sw = Stopwatch.StartNew();
            await env.RunAsync();

            Assert.True(sw.Elapsed >= TimeSpan.FromSeconds(1.5), $"a {sw.Elapsed.TotalMilliseconds} >= 1500");
        }
Esempio n. 4
0
        private IEnumerable <Event> AProcessOnAnotherThread(PseudoRealtimeSimulation env)
        {
            var sw = Stopwatch.StartNew();

            yield return(env.Timeout(TimeSpan.FromSeconds(1)));

            var elapsed = sw.Elapsed;

            Assert.True(elapsed >= TimeSpan.FromMilliseconds(1000), $"c {elapsed.TotalMilliseconds} >= 1000");
            env.SetVirtualtime();
        }
Esempio n. 5
0
        public void Simulate(int rseed = 42)
        {
            // Setup and start the simulation
            var env = new PseudoRealtimeSimulation(rseed);

            env.Log("== Pseudo-Realtime Sample ==");

            env.Process(Process(env));

            env.Run(TimeSpan.FromSeconds(2));
        }
Esempio n. 6
0
        private IEnumerable <Event> AnotherMultiThreadedRealtimeProcess(PseudoRealtimeSimulation env)
        {
            Task.Run(() => AnotherMultiThreadInteractor(env));
            var simulatedDelay = TimeSpan.FromSeconds(5);
            var sw             = Stopwatch.StartNew();

            yield return(env.Timeout(simulatedDelay));

            var elapsed = sw.Elapsed;

            Assert.True(elapsed < (env.Now - env.StartDate), $"b {elapsed.TotalMilliseconds} < {(env.Now - env.StartDate).TotalMilliseconds}");
        }
Esempio n. 7
0
        public async void PseudoRealtimeMultiThreadedTest()
        {
            var env = new PseudoRealtimeSimulation();

            using (var sync = new AutoResetEvent(false)) {
                env.Process(MultiThreadedRealtimeProcess(env, sync));
                var sw = Stopwatch.StartNew();
                await env.RunAsync();

                Assert.True(sw.Elapsed >= TimeSpan.FromSeconds(3.5), $"a {sw.Elapsed} >= {TimeSpan.FromSeconds(3.5)}");
            }
        }
Esempio n. 8
0
        public void PseudoRealtimeEnvTest()
        {
            var then  = DateTime.UtcNow;
            var delay = TimeSpan.FromSeconds(1);
            var env   = new PseudoRealtimeSimulation();

            env.Process(RealtimeDelay(env, delay));
            env.Run();
            var now = DateTime.UtcNow;

            Assert.True(now - then >= delay);
        }
Esempio n. 9
0
        public IEnumerable <Event> Process(PseudoRealtimeSimulation sim)
        {
            var then = sim.Now;
            var sw   = Stopwatch.StartNew();

            yield return(sim.Timeout(TimeSpan.FromSeconds(1)));

            sw.Stop();
            var now = sim.Now;

            Console.WriteLine($"Elapsed wall clock time {sw.Elapsed.TotalSeconds}s, elapsed simulation time {(now - then).TotalSeconds}s.");
        }
Esempio n. 10
0
 public HotstorageSimulation(Settings set)
 {
     settings             = set;
     sim                  = new PseudoRealtimeSimulation(DateTime.UtcNow, settings.Seed);
     ServiceLevel         = new SampleMonitor("SerivceLevel");
     LeadTimes            = new SampleMonitor("LeadTimes");
     Tardiness            = new SampleMonitor("Tardiness");
     BufferUtilization    = new TimeSeriesMonitor(sim, "BufferUtilization");
     CraneUtilization     = new TimeSeriesMonitor(sim, "CraneUtilization");
     HandoverUtilization  = new TimeSeriesMonitor(sim, "HandoverUtilization");
     UpstreamUtilization  = new TimeSeriesMonitor(sim, "UpstreamUtilization");
     lastScheduleSequence = int.MinValue;
     World                = new World {
         Production = new Stack {
             Id = 0, MaxHeight = settings.ProductionMaxHeight, BottomToTop = new List <Block>()
         },
         Buffers = Enumerable.Range(1, settings.BufferCount).Select(x => new Stack {
             Id = x, MaxHeight = settings.BufferMaxHeight, BottomToTop = new List <Block>()
         }).ToList(),
         Handover = new Handover()
         {
             Id = settings.BufferCount + 1, Ready = false
         },
         Now   = new TimeStamp(),
         Crane = new Crane {
             Id = 0, LocationId = 0, Schedule = new CraneSchedule()
             {
                 Moves = new List <CraneMove>()
             }, Load = null, GirderPosition = 0.0, HoistPosition = 1.0
         },
         KPIs            = new Performance(),
         ObservationData = new Uncertainties(),
         InvalidMoves    = new List <CraneMove>()
     };
     crane = new Resource(sim, capacity: 1)
     {
         Utilization = CraneUtilization
     };
     handover = new Resource(sim, capacity: 1)
     {
         Utilization = HandoverUtilization
     };
     upstream = new Resource(sim, capacity: 1)
     {
         Utilization = UpstreamUtilization
     };
     InitializeWorldState();
 }
Esempio n. 11
0
        public void PseudoRealtimeEnvTestStopTest()
        {
            var then = DateTime.UtcNow;
            var env  = new PseudoRealtimeSimulation();

            env.Run(TimeSpan.FromSeconds(1));
            var now = DateTime.UtcNow;

            Assert.True(now - then >= TimeSpan.FromSeconds(1));

            var t = Task.Run(() => env.Run(TimeSpan.FromMinutes(1)));

            Task.Delay(TimeSpan.FromMilliseconds(200)).Wait();
            env.StopAsync();
            Task.Delay(TimeSpan.FromMilliseconds(500)).Wait();
            Assert.True(t.IsCompleted);
        }
Esempio n. 12
0
        public void PseudoRealtimeMixedTest()
        {
            var rtDelay7s = TimeSpan.FromSeconds(7.0);
            var rtDelay1s = TimeSpan.FromSeconds(1.0);
            var vtDelay5s = TimeSpan.FromSeconds(5.0);
            var env       = new PseudoRealtimeSimulation();

            // process 1
            env.Process(MixedTestRealtimeDelay(env, rtDelay7s, vtDelay: TimeSpan.Zero));
            // process 2
            env.Process(MixedTestRealtimeDelay(env, rtDelay1s, vtDelay5s));

            var sw = Stopwatch.StartNew();

            env.Run();
            sw.Stop();
            Assert.True(sw.Elapsed >= TimeSpan.FromSeconds(2)); // process with 7s in realtime is interrupted for 5s in virtual time
        }
Esempio n. 13
0
        private IEnumerable <Event> MixedTestRealtimeDelay(PseudoRealtimeSimulation env, TimeSpan rtDelay, TimeSpan vtDelay)
        {
            var sw = Stopwatch.StartNew();

            yield return(env.Timeout(rtDelay));

            sw.Stop();
            Assert.True(env.Now == env.StartDate + rtDelay);
            // it's not guaranteed that we'd pass rtDelay in wall-clock time when we may switch between real and virtual time

            if (vtDelay > TimeSpan.Zero)
            {
                env.SetVirtualtime();

                sw.Restart();
                yield return(env.Timeout(vtDelay));

                sw.Stop();
                Assert.True(env.Now == env.StartDate + rtDelay + vtDelay);
                Assert.True(sw.Elapsed < TimeSpan.FromMilliseconds(10)); // much less, but 10ms should be a pretty safe upper limit

                env.SetRealtime();
            }
        }
Esempio n. 14
0
 private void AnotherMultiThreadInteractor(PseudoRealtimeSimulation env)
 {
     Task.Delay(500).Wait();
     env.Process(AProcessOnAnotherThread(env));
 }
Esempio n. 15
0
 /// <summary>
 /// Sets up a new process.
 /// The process places an initialize event into the event queue which starts
 /// the process by retrieving events from the generator.
 /// </summary>
 /// <param name="environment">The environment in which the process lives.</param>
 /// <param name="generator">The generator function of the process.</param>
 /// <param name="priority">The priority if multiple processes are started at the same time.</param>
 /// <param name="realtimeScale">A value strictly greater than 0 used to scale real time events (1 = realtime).</param>
 public PseudoRealtimeProcess(PseudoRealtimeSimulation environment, IEnumerable <Event> generator, int priority = 0, double realtimeScale = PseudoRealtimeSimulation.DefaultRealtimeScale)
     : base(environment, generator, priority)
 {
     RealtimeScale = realtimeScale;
 }