Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. /**
  2.  
  3. * Export rule base for use in MATLAB
  4.  
  5. * Writing to *.FIS document
  6.  
  7. *
  8.  
  9. */
  10.  
  11. public void exportToFIS() {
  12.  
  13. Enumeration enumVars = variableList.elements();
  14.  
  15. Enumeration enumRules = ruleList.elements();
  16.  
  17. ContinuousFuzzyRuleVariable outputVar=null;
  18.  
  19. String tempString;
  20.  
  21. int noOfRules, noOfVars;
  22.  
  23. noOfVars = 0;
  24.  
  25. while (enumVars.hasMoreElements()) {
  26.  
  27. FuzzyRuleVariable tempVar = (FuzzyRuleVariable) enumVars.nextElement();
  28.  
  29. noOfVars+= 1;
  30.  
  31. }
  32.  
  33. noOfRules = 0;
  34.  
  35. while (enumRules.hasMoreElements()) {
  36.  
  37. noOfRules +=1;
  38.  
  39. FuzzyRule tempRule = (FuzzyRule) enumRules.nextElement();
  40.  
  41. }
  42.  
  43. try {
  44.  
  45. FileWriter outFile = new FileWriter("Test.fis");
  46.  
  47. PrintWriter out = new PrintWriter(outFile);
  48.  
  49. out.println("[System]");
  50.  
  51. out.println("Name='"+ name + "'");
  52.  
  53. out.println("Type='mamdani'");
  54.  
  55. out.println("Version=2.0");
  56.  
  57. out.println("NumInputs="+(noOfVars-1));
  58.  
  59. out.println("NumOutputs=1");
  60.  
  61. out.println("NumRules="+(noOfRules-1));
  62.  
  63. out.println("AndMethod='min'");
  64.  
  65. out.println("OrMethod='max'");
  66.  
  67. if (correlationMethod== FuzzyDefs.PRODUCT) {
  68.  
  69. out.println("ImpMethod='prod'"); // ("PRODUCT");
  70.  
  71. }else{
  72.  
  73. out.println("ImpMethod='min'"); // ("MINIMISE");
  74.  
  75. }
  76.  
  77. if (inferenceMethod== FuzzyDefs.FUZZYADD) {
  78.  
  79. out.println("AggMethod='sum'"); // "FUZZYADD";
  80.  
  81. }else{
  82.  
  83. out.println("AggMethod='max'"); // "MINMAX";
  84.  
  85. }
  86.  
  87. if (defuzzifyMethod== FuzzyDefs.CENTROID) {
  88.  
  89. out.println("DefuzzMethod='centroid'"); //"CENTROID";
  90.  
  91. }else{
  92.  
  93. out.println("DefuzzMethod='mom'"); //"MAXHEIGHT";
  94.  
  95. }
  96.  
  97. out.println();
  98.  
  99. noOfVars = 1;
  100.  
  101. Enumeration enumVars2 = variableList.elements();
  102.  
  103. while (enumVars2.hasMoreElements()) { // VARIABLES
  104.  
  105. FuzzyRuleVariable tempVar = (FuzzyRuleVariable) enumVars2.nextElement();
  106.  
  107. ContinuousFuzzyRuleVariable tempContVar = (ContinuousFuzzyRuleVariable)tempVar;
  108.  
  109. if (tempContVar.getVariableType() == FuzzyDefs.OUTPUT) {
  110.  
  111. outputVar = tempContVar;
  112.  
  113. }
  114.  
  115. else { // INPUT VARIABLES
  116.  
  117. out.println("[Input "+noOfVars+ "]");
  118.  
  119. out.println("[Input "+tempVar.getVariableNO() + "]");
  120.  
  121. out.println("Name='"+tempVar.getName() + "'");
  122.  
  123. out.println("Range=["+ tempContVar.getDiscourseLo()+ " " + tempContVar.getDiscourseHi()+ "]");
  124.  
  125. Enumeration enumSets = tempContVar.getFuzzySets().elements();
  126.  
  127. int noOfSets = tempContVar.getFuzzySets().size();
  128.  
  129. out.println("NumMFs="+noOfSets);
  130.  
  131. noOfSets = 1;
  132.  
  133. while (enumSets.hasMoreElements()) { // MFs
  134.  
  135. FuzzySet tempSet = (FuzzySet) enumSets.nextElement();
  136.  
  137. out.print("MF"+tempSet.getSetNo()+ "='" + tempSet.getSetName() + "'" );
  138.  
  139. switch (tempSet.getSetType()) {
  140.  
  141. case FuzzyDefs.SHOULDER :
  142.  
  143. out.print(":'trapmf',[" + ((ShoulderFuzzySet)tempSet).getLeftPoint() + ", ");
  144.  
  145. out.println( ((ShoulderFuzzySet)tempSet).getRightPoint()+ "]") ;
  146.  
  147. break;
  148.  
  149. case FuzzyDefs.TRIANGLE :
  150.  
  151. out.print(":'trimf',[" + ((TriangleFuzzySet)tempSet).getLeftPoint() + ", ");
  152.  
  153. out.print( ((TriangleFuzzySet)tempSet).getCenterPoint()+ ", ") ;
  154.  
  155. out.println( ((TriangleFuzzySet)tempSet).getRightPoint()+ "]") ; break;
  156.  
  157. case FuzzyDefs.TRAPEZOID :
  158.  
  159. out.print(":'trapmf',[" + ((TrapezoidFuzzySet)tempSet).getLeftPoint() + ",");
  160.  
  161. out.print( ((TrapezoidFuzzySet)tempSet).getLeftCorePoint()+ ", ") ;
  162.  
  163. out.print( ((TrapezoidFuzzySet)tempSet).getRightCorePoint()+ ", ") ;
  164.  
  165. out.println( ((TrapezoidFuzzySet)tempSet).getRightPoint()+ "]") ;
  166.  
  167. }
  168.  
  169. noOfSets+= 1;
  170.  
  171. } // MFs
  172.  
  173. out.println();
  174.  
  175. noOfVars+= 1;
  176.  
  177. } // else
  178.  
  179. } // while VARIABLES
  180.  
  181. if (outputVar != null) { // OUTPUT VARIABLE
  182.  
  183. out.println("[Output 1]");
  184.  
  185. out.println("Name='"+outputVar.getName() + "'");
  186.  
  187. out.println("Range=["+ outputVar.getDiscourseLo()+ " " + outputVar.getDiscourseHi()+
  188.  
  189. "]");
  190.  
  191. Enumeration enumSets = outputVar.getFuzzySets().elements();
  192.  
  193. int noOfSets = outputVar.getFuzzySets().size();
  194.  
  195. out.println("NumMFs="+noOfSets );
  196.  
  197. noOfSets = 1;
  198.  
  199. while (enumSets.hasMoreElements()) {// MFs
  200.  
  201. FuzzySet tempSet = (FuzzySet) enumSets.nextElement();
  202.  
  203. out.print("MF"+tempSet.getSetNo()+ "='" + tempSet.getSetName() + "'" );
  204.  
  205. switch (tempSet.getSetType()) {
  206.  
  207. case FuzzyDefs.SHOULDER :
  208.  
  209. out.print(":'trapmf',[" + ((ShoulderFuzzySet)tempSet).getLeftPoint() + ", ");
  210.  
  211. out.println( ((ShoulderFuzzySet)tempSet).getRightPoint()+ "]") ;
  212.  
  213. break;
  214.  
  215. case FuzzyDefs.TRIANGLE :
  216.  
  217. out.print(":'trimf',[" + ((TriangleFuzzySet)tempSet).getLeftPoint() + ", ");
  218.  
  219. out.print( ((TriangleFuzzySet)tempSet).getCenterPoint()+ ", ") ;
  220.  
  221. out.println( ((TriangleFuzzySet)tempSet).getRightPoint()+ "]") ; break;
  222.  
  223. case FuzzyDefs.TRAPEZOID :
  224.  
  225. out.print(":'trapmf',[" + ((TrapezoidFuzzySet)tempSet).getLeftPoint() + ",");
  226.  
  227. out.print( ((TrapezoidFuzzySet)tempSet).getLeftCorePoint()+ ", ") ;
  228.  
  229. out.print( ((TrapezoidFuzzySet)tempSet).getRightCorePoint()+ ", ") ;
  230.  
  231. out.println( ((TrapezoidFuzzySet)tempSet).getRightPoint()+ "]") ;
  232.  
  233. }
  234.  
  235. noOfSets+= 1;
  236.  
  237. }// MFs
  238.  
  239. out.println();
  240.  
  241. } // OUTPUT VARIABLE
  242.  
  243. out.println();
  244.  
  245. out.println("[Rules]");
  246.  
  247. enumRules = ruleList.elements(); //get Rules
  248.  
  249. while (enumRules.hasMoreElements()) { // RULES
  250.  
  251. FuzzyRule tempRule = (FuzzyRule) enumRules.nextElement();
  252.  
  253. Vector clauses = tempRule.getAntecedents();
  254.  
  255. StringBuilder printLine = new StringBuilder();
  256.  
  257. for (int j = 0; j < (noOfVars-1); j++)
  258.  
  259. printLine.append("0 ");
  260.  
  261. for (int i = 0; i < clauses.size(); i++) {
  262.  
  263. // print Antecedents - indeks MF na poziciji INPUT varijable
  264.  
  265. FuzzyClause tempClause = (FuzzyClause) (clauses.elementAt(i));
  266.  
  267. String tempChars= ""+ tempClause.getRhs().getSetNo();
  268.  
  269. int first = (tempClause.getLhs().getVariableNO()-1)*2;
  270.  
  271. printLine.replace(first, first+1, tempChars);
  272.  
  273. }
  274.  
  275. out.print(printLine);
  276.  
  277. out.println(", "+ tempRule.getConsequent().getRhs().getSetNo() +" (1) : 1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement