elwinc2799

Untitled

Apr 22nd, 2021
565
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.opencsv.CSVWriter;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.util.Scanner;
  6. import java.util.Random;
  7. import java.util.Arrays;
  8.  
  9. public class main {
  10.     public static int n = 0;    //Counter for Primitive Operation
  11.     public static int min = 0;
  12.     public static boolean trg = false;
  13.  
  14.     public static void main(String[] args) {
  15.         Scanner sc= new Scanner(System.in);
  16.         System.out.println("\n1. Custom input of number of station\n2. Auto generate a series of number up until max number (with CSV file created, OpenCSV jar needed)");
  17.         System.out.print("\nChoice (1/2): ");
  18.         int choice = sc.nextInt();
  19.  
  20.         //custom input of number of station
  21.         if (choice == 1){
  22.             System.out.print("Number of stations: ");
  23.             int temp = sc.nextInt();
  24.             customInput(temp);
  25.         }
  26.         //create CSV file for f(n) and n data (require OpenCSV jar)
  27.         else if (choice == 2){
  28.             System.out.print("Number of stations from 1 until : ");
  29.             int temp = sc.nextInt();
  30.             autoGenerateResultCSV(temp);
  31.         }
  32.     }
  33.  
  34.     public static void customInput(int temp){
  35.         int a[][] = new int[2][temp];       //time taken per station
  36.         int t[][] = new int[2][temp-1];       //time taken to transfer line
  37.         int e[] = new int[2];               //entry time
  38.         int x[] = new int[2];               //exit time
  39.         int route[][] = new int[2][temp-1];
  40.  
  41.         //randomGenerator() used to generate random number for time cost
  42.         for (int i = 0; i < temp; i++){
  43.             if (i < 2){
  44.                 e[i] = randomGenerator();
  45.                 x[i] = randomGenerator();
  46.             }
  47.             if (i != temp-1){
  48.                 t[0][i] = randomGenerator();
  49.                 t[1][i] = randomGenerator();
  50.             }
  51.             a[0][i] = randomGenerator();
  52.             a[1][i] = randomGenerator();
  53.         }
  54.  
  55.         //Display the entry time, exit time, time taken per station and time taken to transfer line
  56.         System.out.println("\nEntry time: " + Arrays.toString(e));
  57.         System.out.println("Exit time: " + Arrays.toString(x) + "\n");
  58.         int j = 0;
  59.         for (int i = 0; i < 2; i++){
  60.             j = j + 1;
  61.             System.out.println("Time taken per station in Line " + j + ": " + Arrays.toString(a[i]));
  62.             System.out.println("Time taken to transfer from Line " + j + " to another line: " + Arrays.toString(t[i]) + "\n");
  63.         }
  64.  
  65.         assemblyTime(a, t, e, x, temp, route);
  66.  
  67.         //trg used for store route taken
  68.         System.out.print("Route taken: ");
  69.         if (trg){
  70.             int k = 0;
  71.             for (int i = 0; i < temp - 1; i++){
  72.                 k += 1;
  73.                 System.out.print("L" + route[0][i] + "S" + k + " -> ");
  74.             }
  75.             System.out.println("L1S4\n");
  76.         }
  77.         else{
  78.             int k = 0;
  79.             for (int i = 0; i < temp - 1; i++){
  80.                 k += 1;
  81.                 System.out.print("L" + route[1][i] + "S" + k + " -> ");
  82.             }
  83.             System.out.println("L2S4\n");
  84.         }
  85.  
  86.         System.out.println("Number of stations: " + temp + "\nTotal number of primitive operations: " + n + "\nMin time: " + min);
  87.     }
  88.  
  89.     public static void autoGenerateResultCSV(int total){
  90.         File file = new File("./result.csv");   //create CSV file in default directory
  91.  
  92.         try {
  93.             FileWriter writer = new FileWriter(file);
  94.             CSVWriter csv = new CSVWriter(writer);
  95.  
  96.             //for-loop to loop different number of stations
  97.             for (int temp = 1; temp <= total; temp++){
  98.                 int a[][] = new int[2][temp];       //time taken per station
  99.                 int t[][] = new int[2][temp-1];       //time taken to transfer line
  100.                 int e[] = new int[2];               //entry time
  101.                 int x[] = new int[2];               //exit time
  102.                 int route[][] = new int[2][temp-1];
  103.  
  104.                 //randomGenerator() used to generate random number for time cost
  105.                 for (int i = 0; i < temp; i++){
  106.                     if (i < 2){
  107.                         e[i] = randomGenerator();
  108.                         x[i] = randomGenerator();
  109.                     }
  110.                     if (i != temp-1){
  111.                         t[0][i] = randomGenerator();
  112.                         t[1][i] = randomGenerator();
  113.                     }
  114.                     a[0][i] = randomGenerator();
  115.                     a[1][i] = randomGenerator();
  116.                 }
  117.  
  118.                 n = 0;
  119.                 assemblyTime(a, t, e, x, temp, route);
  120.  
  121.                 //write data into CSV file in a row
  122.                 String [] data = {Integer.toString(temp),Integer.toString(n)};
  123.                 csv.writeNext(data);
  124.             }
  125.             csv.close();
  126.         }
  127.         catch (Exception e) {
  128.             e.printStackTrace();
  129.         }
  130.  
  131.     }
  132.  
  133.     public static int randomGenerator(){
  134.         int upper = 25;
  135.  
  136.         Random rand = new Random();
  137.         int randomNum = 0;
  138.         while (randomNum == 0){
  139.             randomNum = rand.nextInt(upper);
  140.         }
  141.         return randomNum;
  142.     }
  143.  
  144.     public static void assemblyTime(int a[][], int t[][], int e[], int x[], int len, int route[][]) {
  145.  
  146.         // time taken to leave 1st station in line 1
  147.         int first = e[0] + a[0][0];
  148.         n = n + 4;  //Counter: 2 * array index, addition, assignment
  149.  
  150.         // time taken to leave 1st station in line 2
  151.         int second = e[1] + a[1][0];
  152.         n = n + 4;  //Counter: 2 * array index, addition, assignment
  153.  
  154.         n++;    //Counter: assignment (int i = 1)
  155.         for (int i = 1; i < len; i++) {
  156.             n = n + 1;  //Counter: comparison (i < len)
  157.  
  158.             int up = Math.min(first + a[0][i],second + t[1][i-1] + a[0][i]);
  159.             n = n + 8;  //Counter: 3 * array index, 3 * addition, comparison, assignment
  160.  
  161.             //trg used for store route taken
  162.             if ((first + a[0][i]) == up){
  163.                 route[0][i-1] = 1;
  164.             }
  165.             else {
  166.                 route[0][i-1] = 2;
  167.             }
  168.             int down = Math.min(second + a[1][i],first + t[0][i-1] + a[1][i]);
  169.             n = n + 8;  //Counter: 3 * array index, 3 * addition, comparison, assignment
  170.  
  171.             //trg used for store route taken
  172.             if ((second + a[1][i]) == down){
  173.                 route[1][i-1] = 2;
  174.             }
  175.             else {
  176.                 route[1][i-1] = 1;
  177.             }
  178.  
  179.             first = up;
  180.             second = down;
  181.             n = n + 2;  //Counter: 2 * assignment
  182.  
  183.             n = n + 2;  //Counter: addition, assignment (i++)
  184.         }
  185.         n++;    //Counter: last comparison
  186.  
  187.         first += x[0];
  188.         second += x[1];
  189.         n = n + 6;  //Counter: 2 * array index, 2 * addition, 2 * assignment
  190.  
  191.         min = Math.min(first, second);
  192.         n = n + 2;  //Counter: comparison + assignment
  193.  
  194.         //trg used for checking which route taken
  195.         if (first == min){
  196.             trg = true;
  197.         }
  198.         else {
  199.             trg = false;
  200.         }
  201.     }
  202. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×