Search This Blog

Wednesday, 22 March 2017

Implement Candidate Elimination Algorithm in JAVA

Program:
package k;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class kl {
                static int sg = 0;


                public static void main(String[] args) {
                                List<Map<String, String>> ListOf_S = new ArrayList<Map<String, String>>();
                                List<List<Map<String, String>>> ListOf_G = new ArrayList<List<Map<String, String>>>();

                                String fileName = "D:\\gul\\demo\\src\\demo\\EnjoySport.txt";
                                String G_String = "";
                                String S_String = "";

                                String line = null;
                                try {
                                                FileReader fileReader = new FileReader(fileName);
                                                BufferedReader bufferedReader = new BufferedReader(fileReader);
                                                String line_Atrributes_name = bufferedReader.readLine();
                                                String[] words_attribute_name = line_Atrributes_name.split(",");
                                                Map<String, String> S = new HashMap<String, String>();

                                                for (int i = 0; i < words_attribute_name.length; i++) {
                                                                S.put(words_attribute_name[i], "0");
                                                }
                                                ListOf_S.add(sg, S);
                                                S = ListOf_S.get(sg);

                                                Map<String, String> G = new HashMap<String, String>();
                                                for (int i = 0; i < words_attribute_name.length; i++) {
                                                                G.put(words_attribute_name[i], "?");
                                                }
                                                List<Map<String, String>> lg = new ArrayList<Map<String, String>>();
                                                lg.add(G);
                                                G.clear();

                                                ListOf_G.add(sg, lg);
                                                lg.clear();
                                                Map<String, String> d = new HashMap<String, String>();
                                                for (int i = 0; i < words_attribute_name.length; i++) {
                                                                d.put(words_attribute_name[i], "");
                                                }
                                                while ((line = bufferedReader.readLine()) != null)
{
                                                String words[] = line.split(",");
                                                d.put(words_attribute_name[words.length - 1], words[words.length - 1]);
                                                for (int i = 0; i < words.length - 1; i++)
 {
                                                 d.put(words_attribute_name[i], words[i]);
                                 if (d.get(words_attribute_name[words.length - 1]).equalsIgnoreCase("yes"))
 {
                                                if (ListOf_S.get(sg).get(words_attribute_name[i]).equalsIgnoreCase("0"))
{
                                                S.put(words_attribute_name[i], words[i]);
                                                }
else if (!ListOf_S.get(sg).get(words_attribute_name[i]).equalsIgnoreCase(words[i]))
{
                                                S.put(words_attribute_name[i], "?");
                                                }
                                                }
else if (d.get(words_attribute_name[words.length - 1]).equalsIgnoreCase("no"))
{
if (ListOf_S.get(sg).get(words_attribute_name[i]).equalsIgnoreCase(words[i]) ||                                                                                                                                              ListOf_S.get(sg).get(words_attribute_name[i]).equalsIgnoreCase("?"))
{
                                                G.clear();
                                                }
else
{
                                                for (Map.Entry<String, String> entry : ListOf_S.get(sg).entrySet())
{
                                                G.put(entry.getKey(), "?");
                                                }
                                                G.put(words_attribute_name[i], ListOf_S.get(sg).get(words_attribute_name[i]));
                                                }
                                                if (!G.isEmpty())
{
                                                lg.add(G);
                                                }
                                                }
                                                if (d.get(words_attribute_name[words.length - 1]).equalsIgnoreCase("no"))
{
                                                for (int j = 0; j < words.length - 1; j++)
 {
                                                if (!G.isEmpty())
{
                                                if (j == 0)
{
                                                if (G_String.startsWith("["))
{
                                                                G_String = G_String + "[";
                                                } else {
                                                                G_String = "[" + G_String;
                                                }
                                                }
                G_String = G_String + " " + words_attribute_name[j] + "=>>"+ G.get(words_attribute_name[j]);
                }
                if (j == words.length - 2)
{
                if (!(G_String.endsWith("]")))
{
                                G_String = G_String + "]";
                }
}
}
G.clear();
}
}
if (d.get(words_attribute_name[words.length - 1]).equalsIgnoreCase("yes"))
{
for (int i = 0; i < words.length - 1; i++)
{
                S_String = S_String + " " + words_attribute_name[i] + "=" + S.get(words_attribute_name[i]);
                }
                }
                if (!(S_String.equalsIgnoreCase("")))
{
                                System.out.println("S=" + "{" + S_String + "}");
                                S_String = "";
                }
                if (!(G_String.equalsIgnoreCase("")))
{
                                System.out.println("G=" + "{" + G_String + "}");
                                G_String = "";
                }
}
sg++;
if (S.isEmpty())
{
                ListOf_S.add(sg, ListOf_S.get(sg - 1));
} else {
                ListOf_S.add(sg, S);
                S.clear();
}
if (lg.isEmpty())
{
ListOf_G.add(sg, ListOf_G.get(sg - 1));
} else {
                ListOf_G.add(sg, lg);
                lg.clear();
}
bufferedReader.close();
}catch (FileNotFoundException ex)
{
                System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex)
{
System.out.println("Error reading file '" + fileName + "'");
}
}
}



Output:

S={ sunny=sunny warm=warm normal=high strong=strong warm=warm same=same}
G={[ sunny=>>sunny warm=>>? normal=>>? strong=>>? warm=>>? same=>>?][ sunny=>>? warm=>>warm normal=>>? strong=>>? warm=>>warm same=>>?][ sunny=>>? warm=>>? normal=>>? strong=>>? warm=>>? same=>>same]}
S={ sunny=sunny warm=? normal=high strong=strong warm=? same=?}

No comments:

Post a Comment