package trust.weka4jason;
import trust.weka4jason.utils.Datasetter;
import weka.core.*;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.converters.ArffLoader;
import java.util.List;
import java.util.Random;
import java.io.File;
import java.util.ArrayList;
/**
* Tests of Data Processing :
* translation from arff files (or Java simple data types) to WEKA Data sets
* @author CNR
*
*/
public class wekaTest {
Instance inst;
Random rand = new Random();
/**
* @param args
*/
public static void main(String[] args) {
//(new wekaTest()).test1();
//(new wekaTest()).test2();
(new wekaTest()).test3();
}
/**
* Dynamically create Instances based on fixed attributes
* SET THE DOMAINS of CATEGORIES **STATICALLY**
*/
public void test1(){
// Create list to hold TASK nominal values
List<String> task_nominal_values = new ArrayList<String>();
task_nominal_values.add("appendicitis");
task_nominal_values.add("otitis");
task_nominal_values.add("dental_operation");
// Create list to hold TASK nominal values
List<String> proMnf_nominal_values = new ArrayList<String>();
proMnf_nominal_values.add("dentist");
proMnf_nominal_values.add("surgeon");
proMnf_nominal_values.add("radioterapist");
// Create list to hold TASK nominal values
List<String> dispMnf_nominal_values = new ArrayList<String>();
dispMnf_nominal_values.add("cautious");
dispMnf_nominal_values.add("impulsive");
dispMnf_nominal_values.add("careful");
// Create list to hold CROSS nominal values
List<String> crossMnf_nominal_values = new ArrayList<String>();
crossMnf_nominal_values.add("female");
crossMnf_nominal_values.add("male");
crossMnf_nominal_values.add("muslim");
// PREPARE THE DATA DOMAIN
Attribute task = new Attribute("task", task_nominal_values);
Attribute trustee = new Attribute("trustee");
Attribute mnf1 = new Attribute("mnf1",proMnf_nominal_values);
Attribute mnf2 = new Attribute("mnf2",dispMnf_nominal_values);
Attribute mnf3 = new Attribute("mnf3", crossMnf_nominal_values);
// the following attributes are numeric
Attribute mnf4 = new Attribute("mnf4");
Attribute score = new Attribute("score");
ArrayList<Attribute> attInfo = new ArrayList<Attribute>();
attInfo.add(task);
attInfo.add(trustee);
attInfo.add(mnf1);
attInfo.add(mnf2);
attInfo.add(mnf3);
attInfo.add(mnf4);
attInfo.add(score);
// last arg is the max length of this entry set
Instances BeliefBase = new Instances("BeliefBase", attInfo, 755);
// Create empty instance with three attribute values
inst = new DenseInstance(7);
// Set instance's values for the attributes "task", "weight", and "position"
for(int i=0; i<15; i++){
inst.setValue(task, task_nominal_values.get(rand.nextInt(task_nominal_values.size()))); //0
inst.setValue(trustee, rand.nextInt(100));
inst.setValue(mnf1, proMnf_nominal_values.get(rand.nextInt(mnf1.numValues())));
inst.setValue(mnf2, dispMnf_nominal_values.get(rand.nextInt(mnf2.numValues())));
inst.setValue(mnf3, crossMnf_nominal_values.get(rand.nextInt(mnf3.numValues())));
inst.setValue(mnf4, 888 ); //5
inst.setValue(score, ((double)rand.nextInt(100))/100);
BeliefBase.add(inst);
}
// Print the instance
System.out.println("\n\n The Instances are: \n\n" + BeliefBase);
}
/**
* Beliefbase has to be incremented with experience.
* This test dynamically create Instances based on open STRING attributes
* translates STRING to NOMINAL attributes
* Use a Classifier on the generated instances
*/
public void test2(){
// SET THE DOMAINS of CATEGORIES **STATICALLY**
// problem: this set has to be incremented with experience
// Create list to hold TASK nominal values
List<String> task_nominal_values = new ArrayList<String>();
task_nominal_values.add("appendicitis");
task_nominal_values.add("otitis");
task_nominal_values.add("dental_operation");
List<String> proMnf_values = null;
List<String> dispMnf_values = null;
// Create list to hold CROSS nominal values
List<String> crossMnf_values = null;
// PREPARE THE DATA DOMAIN
Attribute task = new Attribute("task", task_nominal_values);
Attribute trustee = new Attribute("trustee");
Attribute mnf1 = new Attribute("mnf1",proMnf_values);
Attribute mnf2 = new Attribute("mnf2",dispMnf_values);
Attribute mnf3 = new Attribute("mnf3", crossMnf_values);
// the following attributes are numeric
Attribute mnf4 = new Attribute("mnf4");
Attribute score = new Attribute("score");
System.out.println("mnf3:" + Datasetter.isType(mnf3));
System.out.println("mnf4:" + Datasetter.isType(mnf4));
System.out.println("score:" + Datasetter.isType(score));
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes = new ArrayList<Attribute>();
attributes.add(task);
attributes.add(trustee);
attributes.add(mnf1);
attributes.add(mnf2);
attributes.add(mnf3);
attributes.add(mnf4);
attributes.add(score);
// last arg is the max entry set
Instances BeliefBase = new Instances("BeliefBase", attributes, 755);
// SIMULATE DATA INSERTION inside the BB
// Create a list of prof manifesta
List<String> proMnf_nominal_values = new ArrayList<String>();
proMnf_nominal_values.add("dentist");
proMnf_nominal_values.add("surgeon");
// Create list of dispositional manifesta
List<String> dispMnf_nominal_values = new ArrayList<String>();
dispMnf_nominal_values.add("cautious");
dispMnf_nominal_values.add("impulsive");
// Create list of crosscutting manifesta
List<String> crossMnf_nominal_values = new ArrayList<String>();
crossMnf_nominal_values.add("female");
crossMnf_nominal_values.add("male");
// Create empty instance with three attribute values
inst = new DenseInstance(7);
// Set instance's values for the attributes "task", "weight", and "position"
for(int i=0; i<10; i++){
inst.setValue(task, task_nominal_values.get(rand.nextInt(task_nominal_values.size()))); //0
inst.setValue(trustee, rand.nextInt(100));
inst.setValue(mnf1, proMnf_nominal_values.get(rand.nextInt(proMnf_nominal_values.size())));
inst.setValue(mnf2, dispMnf_nominal_values.get(rand.nextInt(dispMnf_nominal_values.size())));
inst.setValue(mnf3, crossMnf_nominal_values.get(rand.nextInt(crossMnf_nominal_values.size())));
inst.setValue(mnf4, 888 ); //5
inst.setValue(score, ((double)rand.nextInt(100))/100);
BeliefBase.add(inst);
}
// Set instance's dataset to be the dataset "race"
//inst.setDataset(race);
Datasetter.addInstance(BeliefBase, "appendicitis",24,"oncologist","careful","aaaaaaa----buddhist",555,0.55);
Datasetter.addInstance(BeliefBase, "appendicitis",24,"oncologist","careful","bbbbbbb----buddhist",666,0.66);
Datasetter.addInstance(BeliefBase, "appendicitis",24,"oncologist","careful","ccccccc----buddhist",777,0.77);
// SIMULATE NEW DATA INSERTION
crossMnf_nominal_values.add("muslim");
crossMnf_nominal_values.add("buddhist");
crossMnf_nominal_values.add("godless");
crossMnf_nominal_values.add("yellow");
dispMnf_nominal_values.add("careful");
dispMnf_nominal_values.add("precise");
proMnf_nominal_values.add("radioterapist");
proMnf_nominal_values.add("oncologist");
System.out.println(" NOW mnf3: " + mnf3.enumerateValues());
System.out.println("Cross Attr *yellow* is: "+BeliefBase.attribute("mnf3").indexOfValue("yellow"));
for(int i=0; i<10; i++){
inst.setValue(task, task_nominal_values.get(rand.nextInt(task_nominal_values.size()))); //0
inst.setValue(trustee, rand.nextInt(100));
inst.setValue(mnf1, proMnf_nominal_values.get(rand.nextInt(proMnf_nominal_values.size())));
inst.setValue(mnf2, dispMnf_nominal_values.get(rand.nextInt(dispMnf_nominal_values.size())));
inst.setValue(mnf3, crossMnf_nominal_values.get(rand.nextInt(crossMnf_nominal_values.size())));
inst.setValue(mnf4, 888 ); //5
inst.setValue(score, ((double)rand.nextInt(100))/100);
BeliefBase.add(inst);
}
// Print the instance
System.out.println("\n\n The Instances are: \n\n" + BeliefBase);
Instances newData= Datasetter.fromStringToNominal(BeliefBase, 3 , 4 , 5 );
System.out.println("\n\n The Filtered Instances are: \n\n" + newData);
}
/**
* This test creates a Perceptron to Classify Instances from an ARFF file
* Beliefbase becomes the imput for WEKA algorithms.
*/
public void test3(){
try{
// load data
ArffLoader loader = new ArffLoader();
loader.setFile(new File("BelWeka.arff"));
Instances structure = loader.getStructure();
Instances data = loader.getDataSet();
structure.addAll(data);
// System.out.println(" Structure: " + structure);
structure.setClassIndex(structure.numAttributes() - 1);
// train NaiveBayes
MultilayerPerceptron nn = new MultilayerPerceptron();
nn.buildClassifier(structure);
System.out.println(" Perceptron created1: " + nn);
Attribute att;
Instance newInstance = new DenseInstance(structure.numAttributes());
for(int i=0; i<structure.numAttributes()-1; i++){
att = structure.attribute(i);
System.out.println(" att " + att);
newInstance.setValue(att, att.enumerateValues().nextElement().toString() );
}
// nn.classifyInstance(newInstance);
/*while ((current = loader.getNextInstance(structure)) != null){
nn.classifyInstance(current);
}*/
System.out.println(" Perceptron created2: " + nn);
}catch(Exception e){
e.printStackTrace();
}
}
}