决策树ID3算法

  1. (转)http://leon-a.javaeye.com/blog/178585
  2. package graph;        
  3.        
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.TreeSet;        
  7.        
  8. /**       
  9.  * 决策树的ID3算法       
  10.  * 参照实现http://www.blog.edu.cn/user2/huangbo929/archives/2006/1533249.shtml     
  11.  * @author Leon.Chen       
  12.  */       
  13. public class DTree {        
  14.             
  15.     /**     
  16.      * root     
  17.      */       
  18.     TreeNode root;        
  19.             
  20.     /**     
  21.      * 可见性数组     
  22.      */       
  23.     private static boolean[] visable;        
  24.             
  25.     private Object[] array;    
  26.       
  27.     private int index;  
  28.        
  29.     /**     
  30.      * @param args     
  31.      */       
  32.     @SuppressWarnings("boxing")        
  33.     public static void main(String[] args) {        
  34.         //初始数据        
  35.         Object[] array = new Object[] {         
  36.                         new String[]{ "Sunny"    ,"Hot"   ,"High"    ,"Weak"    ,"No" },        
  37.                         new String[]{ "Sunny"    ,"Hot"   ,"High"    ,"Strong"  ,"No" },        
  38.                         new String[]{ "Overcast" ,"Hot"   ,"High"    ,"Weak"    ,"Yes"},        
  39.                         new String[]{ "Rain"     ,"Mild"  ,"High"    ,"Weak"    ,"Yes"},        
  40.                         new String[]{ "Rain"     ,"Cool"  ,"Normal"  ,"Weak"    ,"Yes"},        
  41.                         new String[]{ "Rain"     ,"Cool"  ,"Normal"  ,"Strong"  ,"No" },        
  42.                         new String[]{ "Overcast" ,"Cool"  ,"Normal"  ,"Strong"  ,"Yes"},        
  43.                         new String[]{ "Sunny"    ,"Mild"  ,"High"    ,"Weak"    ,"No" },        
  44.                         new String[]{ "Sunny"    ,"Cool"  ,"Normal"  ,"Weak"    ,"Yes"},        
  45.                         new String[]{ "Rain"     ,"Mild"  ,"Normal"  ,"Weak"    ,"Yes"},        
  46.                         new String[]{ "Sunny"    ,"Mild"  ,"Normal"  ,"Strong"  ,"Yes"},        
  47.                         new String[]{ "Overcast" ,"Mild"  ,"High"    ,"Strong"  ,"Yes"},        
  48.                         new String[]{ "Overcast" ,"Hot"   ,"Normal"  ,"Weak"    ,"Yes"},        
  49.                         new String[]{ "Rain"     ,"Mild"  ,"High"    ,"Strong"  ,"No" },        
  50.                         };        
  51.                 
  52.         DTree tree = new DTree();         
  53.         tree.create(array,4);  
  54.     }   
  55.       
  56.     public void create(Object[] array,int index){  
  57.         this.array = array;  
  58.         init(array,index);  
  59.         createDTree(array);  
  60.         printDTree(root);  
  61.     }  
  62.          
  63.     public Object[] getMaxGain(Object[] array){     
  64.         Object[] result = new Object[2];     
  65.         double gain = 0;     
  66.         int index = 0;  
  67.           
  68.         for(int i=0;i<visable.length;i++){     
  69.             if(!visable[i]){     
  70.                 double value = gain(array,i);     
  71.                 if(gain < value){     
  72.                     gain = value;     
  73.                     index = i;     
  74.                 }     
  75.             }     
  76.         }     
  77.         result[0] = gain;     
  78.         result[1] = index;     
  79.         visable[index] = true;     
  80.         return result;     
  81.     }     
  82.          
  83.     public void createDTree(Object[] array) {     
  84.         Object[] maxgain = getMaxGain(array);     
  85.         if (root == null) {     
  86.             root = new TreeNode();     
  87.             root.parent = null;     
  88.             root.parentArrtibute = null;     
  89.             root.arrtibutes = getArrtibutes(((Integer) maxgain[1]).intValue());     
  90.             root.nodeName = getNodeName(((Integer) maxgain[1]).intValue());    
  91.             root.childNodes = new TreeNode[root.arrtibutes.length];  
  92.             insertTree(array,root);  
  93.         }  
  94.     }     
  95.          
  96.     public void insertTree(Object[] array,TreeNode parentNode){  
  97.         String[] arrtibutes = parentNode.arrtibutes;  
  98.         for(int i=0;i<arrtibutes.length;i++){  
  99.             Object[] pickArray = pickUpAndCreateArray(array,arrtibutes[i],getNodeIndex(parentNode.nodeName));  
  100.             Object[] info = getMaxGain(pickArray);  
  101.             double gain = ((Double)info[0]).doubleValue();  
  102.             if(gain != 0){  
  103.                 int index = ((Integer) info[1]).intValue();  
  104.                 System.out.println("gain = "+gain+" ,node name = "+getNodeName(index));  
  105.                 TreeNode currentNode = new TreeNode();  
  106.                 currentNode.parent = parentNode;  
  107.                 currentNode.parentArrtibute = arrtibutes[i];  
  108.                 currentNode.arrtibutes = getArrtibutes(index);  
  109.                 currentNode.nodeName = getNodeName(index);  
  110.                 currentNode.childNodes = new TreeNode[currentNode.arrtibutes.length];  
  111.                 parentNode.childNodes[i] = currentNode;  
  112.                 insertTree(pickArray,currentNode);  
  113.             }else {  
  114.                 TreeNode leafNode = new TreeNode();  
  115.                 leafNode.parent = parentNode;  
  116.                 leafNode.parentArrtibute = arrtibutes[i];  
  117.                 leafNode.arrtibutes = new String[0];  
  118.                 leafNode.nodeName = getLeafNodeName(pickArray);  
  119.                 leafNode.childNodes = new TreeNode[0];  
  120.                 parentNode.childNodes[i] = leafNode;  
  121.             }  
  122.         }  
  123.   
  124.     }  
  125.       
  126.     public void printDTree(TreeNode node){  
  127.         System.out.println(node.nodeName);  
  128.   
  129.         TreeNode[] childs = node.childNodes;  
  130.         for(int i=0;i<childs.length;i++){  
  131.             if(childs[i]!=null){  
  132.                 System.out.println(childs[i].parentArrtibute);  
  133.                 printDTree(childs[i]);  
  134.             }  
  135.         }  
  136.     }  
  137.     
  138.     /**       
  139.      * @param dataArray 原始数组 D      
  140.      * @param criterion 标准值       
  141.      * @return double       
  142.      */       
  143.     public void init(Object[] dataArray,int index) {  
  144.         this.index = index;  
  145.         //数据初始化     
  146.         visable = new boolean[((String[])dataArray[0]).length];        
  147.         for(int i=0;i<visable.length;i++) {      
  148.             if(i == index){     
  149.                 visable[i] = true;      
  150.             }else {     
  151.                 visable[i] = false;      
  152.             }     
  153.         }     
  154.     }     
  155.       
  156.     public Object[] pickUpAndCreateArray(Object[] array,String arrtibute,int index){  
  157.         List<String[]> list = new ArrayList<String[]>();  
  158.         for(int i=0;i<array.length;i++){  
  159.             String[] strs = (String[])array[i];  
  160.             if(strs[index].equals(arrtibute)){  
  161.                 list.add(strs);  
  162.             }  
  163.         }  
  164.         return list.toArray();  
  165.     }  
  166.     
  167.     /**     
  168.      * Entropy(S)     
  169.      * @param array     
  170.      * @return double      
  171.      */       
  172.     public double gain(Object[] array,int index) {        
  173.         String[] playBalls = getArrtibutes(this.index);     
  174.         int[] counts = new int[playBalls.length];        
  175.         for(int i=0;i<counts.length;i++) {     
  176.             counts[i] = 0;        
  177.         }     
  178.         for(int i=0;i<array.length;i++) {     
  179.             String[] strs = (String[])array[i];     
  180.             for(int j=0;j<playBalls.length;j++) {     
  181.                 if(strs[this.index].equals(playBalls[j])) {     
  182.                     counts[j]++;     
  183.                 }     
  184.             }     
  185.         }     
  186.         /**   
  187.          * Entropy(S) = S -p(I) log2 p(I)   
  188.          */    
  189.         double entropyS = 0;     
  190.         for(int i=0;i<counts.length;i++) {        
  191.             entropyS += DTreeUtil.sigma(counts[i],array.length);        
  192.         }     
  193.         String[] arrtibutes = getArrtibutes(index);     
  194.         /**   
  195.          * total ((|Sv| / |S|) * Entropy(Sv))    
  196.          */    
  197.         double sv_total = 0;     
  198.         for(int i=0;i<arrtibutes.length;i++){     
  199.             sv_total += entropySv(array, index,arrtibutes[i],array.length);     
  200.         }     
  201.         return entropyS-sv_total;     
  202.     }     
  203.          
  204.     /**   
  205.      * ((|Sv| / |S|) * Entropy(Sv))   
  206.      * @param array   
  207.      * @param index   
  208.      * @param arrtibute   
  209.      * @param allTotal   
  210.      * @return   
  211.      */    
  212.     public double entropySv(Object[] array,int index,String arrtibute,int allTotal) {     
  213.         String[] playBalls = getArrtibutes(this.index);     
  214.         int[] counts = new int[playBalls.length];     
  215.         for(int i=0;i<counts.length;i++) {     
  216.             counts[i] = 0;        
  217.         }     
  218.     
  219.         for (int i = 0; i < array.length; i++) {     
  220.             String[] strs = (String[]) array[i];     
  221.             if (strs[index].equals(arrtibute)) {     
  222.                 for (int k = 0; k < playBalls.length; k++) {     
  223.                     if (strs[this.index].equals(playBalls[k])) {     
  224.                         counts[k]++;     
  225.                     }     
  226.                 }     
  227.             }     
  228.         }     
  229.     
  230.         int total = 0;     
  231.         double entropySv = 0;      
  232.         for(int i=0;i<counts.length;i++){     
  233.             total += counts[i];     
  234.         }     
  235.         for(int i=0;i<counts.length;i++){     
  236.             entropySv += DTreeUtil.sigma(counts[i],total);      
  237.         }      
  238.         return DTreeUtil.getPi(total, allTotal)*entropySv;     
  239.     }     
  240.             
  241.     @SuppressWarnings("unchecked")     
  242.     public String[] getArrtibutes(int index) {        
  243.         TreeSet<String> set = new TreeSet<String>(new SequenceComparator());        
  244.         for (int i = 0; i < array.length; i++) {        
  245.             String[] strs = (String[]) array[i];        
  246.             set.add(strs[index]);        
  247.         }        
  248.         String[] result = new String[set.size()];        
  249.         return set.toArray(result);        
  250.     }     
  251.             
  252.     public String getNodeName(int index) {      
  253.         String[] strs = new String[]{"Outlook","Temperature","Humidity","Wind","Play ball"};  
  254.         for(int i=0;i<strs.length;i++){  
  255.             if(i == index){  
  256.                 return strs[i];  
  257.             }  
  258.         }  
  259.         return null;      
  260.     }  
  261.       
  262.     public String getLeafNodeName(Object[] array){  
  263.         if(array!=null && array.length>0){  
  264.             String[] strs = (String[])array[0];  
  265.             return strs[index];  
  266.         }  
  267.         return null;          
  268.     }  
  269.       
  270.     public int getNodeIndex(String name) {    
  271.         String[] strs = new String[]{"Outlook","Temperature","Humidity","Wind","Play ball"};  
  272.         for(int i=0;i<strs.length;i++){  
  273.             if(name.equals(strs[i])){  
  274.                 return i;  
  275.             }  
  276.         }  
  277.         return -1;      
  278.     }   
  279. }        
  280.   
  281. package graph;        
  282.        
  283. /**     
  284.  * @author B.Chen     
  285.  */       
  286. public class TreeNode {        
  287.        
  288.     /**     
  289.      * 父     
  290.      */       
  291.     TreeNode parent;      
  292.        
  293.     /**     
  294.      * 指向父的哪个属性     
  295.      */       
  296.     String parentArrtibute;        
  297.        
  298.     /**      
  299.      * 节点名      
  300.      */       
  301.     String nodeName;        
  302.        
  303.     /**     
  304.      * 属性数组     
  305.      */       
  306.     String[] arrtibutes;      
  307.       
  308.     /** 
  309.      * 节点数组 
  310.      */  
  311.     TreeNode[] childNodes;  
  312.   
  313. }        
  314.   
  315. package graph;     
  316.     
  317. public class DTreeUtil {     
  318.     
  319.     /**   
  320.      * 属性值熵的计算 Info(T)=(i=1...k)pi*log(2)pi   
  321.      *    
  322.      * @param x   
  323.      * @param total   
  324.      * @return double   
  325.      */    
  326.     public static double sigma(int x, int total) {  
  327.         if(x == 0){  
  328.             return 0;  
  329.         }  
  330.         double x_pi = getPi(x, total);     
  331.         return -(x_pi * logYBase2(x_pi));     
  332.     }     
  333.     
  334.     /**   
  335.      * log2y   
  336.      *    
  337.      * @param y   
  338.      * @return double   
  339.      */    
  340.     public static double logYBase2(double y) {     
  341.         return Math.log(y) / Math.log(2);     
  342.     }     
  343.     
  344.     /**   
  345.      * pi是当前这个属性出现的概率(=出现次数/总数)   
  346.      *    
  347.      * @param x   
  348.      * @param total   
  349.      * @return double   
  350.      */    
  351.     public static double getPi(int x, int total) {     
  352.         return x * Double.parseDouble("1.0") / total;     
  353.     }    
  354.   
  355. }     
  356.   
  357. package graph;  
  358.   
  359. import java.util.Comparator;  
  360.   
  361. public class SequenceComparator implements Comparator {  
  362.   
  363.     public int compare(Object o1, Object o2) throws ClassCastException {  
  364.         String str1 = (String) o1;  
  365.         String str2 = (String) o2;  
  366.         return str1.compareTo(str2);  
  367.     }  
  368.   

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值