first commit
This commit is contained in:
commit
9508dda2a3
36
.classpath
Executable file
36
.classpath
Executable file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/classes" path="src">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.settings/
|
||||
.idea/
|
||||
.vscode/
|
||||
target/
|
23
.project
Executable file
23
.project
Executable file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>DecisionTree-ID3</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
38
ID3.iml
Executable file
38
ID3.iml
Executable file
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/16.0.2/annotations-16.0.2.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$USER_HOME$/Downloads/jocl-2.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar:///usr/share/java/mysql-connector-java.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
308
SplitDataSet.cl
Executable file
308
SplitDataSet.cl
Executable file
@ -0,0 +1,308 @@
|
||||
__kernel void splitDataSet(__global char *in, __global int *out, const int rowSpan,
|
||||
const int colSpan, __global char *pat, const int pat_length, __global char* out_char,
|
||||
__global char* nullChar)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan);
|
||||
int end = start + rowSpan;
|
||||
int N = rowSpan;
|
||||
int M = pat_length;
|
||||
bool found = false;
|
||||
char nullCharTemp = nullChar[0];
|
||||
if(in[start]!=nullCharTemp)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (in[k + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
out[idx] = 1;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(found)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
out_char[k] = in[k];
|
||||
}
|
||||
}
|
||||
if(!found)
|
||||
{
|
||||
out[idx] = 0;
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
out_char[k] = nullCharTemp;
|
||||
}
|
||||
|
||||
//out_char[start] = ' ';
|
||||
|
||||
}
|
||||
}
|
||||
__kernel void sumReduction(__global int *A, __global int* C, int offset)
|
||||
{
|
||||
int global_id = get_global_id(0);
|
||||
//if(get_global_size(0) < 5)
|
||||
// printf("Thread id = %d", global_id);
|
||||
int start = global_id * offset;
|
||||
int end = start + offset;
|
||||
int i;
|
||||
for(i=start; i<end; i++)
|
||||
{
|
||||
C[global_id] += A[i];
|
||||
}
|
||||
}
|
||||
|
||||
__kernel void foldKernel(__global int *arVal, int offset)
|
||||
{
|
||||
int gid = get_global_id(0);
|
||||
arVal[gid] = arVal[gid]+arVal[gid+offset];
|
||||
}
|
||||
|
||||
__kernel void allOneValues(__global char* examples, __global int* yesArray, __global int* noArray, const int rowSpan,
|
||||
__global char* yes, __global char* no, __global char* nullChar)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan);
|
||||
int end = start + rowSpan;
|
||||
int N = rowSpan;
|
||||
int M = 16;
|
||||
bool found = false;
|
||||
|
||||
|
||||
char nullCharTemp = nullChar[0];
|
||||
if(examples[start]!=nullCharTemp)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examples[k + j] != yes[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(found)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
//out_char[k] = in[k];
|
||||
}
|
||||
//printf("Found at %d", idx);
|
||||
yesArray[idx] = 1;
|
||||
noArray[idx] = 0;
|
||||
}
|
||||
if(!found)
|
||||
{
|
||||
//out[idx] = 0;
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
//out_char[k] = nullCharTemp;
|
||||
}
|
||||
|
||||
//out_char[start] = ' ';
|
||||
|
||||
yesArray[idx] = 0;
|
||||
noArray[idx] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
__kernel void splitDataSetNew(__global char *examplesChar, __global int* examplesInt,
|
||||
const int rowSpan, __global char *pat,
|
||||
const int pat_length, __global int* examplesViInt)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan);
|
||||
int end = start + rowSpan;
|
||||
int N = rowSpan;
|
||||
int M = pat_length;
|
||||
bool found = false;
|
||||
if(examplesInt[idx] == 1)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examplesChar[k + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(found)
|
||||
{
|
||||
examplesViInt[idx] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
examplesViInt[idx] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
__kernel void splitDataSet3(__global char *examplesChar, __global int* examplesInt,
|
||||
const int rowSpan, const int cellSpan, const int index, __global char *pat,
|
||||
const int pat_length, __global int* examplesViInt)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan + (2*index+1)*cellSpan);
|
||||
int end = start + cellSpan;
|
||||
//int N = rowSpan;
|
||||
int M = pat_length;
|
||||
bool found = false;
|
||||
|
||||
if(examplesInt[idx] == 1)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examplesChar[k + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
found = true;
|
||||
examplesViInt[idx] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!found)
|
||||
{
|
||||
examplesViInt[idx] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
__kernel void splitDataSet4(__global char *examplesChar, __global int* examplesInt,
|
||||
const int rowSpan, const int cellSpan, const int index, __global char *pat,
|
||||
const int pat_length,
|
||||
__global char* yes, __global int* yesArray,
|
||||
__global int* noArray, int targetIndex)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan + (2*index+1)*cellSpan);
|
||||
int end = start + cellSpan;
|
||||
//int N = rowSpan;
|
||||
int M = pat_length;
|
||||
bool found = false;
|
||||
|
||||
if(examplesInt[idx] == 1)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examplesChar[k + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
found = true;
|
||||
//examplesViInt[idx] = 1;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(found)
|
||||
{
|
||||
int start = (idx * rowSpan + (2*targetIndex+1)*cellSpan);
|
||||
int end = start + cellSpan;
|
||||
int M = 6;
|
||||
bool yesFound = false;
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examplesChar[k + j] != yes[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
//found = true;
|
||||
//examplesViInt[idx] = 1;
|
||||
yesArray[idx] = 1;
|
||||
noArray[idx] = 0;
|
||||
yesFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!yesFound){
|
||||
noArray[idx] = 1;
|
||||
yesArray[idx] = 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
yesArray[idx] = 0;
|
||||
noArray[idx] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
__kernel void getCount(__global char *examplesChar, __global int* examplesInt,
|
||||
const int rowSpan, const int cellSpan, const int index, __global char *pat,
|
||||
const int pat_length, __global int* countArray)
|
||||
{
|
||||
const int idx = get_global_id(0);
|
||||
int start = (idx * rowSpan + (2*index+1)*cellSpan);
|
||||
int end = start + cellSpan;
|
||||
//int N = rowSpan;
|
||||
int M = pat_length;
|
||||
bool found = false;
|
||||
if(examplesInt[idx] == 1)
|
||||
{
|
||||
for(int k=start;k<end;k++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (examplesChar[k + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
//out[idx] = 1;
|
||||
found = true;
|
||||
countArray[idx] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!found)
|
||||
{
|
||||
countArray[idx] = 0;
|
||||
}
|
||||
}
|
13
SumReduction.cl
Executable file
13
SumReduction.cl
Executable file
@ -0,0 +1,13 @@
|
||||
__kernel void sumReduction(__global int *A, __global int* C, int offset)
|
||||
{
|
||||
int global_id = get_global_id(0);
|
||||
//if(get_global_size(0) < 5)
|
||||
// printf("Thread id = %d", global_id);
|
||||
int start = global_id * offset;
|
||||
int end = start + offset;
|
||||
int i;
|
||||
for(i=start; i<end; i++)
|
||||
{
|
||||
C[global_id] += A[i];
|
||||
}
|
||||
}
|
37
pom.xml
Executable file
37
pom.xml
Executable file
@ -0,0 +1,37 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.nova</groupId>
|
||||
<artifactId>id3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>ID3</name>
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<release>11</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>6.0.6</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.jocl/jocl -->
|
||||
<dependency>
|
||||
<groupId>org.jocl</groupId>
|
||||
<artifactId>jocl</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
5
programSource.cl
Executable file
5
programSource.cl
Executable file
@ -0,0 +1,5 @@
|
||||
__kernel void sampleKernel(__global const float *a, __global const float* b, __global float* c)
|
||||
{
|
||||
int gid = get_global_id(0);
|
||||
c[gid] = a[gid]+b[gid];
|
||||
}
|
7
properties.txt
Executable file
7
properties.txt
Executable file
@ -0,0 +1,7 @@
|
||||
display=select * from Adult2
|
||||
insert=insert into Climate values (?,?,?,?,?)
|
||||
insertTree = insert into DecisionTree(Parent, Edge, Child) values (?,?,?)
|
||||
resetAutoIncrement = alter table DecisionTree AUTO_INCREMENT = 0
|
||||
deleteAll = delete from DecisionTree
|
||||
delete= delete from Climate where Day = ?
|
||||
update= update Climate set Play=? where Day=?
|
44
src/com/AI/Attribute.java
Executable file
44
src/com/AI/Attribute.java
Executable file
@ -0,0 +1,44 @@
|
||||
package com.AI;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
class Attribute
|
||||
{
|
||||
private String attributeName;
|
||||
private int attributeIndex;
|
||||
private Set<String> valuesSet;
|
||||
|
||||
void addToValuesSet(String value)
|
||||
{
|
||||
valuesSet.add(value);
|
||||
}
|
||||
|
||||
public Attribute()
|
||||
{
|
||||
valuesSet = new HashSet<>();
|
||||
}
|
||||
|
||||
public Set<String> getValuesSet() {
|
||||
return valuesSet;
|
||||
}
|
||||
|
||||
public void setValuesSet(Set<String> valuesSet) {
|
||||
this.valuesSet = valuesSet;
|
||||
}
|
||||
|
||||
public String getAttributeName() {
|
||||
return attributeName;
|
||||
}
|
||||
|
||||
public void setAttributeName(String attributeName) {
|
||||
this.attributeName = attributeName;
|
||||
}
|
||||
|
||||
public int getAttributeIndex() {
|
||||
return attributeIndex;
|
||||
}
|
||||
|
||||
public void setAttributeIndex(int attributeIndex) {
|
||||
this.attributeIndex = attributeIndex;
|
||||
}
|
||||
}
|
727
src/com/AI/ID3.java
Executable file
727
src/com/AI/ID3.java
Executable file
@ -0,0 +1,727 @@
|
||||
package com.AI;
|
||||
|
||||
import java.util.*;
|
||||
class ID3
|
||||
{
|
||||
private ArrayList<Attribute> attributesList;
|
||||
private Set<String> attributesSet;
|
||||
private double P, N;
|
||||
private String modeOfTargetAttribute;
|
||||
private MyJOCL myJocl;
|
||||
private Attribute targetAttribute;
|
||||
private String modeOfCurrentAttribute;
|
||||
int examplesSize;
|
||||
int maxLength;
|
||||
int cellSpan;
|
||||
|
||||
ID3()
|
||||
{
|
||||
attributesList = new ArrayList<>();
|
||||
attributesSet = new LinkedHashSet<>();
|
||||
targetAttribute = new Attribute();
|
||||
P = 1;
|
||||
N = 1;
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getAttributesSet() {
|
||||
return attributesSet;
|
||||
}
|
||||
|
||||
public void setAttributesSet(Set<String> attributesSet) {
|
||||
this.attributesSet = attributesSet;
|
||||
}
|
||||
|
||||
public double getP() {
|
||||
return P;
|
||||
}
|
||||
|
||||
public void setP(double p) {
|
||||
this.P = p;
|
||||
}
|
||||
|
||||
public double getN() {
|
||||
return N;
|
||||
}
|
||||
|
||||
public void setN(double n) {
|
||||
this.N = n;
|
||||
}
|
||||
|
||||
public ArrayList<Attribute> getAttributesList() {
|
||||
return attributesList;
|
||||
}
|
||||
|
||||
public void setAttributesList(ArrayList<Attribute> attribute) {
|
||||
this.attributesList = attribute;
|
||||
}
|
||||
|
||||
public Attribute getTargetAttribute() {
|
||||
return targetAttribute;
|
||||
}
|
||||
|
||||
public void setTargetAttribute(Attribute targetAttribute) {
|
||||
this.targetAttribute = targetAttribute;
|
||||
}
|
||||
|
||||
public String getModeOfTargetAttribute() {
|
||||
return modeOfTargetAttribute;
|
||||
}
|
||||
|
||||
public void setModeOfTargetAttribute(String modeOfTargetAttribute) {
|
||||
this.modeOfTargetAttribute = modeOfTargetAttribute;
|
||||
}
|
||||
|
||||
public int getExamplesSize() {
|
||||
return examplesSize;
|
||||
}
|
||||
|
||||
public void setExamplesSize(int examplesSize) {
|
||||
this.examplesSize = examplesSize;
|
||||
}
|
||||
|
||||
public int getMaxLength() {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
public void setMaxLength(int maxLength) {
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
|
||||
public int getCellSpan() {
|
||||
return cellSpan;
|
||||
}
|
||||
|
||||
public void setCellSpan(int cellSpan) {
|
||||
this.cellSpan = cellSpan;
|
||||
}
|
||||
|
||||
public MyJOCL getMyJocl() {
|
||||
return myJocl;
|
||||
}
|
||||
|
||||
public void setMyJocl(MyJOCL myJocl) {
|
||||
this.myJocl = myJocl;
|
||||
}
|
||||
|
||||
Set<String> getUniqueAttributes(String targetAttribute, String indexAttribute, ArrayList<Row> examples)
|
||||
{
|
||||
attributesSet = new LinkedHashSet<>();
|
||||
Row row = examples.get(0);
|
||||
for (Cell cell : row.getCellsList()) {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + " ");
|
||||
if (cell.getAttributeName().equals(targetAttribute) ||
|
||||
cell.getAttributeName().equalsIgnoreCase(indexAttribute))
|
||||
continue;
|
||||
attributesSet.add(cell.getAttributeName());
|
||||
}
|
||||
this.targetAttribute.setAttributeName(targetAttribute);
|
||||
return attributesSet;
|
||||
}
|
||||
|
||||
void setAttributesNew(ArrayList<Row> examples)
|
||||
{
|
||||
attributesList = new ArrayList<>();
|
||||
Attribute attribute;
|
||||
for (String s : attributesSet) {
|
||||
attribute = new Attribute();
|
||||
attribute.setAttributeName(s);
|
||||
for (Row row :examples) {
|
||||
for (Cell cell : row.getCellsList()) {
|
||||
if(cell.getAttributeName().equalsIgnoreCase(s))
|
||||
attribute.addToValuesSet(cell.getValue());
|
||||
}
|
||||
}
|
||||
attributesList.add(attribute);
|
||||
}
|
||||
}
|
||||
|
||||
void setAttributesIndex(List<Attribute> attributesList)
|
||||
{
|
||||
int i = 1;
|
||||
for(Attribute a: attributesList)
|
||||
{
|
||||
a.setAttributeIndex(i++);
|
||||
}
|
||||
targetAttribute.setAttributeIndex(i);
|
||||
}
|
||||
void displayAttributesList(List<Attribute> attributeList)
|
||||
{
|
||||
for(Attribute a: attributeList)
|
||||
{
|
||||
System.out.println("Attribute: " + a.getAttributeName());
|
||||
for(String value: a.getValuesSet())
|
||||
{
|
||||
System.out.print("Value: " + value);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
static double calcEntropy(double p, double n) {
|
||||
if (p == 0 || n == 0)
|
||||
return 0;
|
||||
else
|
||||
return ((-p / (p + n)) * Logarithms.log2(p / (p + n))) - ((n / (p + n)) * Logarithms.log2(n / (p + n)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Node createDecisionTree(NaryTree tree, Set<String> attributesSet, ArrayList<Row> examples) {
|
||||
|
||||
|
||||
String targetAttributeValue = allOneValues(examples);
|
||||
if(targetAttributeValue.equalsIgnoreCase("Yes"))
|
||||
return new Node("Yes");
|
||||
else if(targetAttributeValue.equalsIgnoreCase("No"))
|
||||
return new Node("No");
|
||||
if (attributesSet.isEmpty()) {
|
||||
return new Node(modeOfCurrentAttribute);
|
||||
}
|
||||
Node node;
|
||||
String attributeName;
|
||||
attributeName = getBestAttribute(examples,attributesSet);
|
||||
|
||||
Attribute attribute = new Attribute();
|
||||
|
||||
for (Attribute a : getAttributesList()) {
|
||||
if (a.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
attribute = a;
|
||||
}
|
||||
|
||||
|
||||
Set<String> uniqueValues = attribute.getValuesSet();
|
||||
|
||||
node = new Node(attributeName);
|
||||
|
||||
// node.setAttributeName(attributeName);
|
||||
if (tree.getHead() == null) {
|
||||
tree.setHead(node);
|
||||
|
||||
}
|
||||
|
||||
for (String s : uniqueValues) {
|
||||
// System.out.println("For value: " + s);
|
||||
ArrayList<Row> examplesVi = splitDataSet(examples,s,attributeName);
|
||||
|
||||
// System.out.println("Displaying examplesVi");
|
||||
// for(Row row: examplesVi)
|
||||
// {
|
||||
// for(Cell cell : row.getCellsList())
|
||||
// {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
|
||||
if(examplesVi.isEmpty())
|
||||
{
|
||||
tree.createNode(null,node,s,new Node(modeOfTargetAttribute));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (Iterator<String> it = attributesSet.iterator(); it.hasNext(); )
|
||||
{
|
||||
String str = it.next();
|
||||
if (str.equalsIgnoreCase(attributeName))
|
||||
{
|
||||
System.out.println("Removed: " + str);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
Node temp = createDecisionTree(tree, attributesSet, examplesVi);
|
||||
tree.createNode(null, node, s, temp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void setModeAndPN(ArrayList<Row> examples)
|
||||
{
|
||||
int yesCounter = 0, noCounter = 0;
|
||||
for(Row row: examples)
|
||||
{
|
||||
for(Cell cell: row.getCellsList())
|
||||
{
|
||||
if(cell.getValue().equalsIgnoreCase("Yes"))
|
||||
yesCounter++;
|
||||
else if(cell.getValue().equalsIgnoreCase("No"))
|
||||
noCounter++;
|
||||
}
|
||||
}
|
||||
this.P = yesCounter;
|
||||
this.N = noCounter;
|
||||
if(P > N)
|
||||
modeOfTargetAttribute = "Yes";
|
||||
else
|
||||
modeOfTargetAttribute = "No";
|
||||
|
||||
System.out.println("Most common value: " + modeOfTargetAttribute);
|
||||
}
|
||||
double getClassEntropy(ArrayList<Row> examples)
|
||||
{
|
||||
int yesCounter = 0, noCounter = 0;
|
||||
double classEntropy;
|
||||
for(Row row: examples)
|
||||
{
|
||||
for(Cell cell: row.getCellsList())
|
||||
{
|
||||
if(cell.getValue().equalsIgnoreCase("Yes"))
|
||||
yesCounter++;
|
||||
else if(cell.getValue().equalsIgnoreCase("No"))
|
||||
noCounter++;
|
||||
}
|
||||
}
|
||||
P = yesCounter;
|
||||
N = noCounter;
|
||||
classEntropy = calcEntropy(yesCounter,noCounter);
|
||||
return classEntropy;
|
||||
}
|
||||
|
||||
String getBestAttribute(ArrayList<Row> examples, Set<String> attributesSet)
|
||||
{
|
||||
if(attributesSet.isEmpty())
|
||||
return "Attributes set is empty";
|
||||
double classEntropy = getClassEntropy(examples);
|
||||
System.out.println("Class Entropy = " + classEntropy);
|
||||
double attributeEntropy, maxGain=0, valueEntropy=0, gain = 0;
|
||||
int p, n;
|
||||
String bestAttribute = "";
|
||||
Set<String> uniqueValues;
|
||||
|
||||
for(String attributeName: attributesSet)
|
||||
{
|
||||
attributeEntropy =0;
|
||||
|
||||
Attribute attribute = new Attribute();
|
||||
for (Attribute a : getAttributesList()) {
|
||||
if (a.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
attribute = a;
|
||||
}
|
||||
|
||||
int attributeIndex = attribute.getAttributeIndex();
|
||||
int targetAttributeIntex = targetAttribute.getAttributeIndex();
|
||||
uniqueValues = attribute.getValuesSet();
|
||||
|
||||
for(String value: uniqueValues) {
|
||||
p=0;
|
||||
n=0;
|
||||
for (Row row : examples) {
|
||||
// boolean flag = false;
|
||||
// for (Cell cell : row.getCellsList()) {
|
||||
// if (cell.getValue().equalsIgnoreCase(value) && cell.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
// flag= true;
|
||||
// }
|
||||
// for (Cell cell : row.getCellsList()) {
|
||||
// if(flag && cell.getValue().equalsIgnoreCase("Yes"))
|
||||
// p++;
|
||||
// else if(flag && cell.getValue().equalsIgnoreCase("No"))
|
||||
// n++;
|
||||
// }
|
||||
Cell cell = row.getCellsList().get(attributeIndex);
|
||||
Cell targetCell = row.getCellsList().get(targetAttributeIntex);
|
||||
if (cell.getValue().equalsIgnoreCase(value) && targetCell.getValue().equalsIgnoreCase("Yes"))
|
||||
p++;
|
||||
else if(cell.getValue().equalsIgnoreCase(value) && targetCell.getValue().equalsIgnoreCase("No"))
|
||||
n++;
|
||||
//
|
||||
}
|
||||
|
||||
// System.out.println("P = " + P + " " + "N = " + N);
|
||||
valueEntropy = calcEntropy(p, n);
|
||||
// System.out.println("Value Entropy = " + valueEntropy);
|
||||
attributeEntropy = attributeEntropy + ((p + n) / (P + N)) * valueEntropy;
|
||||
|
||||
}
|
||||
System.out.println("Attribute Entropy" + "(" + attributeName + ") = " + attributeEntropy);
|
||||
gain = classEntropy - attributeEntropy;
|
||||
if(gain >= maxGain)
|
||||
{
|
||||
// System.out.println("1");
|
||||
maxGain = gain;
|
||||
bestAttribute = attributeName;
|
||||
}
|
||||
System.out.println("Gain = " + gain + " " + "Max Gain = " + maxGain);
|
||||
|
||||
}
|
||||
System.out.println("Best Attribute = " + bestAttribute);
|
||||
return bestAttribute;
|
||||
}
|
||||
|
||||
void test(ArrayList<Row> examples, Set<String> attributesSet)
|
||||
{
|
||||
double classEntropy = getClassEntropy(examples);
|
||||
String bestAttribute = getBestAttribute(examples,attributesSet);
|
||||
|
||||
System.out.println("Class Entropy = " + classEntropy + " Best Attribute = " + bestAttribute);
|
||||
}
|
||||
|
||||
ArrayList<Row> splitDataSet(ArrayList<Row> examples, String value, String attributeName)
|
||||
{
|
||||
ArrayList<Row> examplesVi = new ArrayList<>();
|
||||
int attributeIndex = targetAttribute.getAttributeIndex();
|
||||
|
||||
for (Row row : examples)
|
||||
{
|
||||
boolean flag = false;
|
||||
for (Cell cell : row.getCellsList())
|
||||
{
|
||||
if (cell.getValue().equalsIgnoreCase(value) && cell.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
flag= true;
|
||||
}
|
||||
// Cell cell = row.getCellsList().get(attributeIndex);
|
||||
// if (cell.getValue().equalsIgnoreCase(value) && cell.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
// examplesVi.add(row);
|
||||
|
||||
if(flag)
|
||||
examplesVi.add(row);
|
||||
}
|
||||
|
||||
return examplesVi;
|
||||
}
|
||||
|
||||
private String allOneValues(ArrayList<Row> examples)
|
||||
{
|
||||
int yesCount = 0;
|
||||
int noCount = 0;
|
||||
for (Row row : examples)
|
||||
{
|
||||
|
||||
for (Cell cell : row.getCellsList())
|
||||
{
|
||||
if(cell.getValue().equalsIgnoreCase("Yes"))
|
||||
yesCount++;
|
||||
else if(cell.getValue().equalsIgnoreCase("No"))
|
||||
noCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if(yesCount > noCount)
|
||||
modeOfCurrentAttribute = "Yes";
|
||||
else
|
||||
modeOfCurrentAttribute = "No";
|
||||
|
||||
if(yesCount == 0)
|
||||
return "No";
|
||||
else if(noCount == 0)
|
||||
return "Yes";
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
String allOneValuesParallel(int [] examplesInt, int examplesSize, int maxLength, int cellSpan)
|
||||
{
|
||||
|
||||
int yesCount = myJocl.getCount(examplesInt, examplesSize,maxLength,"Yes", cellSpan, targetAttribute);
|
||||
// System.out.println("P = " + yesCount);
|
||||
int noCount = myJocl.getCount(examplesInt, examplesSize,maxLength,"No", cellSpan, targetAttribute);
|
||||
// System.out.println("N = " + noCount);
|
||||
|
||||
if(yesCount > noCount)
|
||||
modeOfCurrentAttribute = "Yes";
|
||||
else
|
||||
modeOfCurrentAttribute = "No";
|
||||
|
||||
if(yesCount == 0)
|
||||
return "No";
|
||||
else if(noCount == 0)
|
||||
return "Yes";
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
double getClassEntropyParallel(int [] examplesInt, int examplesSize, int maxLength, int cellSpan)
|
||||
{
|
||||
double classEntropy;
|
||||
int yesCounter = myJocl.getCount(examplesInt, examplesSize,maxLength,"Yes", cellSpan, targetAttribute);
|
||||
int noCounter = myJocl.getCount(examplesInt, examplesSize,maxLength,"No", cellSpan, targetAttribute);
|
||||
P = yesCounter;
|
||||
N = noCounter;
|
||||
classEntropy = calcEntropy(yesCounter,noCounter);
|
||||
return classEntropy;
|
||||
}
|
||||
|
||||
int[] splitDataSetParallel(int[] examplesInt, String value, String attributeName, int examplesSize, int maxLength, int cellSpan)
|
||||
{
|
||||
// int[] examplesVi = new ArrayList<>();
|
||||
int attributeIndex = targetAttribute.getAttributeIndex();
|
||||
|
||||
ReturnObj returnObj = myJocl.testDataSetParallel(examplesInt, examplesSize, maxLength, cellSpan, attributeName, value, attributesList, targetAttribute);
|
||||
|
||||
return returnObj.getExamplesInt();
|
||||
}
|
||||
|
||||
String getBestAttributeParallel(Set<String> attributesSet,
|
||||
int[] examplesInt,int examplesSize, int maxLength, int cellSpan)
|
||||
{
|
||||
if(attributesSet.isEmpty())
|
||||
return "Attributes set is empty";
|
||||
double classEntropy = getClassEntropyParallel(examplesInt,examplesSize,maxLength,cellSpan);
|
||||
System.out.println("Class Entropy = " + classEntropy);
|
||||
double attributeEntropy, maxGain=0, valueEntropy=0, gain = 0;
|
||||
int p, n;
|
||||
String bestAttribute = "";
|
||||
Set<String> uniqueValues;
|
||||
for(String attributeName: attributesSet)
|
||||
{
|
||||
attributeEntropy =0;
|
||||
p=0;
|
||||
n=0;
|
||||
Attribute attribute = new Attribute();
|
||||
for (Attribute a : getAttributesList()) {
|
||||
if (a.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
attribute = a;
|
||||
}
|
||||
|
||||
uniqueValues = attribute.getValuesSet();
|
||||
|
||||
for(String value: uniqueValues) {
|
||||
|
||||
ReturnObj returnObj = myJocl.testDataSetParallel2(examplesInt,examplesSize,maxLength,cellSpan
|
||||
,attributeName,value,attributesList,targetAttribute);
|
||||
p = returnObj.getYesCount();
|
||||
// System.out.println("Yes count = " + p);
|
||||
n = returnObj.getNoCount();
|
||||
// System.out.println("No count = " + n);
|
||||
|
||||
|
||||
// System.out.println("P = " + P + " " + "N = " + N);
|
||||
valueEntropy = calcEntropy(p, n);
|
||||
// System.out.println("Value Entropy = " + valueEntropy);
|
||||
attributeEntropy = attributeEntropy + ((p + n) / (P + N)) * valueEntropy;
|
||||
|
||||
}
|
||||
System.out.println("Attribute Entropy" + "(" + attributeName + ") = " + attributeEntropy);
|
||||
gain = classEntropy - attributeEntropy;
|
||||
if(gain >= maxGain)
|
||||
{
|
||||
// System.out.println("1");
|
||||
maxGain = gain;
|
||||
bestAttribute = attributeName;
|
||||
}
|
||||
System.out.println("Gain = " + gain + " " + "Max Gain = " + maxGain);
|
||||
|
||||
}
|
||||
System.out.println("Best Attribute = " + bestAttribute);
|
||||
return bestAttribute;
|
||||
}
|
||||
|
||||
Node createDecisionTreeParallel(NaryTree tree, Set<String> attributesSet, int[] examplesInt) {
|
||||
|
||||
|
||||
String targetAttributeValue = allOneValuesParallel(examplesInt,examplesSize,maxLength,cellSpan);
|
||||
if(targetAttributeValue.equalsIgnoreCase("Yes"))
|
||||
return new Node("Yes");
|
||||
else if(targetAttributeValue.equalsIgnoreCase("No"))
|
||||
return new Node("No");
|
||||
if (attributesSet.isEmpty()) {
|
||||
return new Node(modeOfCurrentAttribute);
|
||||
}
|
||||
Node node;
|
||||
String attributeName;
|
||||
attributeName = getBestAttributeParallel(attributesSet,examplesInt,examplesSize,maxLength,cellSpan);
|
||||
|
||||
Attribute attribute = new Attribute();
|
||||
|
||||
for (Attribute a : getAttributesList()) {
|
||||
if (a.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
attribute = a;
|
||||
}
|
||||
|
||||
|
||||
Set<String> uniqueValues = attribute.getValuesSet();
|
||||
|
||||
node = new Node(attributeName);
|
||||
|
||||
// node.setAttributeName(attributeName);
|
||||
if (tree.getHead() == null) {
|
||||
tree.setHead(node);
|
||||
|
||||
}
|
||||
|
||||
for (String s : uniqueValues) {
|
||||
System.out.println("For value: " + s);
|
||||
int[] examplesViInt = splitDataSetParallel(examplesInt,s,attributeName,examplesSize,maxLength,cellSpan);
|
||||
|
||||
// System.out.println("Displaying examplesVi");
|
||||
// for(Row row: examplesVi)
|
||||
// {
|
||||
// for(Cell cell : row.getCellsList())
|
||||
// {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
|
||||
if(reduceArray(examplesViInt) == 0)
|
||||
{
|
||||
// System.out.println("Array size 0");
|
||||
tree.createNode(null, node, s, new Node(modeOfTargetAttribute));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (Iterator<String> it = attributesSet.iterator(); it.hasNext(); )
|
||||
{
|
||||
String str = it.next();
|
||||
if (str.equalsIgnoreCase(attributeName))
|
||||
{
|
||||
System.out.println("Removed: " + str);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
Node temp = createDecisionTreeParallel(tree, attributesSet, examplesViInt);
|
||||
// System.out.println("Test: " + temp.getAttributeName());
|
||||
tree.createNode(null, node, s, temp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
Node createDecisionTreeParallel2(ArrayList<Row> examples, NaryTree tree, Set<String> attributesSet, int[] examplesInt) {
|
||||
|
||||
|
||||
String targetAttributeValue = allOneValues(examples);
|
||||
if(targetAttributeValue.equalsIgnoreCase("Yes"))
|
||||
return new Node("Yes");
|
||||
else if(targetAttributeValue.equalsIgnoreCase("No"))
|
||||
return new Node("No");
|
||||
if (attributesSet.isEmpty()) {
|
||||
return new Node(modeOfCurrentAttribute);
|
||||
}
|
||||
Node node;
|
||||
String attributeName;
|
||||
attributeName = getBestAttributeParallel(attributesSet,examplesInt,examplesSize,maxLength,cellSpan);
|
||||
|
||||
Attribute attribute = new Attribute();
|
||||
|
||||
for (Attribute a : getAttributesList()) {
|
||||
if (a.getAttributeName().equalsIgnoreCase(attributeName))
|
||||
attribute = a;
|
||||
}
|
||||
|
||||
|
||||
Set<String> uniqueValues = attribute.getValuesSet();
|
||||
|
||||
node = new Node(attributeName);
|
||||
|
||||
// node.setAttributeName(attributeName);
|
||||
if (tree.getHead() == null) {
|
||||
tree.setHead(node);
|
||||
|
||||
}
|
||||
|
||||
for (String s : uniqueValues) {
|
||||
// System.out.println("For value: " + s);
|
||||
ArrayList<Row> examplesVi = splitDataSet(examples,s,attributeName);
|
||||
|
||||
// System.out.println("Displaying examplesVi");
|
||||
// for(Row row: examplesVi)
|
||||
// {
|
||||
// for(Cell cell : row.getCellsList())
|
||||
// {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
|
||||
if(examplesVi.isEmpty())
|
||||
{
|
||||
tree.createNode(null,node,s,new Node(modeOfTargetAttribute));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (Iterator<String> it = attributesSet.iterator(); it.hasNext(); )
|
||||
{
|
||||
String str = it.next();
|
||||
if (str.equalsIgnoreCase(attributeName))
|
||||
{
|
||||
System.out.println("Removed: " + str);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
Node temp = createDecisionTree(tree, attributesSet, examplesVi);
|
||||
tree.createNode(null, node, s, temp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void setModeAndPNParallel(int[] examplesInt)
|
||||
{
|
||||
this.P = myJocl.getCount(examplesInt,examplesSize,maxLength,"Yes",cellSpan,targetAttribute);
|
||||
this.N = myJocl.getCount(examplesInt,examplesSize,maxLength,"No",cellSpan,targetAttribute);
|
||||
if(P > N)
|
||||
modeOfTargetAttribute = "Yes";
|
||||
else
|
||||
modeOfTargetAttribute = "No";
|
||||
|
||||
System.out.println("Most common value: " + modeOfTargetAttribute);
|
||||
}
|
||||
|
||||
private int reduceArray(int[] array)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int value : array) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ReturnObj
|
||||
{
|
||||
private int [] examplesInt;
|
||||
private int size;
|
||||
private int yesCount;
|
||||
private int noCount;
|
||||
|
||||
ReturnObj()
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public int[] getExamplesInt() {
|
||||
return examplesInt;
|
||||
}
|
||||
|
||||
public void setExamplesInt(int[] examplesInt) {
|
||||
this.examplesInt = examplesInt;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public int getYesCount() {
|
||||
return yesCount;
|
||||
}
|
||||
|
||||
public void setYesCount(int yesCount) {
|
||||
this.yesCount = yesCount;
|
||||
}
|
||||
|
||||
public int getNoCount() {
|
||||
return noCount;
|
||||
}
|
||||
|
||||
public void setNoCount(int noCount) {
|
||||
this.noCount = noCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
255
src/com/AI/Main.java
Executable file
255
src/com/AI/Main.java
Executable file
@ -0,0 +1,255 @@
|
||||
package com.AI;
|
||||
|
||||
import com.DBpackage.MyDatabase;
|
||||
|
||||
//import java.io.BufferedReader;
|
||||
//import java.io.IOException;
|
||||
//import java.io.InputStreamReader;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
class Main
|
||||
{
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyDatabase myDatabase = new MyDatabase();
|
||||
myDatabase.connectDatabase();
|
||||
// MySwing mySwing = new MySwing(myDatabase);
|
||||
// mySwing.start();
|
||||
// String indexAttribute, targetAttribute;
|
||||
|
||||
// try {
|
||||
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
|
||||
// System.out.println("Enter index attribute");
|
||||
// indexAttribute = bufferedReader.readLine();
|
||||
// System.out.println("Enter target attribute");
|
||||
//
|
||||
// }
|
||||
// catch(IOException e)
|
||||
// {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
|
||||
NaryTree tree = new NaryTree();
|
||||
ID3 id3 = new ID3();
|
||||
try {
|
||||
ArrayList<Row> examples = myDatabase.storeValues();
|
||||
// for (Row row : examples) {
|
||||
// for (Cell cell : row.getCellsList()) {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
Set<String> attributesSet = id3.getUniqueAttributes("Income", "Person", examples);
|
||||
// Set<String> attributesSet = id3.getUniqueAttributes("Play", "Day", examples);
|
||||
// Set<String> attributesSet = id3.getUniqueAttributes("Result_of_Treatment", "Patient", examples);
|
||||
|
||||
|
||||
for(String s: attributesSet)
|
||||
System.out.println(s);
|
||||
|
||||
id3.setAttributesNew(examples);
|
||||
// System.out.println(id3.getAttributesList().size());
|
||||
List<Attribute> attributesList = id3.getAttributesList();
|
||||
id3.setAttributesIndex(attributesList);
|
||||
for(Attribute a: id3.getAttributesList())
|
||||
System.out.printf("Attribute = %s Index = %d \n",a.getAttributeName(),a.getAttributeIndex());
|
||||
id3.displayAttributesList(attributesList);
|
||||
|
||||
// id3.setModeAndPN(examples);
|
||||
// id3.createDecisionTree(tree, attributesSet, examples);
|
||||
// tree.displayTree();
|
||||
// myDatabase.deleteTree();
|
||||
// tree.dbTest(myDatabase);
|
||||
|
||||
|
||||
MyJOCL myJocl = new MyJOCL();
|
||||
int examplesSize = examples.size();
|
||||
int maxLength = 1000;
|
||||
int cellSpan = 20;
|
||||
char[] examplesChar = new char[examplesSize*maxLength];
|
||||
|
||||
id3.setMaxLength(maxLength);
|
||||
id3.setExamplesSize(examplesSize);
|
||||
id3.setCellSpan(cellSpan);
|
||||
|
||||
myJocl.setMaxLength(maxLength);
|
||||
myJocl.setExamplesSize(examplesSize);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for (Row row : examples) {
|
||||
// for (Cell cell : row.getCellsList()) {
|
||||
// System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
System.out.println("Examples size = " + examplesSize);
|
||||
// Row row = examples.get(0);
|
||||
// System.out.print("Row size = " + row.getCellsList().size());
|
||||
long start = System.nanoTime();
|
||||
|
||||
for(int i=0;i<examplesSize;i++)
|
||||
{
|
||||
Row row = examples.get(i);
|
||||
// StringBuilder temp = new StringBuilder();
|
||||
for(int j=0;j<row.getCellsList().size();j++)
|
||||
{
|
||||
Cell cell = row.getCellsList().get(j);
|
||||
// System.out.println("Cell = " + cell.getAttributeName());
|
||||
|
||||
// ArrayList<Row> temp = examples.
|
||||
// temp.append(cell.getAttributeName());
|
||||
// temp.append(" ").append(cell.getValue()).append(" ");
|
||||
String attributeName = cell.getAttributeName();
|
||||
String value = cell.getValue();
|
||||
for(int k=0;k<attributeName.length();k++)
|
||||
{
|
||||
examplesChar[i*maxLength + 2*j*cellSpan + k] = cell.getAttributeName().toCharArray()[k];
|
||||
}
|
||||
for(int k=0;k<value.length();k++)
|
||||
{
|
||||
examplesChar[i*maxLength + (2*j+1)*cellSpan + k] = cell.getValue().toCharArray()[k];
|
||||
|
||||
}
|
||||
|
||||
// if(j%2==0)
|
||||
// examplesChar[i*maxLength + j*cellSpan] = cell.getAttributeName().toCharArray()[j];
|
||||
// else
|
||||
// examplesChar[i*maxLength + j*cellSpan] = cell.getValue().toCharArray()[j];
|
||||
}
|
||||
if(i>0)
|
||||
examplesChar[i*maxLength-1] = '\n';
|
||||
|
||||
// temp.append("\n");
|
||||
// System.out.print("Temp = " + temp);
|
||||
// for(int j=0;j<temp.length();j++)
|
||||
// {
|
||||
// examplesChar[i*maxLength+j] = temp.toString().toCharArray()[j];
|
||||
//
|
||||
// }
|
||||
}
|
||||
myJocl.setExamplesCharBuffer(examplesChar);
|
||||
long end = System.nanoTime();
|
||||
double setupTime = (double)(end-start)/1000000000;
|
||||
|
||||
|
||||
// for(int i=0; i<examplesSize;i++)
|
||||
// {
|
||||
//
|
||||
// for(int j = 0;j<maxLength;j++)
|
||||
// {
|
||||
//// if(examplesChar[i*maxLength+j]=='\0')
|
||||
//// continue;
|
||||
// System.out.print(examplesChar[i*maxLength + j]);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
// int k = 2;
|
||||
// for(int i=0; i<examplesSize;i++)
|
||||
// {
|
||||
// for(int j =0;j<cellSpan;j++)
|
||||
// {
|
||||
// System.out.print(examplesChar[i*maxLength + (2*k+1)*cellSpan + j]);
|
||||
// }
|
||||
// }
|
||||
// myJocl.allOneValues(examplesChar, examplesSize);
|
||||
|
||||
// for(String attributeName: attributesSet)
|
||||
// {
|
||||
//// myJocl.splitDataSetParallel(examplesChar, examplesSize, maxLength, attributeName, id3.getAttributesList());
|
||||
//
|
||||
// }
|
||||
// id3.allOneValuesParallel(examplesChar, examplesSize);
|
||||
id3.setMyJocl(myJocl);
|
||||
|
||||
int[] examplesInt = new int[examplesSize];
|
||||
for(int i = 0; i< examplesSize; i++)
|
||||
{
|
||||
examplesInt[i] = 1;
|
||||
}
|
||||
|
||||
// start = System.nanoTime();
|
||||
|
||||
// myJocl.testDataSetParallel(examplesInt,examplesSize, maxLength, cellSpan,
|
||||
// "Income","Yes", attributesList, id3.getTargetAttribute());
|
||||
// end = System.nanoTime();
|
||||
// System.out.println("Time elapsed Parallel: " + (double)(end-start)/1000000000);
|
||||
//
|
||||
// start = System.nanoTime();
|
||||
//// for(int k=0;k<10;k++)
|
||||
// id3.splitDataSet(examples,"Yes","Income");
|
||||
// end = System.nanoTime();
|
||||
// System.out.println("Time elapsed Serial: " + (double)(end-start)/1000000000);
|
||||
//
|
||||
// id3.allOneValuesParallel(examplesInt, examplesSize,maxLength,cellSpan);
|
||||
//
|
||||
// double classEntropy = id3.getClassEntropyParallel(examplesInt, examplesSize,maxLength,cellSpan);
|
||||
// System.out.println("Class Entropy Parallel = " + classEntropy);
|
||||
//
|
||||
// classEntropy = id3.getClassEntropy(examples);
|
||||
// System.out.println("Class Entropy Serial = " + classEntropy);
|
||||
|
||||
String attributeName = "Education";
|
||||
String value = "Bachelors";
|
||||
id3.setModeAndPNParallel(examplesInt);
|
||||
|
||||
start = System.nanoTime();
|
||||
|
||||
|
||||
|
||||
// myJocl.testDataSetParallel2(examplesInt,examplesSize,maxLength,cellSpan,attributeName,value,
|
||||
// attributesList,id3.getTargetAttribute());
|
||||
// id3.getBestAttributeParallel(attributesSet,examplesInt,examplesSize,maxLength,cellSpan);
|
||||
// id3.createDecisionTreeParallel2(examples,tree,attributesSet,examplesInt);
|
||||
end = System.nanoTime();
|
||||
System.out.println("Time elapsed setup: " + setupTime);
|
||||
System.out.println("Time elapsed Parallel: " + (double)(end-start)/1000000000);
|
||||
|
||||
|
||||
|
||||
NaryTree tree2 = new NaryTree();
|
||||
start = System.nanoTime();
|
||||
// myJocl.testDataSetParallel2(examplesInt,examplesSize,maxLength,cellSpan,attributeName,value,
|
||||
// attributesList,id3.getTargetAttribute());
|
||||
// id3.getBestAttribute(examples,attributesSet);
|
||||
id3.createDecisionTree(tree2,attributesSet,examples);
|
||||
end = System.nanoTime();
|
||||
System.out.println("Time elapsed Serial: " + (double)(end-start)/1000000000);
|
||||
|
||||
// int [] examplesViInt = id3.splitDataSetParallel(examplesInt,value,attributeName,examplesSize,maxLength,cellSpan);
|
||||
//
|
||||
//
|
||||
// System.out.println(Arrays.toString(examplesViInt));
|
||||
|
||||
// int size = reduceArray(examplesViInt);
|
||||
// System.out.println("size = " + size);
|
||||
// id3.setModeOfTargetAttribute("Yes");
|
||||
|
||||
// tree.displayTree();
|
||||
|
||||
// myJocl.sumReduction2(10, new int[1]);
|
||||
myJocl.endCL();
|
||||
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
static int reduceArray(int[] array)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int value : array) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
439
src/com/AI/MySwing.java
Executable file
439
src/com/AI/MySwing.java
Executable file
@ -0,0 +1,439 @@
|
||||
package com.AI;
|
||||
import com.DBpackage.MyDatabase;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
class MySwing extends JFrame
|
||||
{
|
||||
private MyDatabase myDatabase;
|
||||
private ID3 id3;
|
||||
private NaryTree decisionTree;
|
||||
|
||||
MySwing(MyDatabase myDatabase)
|
||||
{
|
||||
this.myDatabase = myDatabase;
|
||||
this.myDatabase.connectDatabase();
|
||||
id3 = new ID3();
|
||||
decisionTree = new NaryTree();
|
||||
}
|
||||
|
||||
private class InsertPanel extends JPanel //means panel for insert pane, not inserting a panel
|
||||
{
|
||||
private JLabel label_roll;
|
||||
private JLabel label_fname;
|
||||
private JLabel label_lname;
|
||||
private JLabel label_marks;
|
||||
private JLabel c;
|
||||
InsertPanel()
|
||||
{
|
||||
setLayout(new FlowLayout());
|
||||
c = new JLabel();
|
||||
label_roll =new JLabel();
|
||||
label_fname =new JLabel();
|
||||
label_lname =new JLabel();
|
||||
label_marks =new JLabel();
|
||||
JTextField roll = new JTextField(10);
|
||||
JTextField fname = new JTextField(10);
|
||||
JTextField lname = new JTextField(10);
|
||||
JTextField marks = new JTextField(10);
|
||||
|
||||
c.setText("Hello");
|
||||
label_roll.setText("Roll Number: ");
|
||||
label_fname.setText("First Name: ");
|
||||
label_lname.setText("Last Name: ");
|
||||
label_marks.setText("Marks: ");
|
||||
|
||||
JButton submit = new JButton();
|
||||
JButton reset = new JButton();
|
||||
submit.setText("Submit");
|
||||
reset.setText("Reset");
|
||||
|
||||
add(label_roll);
|
||||
add(roll);
|
||||
add(label_fname);
|
||||
add(fname);
|
||||
add(label_lname);
|
||||
add(lname);
|
||||
add(label_marks);
|
||||
add(marks);
|
||||
//add(ps);
|
||||
add(submit);
|
||||
add(reset);
|
||||
add(c);
|
||||
|
||||
submit.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
int r,m;
|
||||
String fn,ln;
|
||||
c.setText("Button clicked!");
|
||||
|
||||
/*preparedStatement.setString(1, "Gary");
|
||||
preparedStatement.setString(2, "Larson");
|
||||
preparedStatement.setLong (3, 123);*/
|
||||
//myDatabase.insertValues(sql);
|
||||
r = Integer.parseInt(roll.getText());
|
||||
m = Integer.parseInt(marks.getText());
|
||||
fn = fname.getText();
|
||||
ln = lname.getText();
|
||||
myDatabase.insertValues(r,fn,ln,m);
|
||||
//myDatabase.deleteValues(107);
|
||||
//myDatabase.updateValues("Henry",106);
|
||||
|
||||
}
|
||||
});
|
||||
reset.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
roll.setText("");
|
||||
fname.setText("");
|
||||
lname.setText("");
|
||||
marks.setText("");
|
||||
c.setText("Cleared!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private class UpdatePanel extends JPanel
|
||||
{
|
||||
UpdatePanel()
|
||||
{
|
||||
JButton upd = new JButton();
|
||||
JLabel new_fname_label = new JLabel();
|
||||
JLabel id_label = new JLabel();
|
||||
JTextField new_fname = new JTextField(10);
|
||||
JTextField id = new JTextField(10);
|
||||
|
||||
upd.setText("Update");
|
||||
new_fname_label.setText("New first name: ");
|
||||
id_label.setText("Enter the id of the record to be updated: ");
|
||||
|
||||
add(new_fname_label);
|
||||
add(new_fname);
|
||||
add(id_label);
|
||||
add(id);
|
||||
add(upd);
|
||||
upd.addActionListener(new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
String name = new_fname.getText();
|
||||
int i = Integer.parseInt(id.getText());
|
||||
myDatabase.updateValues(name, i);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private class DeletePanel extends JPanel // panel for delete not deleting a panel
|
||||
{
|
||||
DeletePanel()
|
||||
{
|
||||
JLabel id_label = new JLabel();
|
||||
JTextField id = new JTextField(10);
|
||||
JButton delete_but = new JButton();
|
||||
|
||||
id_label.setText("Enter id of the record to be deleted");
|
||||
delete_but.setText("Delete");
|
||||
|
||||
add(id_label);
|
||||
add(id);
|
||||
add(delete_but);
|
||||
|
||||
delete_but.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int i = Integer.parseInt(id.getText());
|
||||
myDatabase.deleteValues(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private class DisplayPanel extends JPanel
|
||||
{
|
||||
private JButton show;
|
||||
private JLabel show_label;
|
||||
DisplayPanel()
|
||||
{
|
||||
setLayout(new FlowLayout());
|
||||
show = new JButton();
|
||||
show_label = new JLabel();
|
||||
show.setText("Display");
|
||||
show_label.setText("Click to display table");
|
||||
add(show_label);
|
||||
add(show);
|
||||
show.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
myDatabase.displayValues();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class StorePanel extends JPanel
|
||||
{
|
||||
private JButton store;
|
||||
private JLabel store_label;
|
||||
StorePanel()
|
||||
{
|
||||
|
||||
store = new JButton();
|
||||
store_label = new JLabel();
|
||||
|
||||
store.setText("Store");
|
||||
store_label.setText("Click to store and display table");
|
||||
add(store_label);
|
||||
add(store);
|
||||
store.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
try
|
||||
{
|
||||
JTable table = new JTable();
|
||||
Row climate;
|
||||
ArrayList<Row> climateArrayList = myDatabase.storeValues();
|
||||
|
||||
id3 = new ID3();
|
||||
// id3.setTableData(climateArrayList);
|
||||
//id3.createNode();
|
||||
|
||||
// for (Row c : climateArrayList) {
|
||||
// System.out.println(c.getDay() + " " +
|
||||
// c.getWeather() + " " + c.getTemperature() + " " +
|
||||
// c.getHumidity() + " " + c.getWind());
|
||||
// }
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class DecisionTreePanel extends JPanel
|
||||
{
|
||||
private static final int DISTANCE_X = 4;
|
||||
private static final int DISTANCE_Y = 65;
|
||||
private static final int radius = 20;
|
||||
private static final int diameter = radius * 2;
|
||||
private int treeArity = 3;
|
||||
private int treeDepth = 3;
|
||||
private int childDistanceX;
|
||||
|
||||
private JPanel treeDisplayPanel;
|
||||
char status;
|
||||
|
||||
private NaryTree createDecisionTree()
|
||||
{
|
||||
NaryTree tree = new NaryTree();
|
||||
try {
|
||||
ArrayList<Row> examples = myDatabase.storeValues();
|
||||
for (Row row : examples) {
|
||||
for (Cell cell : row.getCellsList()) {
|
||||
System.out.print(cell.getAttributeName() + " " + cell.getValue() + ", ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
Set<String> attributesSet = id3.getUniqueAttributes("Play", "Day", examples);
|
||||
|
||||
id3.setAttributesNew(examples);
|
||||
id3.setModeAndPN(examples);
|
||||
id3.createDecisionTree(tree, attributesSet, examples);
|
||||
tree.displayTree();
|
||||
myDatabase.deleteTree();
|
||||
tree.dbTest(myDatabase);
|
||||
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
DecisionTreePanel()
|
||||
{
|
||||
// setLayout(new FlowLayout());
|
||||
// setSize(800,600);
|
||||
status = 'n';
|
||||
JButton displayButton = new JButton();
|
||||
JButton clearButton = new JButton();
|
||||
JButton createTreeButton = new JButton();
|
||||
displayButton.setText("Display Tree");
|
||||
clearButton.setText("Clear");
|
||||
createTreeButton.setText("Create Tree");
|
||||
displayButton.addActionListener(new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
status = 'y';
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
clearButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
status = 'n';
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
createTreeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
decisionTree = createDecisionTree();
|
||||
}
|
||||
});
|
||||
add(displayButton);
|
||||
add(clearButton);
|
||||
add(createTreeButton);
|
||||
childDistanceX = 100;
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
// System.out.println("Inside paintcomponent");
|
||||
Dimension size = getSize();
|
||||
int x = size.width / 2;
|
||||
int y = ( treeDepth * DISTANCE_Y) / 2;
|
||||
|
||||
// drawTree(g, x, y, treeArity, treeDepth);
|
||||
if(status == 'y')
|
||||
{
|
||||
// g.drawOval(x,y,diameter,diameter);
|
||||
System.out.println("Inside if");
|
||||
// drawMyTree(g, x, y);
|
||||
displayTree(g,x,y);
|
||||
}
|
||||
}
|
||||
|
||||
// public void drawTree(Graphics g, int x, int y, int arity, int depth) {
|
||||
// if (depth > 0) {
|
||||
// int childDistanceX = (int) (Math.pow(arity, depth) * DISTANCE_X);
|
||||
// int childX = x - (arity - 1) * childDistanceX / 2;
|
||||
// int childY = y - DISTANCE_Y;
|
||||
//
|
||||
// for (int child = 0; child < arity; child++) {
|
||||
// g.drawLine(x, y, childX, childY);
|
||||
// drawTree(g, childX, childY, arity, depth - 1);
|
||||
// childX += childDistanceX;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
public void drawMyTree(Graphics g, int x, int y)
|
||||
{
|
||||
System.out.println("Inside drawmytree");
|
||||
g.drawOval(x,y,diameter,diameter);
|
||||
int childX = x - childDistanceX / 2;
|
||||
int childY = y + DISTANCE_Y;
|
||||
g.drawLine(x + radius ,y + diameter,childX + childDistanceX + radius,childY);
|
||||
g.drawLine(x + radius ,y + diameter,childX + radius,childY);
|
||||
|
||||
|
||||
g.drawOval(childX,childY,diameter,diameter);
|
||||
g.drawOval(childX+childDistanceX,childY,diameter,diameter);
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
private void traverse(Node node, Graphics g, int x, int y, int i)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
System.out.println();
|
||||
return;
|
||||
}
|
||||
int childX = x - childDistanceX;
|
||||
int childY = y + DISTANCE_Y;
|
||||
i++;
|
||||
g.drawOval(x,y,diameter,diameter);
|
||||
if(node.getAttributeName().equalsIgnoreCase("Yes") ||
|
||||
node.getAttributeName().equalsIgnoreCase("No"))
|
||||
if(node.getValuesList() == null) {
|
||||
return;
|
||||
}
|
||||
for(Node.Value v: node.getValuesList())
|
||||
{
|
||||
// g.drawOval(childX,y,diameter,diameter);
|
||||
g.drawLine(x+radius, y + diameter,childX + radius, childY);
|
||||
traverse(v.getNext(), g, childX, childY, i);
|
||||
childX = childX + childDistanceX;
|
||||
}
|
||||
|
||||
}
|
||||
void displayTree(Graphics g, int x, int y)
|
||||
{
|
||||
System.out.println("Displaying tree");
|
||||
if(decisionTree.getHead()!=null) {
|
||||
int i = 0;
|
||||
traverse(decisionTree.getHead(), g, x, y, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Head node not created/initialized");
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
JTabbedPane jtp = new JTabbedPane();
|
||||
|
||||
//This creates the template on the windowed application that we will be using
|
||||
jtp.setBounds(10,10,1030,640);
|
||||
|
||||
InsertPanel insertPanel = new InsertPanel();
|
||||
com.AI.MySwing.UpdatePanel updatePanel = new com.AI.MySwing.UpdatePanel();
|
||||
com.AI.MySwing.DeletePanel deletePanel = new com.AI.MySwing.DeletePanel();
|
||||
com.AI.MySwing.DisplayPanel displayPanel = new com.AI.MySwing.DisplayPanel();
|
||||
com.AI.MySwing.StorePanel storePanel = new com.AI.MySwing.StorePanel();
|
||||
DecisionTreePanel decisionTreePanel = new DecisionTreePanel();
|
||||
|
||||
//This adds the first and second tab to our tabbed pane object and names it
|
||||
jtp.addTab("Insert", insertPanel);
|
||||
jtp.addTab("Update", updatePanel);
|
||||
jtp.addTab("Delete", deletePanel);
|
||||
jtp.addTab("Display", displayPanel);
|
||||
jtp.addTab("Store", storePanel);
|
||||
jtp.addTab("Decision-Tree", decisionTreePanel);
|
||||
|
||||
getContentPane().add(jtp);
|
||||
setLayout(null);
|
||||
setVisible(true);
|
||||
setSize(1050,700);
|
||||
addWindowListener(new WindowAdapter()
|
||||
{
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e)
|
||||
{
|
||||
|
||||
System.out.println("Closing");
|
||||
myDatabase.closeConnection();
|
||||
dispose();
|
||||
}
|
||||
|
||||
});
|
||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||
}
|
||||
|
||||
}
|
221
src/com/AI/NaryTree.java
Executable file
221
src/com/AI/NaryTree.java
Executable file
@ -0,0 +1,221 @@
|
||||
package com.AI;
|
||||
import com.DBpackage.MyDatabase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Node
|
||||
{
|
||||
private String attributeName;
|
||||
private ArrayList<Value> valuesList;
|
||||
|
||||
class Value
|
||||
{
|
||||
private String value;
|
||||
private Node next;
|
||||
|
||||
public Value()
|
||||
{
|
||||
next = null;
|
||||
}
|
||||
|
||||
String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
Node getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Node()
|
||||
{
|
||||
valuesList = new ArrayList<>();
|
||||
}
|
||||
public Node(String attributeName)
|
||||
{
|
||||
this.setAttributeName(attributeName);
|
||||
valuesList = new ArrayList<>();
|
||||
}
|
||||
public String getAttributeName()
|
||||
{
|
||||
return attributeName;
|
||||
}
|
||||
|
||||
public void setAttributeName(String attributeName)
|
||||
{
|
||||
this.attributeName = attributeName;
|
||||
}
|
||||
|
||||
public ArrayList<Value> getValuesList() {
|
||||
return valuesList;
|
||||
}
|
||||
|
||||
public void setValuesList(ArrayList<Value> valuesList) {
|
||||
this.valuesList = valuesList;
|
||||
}
|
||||
|
||||
void setVal(String value, Node obj)
|
||||
{
|
||||
|
||||
Value valueObj = new Value();
|
||||
valueObj.setValue(value);
|
||||
valueObj.setNext(obj);
|
||||
//obj.getValuesList().get(0).setPrevious(this);
|
||||
//if(obj.getPrevious().getValuesList().get(0).isVisited() == true);
|
||||
valuesList.add(valueObj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class NaryTree
|
||||
{
|
||||
private Node head;
|
||||
|
||||
NaryTree()
|
||||
{
|
||||
head = null;
|
||||
}
|
||||
|
||||
public Node getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public void setHead(Node head) {
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
/*Node createNode(Node h, Node parent, String edgeName, Node child) {
|
||||
if (head == null)
|
||||
{
|
||||
System.out.println("Initializing head node");
|
||||
// h = new Node(parentName);
|
||||
// this.setHead(h);
|
||||
// System.out.println("Head: " + h.getAttributeName());
|
||||
// return h;
|
||||
head = parent;
|
||||
return head;
|
||||
}
|
||||
else
|
||||
{
|
||||
createLink(parent, child, edgeName);
|
||||
return child;
|
||||
}
|
||||
}*/
|
||||
Node createNode(Node h, Node parent, String edgeName, Node child) {
|
||||
if (head == null)
|
||||
{
|
||||
System.out.println("Initializing head node");
|
||||
// h = new Node(parentName);
|
||||
// this.setHead(h);
|
||||
// System.out.println("Head: " + h.getAttributeName());
|
||||
// return h;
|
||||
head = parent;
|
||||
return head;
|
||||
}
|
||||
else
|
||||
{
|
||||
createLink(parent, child, edgeName);
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
private void createLink(Node parent, Node child, String value)
|
||||
{
|
||||
// System.out.println("Edge: " + value + " Child: " + child.getAttributeName() );
|
||||
parent.setVal(value, child);
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
private void traverse(Node node)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
System.out.println();
|
||||
return;
|
||||
}
|
||||
System.out.print(" " + node.getAttributeName() + " ");
|
||||
if(node.getAttributeName().equalsIgnoreCase("Yes") ||
|
||||
node.getAttributeName().equalsIgnoreCase("No"))
|
||||
System.out.println();
|
||||
if(node.getValuesList() == null) {
|
||||
System.out.print(node.getAttributeName());
|
||||
return;
|
||||
}
|
||||
|
||||
for(Node.Value v: node.getValuesList())
|
||||
{
|
||||
System.out.print(" " + v.getValue());
|
||||
traverse(v.getNext());
|
||||
}
|
||||
|
||||
}
|
||||
void displayTree()
|
||||
{
|
||||
System.out.println("Displaying tree(pre-order):");
|
||||
System.out.println("------------------");
|
||||
if(head!=null) {
|
||||
traverse(head);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Head node not created/initialized");
|
||||
}
|
||||
System.out.println();
|
||||
System.out.print("------------------");
|
||||
System.out.println();
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private void traverseDBTest(Node node, MyDatabase myDatabase)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
System.out.println();
|
||||
return;
|
||||
}
|
||||
|
||||
//System.out.print(" " + node.getAttributeName());
|
||||
if(node.getValuesList() == null) {
|
||||
// System.out.print(node.getAttributeName());
|
||||
return;
|
||||
}
|
||||
for(Node.Value value : node.getValuesList())
|
||||
{
|
||||
// System.out.print(" " + sn.getValue());
|
||||
if(value.getNext().getAttributeName() == null)
|
||||
System.out.println("null");
|
||||
else {
|
||||
myDatabase.insertTree(node.getAttributeName(), value.getValue(), value.getNext().getAttributeName());
|
||||
System.out.println("Parent: " + node.getAttributeName()
|
||||
+ " Edge: " + value.getValue()
|
||||
+ " Child: " + value.getNext().getAttributeName());
|
||||
}
|
||||
traverseDBTest(value.getNext(),myDatabase);
|
||||
}
|
||||
//System.out.println("Node named" + nodeName + "does not exist in the tree");
|
||||
|
||||
}
|
||||
|
||||
void dbTest(MyDatabase myDatabase)
|
||||
{
|
||||
if(head!=null) {
|
||||
traverseDBTest(head, myDatabase);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Head node not created/initialized");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
65
src/com/AI/Row.java
Executable file
65
src/com/AI/Row.java
Executable file
@ -0,0 +1,65 @@
|
||||
package com.AI;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Cell
|
||||
{
|
||||
private String attributeName;
|
||||
private String value;
|
||||
|
||||
public String getAttributeName() {
|
||||
return attributeName;
|
||||
}
|
||||
|
||||
public void setAttributeName(String attributeName) {
|
||||
this.attributeName = attributeName;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
public class Row
|
||||
{
|
||||
private ArrayList<Cell> cellsList;
|
||||
|
||||
public ArrayList<Cell> getCellsList() {
|
||||
return cellsList;
|
||||
}
|
||||
|
||||
public void setCellsList(ArrayList<Cell> cellsList) {
|
||||
this.cellsList = cellsList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Row()
|
||||
{
|
||||
cellsList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setRow(String attributeName, String value)
|
||||
{
|
||||
Cell cell = new Cell();
|
||||
cell.setAttributeName(attributeName);
|
||||
cell.setValue(value);
|
||||
cellsList.add(cell);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Logarithms
|
||||
{
|
||||
public static double log2(double exponent)
|
||||
{
|
||||
return Math.log10(exponent) / 0.3010299956639812;
|
||||
}
|
||||
}
|
1302
src/com/AI/jocl.java
Executable file
1302
src/com/AI/jocl.java
Executable file
File diff suppressed because it is too large
Load Diff
318
src/com/DBpackage/MyDatabase.java
Executable file
318
src/com/DBpackage/MyDatabase.java
Executable file
@ -0,0 +1,318 @@
|
||||
package com.DBpackage;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.AI.*;
|
||||
|
||||
class myProperties
|
||||
{
|
||||
Properties loadConfig() throws IOException
|
||||
{
|
||||
FileInputStream fis = new FileInputStream("properties.txt");
|
||||
Properties prop = new Properties();
|
||||
prop.load(fis);
|
||||
return prop;
|
||||
}
|
||||
}
|
||||
|
||||
public class MyDatabase
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// JDBC driver name and database URL
|
||||
//private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
||||
private static final String DB_URL = "jdbc:mysql://localhost/ID3?autoReconnect=true&useSSL=false";
|
||||
|
||||
// Database credentials
|
||||
private static final String USER = "id3";
|
||||
private static final String PASS = "id3";
|
||||
|
||||
private Properties prop = new Properties();
|
||||
|
||||
private Connection conn = null;
|
||||
private Statement stmt = null;
|
||||
|
||||
private PreparedStatement preparedStatement;
|
||||
|
||||
|
||||
public MyDatabase()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("Loading properties file");
|
||||
myProperties mp = new myProperties();
|
||||
this.prop = mp.loadConfig();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void connectDatabase()
|
||||
{
|
||||
try
|
||||
{
|
||||
//STEP 2: Register JDBC driver
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
//STEP 3: Open a connection
|
||||
System.out.println("Connecting to selected database...");
|
||||
conn = DriverManager.getConnection(DB_URL, USER, PASS);
|
||||
System.out.println("Connected to database successfully...");
|
||||
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
//Handle errors for JDBC
|
||||
se.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
//Handle errors for Class.forName
|
||||
e.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
/*public void storeValues()
|
||||
{
|
||||
String sql = prop.getProperty("display");
|
||||
System.out.println("Displaying Table");
|
||||
try
|
||||
{
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
ResultSet resultSet = preparedStatement.executeQuery(sql);
|
||||
ResultSetMetaData rsmd = resultSet.getMetaData();
|
||||
int columnsNumber = rsmd.getColumnCount();
|
||||
while (resultSet.next())
|
||||
{
|
||||
for (int i = 1; i <= columnsNumber; i++)
|
||||
{
|
||||
String columnValue = resultSet.getString(i);
|
||||
System.out.print(columnValue + " " + rsmd.getAttributeName(i));
|
||||
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
|
||||
public void insertValues(int r,String fn, String ln, int m)
|
||||
{
|
||||
//String sql = "insert into Registration values (?,?,?,?)";
|
||||
String sql = prop.getProperty("insert");
|
||||
System.out.println("Inserting records into the table...");
|
||||
try
|
||||
{
|
||||
/*
|
||||
stmt = conn.createStatement();
|
||||
stmt.executeUpdate(str);
|
||||
*/
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.setInt(1,r);
|
||||
preparedStatement.setString(2,fn);
|
||||
preparedStatement.setString(3,ln);
|
||||
preparedStatement.setInt(4,m);
|
||||
|
||||
int nodesAffected = preparedStatement.executeUpdate();
|
||||
|
||||
System.out.println("Number of nodes affected:" + nodesAffected);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
System.out.println("Records inserted successfully");
|
||||
}
|
||||
|
||||
public void deleteTree()
|
||||
{
|
||||
String sql = prop.getProperty("deleteAll");
|
||||
System.out.println("Deleting all rows from table");
|
||||
try{
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
int deletedRows = preparedStatement.executeUpdate();
|
||||
|
||||
if(deletedRows > 0)
|
||||
System.out.println("All rows deleted");
|
||||
else
|
||||
System.out.println("Table already empty");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
sql = prop.getProperty("resetAutoIncrement");
|
||||
try {
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
public void insertTree( String parent, String edge, String child)
|
||||
{
|
||||
String sql = prop.getProperty("insertTree");
|
||||
// System.out.println("Inserting tree records into the table...");
|
||||
try
|
||||
{
|
||||
/*
|
||||
stmt = conn.createStatement();
|
||||
stmt.executeUpdate(str);
|
||||
*/
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.setString(1,parent);
|
||||
preparedStatement.setString(2,edge);
|
||||
preparedStatement.setString(3,child);
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
// System.out.println("Number of nodes affected:" + rowsAffected);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteValues(int id)
|
||||
{
|
||||
String sql = prop.getProperty("delete");
|
||||
System.out.println("Deleting value from table");
|
||||
try
|
||||
{
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
//preparedStatement.setInt(1,"id");
|
||||
preparedStatement.setInt(1,id);
|
||||
System.out.println("SQL command = " + preparedStatement);
|
||||
int nodesAffected = preparedStatement.executeUpdate();
|
||||
System.out.println("Number of nodes affected:" + nodesAffected);
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public void updateValues(String fname_new, int id)
|
||||
{
|
||||
String sql = prop.getProperty("update");
|
||||
System.out.println("Updating value fname with id = " + id );
|
||||
try
|
||||
{
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
//preparedStatement.setInt(1,"id");
|
||||
preparedStatement.setString(1,fname_new);
|
||||
preparedStatement.setInt(2,id);
|
||||
System.out.println("SQL command = " + preparedStatement);
|
||||
int nodesAffected = preparedStatement.executeUpdate();
|
||||
System.out.println("Number of nodes affected:" + nodesAffected);
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void displayValues()
|
||||
{
|
||||
String sql = prop.getProperty("display");
|
||||
System.out.println("Displaying Table");
|
||||
try
|
||||
{
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
ResultSet resultSet = preparedStatement.executeQuery(sql);
|
||||
ResultSetMetaData rsmd = resultSet.getMetaData();
|
||||
int columnsNumber = rsmd.getColumnCount();
|
||||
while (resultSet.next())
|
||||
{
|
||||
for (int i = 1, j = 0; i <= columnsNumber ; i++,j++)
|
||||
{
|
||||
if (i > 1) System.out.print(", ");
|
||||
String columnValue = resultSet.getString(i);
|
||||
System.out.print(columnValue + " " + rsmd.getColumnName(i));
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Row> storeValues() throws SQLException
|
||||
{
|
||||
String sql = prop.getProperty("display");
|
||||
PreparedStatement preparedStatement = conn.prepareStatement(sql);
|
||||
System.out.println("Storing Table");
|
||||
|
||||
ResultSet resultSet;
|
||||
resultSet = preparedStatement.executeQuery(sql);
|
||||
ResultSetMetaData rsmd = resultSet.getMetaData();
|
||||
int columnCount = rsmd.getColumnCount();
|
||||
|
||||
// ArrayList<String> hotelResultList = new ArrayList<>(columnCount);
|
||||
// while (results.next()) {
|
||||
// int i = 1;
|
||||
// while(i <= columnCount) {
|
||||
// hotelResultList.add(results.getString(i++));
|
||||
|
||||
ArrayList<Row> tableData = new ArrayList<>(columnCount);
|
||||
while (resultSet.next()) {
|
||||
int i = 1;
|
||||
Row row = new Row();
|
||||
while(i<= columnCount)
|
||||
{
|
||||
row.setRow(rsmd.getColumnName(i),resultSet.getString(rsmd.getColumnName(i++)));
|
||||
}
|
||||
tableData.add(row);
|
||||
}
|
||||
return tableData;
|
||||
}
|
||||
public void closeConnection()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
if(stmt!=null)
|
||||
conn.close();
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
}// do nothing
|
||||
try
|
||||
{
|
||||
if(conn!=null)
|
||||
conn.close();
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
se.printStackTrace();
|
||||
}
|
||||
System.out.println("Database connection closed successfully");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user