Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Diagnostics.Eventing.Reader;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Net.NetworkInformation;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace SMO
- {
- class Program
- {
- enum Status
- {
- NotOperated,InChannel,InQueue,Lost,FromQtoCh,Checked
- }
- class Action
- {
- public double time;
- public Status status;
- public Action(double t, Status b)
- {
- this.time = t;
- this.status=b;
- }
- }
- static Random r=new Random();
- static double exp(double lambda)
- {
- return -(1/lambda)*Math.Log(r.NextDouble());
- }
- static List<Action> exp2(double lambda)
- {
- double sum = 0;
- double temp = 0;
- List<Action> Distribution=new List<Action>();
- while (sum<1000)
- {
- sum += -(1 / lambda) * Math.Log(r.NextDouble());
- Distribution.Add(new Action(sum, 0));
- }
- return Distribution;
- }
- static void Insert(List<Action> instream, Action action)
- {
- for (int i = 0; i < instream.Count; i++)
- {
- if (action.time < instream[i].time)
- {
- instream.Insert(i, action);
- break;
- }
- }
- }
- static Action FindFirst(List<Action> instream)
- {
- int count = 0;
- for (int i = 0; i < instream.Count; i++)
- {
- if (instream[i].status == Status.InQueue)
- {
- count = i;
- break;
- }
- }
- return instream[count];
- }
- static void Main(string[] args)
- {
- double s = 0;
- double q = 0;
- double T = 10;
- Action find;
- Stopwatch stopWatch = new Stopwatch();
- for (int j = 0; j < T; j++)
- {
- double lambda = 6;
- double mu = 2;
- int n = 3;
- int N = 4;
- int k = 0;
- double time;
- double diff = 0;
- int waiters = 0;
- int a = 0;
- int b=0;
- int lost=0;
- List<Action> instream = exp2(lambda);
- stopWatch.Start();
- // status: 0 - необслуженное требование, 1 - в очереди,2-обслуживание закончено,3-требование потеряно
- for (int i = 0; i < instream.Count; i++)
- {
- if(instream[i].time<1000)
- {
- if (instream[i].status == Status.NotOperated) //встречаем необслуженное требование
- {
- a++;
- if ((n != 0)) // Проверка свободных каналов
- {
- n--;
- instream[i].status = Status.Checked;
- time = exp(mu) + instream[i].time; // Генерация времени выхода из системы
- Insert(instream, new Action(time, Status.InChannel));
- //Добавляем требование со статусом обслуживание законченно
- // Console.WriteLine("In channel");
- }
- else if (k < N) //Если свободных каналов нет,но есть места в очереди
- {
- k++;
- instream[i].status = Status.InQueue; //В очереди
- // Console.WriteLine("In queue");
- }
- else
- {
- instream[i].status = Status.Lost;
- // Console.WriteLine("lost");
- lost++;
- }
- }
- else if (instream[i].status == Status.InChannel) //Если требование обслужилось
- {
- if (k != 0) //В очереди кто-то есть
- {
- // var find = instream.First(x => x.status == Status.InQueue); //Ищем первый элемент стоящий в очереди
- find = FindFirst(instream);
- find.status = Status.FromQtoCh;
- time = instream[i].time + exp(mu); //генерация времени
- diff = diff + instream[i].time - find.time;
- waiters++;
- Insert(instream, new Action(time, Status.InChannel));
- //Добавляем требование со статусом обслуживание законченно
- k--;
- // Console.WriteLine("From queue to channel");
- }
- else //В очереди никого нет
- {
- n++;
- }
- }
- }
- }
- if (waiters!=0)
- s +=diff/waiters;
- q+= lost/(double) a;
- //q = q/instream.Count;
- Console.WriteLine(q);
- Console.WriteLine(j);
- stopWatch.Stop();
- Console.WriteLine(stopWatch.Elapsed);
- }
- Console.WriteLine(s/T);
- Console.WriteLine(q/T);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement