Advertisement
kimo12

Untitled

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