ASMolog

Untitled

Nov 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace samm2
  8. {
  9.     class QueuingSystem
  10.     {
  11.         private double _lambda, _mu;
  12.         private Random _random;
  13.         private double tempTimeInput = 0;
  14.         private double tempTimeFirst = 0;
  15.         private double tempTimeSecond = 0;
  16.         private Calculator calculator = new Calculator();
  17.         private double inputTime = 0;
  18.         private double outputTime = 0;
  19.         private double firstStageTime = 0;
  20.         private double secondStageTime = 0;
  21.         private double tempMuGl = 0;
  22.         private List<double> times = new List<double>();
  23.         private int[] t = new int[3];
  24.  
  25.         private int inputStep = 1;
  26.         private int outputStep = 0;
  27.         private int queueCounter = 0;
  28.  
  29.         public QueuingSystem(double lambda, double mu)
  30.         {
  31.             _lambda = lambda;
  32.             _mu = 2 * mu;
  33.             _random = new Random();
  34.             inputTime = getTime(_lambda);
  35.             outputTime = getTime(_mu);
  36.         }
  37.  
  38.         public double Lambda { get => _lambda; }
  39.         public double Mu { get => _mu; }
  40.  
  41.         public string Imitate(int numberTacts)
  42.         {
  43.             calculator.Clear();
  44.  
  45.             for (double i = 0; i < numberTacts; i += 0.01)
  46.             {
  47.                 GetNextState(i);
  48.             }
  49.  
  50.             var a = calculator.getA(numberTacts);
  51.             var l = calculator.getL(numberTacts);
  52.  
  53.             return "A = " + a.ToString() + ";\r\n Lоч = " + l.ToString() + ";";
  54.         }
  55.  
  56.         private double getTime(double intensity)
  57.         {
  58.             return (double)((-1 / intensity) * Math.Log(_random.NextDouble()));
  59.         }
  60.  
  61.         private void GetNextState(double currentTime)
  62.         {
  63.             if (inputTime <= currentTime)
  64.             {
  65.                 inputTime = getTime(_lambda) + currentTime;
  66.                 if (inputStep == 4)
  67.                 {
  68.                     inputStep = 1;
  69.  
  70.                     if (outputStep == 0) // not processes
  71.                     {
  72.                         outputStep = 1;
  73.                         outputTime = getTime(_mu) + currentTime;
  74.                     }
  75.                     else
  76.                     {
  77.                         queueCounter++;
  78.                     }
  79.                 }
  80.                 else // inputStep = 1..3
  81.                 {
  82.                     inputStep++;
  83.                 }
  84.             }
  85.             if (outputTime <= currentTime && outputStep != 0)
  86.             {
  87.                 if (outputStep == 3)
  88.                 {
  89.  
  90.                     if (queueCounter > 0)
  91.                     {
  92.                         outputTime = getTime(_mu) + currentTime;
  93.                         outputStep = 1;
  94.                         queueCounter--;
  95.                     }
  96.                     else
  97.                     {
  98.                         outputStep = 0;
  99.                     }
  100.                 }
  101.                 else // outputStep = 1..2
  102.                 {
  103.                     outputTime = getTime(_mu) + currentTime;
  104.                     outputStep++;
  105.                 }
  106.             }
  107.         }
  108.     }
  109. }
Add Comment
Please, Sign In to add comment