Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const random = require('random');
  2.  
  3. class Storage {
  4.     constructor(a)
  5.     {
  6.         this.work = false;
  7.         this.next = a;
  8.     }
  9. }
  10.  
  11. class Operator {
  12.     constructor(a, b)
  13.     {
  14.         this.work = false;
  15.         this.a = a;
  16.         this.b = b;
  17.     }
  18.     next()
  19.     {
  20.         return Math.round((Math.random() * (this.b - this.a + 1)) + this.a);
  21.     }
  22. }
  23.  
  24. class Generator {
  25.     constructor(a, b)
  26.     {
  27.         this.a = a;
  28.         this.b = b;
  29.     }
  30.     next()
  31.     {
  32.         return Math.round((Math.random() * (this.b - this.a + 1)) + this.a);
  33.     }
  34. }
  35.  
  36. class System {
  37.     constructor()
  38.     {
  39.         this.dt = 1;
  40.         this.count = 0;
  41.         this.queue1 = [];
  42.         this.queue2 = [];
  43.         this.st1 = new Storage(15);
  44.         this.st2 = new Storage(30);
  45.         this.op1 = new Operator(15, 25);
  46.         this.op2 = new Operator(30, 50);
  47.         this.op3 = new Operator(20, 60);
  48.         this.generator = new Generator(8, 12);
  49.     }
  50.  
  51.     timeModelling()
  52.     {
  53.         let aim = 300;
  54.         let genTime = this.generator.next();
  55.         let op1Time = 0;
  56.         let op2Time = 0;
  57.         let op3Time = 0;
  58.         let st1Time = 0;
  59.         let st2Time = 0;
  60.         let amount = 0;
  61.         let broken = 0;
  62.         let currentTime = 0;
  63.  
  64.         while (this.count < aim)
  65.         {
  66.             if (currentTime >= op1Time && this.op1.work)
  67.             {
  68.                 this.queue1.push(1);
  69.                 this.op1.work = false;
  70.             }
  71.             if (currentTime >= op2Time && this.op2.work)
  72.             {
  73.                 this.queue1.push(1);
  74.                 this.op2.work = false;
  75.             }
  76.             if (currentTime >= op3Time && this.op3.work)
  77.             {
  78.                 this.queue2.push(1);
  79.                 this.op3.work = false;
  80.             }
  81.  
  82.             if (amount != 300)
  83.             {
  84.                 if (currentTime >= genTime)
  85.                 {
  86.                     genTime = currentTime + this.generator.next();
  87.                     amount++;
  88.                     if (this.op1.work)
  89.                     {
  90.                         if (this.op2.work)
  91.                         {
  92.                             if (this.op3.work)
  93.                                 broken++;
  94.                             else
  95.                             {
  96.                                 this.op3.work = true;
  97.                                 op3Time = currentTime + this.op3.next();
  98.                             }
  99.                         }
  100.                         else
  101.                         {
  102.                             this.op2.work = true;
  103.                             op2Time = currentTime + this.op2.next();
  104.                         }
  105.                     }
  106.                     else
  107.                     {
  108.                         this.op1.work = true;
  109.                         op1Time = currentTime + this.op1.next();
  110.                     }
  111.                 }
  112.             }
  113.  
  114.             if(currentTime >= st1Time)
  115.             {
  116.                 if (this.st1.work)
  117.                 {
  118.                     this.count++;
  119.                     this.st1.work = false;
  120.                     this.queue1.pop();
  121.                     if (this.count == aim)
  122.                     {
  123.                         console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", currentTime, "\nВероятность отказа: ", (broken/amount)*100, "%");
  124.                         break;
  125.                     }
  126.                 }
  127.                 if (this.queue1.length > 0)
  128.                 {
  129.                     if (!this.st1.work)
  130.                     {
  131.                         st1Time = currentTime + this.st1.next; 
  132.                         this.st1.work = true;
  133.                     }  
  134.                 }
  135.             }
  136.             if(currentTime >= st2Time)
  137.             {
  138.                 if (this.st2.work)
  139.                 {
  140.                     this.count++;
  141.                     this.st2.work = false;
  142.                     this.queue2.pop();
  143.                     if (this.count == aim)
  144.                     {
  145.                         console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", currentTime, "\nВероятность отказа: ", (broken/amount)*100, "%");
  146.                         break;
  147.                     }
  148.                 }
  149.                 if (this.queue2.length > 0)
  150.                 {
  151.                     if (!this.st2.work)
  152.                     {
  153.                         st2Time = currentTime + this.st2.next; 
  154.                         this.st2.work = true;
  155.                     }
  156.                 }
  157.             }
  158.  
  159.             if (amount == 300 && this.queue1.length == 0 && this.queue2.length == 0 && !this.op1.work && !this.op2.work && !this.op3.work)
  160.             {
  161.                 console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", currentTime, "\nВероятность отказа: ", (broken/amount)*100, "%");
  162.                 break;
  163.             }
  164.  
  165.             currentTime += this.dt;
  166.         }
  167.     }  
  168. }
  169.  
  170. function calculate()
  171. {
  172.     let system = new System();
  173.     system.timeModelling();
  174. }
  175.  
  176. calculate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement