Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1361 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Algorithme d'arbre de decision:Jtree non affiché!Aidez moi svp

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Algorithme d'arbre de decision:Jtree non affiché!Aidez moi svp

n°1872576
klarkweeli​ng
Posté le 14-04-2009 à 01:06:01  profilanswer
 

J'ai essayé sans cesse de pouvoir afficher le Jtree mais j'arrive pas.J'utilise l'algorithme ID3 de Quilan.
Mon projet contient 5 classes:
-main
-Table
-Window
-ID3
-Noeud

 

Main.java

Code :
  1. package test1;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class main
  6. {
  7. public static void main(String[] args)
  8. {
  9. JFrame TPsys = new Window();
  10. TPsys.setVisible(true);
  11. }
  12. }
 

Window.java

Code :
  1. package test1;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.Graphics2D;
  7. import java.awt.Graphics;
  8. /*import java.awt.Image;
  9. import java.io.File;
  10. import java.io.IOException;
  11.  
  12. import javax.imageio.ImageIO;*/
  13. import javax.swing.*;
  14.  
  15. public class Window extends JFrame implements ActionListener {
  16.  
  17. private static final long serialVersionUID = 1L; //eclipse me met un warning si je le met pas
  18. public String Apprentissage[][] = new String[100][100];
  19. //public String Base_de_test[][]=new String [11][11];
  20. JButton ok, build;
  21. int i = 0;
  22. JTextField nb_att, nb_exp;
  23. String Name_algo;
  24. ID3 idd;
  25. String type[] = {"ID3", "C4.5", "CART", "CHAID"};
  26. Graphics2D surface;
  27. public Table T1 = new Table(0, 0, Apprentissage, "EXEMPLE/ATTRIBUT", true);
  28. //public Table T2=new Table(0,0,Base_de_test,"EXEMPLE/ATTRIBUT",true);
  29. public JTable tab_appr = new JTable(T1);
  30. //public JTable tab_test=new JTable(T2);
  31.  
  32. public Window() {
  33. super("Window" );
  34. getContentPane().setLayout(null);
  35.  
  36. JScrollPane r1 = new JScrollPane(tab_appr);
  37. r1.setBounds(20, 300, 500, 100);
  38. //JScrollPane r2=new JScrollPane(tab_test);
  39. //r2.setBounds(250, 400, 200, 200);
  40. add(r1);
  41. //add(r2);
  42.  
  43.  
  44. ok = new JButton("ok" );
  45. build = new JButton("Build" );
  46.  
  47. nb_att = new JTextField("0" );
  48. nb_exp = new JTextField("0" );
  49.  
  50. JComboBox Ltype = new JComboBox(type);
  51.  
  52. this.setLocation(((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 500) / 2,
  53. ((int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 500) / 2);
  54.  
  55. this.setSize(700, 700);
  56.  
  57. nb_att.setBounds(10, 100, 150, 20);
  58. nb_exp.setBounds(10, 150, 150, 20);
  59. ok.setBounds(200, 150, 50, 20);
  60. build.setBounds(200, 100, 80, 20);
  61. Ltype.setBounds(10, 200, 100, 20);
  62.  
  63. getContentPane().add(ok);
  64. getContentPane().add(build);
  65. getContentPane().add(nb_att);
  66. getContentPane().add(nb_exp);
  67. getContentPane().add(Ltype);
  68. ok.addActionListener(this);
  69. build.addActionListener(this);
  70.  
  71. Ltype.addActionListener(new ActionListener() {
  72.  
  73. public void actionPerformed(ActionEvent a) {
  74. JComboBox cb = (JComboBox) a.getSource();
  75. Name_algo = (String) cb.getSelectedItem();
  76.  
  77. }
  78. });
  79.  
  80. }
  81.  
  82. public void actionPerformed(ActionEvent e) {
  83. // this.repaint();
  84.  
  85. if (e.getSource() == ok) {
  86.  
  87. int lin = Integer.parseInt(nb_exp.getText());
  88. int col = Integer.parseInt(nb_att.getText());
  89. this.T1.lin = lin;
  90. this.T1.col = col;
  91. //this.T2.lin=lin;
  92. //this.T2.col=col;
  93. for (int i = 0; i < lin; i++) {
  94. for (int j = 0; j < col; j++) {
  95. this.Apprentissage[i][j] = "";
  96. //this.Base_de_test[j]="0";
  97.  
  98. }
  99. }
  100.  
  101. this.T1.fireTableStructureChanged();
  102. //this.T2.fireTableStructureChanged();
  103. //this.printAll(this.surface);
  104.  
  105. } else if (e.getSource() == build) {
  106. // if (Name_algo.equals("ID3" )) {
  107. ID3 idd = new ID3(this);
  108. idd.setVisible(true);
  109.  
  110. // }
  111. }
  112. }
  113.  
  114. public int get_nb_att() {
  115. String text = new String();
  116. text = nb_att.getText();
  117. Integer nbb = new Integer(text);
  118. int nb = nbb.intValue();
  119.  
  120. return (nb);
  121.  
  122. }
  123.  
  124. public int get_nb_exp() {
  125. String text = new String();
  126. text = nb_exp.getText();
  127. Integer nbb = new Integer(text);
  128. int nb = nbb.intValue();
  129.  
  130. return (nb);
  131.  
  132. }
  133. }
  134.  
  135. [b]Table.java[/b]
  136.  
  137. package test1;
  138.  
  139. import javax.swing.table.AbstractTableModel;
  140.  
  141. public class Table extends AbstractTableModel {
  142.  
  143. /**
  144. *
  145. */
  146. private static final long serialVersionUID = 1L;
  147. //Object D[][]={{6,9,1},{2,3,4},{5,6,7}};
  148. public int col;
  149. public int lin;
  150. String[][] T;
  151. public boolean modif;
  152.  
  153. public Table(int lin, int col, String[][] T, String S, boolean modif) {
  154. super();
  155. this.lin = lin;
  156. this.col = col;
  157. this.T = T;
  158. this.S = S;
  159. this.modif = modif;
  160.  
  161. }
  162.  
  163. public int getColumnCount() {
  164.  
  165. return col + 1;
  166. }
  167.  
  168. public int getRowCount() {
  169.  
  170. return lin;
  171. }
  172.  
  173. public Object getValueAt(int rowIndex, int columnIndex) {
  174. //fireTableDataChanged();
  175.  
  176. //return D[rowIndex][columnIndex];
  177. if (columnIndex > 0) {
  178. return this.T[rowIndex][columnIndex - 1];
  179. }
  180. if (S.equals("Apprentissage" )) {
  181. return "Exp" + rowIndex;
  182. }
  183. return "Exp" + rowIndex;
  184. }
  185.  
  186. public void setValueAt(Object val, int row, int col) {
  187. if (col > 0) {
  188. (this.T[row][col - 1]) = val.toString();
  189. }
  190.  
  191.  
  192. fireTableDataChanged();
  193. }
  194.  
  195. public boolean isCellEditable(int row, int col) {
  196. return modif;
  197.  
  198. }
  199.  
  200. public String getColumnName(int columnIndex) {
  201. if (columnIndex > 0) {
  202. return "Att" + (columnIndex - 1);
  203. }
  204. return S;
  205. }
  206. }
 

ID3.java

 
Code :
  1. package test1;
  2. //import java.io.BufferedReader;
  3. //import java.io.File;
  4. //import java.io.FileInputStream;
  5. //import java.io.InputStreamReader;
  6. //import java.util.StringTokenizer;
  7. import java.util.Vector;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JTree;
  11. import javax.swing.tree.DefaultMutableTreeNode;
  12. import javax.swing.JPanel;
  13. import java.awt.BorderLayout;
  14. import javax.swing.JScrollPane;
  15.  
  16. public class ID3 extends JFrame {
  17.  
  18. private static ID3 instance = null;
  19. private Window win;
  20. private static final long serialVersionUID = 1L; //eclipse me met un warning si je le met pas
  21. private String nomAttributs[] = null; // contient les noms des attributs
  22. private int nbExemples = 0; // nombre d'exemples
  23. private int nbAttributs = 0; // nombre d'attributs
  24. private Vector valeurAttributs[]; // tableau contenant les valeurs des attributs (string)
  25. private Vector Exemples = null; // contient les exemples sous forme de chiffres
  26. private Noeud root = null; // noeud racine
  27. private JPanel jPanel = null;
  28. private JTree arbre = null;
  29.  
  30. /**
  31. * utilisation du pattern singleton
  32. *
  33. */
  34. public ID3(Window w) {
  35. super();
  36. win=w;
  37. initialize();
  38. }
  39.  
  40. public static ID3 getInstance() {
  41. if (instance == null) {
  42. //instance = new ID3();
  43. }
  44. return instance;
  45. }
  46.  
  47. private void initialize() {
  48.  
  49. try {
  50. readData();
  51. createArbre();
  52. afficherArbre();
  53. } catch (Exception e1) {
  54. e1.printStackTrace();
  55. }
  56.  
  57. }
  58.  
  59. /**
  60. * met la machine en route, cette methode est appelée à chaque changement de selection de fichier d'entrée
  61. * @param fichier
  62. * @throws Exception
  63. */
  64. /**
  65. * Cette fonction renvoie la valeur numérique correspondant
  66. * à la valeur de l'attribut (string)
  67. * @param attribut
  68. * @param value
  69. * @return
  70. */
  71. public int valeurAttribut(int attribut, String value) {
  72. int valeur = valeurAttributs[attribut].indexOf(value); //renvoie l'index de l'objet dans le vecteur (-1 si inexistant)
  73. if (valeur < 0) //Value est inexistante dans le vecteur
  74. {
  75. valeurAttributs[attribut].addElement(value);
  76. valeur = valeurAttributs[attribut].size() - 1;
  77. }
  78. return valeur;
  79. }
  80.  
  81. /**
  82. * Cette fonction ouvre le fichier contenant les exemples et les intègre au programme
  83. * @param nomFichier
  84. * @throws Exception
  85. */
  86. public void readData() throws Exception {
  87. Exemples = new Vector();
  88. nbExemples = 0;
  89. nbAttributs = 0;
  90.  
  91. nbAttributs = win.get_nb_att();
  92. nbExemples = win.get_nb_exp();
  93.  
  94. nomAttributs = new String[nbAttributs];
  95. for (int i = 0; i < nbAttributs; i++) {
  96. nomAttributs[i] = (String) win.tab_appr.getValueAt(0, i+1); //nomAttributs contient le nom de tous les attributs + le comcept cible
  97. }
  98. for (int i = 0; i < nbAttributs; i++)
  99. System.out.println("nom d'attribut"+i+"est"+nomAttributs[i]);
  100.  
  101. valeurAttributs = new Vector[nbAttributs];
  102. for (int i = 0; i < nbAttributs; i++) {
  103. valeurAttributs[i] = new Vector();
  104. }
  105.  
  106. /*
  107. * recupération des données
  108. */
  109. int[] exemple;
  110. //while(true)
  111. //{
  112. exemple = new int[nbAttributs];
  113. String value;
  114.  
  115. for (int i = 1; i < nbExemples; i++) {
  116. for (int j = 0; j < nbAttributs; j++) {
  117. value = (String) win.tab_appr.getValueAt(i, j+1);
  118. exemple[j] = valeurAttribut(j, value);
  119. System.out.println("exemple"+j+"est"+exemple[j]);
  120. }
  121. Exemples.add(exemple);
  122. }
  123. for (int i = 0; i < nbAttributs; i++)
  124. System.out.println("valeur attribut"+i+"est+"+valeurAttributs[i]);
  125.  
  126.  
  127. //}
  128.  
  129. }
  130.  
  131. /**
  132. * c'est comme le Port-Salut
  133. *
  134. */
  135. public void createArbre() {
  136. root = new Noeud(valeurAttributs, Exemples, nbAttributs, nomAttributs);
  137. root.generationArbre();
  138. }
  139.  
  140. /**
  141. * c'est comme le Port-Salut
  142. *
  143. */
  144. public void afficherArbre() {
  145. if (arbre != null) {
  146. jPanel.remove(arbre);
  147. }
  148. DefaultMutableTreeNode racine = root.generationAffichage();
  149. arbre = new JTree(racine);
  150. getContentPane().add(arbre,BorderLayout.CENTER);
  151. JScrollPane myScrollpane = new JScrollPane(arbre);
  152.  
  153. this.setVisible(true);
  154.  
  155. }
  156.  
  157. /**
  158. * This method initializes jPanel
  159. *
  160. * @return javax.swing.JPanel
  161. */
  162. private JPanel getJPanel() {
  163. if (jPanel == null) {
  164. jPanel = new JPanel();
  165. jPanel.setLayout(new BorderLayout());
  166.  
  167. }
  168. return jPanel;
  169. }
  170. }
  171.  
  172.  
  173. [b]Noeud.java[/b]
  174.  
  175. package test1;
  176.  
  177. import java.util.Vector;
  178.  
  179. import javax.swing.tree.DefaultMutableTreeNode;
  180.  
  181. public class Noeud {
  182.  
  183. private int attribut = -1; // -1 si les fils ont des valeurs differentes
  184. private int valeur = -1; // valeur numérique de l'attribut père, qui enendre ce noeud
  185. private Noeud pere = null; // c'est comme le Port-Salut
  186. private Noeud[] fils = null; // c'est comme le Port-Salut
  187. private String nomAttributs[];
  188. private int nbAttributs = 0;
  189. private Vector valeurAttributs[];
  190. private Vector Exemples = new Vector(); // contient les sous-exemples des exemples du noeud père
  191. // correspondant à la valeur qui engendra ce noeud
  192. private int etiquette = -1; // -1 il ne s'agit pas d'une feuille
  193. // sinon c'est une feuille et etiquette représente la valeur du concept cible
  194.  
  195. /**
  196. *
  197. * @param attributs2
  198. * @param exemples
  199. * @param attributs
  200. * @param exemples2
  201. */
  202. public Noeud(Vector[] attributs2, Vector exemples, int attributs, String[] nomAttributs) {
  203. super();
  204. Exemples = exemples;
  205. nbAttributs = attributs;
  206. valeurAttributs = attributs2;
  207. this.nomAttributs = nomAttributs;
  208. }
  209.  
  210. public DefaultMutableTreeNode generationAffichage() {
  211. if (pere != null && etiquette != -1) // feuille
  212. {
  213. noeud = new DefaultMutableTreeNode(valeurAttributs[pere.attribut].elementAt(valeur) + " : " +
  214. nomAttributs[nbAttributs - 1] + " = " +
  215. valeurAttributs[attribut].elementAt(etiquette));
  216. return noeud;
  217. }
  218. if (pere != null && etiquette == -1) // noeud central
  219. {
  220. noeud = new DefaultMutableTreeNode(valeurAttributs[pere.attribut].elementAt(valeur) + " : " +
  221. nomAttributs[attribut]);
  222. for (int i = 0; i < fils.length; i++) {
  223. if (fils[i] != null) {
  224. noeud.add(fils[i].generationAffichage());
  225. }
  226. }
  227. return noeud;
  228. }
  229. if (pere == null && etiquette == -1) //racine
  230. {
  231. noeud = new DefaultMutableTreeNode(nomAttributs[attribut]);
  232. for (int i = 0; i < fils.length; i++) {
  233. if (fils[i] != null) {
  234. noeud.add(fils[i].generationAffichage());
  235. }
  236. }
  237. return noeud;
  238. }
  239. return noeud;
  240. }
  241.  
  242. public void complementNoeud(int valeur, Noeud pere) {
  243. this.valeur = valeur;
  244. this.pere = pere;
  245. }
  246.  
  247. public void generationArbre() {
  248. Vector sousExemle;
  249. attribut = attributGanant();
  250. if (attribut != -1) {
  251. fils = new Noeud[valeurAttributs[attribut].size()];
  252. for (int i = 0; i < valeurAttributs[attribut].size(); i++) {
  253. sousExemle = sousExemple(attribut, i);
  254. if (sousExemle.size() > 0) {
  255. fils[i] = new Noeud(valeurAttributs, sousExemle, nbAttributs, nomAttributs);
  256. fils[i].complementNoeud(i, this);
  257. fils[i].generationArbre();
  258. }
  259. }
  260.  
  261. /*
  262. * à partir d'ici les fils sont formés, on regarde s'ils ont la même étique
  263. * si oui alors ce noeud peux devenir une feuille
  264. */
  265. int previous = -1;
  266. for (int i = 0; i < fils.length; i++) {
  267. if (fils[i] != null && previous == -1) // on recherche la premiere etiquette valide
  268. {
  269. previous = fils[i].etiquette;
  270. }
  271. if (previous != -1 && fils[i] != null && fils[i].etiquette != previous) // on verifie si les noeuds inferieurs ont la même etiquette
  272. {
  273. return;
  274. }
  275. }
  276. if (previous == -1) {
  277. return;
  278. }
  279. //si on arrive jusqu'ici c'est que les noeuds inferieur ne servent à rien, donc on les virre
  280. attribut = nbAttributs - 1;
  281. etiquette = previous;
  282. fils = null;
  283. } else // feuille
  284. {
  285. attribut = nbAttributs - 1;
  286. etiquette = ((int[]) Exemples.elementAt(0))[attribut];
  287. }
  288.  
  289. }
  290.  
  291. /**
  292. * Donne un sous ensemble d'exemple pour un attribut et une valeur de cette attribut
  293. * @param pattribut
  294. * @param pvaleur
  295. * @return
  296. */
  297. public Vector sousExemple(int pattribut, int pvaleur) {
  298. Vector sousExemples = new Vector();
  299. int num = Exemples.size();
  300. for (int i = 0; i < num; i++) {
  301. int[] exemple = (int[]) Exemples.elementAt(i);
  302. if (exemple[pattribut] == pvaleur) {
  303. sousExemples.addElement(exemple);
  304. }
  305. }
  306. return sousExemples;
  307. }
  308.  
  309. /**
  310. * Determine quel attribut doit figurer dans ce noeud
  311. * @return
  312. */
  313. public int attributGanant() {
  314. double gain[] = new double[nbAttributs - 1]; //[nbAttributs-1];
  315. int attribut = -1;
  316. double max = -1;
  317. Vector attributeValues = new Vector();
  318. for (int i = 0; i < nbAttributs - 1; i++) {
  319. attributeValues.removeAllElements();
  320. for (int j = 0; j < Exemples.size(); j++) {
  321. attributeValues.addElement(new Integer(((int[]) Exemples.elementAt(j))[i]));
  322. }
  323. gain[i] = gainAttribut(attributeValues);
  324. }
  325. for (int i = 0; i < gain.length; i++) {
  326. if (gain[i] > max) {
  327. max = gain[i];
  328. attribut = i;
  329. }
  330. }
  331. if (max == 0) {
  332. return -1;
  333. }
  334. return attribut;
  335. }
  336.  
  337. /**
  338. * donne le gain d'un vecteur
  339. * @param data
  340. * @return
  341. */
  342. public double gainAttribut(Vector data) {
  343. double gain = 0;
  344. double max = data.size();
  345. int nbApparitionTotal = 0;
  346. int i = 0;
  347. double nbApparition = 0;
  348. double proba = 0;
  349. while (nbApparitionTotal < max) {
  350. nbApparition = 0;
  351. for (int j = 0; j < data.size(); j++) {
  352. if (((Integer) data.elementAt(j)).intValue() == i) {
  353. nbApparition++;
  354. nbApparitionTotal++;
  355. }
  356. }
  357. proba = nbApparition / max;
  358. gain += -(proba) * Math.log(proba);
  359. i++;
  360. }
  361. return gain;
  362. }
  363.  
  364. public int getAttribut() {
  365. return attribut;
  366. }
  367.  
  368. public Noeud[] getFils() {
  369. return fils;
  370. }
  371.  
  372. public Noeud getPere() {
  373. return pere;
  374. }
  375.  
  376. public int getValeur() {
  377. return valeur;
  378. }
  379. }
 

[i]Merci de m'aider

 

Édité par Elmoricq : ajout des balises [code] pour plus de lisibilité.


Message édité par Elmoricq le 14-04-2009 à 13:22:27
mood
Publicité
Posté le 14-04-2009 à 01:06:01  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Algorithme d'arbre de decision:Jtree non affiché!Aidez moi svp

 

Sujets relatifs
creer un contenuer, il ne s'affiche pasQuestion bête Aidez moi je vais m'ouvrir les veines sinon
arbre généalogiquealgorithme de mesure d'harmoniques d'un signal de fréquence connu
Algorithme Crible d'Ératosthène en distribué (application réparti)[C] Algorithme de Dijkstra
un exercice en c . aidez moi , c urgent!! merciAlgorithme
Algorithme création géométrie à partir de surfaces[sql]Requête qui affiche les doublons à partir d'un critère
Plus de sujets relatifs à : Algorithme d'arbre de decision:Jtree non affiché!Aidez moi svp


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR