Advertisement
kimo12

Untitled

Apr 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.80 KB | None | 0 0
  1. package signalFlowGraph;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. public class AnalyzeSFG {
  9.  
  10.     private int[][] adjMatrix;
  11.     private int numberOfNodes;
  12.     private String path = new String();
  13.     private int forwardPathGain = 1;
  14.     private ArrayList<String> loopsPath = new ArrayList<>();
  15.     private ArrayList<String> forwardPath = new ArrayList<>();
  16.     private ArrayList<Integer> forwardGains = new ArrayList<>();
  17.     private ArrayList<Integer> loopsGains = new ArrayList<>();
  18.     private ArrayList<Point> searchLoops = new ArrayList<>();
  19.     private ArrayList<ArrayList<String>> nonTouchingLoopsPath = new ArrayList<>();
  20.     private ArrayList<ArrayList<Integer>> nonTouchingLoopsGains = new ArrayList<>();
  21.     private ArrayList<Integer> nonTouchedLoopsWithPathGains = new ArrayList<>();
  22.     private Scanner scan;
  23.  
  24.     public static void main(String[] args) {
  25.         AnalyzeSFG g = new AnalyzeSFG();
  26.         g.inputs();
  27.         g.printMatrix();
  28.         g.traverseForwardPath(0);
  29.         g.loops();
  30.         g.nonTouchingLoops();
  31.         g.print();
  32.     }
  33.  
  34.     private void makeAllZeros(int[][] matrix) {
  35.         for (int i = 0; i < matrix.length; i++)
  36.             Arrays.fill(matrix[i], 0);
  37.     }
  38.  
  39.     private void inputs() {
  40.         scan = new Scanner(System.in);
  41.         System.out.print("Enter The Number Of Nodes : ");
  42.         numberOfNodes = scan.nextInt();
  43.         adjMatrix = new int[numberOfNodes][numberOfNodes];
  44.         makeAllZeros(adjMatrix);
  45.         System.out.println("To Get Out From Entering Signal Flow Graph Representation");
  46.         System.out.println("Put From = 0 ");
  47.         while (true) {
  48.             int from, to, gain;
  49.             System.out.print("From : ");
  50.             from = scan.nextInt() - 1;
  51.             if (from < 0) {
  52.                 break;
  53.             }
  54.             System.out.print("To : ");
  55.             to = scan.nextInt() - 1;
  56.             System.out.print("gain : ");
  57.             gain = scan.nextInt();
  58.             adjMatrix[to][from] = gain;
  59.         }
  60.         addFinalNode();
  61.     }
  62.  
  63.     private void printMatrix() {
  64.         for (int i = 0; i < numberOfNodes; i++) {
  65.             for (int j = 0; j < numberOfNodes; j++) {
  66.                 System.out.print(adjMatrix[i][j] + " ");
  67.             }
  68.             System.out.println();
  69.         }
  70.  
  71.     }
  72.  
  73.     private void print() {
  74.         System.out.println("Forward Paths");
  75.         for (int i = 0; i < this.forwardGains.size(); i++) {
  76.             System.out.println(this.forwardGains.get(i));
  77.             System.out.println(this.forwardPath.get(i));
  78.         }
  79.         System.out.println("\nLoops");
  80.         for (int i = 0; i < this.loopsPath.size(); i++) {
  81.             System.out.println(this.loopsPath.get(i));
  82.             System.out.println(this.loopsGains.get(i));
  83.         }
  84.     }
  85.  
  86.     private int[][] copyMatrix(int[][] copyTo, int[][] copyFrom) {
  87.         for (int i = 0; i < copyFrom.length; i++)
  88.             for (int j = 0; j < copyFrom.length; j++)
  89.                 copyTo[i][j] = copyFrom[i][j];
  90.         return copyTo;
  91.     }
  92.  
  93.     private int[] getLoopPathArray(String gainString) {
  94.         gainString = gainString.substring(1, gainString.length()).replace("x", " ");
  95.         String[] arrayString = gainString.split(" ");
  96.         int[] array = new int[arrayString.length];
  97.         for (int i = 0; i < arrayString.length; i++) {
  98.             array[i] = Integer.parseInt(arrayString[i]);
  99.         }
  100.         return array;
  101.     }
  102.  
  103.     private int getSimpleLoopGain(String gainString) {
  104.         int[] array = getLoopPathArray(gainString);
  105.         int column, row;
  106.         int gain = 1;
  107.         for (int i = 1; i < array.length; i++) {
  108.             column = array[i - 1];
  109.             row = array[i];
  110.             gain *= adjMatrix[row][column];
  111.         }
  112.         return gain;
  113.         // x1x2x3x4x1
  114.     }
  115.  
  116.     private int getSimpleLoopGain(int[] array) {
  117.         int column, row;
  118.         int gain = 1;
  119.         for (int i = 1; i < array.length; i++) {
  120.             column = array[i - 1];
  121.             row = array[i];
  122.             gain *= adjMatrix[row][column];
  123.         }
  124.         return gain;
  125.         // x1x2x3x4x1
  126.     }
  127.  
  128.     private void addFinalNode() {
  129.         boolean addColumn = false;
  130.         for (int i = 0; i < this.adjMatrix.length; i++)
  131.             if (this.adjMatrix[i][this.numberOfNodes - 1] != 0) {
  132.                 addColumn = true;
  133.                 break;
  134.             }
  135.  
  136.         if (addColumn == true) {
  137.             int newDimension = this.numberOfNodes + 1;
  138.             int[][] copy = new int[newDimension][newDimension];
  139.             makeAllZeros(copy);
  140.             copy = copyMatrix(copy, adjMatrix);
  141.             copy[newDimension - 1][newDimension - 2] = 1;
  142.             adjMatrix = copy;
  143.             this.numberOfNodes = newDimension;
  144.         }
  145.     }
  146.  
  147.     private void loops() {
  148.         // searching for self loops
  149.         for (int i = 0; i < numberOfNodes; i++) {
  150.             if (adjMatrix[i][i] != 0) {
  151.                 String selfLoop = "x" + i + "x" + i;
  152.                 loopsPath.add(selfLoop);
  153.                 loopsGains.add(adjMatrix[i][i]);
  154.             }
  155.         }
  156.         // finding possible loops path
  157.         for (int i = 1; i < numberOfNodes; i++) {
  158.             for (int j = i - 1; j >= 0; j--) {
  159.                 if (adjMatrix[j][i] != 0) {
  160.                     // from i to j (reverse path) j <==== i
  161.                     Point x = new Point(j, i);
  162.                     searchLoops.add(x);
  163.                 }
  164.             }
  165.         }
  166.         // search in the forwardPathes for path from j to i
  167.         // if found j ===> i then the is a loop j => i => j
  168.         for (int i = 0; i < this.forwardPath.size(); i++) {
  169.             String current = this.forwardPath.get(i);
  170.             for (int j = 0; j < this.searchLoops.size(); j++) {
  171.                 int x = this.searchLoops.get(j).x;
  172.                 String from = "x" + x;
  173.                 if (current.contains(from)) {
  174.                     int y = this.searchLoops.get(j).y;
  175.                     String to = "x" + y;
  176.                     if (current.contains(to)) {
  177.                         // this is a loop we must calculate the gain and print
  178.                         // the loop path
  179.                         int start = current.lastIndexOf(from);
  180.                         int end = current.lastIndexOf(to) + 1;
  181.                         String loop = current.substring(start, end + 1) + current.substring(start, start + 2);
  182.                         // Check that this loop isn't already taken
  183.                         boolean exist = false;
  184.                         for (int k = 0; k < this.loopsPath.size(); k++) {
  185.                             if (loop.equals(this.loopsPath.get(k))) {
  186.                                 exist = true;
  187.                             }
  188.                         }
  189.                         if (!exist) {
  190.                             loopsPath.add(loop);
  191.                             loopsGains.add(getSimpleLoopGain(loop));
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.     }
  198.  
  199.     private void nonTouchingLoops() {
  200.         ArrayList<String> pathes = new ArrayList<>();
  201.         ArrayList<Integer> gains = new ArrayList<>();
  202.         // search for the two non-touching loops
  203.         for (int i = 0; i < this.loopsPath.size(); i++) {
  204.             pathes.add(loopsPath.get(i));
  205.             gains.add(loopsGains.get(i));
  206.         }
  207.         this.nonTouchingLoopsPath.add(pathes);
  208.         this.nonTouchingLoopsGains.add(gains);
  209.         pathes = new ArrayList<>();
  210.         gains = new ArrayList<>();
  211.         for (int i = 0; i < this.nonTouchingLoopsPath.get(0).size(); i++) {
  212.             for (int j = i + 1; j < this.nonTouchingLoopsPath.get(0).size(); j++) {
  213.                 if (!areTouching(this.nonTouchingLoopsPath.get(0).get(i), this.nonTouchingLoopsPath.get(0).get(j))) {
  214.                     String nonTouchingLoop = this.nonTouchingLoopsPath.get(0).get(i)
  215.                             + this.nonTouchingLoopsPath.get(0).get(j);
  216.                     int gain = this.nonTouchingLoopsGains.get(0).get(i) * this.nonTouchingLoopsGains.get(0).get(j);
  217.                     pathes.add(nonTouchingLoop);
  218.                     gains.add(gain);
  219.                 }
  220.             }
  221.         }
  222.         if (pathes.size() > 0) {
  223.             this.nonTouchingLoopsPath.add(pathes);
  224.             this.nonTouchingLoopsGains.add(gains);
  225.         }
  226.         // search for more than two non touching loops
  227.         int k = 1;
  228.         while (nonTouchingLoopsPath.size() == k + 1) {
  229.             pathes = new ArrayList<>();
  230.             gains = new ArrayList<>();
  231.             for (int i = 0; i < this.nonTouchingLoopsPath.get(k).size(); i++) {
  232.                 for (int j = 0; j < this.nonTouchingLoopsPath.get(k - 1).size(); j++) {
  233.                     if (!areTouching(this.nonTouchingLoopsPath.get(k).get(i),
  234.                             this.nonTouchingLoopsPath.get(k - 1).get(j))) {
  235.                         String nonTouchingLoop = this.nonTouchingLoopsPath.get(k).get(i)
  236.                                 + this.nonTouchingLoopsPath.get(k - 1).get(j);
  237.                         int gain = this.nonTouchingLoopsGains.get(k).get(i)
  238.                                 * this.nonTouchingLoopsGains.get(k - 1).get(j);
  239.                         pathes.add(nonTouchingLoop);
  240.                         gains.add(gain);
  241.                     }
  242.                 }
  243.             }
  244.             if (pathes.size() > 0) {
  245.                 this.nonTouchingLoopsPath.add(pathes);
  246.                 this.nonTouchingLoopsGains.add(gains);
  247.                 k++;
  248.             } else {
  249.                 k++;
  250.             }
  251.         }
  252.         // printing the nonTouched loops
  253.         if (nonTouchingLoopsPath.size() > 1) {
  254.             System.out.println("nonTouching");
  255.             for (int i = 1; i < this.nonTouchingLoopsPath.size(); i++) {
  256.                 for (int j = 1; j < this.nonTouchingLoopsPath.get(i).size(); j++) {
  257.                     System.out.println(this.nonTouchingLoopsPath.get(i).get(j));
  258.                     System.out.println(this.nonTouchingLoopsGains.get(i).get(j));
  259.                 }
  260.             }
  261.         }
  262.     }
  263.  
  264.     private void nonTouchingWithPathGain() {
  265.         ArrayList<ArrayList<Integer>> nonTouchedLoopsWithPathGain = new ArrayList<>();
  266.         ArrayList<Integer> gains;
  267.         for (int i = 0; i < forwardPath.size(); i++) {
  268.             gains = new ArrayList<>();
  269.             for (int j = 0; j < this.nonTouchingLoopsPath.size(); j++) {
  270.                 int sum = 0;
  271.                 for (int k = 0; k < this.nonTouchingLoopsPath.get(j).size(); k++) {
  272.                     if (!areTouching(forwardPath.get(i), this.nonTouchingLoopsPath.get(j).get(k))) {
  273.                         sum += this.nonTouchingLoopsGains.get(j).get(k);
  274.                     }
  275.                 }
  276.                 gains.add(sum);
  277.             }
  278.             nonTouchedLoopsWithPathGain.add(gains);
  279.         }
  280.         for (int i = 0; i < nonTouchedLoopsWithPathGain.size(); i++) {
  281.             int gainSum = 1;
  282.             for (int j = 0; j < nonTouchedLoopsWithPathGain.get(i).size(); j++) {
  283.                 if (j % 2 == 0) {
  284.                     gainSum -= nonTouchedLoopsWithPathGain.get(i).get(j);
  285.                 } else {
  286.                     gainSum += nonTouchedLoopsWithPathGain.get(i).get(j);
  287.                 }
  288.             }
  289.             this.nonTouchedLoopsWithPathGains.add(gainSum);
  290.         }
  291.     }
  292.  
  293.     private int numerator() {
  294.         nonTouchingWithPathGain();
  295.         int numerator = 0;
  296.         for (int i = 0; i < forwardPath.size(); i++) {
  297.             numerator += this.forwardGains.get(i) * this.nonTouchedLoopsWithPathGains.get(i);
  298.         }
  299.         return numerator;
  300.     }
  301.  
  302.     private int denominator() {
  303.         // 1 - (sum gains individual) + (sum gain of two non touching)
  304.         ArrayList<Integer> sumGains = new ArrayList<>();
  305.         for (int i = 0; i < this.nonTouchingLoopsPath.size(); i++) {
  306.             int sum = 0;
  307.             for (int j = 0; j < this.nonTouchingLoopsPath.get(i).size(); j++) {
  308.                 sum += this.nonTouchingLoopsGains.get(i).get(j);
  309.             }
  310.             sumGains.add(sum);
  311.         }
  312.         int denominator = 1;
  313.         for (int k = 0; k < sumGains.size(); k++) {
  314.             if (k % 2 == 0) {
  315.                 denominator -= sumGains.get(k);
  316.             } else {
  317.                 denominator += sumGains.get(k);
  318.             }
  319.         }
  320.         return denominator;
  321.     }
  322.  
  323.     private double masonValue() {
  324.         return numerator() / denominator();
  325.     }
  326.  
  327.     private boolean areTouching(String loopPath1, String loopPath2) {
  328.         return compareTwoLoops(getLoopPathArray(loopPath1), getLoopPathArray(loopPath2));
  329.     }
  330.  
  331.     private boolean compareTwoLoops(int[] loopPath1, int[] loopPath2) {
  332.         for (int i = 0; i < loopPath1.length; i++)
  333.             for (int j = 0; j < loopPath2.length; j++)
  334.                 if (loopPath1[i] == loopPath2[j])
  335.                     return true;
  336.         return false;
  337.     }
  338.  
  339.     private void traverseForwardPath(int columnIndex) {
  340.  
  341.         if (isLastNode(columnIndex)) {
  342.             addToForwardPath(columnIndex, 1);
  343.             forwardPath.add(path);
  344.             forwardGains.add(forwardPathGain);
  345.             path = path.replace("x" + columnIndex, "");
  346.             return;
  347.         }
  348.  
  349.         int rowIndex = -1;
  350.         rowIndex = getNextNode(rowIndex + 1, columnIndex);
  351.         while (rowIndex != -1) {
  352.             if (rowIndex <= columnIndex) {
  353.                 rowIndex = getNextNode(rowIndex + 1, columnIndex);
  354.                 continue;
  355.             }
  356.             addToForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  357.             traverseForwardPath(rowIndex);
  358.             removeFromForwardPath(columnIndex, adjMatrix[rowIndex][columnIndex]);
  359.             rowIndex = getNextNode(rowIndex + 1, columnIndex);
  360.         }
  361.     }
  362.  
  363.     private void removeFromForwardPath(int currentNode, int gain) {
  364.         String toBeRemoved = "x" + currentNode;
  365.         if (path.contains(toBeRemoved))
  366.             path = path.replace(toBeRemoved, "");
  367.         forwardPathGain /= gain;
  368.  
  369.     }
  370.  
  371.     private void addToForwardPath(int currentNode, int gain) {
  372.         path += "x" + currentNode;
  373.         forwardPathGain *= gain;
  374.     }
  375.  
  376.     private int getNextNode(int rowIndex, int columnIndex) {
  377.         for (; rowIndex < this.numberOfNodes; rowIndex++)
  378.             if (adjMatrix[rowIndex][columnIndex] != 0)
  379.                 return rowIndex;
  380.         return -1;
  381.     }
  382.  
  383.     private boolean isLastNode(int columnIndex) {
  384.         for (int i = 0; i < this.numberOfNodes; i++)
  385.             if (adjMatrix[i][columnIndex] != 0)
  386.                 return false;
  387.         return true;
  388.     }
  389.  
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement