Advertisement
elwinc2799

Untitled

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