Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
136
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 = 0.001;
  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.     eventModelling()
  52.     {
  53.         let aim = 300;
  54.         let genTime = this.generator.next();
  55.         let op1Time = genTime + 0.1;
  56.         let op2Time = genTime + 0.1;
  57.         let op3Time = genTime + 0.1;
  58.         let st1Time = genTime + 0.1;
  59.         let st2Time = genTime + 0.1;
  60.         let amount = 0;
  61.         let broken = 0;
  62.  
  63.         while (this.count < aim)
  64.         {
  65.             if (!this.op1.work)
  66.                 op1Time = 99999;
  67.             if (!this.op2.work)
  68.                 op2Time = 99999;
  69.             if (!this.op3.work)
  70.                 op3Time = 99999;
  71.             if (!this.st1.work)
  72.                 st1Time = 99999;
  73.             if (!this.st2.work)
  74.                 st2Time = 99999;
  75.  
  76.             if (op1Time == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime) && this.op1.work)
  77.             {
  78.                 this.queue1.push(1);
  79.                 st1Time = op1Time;
  80.                 this.op1.work = false;
  81.             }
  82.             if (op2Time == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime) && this.op2.work)
  83.             {
  84.                 this.queue1.push(1);
  85.                 st1Time = op2Time;
  86.                 this.op2.work = false;
  87.             }
  88.             if (op3Time == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime) && this.op3.work)
  89.             {
  90.                 this.queue2.push(1);
  91.                 st2Time = op3Time;
  92.                 this.op3.work = false;
  93.             }
  94.  
  95.             if (amount != 300)
  96.             {
  97.                 if (genTime == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime))
  98.                 {
  99.                     amount++;
  100.                     if (this.op1.work)
  101.                     {
  102.                         if (this.op2.work)
  103.                         {
  104.                             if (this.op3.work)
  105.                                 broken++;
  106.                             else
  107.                             {
  108.                                 this.op3.work = true;
  109.                                 op3Time = genTime + this.op3.next();
  110.                             }
  111.                         }
  112.                         else
  113.                         {
  114.                             this.op2.work = true;
  115.                             op2Time = genTime + this.op2.next();
  116.                         }
  117.                     }
  118.                     else
  119.                     {
  120.                         this.op1.work = true;
  121.                         op1Time = genTime + this.op1.next();
  122.                     }
  123.                     genTime += this.generator.next();
  124.                 }
  125.             }
  126.             else
  127.                 genTime = 99999;
  128.  
  129.             if(st1Time == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime) && this.queue1.length > 0)
  130.             {
  131.                 if (this.st1.work)
  132.                 {
  133.                     this.count++;
  134.                     this.st1.work = false;
  135.                     this.queue1.pop();
  136.                     if (this.count == aim)
  137.                     {
  138.                         console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime), "\nВероятность отказа: ", (broken/amount)*100, "%");
  139.                         break;
  140.                     }
  141.                 }
  142.                 if (this.queue1.length > 0)
  143.                 {
  144.                     if (!this.st1.work)
  145.                     {
  146.                         st1Time = st1Time + this.st1.next; 
  147.                         this.st1.work = true;
  148.                     }  
  149.                 }
  150.             }
  151.             if(st2Time == Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime) && this.queue2.length > 0)
  152.             {
  153.                 if (this.st2.work)
  154.                 {
  155.                     this.count++;
  156.                     this.st2.work = false;
  157.                     this.queue2.pop();
  158.                     if (this.count == aim)
  159.                     {
  160.                         console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime), "\nВероятность отказа: ", (broken/amount)*100, "%");
  161.                         break;
  162.                     }
  163.                 }
  164.                 if (this.queue2.length > 0)
  165.                 {
  166.                     if (!this.st2.work)
  167.                     {
  168.                         st2Time = st2Time + this.st2.next; 
  169.                         this.st2.work = true;
  170.                     }
  171.                 }
  172.             }
  173.  
  174.             if (amount == 300 && this.queue1.length == 0 && this.queue2.length == 0 && !this.op1.work && !this.op2.work && !this.op3.work)
  175.             {
  176.                 console.log("Количество отказов: ", broken,"\nКоличество поступивших заявок: ", amount, "\nКоличество обработанных заявок: ", this.count, "\nВремя работы: ", Math.min(op1Time, op2Time, op3Time, st1Time, st2Time, genTime), "\nВероятность отказа: ", (broken/amount)*100, "%");
  177.                 break;
  178.             }
  179.         }
  180.     }
  181. }
  182.  
  183. function calculate()
  184. {
  185.     let system = new System;
  186.     system.eventModelling();
  187. }
  188.  
  189. calculate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement