Advertisement
mcgrab

SMO

Mar 16th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.73 KB | None | 0 0
  1. using System;
  2. using System.CodeDom;
  3. using System.CodeDom.Compiler;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Diagnostics.Eventing.Reader;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Net.NetworkInformation;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14.  
  15. namespace SMO
  16. {
  17.  
  18.    
  19.     class Program
  20.     {
  21.         enum Status
  22.         {
  23.             NotOperated,InChannel,InQueue,Lost,FromQtoCh,Checked
  24.         }
  25.         class Action
  26.         {
  27.             public double time;
  28.             public Status status;
  29.            
  30.            
  31.             public Action(double t, Status b)
  32.             {
  33.                 this.time = t;
  34.                 this.status=b;
  35.             }        
  36.         }
  37.  
  38.        
  39.         static Random r=new Random();
  40.         static double  exp(double lambda)
  41.         {
  42.             return -(1/lambda)*Math.Log(r.NextDouble());
  43.         }
  44.  
  45.         static List<Action> exp2(double lambda)
  46.         {
  47.             double sum = 0;
  48.             double temp = 0;
  49.             List<Action> Distribution=new List<Action>();
  50.  
  51.             while (sum<1000)
  52.             {
  53.  
  54.                 sum += -(1 / lambda) * Math.Log(r.NextDouble());
  55.  
  56.                 Distribution.Add(new Action(sum, 0));
  57.             }
  58.  
  59.             return Distribution;
  60.         }
  61.  
  62.        
  63.  
  64.         static void Insert(List<Action> instream, Action action)
  65.         {
  66.             for (int i = 0; i < instream.Count; i++)
  67.             {
  68.                 if (action.time < instream[i].time)
  69.                 {
  70.                     instream.Insert(i, action);
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.  
  76.  
  77.         static Action FindFirst(List<Action> instream)
  78.         {
  79.             int count = 0;
  80.             for (int i = 0; i < instream.Count; i++)
  81.             {
  82.                 if (instream[i].status == Status.InQueue)
  83.                 {
  84.                     count = i;
  85.                     break;
  86.                    
  87.                 }
  88.             }
  89.  
  90.             return instream[count];
  91.  
  92.         }
  93.  
  94.         static void Main(string[] args)
  95.         {
  96.  
  97.            
  98.             double s = 0;
  99.             double q = 0;
  100.             double T = 10;
  101.             Action find;
  102.             Stopwatch stopWatch = new Stopwatch();
  103.  
  104.             for (int j = 0; j < T; j++)
  105.             {
  106.  
  107.                
  108.                 double lambda = 6;
  109.                 double mu = 2;
  110.                 int n = 3;
  111.                 int N = 4;
  112.                 int k = 0;
  113.                 double time;
  114.                 double diff = 0;
  115.                 int waiters = 0;
  116.                 int a = 0;
  117.                 int b=0;
  118.                 int lost=0;
  119.                
  120.                
  121.                 List<Action> instream = exp2(lambda);
  122.                
  123.                 stopWatch.Start();
  124.  
  125.          
  126.            
  127.                 // status: 0 - необслуженное требование, 1 - в очереди,2-обслуживание закончено,3-требование потеряно
  128.  
  129.                 for (int i = 0; i < instream.Count; i++)
  130.                 {
  131.  
  132.                     if(instream[i].time<1000)
  133.                     {
  134.                        
  135.                     if (instream[i].status == Status.NotOperated) //встречаем необслуженное требование
  136.                     {
  137.  
  138.                         a++;
  139.  
  140.                         if ((n != 0)) // Проверка свободных каналов
  141.                         {
  142.                             n--;
  143.                             instream[i].status = Status.Checked;
  144.                             time = exp(mu) + instream[i].time; // Генерация времени выхода из системы
  145.                             Insert(instream, new Action(time, Status.InChannel));
  146.                             //Добавляем требование со статусом обслуживание законченно  
  147.                             //  Console.WriteLine("In channel");
  148.  
  149.                         }
  150.                         else if (k < N) //Если свободных каналов нет,но есть места в очереди
  151.                         {
  152.  
  153.                             k++;
  154.                             instream[i].status = Status.InQueue; //В очереди
  155.                             // Console.WriteLine("In queue");
  156.  
  157.  
  158.                         }
  159.  
  160.                         else
  161.                         {
  162.  
  163.                             instream[i].status = Status.Lost;
  164.                             //  Console.WriteLine("lost");
  165.                             lost++;
  166.  
  167.                         }
  168.                     }
  169.                     else if (instream[i].status == Status.InChannel) //Если требование обслужилось
  170.                     {
  171.  
  172.  
  173.  
  174.  
  175.  
  176.                         if (k != 0) //В очереди кто-то есть
  177.                         {
  178.  
  179.                             // var find = instream.First(x => x.status == Status.InQueue); //Ищем первый элемент стоящий в очереди
  180.                             find = FindFirst(instream);
  181.                             find.status = Status.FromQtoCh;
  182.                             time = instream[i].time + exp(mu); //генерация времени
  183.                             diff = diff + instream[i].time - find.time;
  184.                             waiters++;
  185.                             Insert(instream, new Action(time, Status.InChannel));
  186.                             //Добавляем требование со статусом обслуживание законченно  
  187.                             k--;
  188.                             // Console.WriteLine("From queue to channel");
  189.                         }
  190.  
  191.                         else //В очереди никого нет
  192.                         {
  193.                             n++;
  194.                         }
  195.  
  196.  
  197.  
  198.                     }
  199.  
  200.                 }
  201.  
  202.  
  203.             }
  204.  
  205.                 if (waiters!=0)
  206.                    
  207.                
  208.                 s +=diff/waiters;
  209.                 q+= lost/(double) a;
  210.                
  211.                
  212.                 //q = q/instream.Count;
  213.                
  214.                 Console.WriteLine(q);
  215.                
  216.                 Console.WriteLine(j);
  217.                
  218.                
  219.                 stopWatch.Stop();
  220.                Console.WriteLine(stopWatch.Elapsed);
  221.  
  222.             }
  223.                 Console.WriteLine(s/T);
  224.                 Console.WriteLine(q/T);
  225.                
  226.                
  227.            
  228.                
  229.  
  230.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement