Advertisement
FNSY

Untitled

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