package trust.weka4jason;
import java.io.File;
import weka.classifiers.Classifier;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
public class WekaPerceptronClassifier extends WekaClassifier{
public WekaPerceptronClassifier(){
super.classifier = new MultilayerPerceptron();
}
public Classifier getClassifier() throws Exception{
//filter string attributes
dataset.nominalFilter();
Instances data = dataset.getDataset();
//set class attribute
data.setClassIndex(data.numAttributes()-1);
//set dataset
setDataset(data);
//build classifier
classifier.buildClassifier(data);
return classifier;
}
public double agentTrustEvuation(Object... list) throws Exception {
Instance agent = dataset.createNominalInstance(list);
if(agent == null){
//System.out.println("Agent "+agent);
return 1;
}
else{
testInstance(agent);
//System.out.println("Agent "+agent);
return agent.classValue();
}
}
public static void main(String[] args){
try{
final long startTime = System.currentTimeMillis();
final long endTime;
final float duration;
// load data
ArffLoader loader = new ArffLoader();
loader.setFile(new File("./src/trust/weka4jason/trainingSet.arff"));
Instances data = loader.getDataSet();
WekaPerceptronClassifier classifier= new WekaPerceptronClassifier();
System.out.println(classifier);
//set options
classifier.setWekaOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H 4 -C -I -R");
classifier.setDataset(data);
classifier.getClassifier();
endTime = System.currentTimeMillis();;
duration = (endTime - startTime)/1000F;
System.out.println("\nElapsed time: "+(duration)+"sec");
//test on instance
Instance instance = new DenseInstance(5);
instance.setValue(data.attribute("Task"), "chickenpox");
instance.setValue(data.attribute("Ability"), "pediatrician");
instance.setValue(data.attribute("Disposition"), "distracted");
instance.setValue(data.attribute("Cross"), "female");
classifier.testInstance(instance);
System.out.println(instance);
//classifier.getWekaDataset().stringFilter();
System.out.println(classifier.getWekaDataset().getDataset());
classifier.addInstanceToDataset(1000, "chichenpox", "pediatrician", "distracted", "female", 0);
classifier.getWekaDataset().nominalFilter();
System.out.println(classifier.getWekaDataset().getDataset());
System.out.println(classifier.agentTrustEvuation("chickenpox", "pediatrician", "distracted", "female"));
}catch(Exception e){
e.printStackTrace();
}
}
}