Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. ackage model;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.math.BigDecimal;
  8. import java.math.RoundingMode;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Random;
  15. import java.util.Set;
  16. import org.apache.logging.log4j.LogManager;
  17. import org.apache.logging.log4j.Logger;
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.Row;
  22.  
  23. public class ModelActivation {
  24.     static final Logger logger = LogManager.getLogger(ModelActivation.class.getName());
  25.     private int normal_n;
  26.     private int normal_a;
  27.     private double normal_b;
  28.     private double eksponent_lambda;
  29.     private double puasson_l;
  30.     private int erlang_l;
  31.     private double erlang_lambda;
  32.     private Random rand;
  33.     public static List<String> queueTaskName;
  34.     public static List<Double> queueTaskRand;
  35.    
  36.     public static List<ModelOutput> log;
  37.    
  38.     public static String taskName;
  39.     public static double Tmodel;
  40.     public static double L1;
  41.     public static double L2;
  42.     public static double h;
  43.     public static double TEnd;
  44.     public static int s;
  45.     public static int q;
  46.     public static int i = 0;
  47.     public static int l1counter = 0;
  48.     public static int l2counter = 0;
  49.  
  50.     public ModelActivation() {
  51.         queueTaskName = new ArrayList<String>();
  52.         queueTaskRand = new ArrayList<Double>();
  53.         normal_n = 1000;
  54.         normal_a = 15;
  55.         normal_b = 1/3;
  56.         eksponent_lambda = 1.5;
  57.         puasson_l = 0.2;
  58.         erlang_l = 3;
  59.         erlang_lambda = 0.5;
  60.         rand = new Random();
  61.        
  62.         taskName = "-";
  63.         Tmodel = 0;
  64.         L1 = 0;
  65.         L2 = 0;
  66.         h = 501;
  67.         TEnd = 500.00;
  68.         s = 0;
  69.         q = 0;
  70.        
  71.         log = new ArrayList<ModelOutput>();
  72.     }
  73.    
  74.     public void insertIntoQueue(String name, double rand) {
  75.         queueTaskName.add(name);
  76.         queueTaskRand.add(rand);
  77.     }
  78.    
  79.     public int queueSize() {
  80.         return queueTaskName.size();
  81.     }
  82.    
  83.     public void remFirstFromQueue() {
  84.         queueTaskName.remove(0);
  85.         queueTaskRand.remove(0);
  86.     }
  87.    
  88.     public String getTaskFromQueueName() {
  89.         return queueTaskName.get(0);
  90.     }
  91.    
  92.     public static double minValue(double a, double b) {
  93.         return Math.min(a, b);
  94.     }
  95.    
  96.     public void showReport() {
  97.         System.out.println(taskName + "\t" + Tmodel + "\t\t" + L1 + "\t\t" + L2 + "\t\t" + h + "\t\t" + TEnd + "\t" + s + "\t" + q);
  98.     }
  99.    
  100.     public void insertIntoList(String taskName, double modelTime, double l1, double l2,
  101.             double h, double tend, int serverStatus, int lifoStatus) {
  102.         log.add(new ModelOutput(taskName, Tmodel, L1, L2, h, TEnd, s, q));
  103.     }
  104.    
  105.     public double getRand() {
  106.         double generated = rand.nextDouble();
  107.         return generated;
  108.     }
  109.    
  110.     public double normal() {
  111.         double z = 0, numbers = 0;
  112.         for(int i = 0; i < normal_n; i++) {
  113.             numbers = numbers + getRand();
  114.         }
  115.         z = (numbers - normal_n/2)/Math.sqrt(normal_n/15);
  116.         double x = z*normal_b + normal_a;
  117.         if (x < 0) {
  118.             x = normal();
  119.         }
  120.         return round(x);
  121.     }
  122.    
  123.     public double eksponent() {
  124.         double value = -(1.0/eksponent_lambda) * Math.log(1 - getRand());
  125.         return round(value);
  126.     }
  127.    
  128.     public double eksponent(double lambda) {
  129.         double value = -(1.0/lambda) * Math.log(1 - getRand());
  130.         return value;
  131.     }
  132.    
  133.     public double puasson() {
  134.         double value = eksponent(puasson_l);
  135.         return round(value);
  136.     }
  137.    
  138.     public double erlang() {
  139.         double value = 0;
  140.         for(int i = 0; i < erlang_l; i++) {
  141.             value += eksponent(erlang_lambda);
  142.         }
  143.         return round(value);
  144.     }
  145.    
  146.     public double round(double value) {
  147.         BigDecimal bd = new BigDecimal(value);
  148.         bd = bd.setScale(2, RoundingMode.HALF_UP);
  149.         return bd.doubleValue();
  150.     }
  151.    
  152.     public static void main(String[] args) throws InterruptedException {
  153.         ModelActivation model = new ModelActivation();
  154.         queueTaskName = new ArrayList<String>();
  155.         queueTaskRand = new ArrayList<Double>();
  156.         L1 = model.erlang();
  157.         L2 = model.puasson();
  158.         model.showReport();
  159.         model.insertIntoList(taskName, Tmodel, L1, L2, h, TEnd, s, q);
  160.         double averageTimeInqueue = 0;
  161.         System.out.println("Task \t Tm \t L1 \t L2 \t h \t Tend \t S \t Q");
  162.         while(Tmodel <= TEnd) {
  163.             i++;
  164.             Tmodel = minValue(L1, L2);
  165.             if(Tmodel == L1) {
  166.                 if(s == 0) {
  167.                     s = 1;
  168.                     h = Tmodel +  model.round(model.normal());
  169.                     taskName = "L1";
  170.                     q = model.queueSize();
  171.                 } if(s == 1) {
  172.                     model.insertIntoQueue("L1", L1);
  173.                     q = model.queueSize();
  174.                 }
  175.                 L1 = Tmodel + model.round(model.erlang());
  176.             }
  177.              else if (Tmodel == L2) {
  178.                 if(s == 0) {
  179.                     s = 1;
  180.                     h = Tmodel + model.round(model.eksponent());
  181.                     taskName = "L2";
  182.                     q = model.queueSize();
  183.             } if (s == 1) {
  184.                 model.insertIntoQueue("L2", L2);
  185.                 q = model.queueSize();
  186.                 }
  187.                 L2 = Tmodel + model.round(model.puasson());
  188.             }
  189.             if (Tmodel >= h) {
  190.                 if (q == 0) {
  191.                  h = 501;
  192.                  s = 0;
  193.                 } else {
  194.                    if(q!=0){
  195.                       if (model.getTaskFromQueueName().equalsIgnoreCase("L1")) {
  196.                         h = Tmodel + model.normal();
  197.                         taskName = "L1";
  198.                           model.remFirstFromQueue();
  199.                           q = model.queueSize();
  200.                       } else if (model.getTaskFromQueueName().equalsIgnoreCase("L2")) {
  201.                         h = Tmodel + model.eksponent();
  202.                         taskName = "L2";
  203.                           model.remFirstFromQueue();
  204.                           q = model.queueSize();
  205.                       }
  206.                   }
  207.                 }
  208.                }
  209.             model.showReport();
  210.             model.insertIntoList(taskName, Tmodel, L1, L2, h, TEnd, s, q);
  211.         }
  212.         double time = 500;
  213.         averageTimeInqueue = time /(i - q);
  214.         System.out.println("queue = " + q);
  215.         System.out.println("Number of Tasks = " + i);
  216.         System.out.println("Average time in a queue = " + averageTimeInqueue);
  217.     }
  218. }
  219.  
  220. class ModelOutput{
  221.     String taskName;
  222.     int rownum;
  223.     double modelTime;
  224.     double l1;
  225.     double l2;
  226.     double h;
  227.     double tend;
  228.     int serverStatus;
  229.     int lifoStatus;
  230.    
  231.     public ModelOutput(String taskName, double modelTime, double l1, double
  232.             l2, double h, double tend, int serverStatus, int lifoStatus) {
  233.         setTaskName(taskName);
  234.         setModelTime(modelTime);
  235.         setL1(l1);
  236.         setL2(l2);
  237.         setH(h);
  238.         setTend(tend);
  239.         setServerStatus(serverStatus);
  240.         setFifoStatus(lifoStatus);
  241.     }
  242.    
  243.     public String getTaskName() {
  244.         return taskName;
  245.     }
  246.    
  247.     public void setTaskName(String taskName) {
  248.         this.taskName = taskName;
  249.     }
  250.     public int getRownum() {
  251.         return rownum;
  252.     }
  253.     public void setRownum(int rownum) {
  254.         this.rownum = rownum;
  255.     }
  256.     public double getModelTime() {
  257.         return modelTime;
  258.     }
  259.     public void setModelTime(double modelTime) {
  260.         this.modelTime = modelTime;
  261.     }
  262.     public double getL1() {
  263.         return l1;
  264.     }
  265.     public void setL1(double l1) {
  266.         this.l1 = l1;
  267.     }
  268.     public double getL2() {
  269.         return l2;
  270.     }
  271.     public void setL2(double l2) {
  272.         this.l2 = l2;
  273.     }
  274.     public double getH() {
  275.         return h;
  276.     }
  277.     public void setH(double h) {
  278.         this.h = h;
  279.     }
  280.     public double getTend() {
  281.         return tend;
  282.     }
  283.     public void setTend(double tend) {
  284.         this.tend = tend;
  285.     }
  286.     public int getServerStatus() {
  287.         return serverStatus;
  288.     }
  289.     public void setServerStatus(int serverStatus) {
  290.         this.serverStatus = serverStatus;
  291.     }
  292.     public int getFifoStatus() {
  293.         return lifoStatus;
  294.     }
  295.     public void setFifoStatus(int fifoStatus) {
  296.         this.lifoStatus = fifoStatus;
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement