Package trust.jfcm.learning.examples

Source Code of trust.jfcm.learning.examples.Test

package trust.jfcm.learning.examples;


import java.text.NumberFormat;
import java.util.Locale;
import java.io.FileReader;
import java.io.InputStream;

import trust.jfcm.learning.FcmLearning;
import trust.jfcm.learning.FcmLearningIO;
import trust.jfcm.learning.FcmTrainingSet;
import trust.jfcm.learning.InputLearningConcept;
import trust.jfcm.learning.LearningWeightedConnection;
import trust.jfcm.learning.supervised.LinearBP;
import trust.jfcm.learning.training.LinearRegression;
import trust.jfcm.utils.FcmRunner;
import trust.jfcm.utils.SimpleFcmRunner;


public class Test {
 
  FcmRunner runner;
  FcmLearning map;
  LinearRegression trainer;
 
  FcmTrainingSet dataset;
 
  public Test() {
    String fcm_xml = "FcmLearning.xml";
   
    try{
      InputStream xmlStream = getClass().getResourceAsStream(fcm_xml);
      map = (FcmLearning) FcmLearningIO.loadMapFromXml(xmlStream).get("agent-map");
      runner = new SimpleFcmRunner(map, 0.000001, 1000);
      //System.out.println(map);
     
      //System.out.println("Trying to clone map...");
      //map = (CognitiveMap) map.clone();
      runner = new SimpleFcmRunner(map, 0.000001, 1000);
      //System.out.println(map);
     
     
     
     
     
    } catch(Exception e){
      // TODO Auto-generated catch block
      System.out.println("The resource:\'" + fcm_xml + "\'  could not be located ");
      e.printStackTrace();
    }   
  }
 
  /**
   * Scenario for the Medical Doctor Pag 320
   * @throws Exception
   */
  public void set_scenario() throws Exception {
    map.resetConcepts();
   
    //BELIEFS
    //check if node ability exists
    /*
    if(map.getConcepts().containsKey("ability")){
      map.getConcepts().get("ability").setOutput(0.3);
      map.getConcepts().get("ability").setFixedOutput(true);
     
      map.getConcepts().get("disposition").setOutput(0.7);
      map.getConcepts().get("disposition").setFixedOutput(true);
     
    }*/
    map.getConcepts().get("efactors").setFixedOutput(false);
   
    InputLearningConcept ped = new InputLearningConcept();
    ped.setName("pediatrician");
    ped.setFixedOutput(true);
    ped.setTrainingFunction(new LinearBP());
    map.addConcept(ped);
    LearningWeightedConnection conn1 = new LearningWeightedConnection();
    conn1.setName("ped_ability");
    conn1.setFrom(ped);
    conn1.setTo(map.getConcept("static_ability"));
    conn1.setWeight(0.9);
    map.addConnection(conn1);
    map.connect(conn1.getFrom().getName(), conn1.getName(), conn1.getTo().getName());
   
    InputLearningConcept ent = new InputLearningConcept();
    ent.setName("ent");
    ent.setFixedOutput(true);
    ent.setTrainingFunction(new LinearBP());
    map.addConcept(ent);
    LearningWeightedConnection conn10 = new LearningWeightedConnection();
    conn10.setName("ent_ability");
    conn10.setFrom(ent);
    conn10.setTo(map.getConcept("static_ability"));
    conn10.setWeight(0.3);
    map.addConnection(conn10);
    map.connect(conn10.getFrom().getName(), conn10.getName(), conn10.getTo().getName());
   
   
    InputLearningConcept cautious = new InputLearningConcept();
    cautious.setName("cautious");
    cautious.setOutput(1.0);
    cautious.setFixedOutput(true);
    cautious.setTrainingFunction(new LinearBP());
    map.addConcept(cautious);
    LearningWeightedConnection conn2 = new LearningWeightedConnection();
    conn2.setName("cautious_disposition");
    conn2.setFrom(cautious);
    conn2.setTo(map.getConcept("static_disposition"));
    conn2.setWeight(0.9);
    map.addConnection(conn2);
    map.connect(conn2.getFrom().getName(), conn2.getName(), conn2.getTo().getName());
   
    InputLearningConcept female = new InputLearningConcept();
    female.setName("female");
    female.setOutput(1.0);
    female.setFixedOutput(true);
    female.setTrainingFunction(new LinearBP());
    map.addConcept(female);
    LearningWeightedConnection conn3 = new LearningWeightedConnection();
    conn3.setName("female_cross");
    conn3.setFrom(female);
    conn3.setTo(map.getConcept("cross"));
    conn3.setWeight(0.9);
    map.addConnection(conn3);
    map.connect(conn3.getFrom().getName(), conn3.getName(), conn3.getTo().getName());
   
    map.getConcept("static_disposition").setBinary(true);
    map.getConcept("static_ability").setBinary(true);
    map.getConcept("cross").setBinary(true);
  }
 
 
  public void test_scenario(boolean partialTree) throws Exception{
   
    //Print initial values
   
   
    set_scenario();
   

    System.out.println("Adjacency Matrix...");
    System.out.println(map.adjacencyMatrix());
   
   
    // run map and show results
    System.out.println("\n Initial values:" + map.toString() + "\n ");
    map.normalizeNetworkWeights(false);
    System.out.println("\n Normalized net:" + map.toString() + "\n ");
   
    map.getConcept("pediatrician").setOutput(1.0);
    map.getConcept("cautious").setOutput(1.0);
    map.getConcept("female").setOutput(1.0);
    map.getConcept("ent").setOutput(0.0);
    showResults("Scenario", runner.converge());
    System.out.println("\n Converged:"+map);
    System.out.println("\n***********\n Trust for Pedriatrician is:\n" + map.getConcepts().get("trust").getOutput() + "\n***********");
   
    map.resetConcepts();
   
    map.getConcept("ent").setOutput(1.0);
    map.getConcept("cautious").setOutput(1.0);
    map.getConcept("female").setOutput(1.0);
    map.getConcept("pediatrician").setOutput(0.0);
    //runner = new SimpleFcmRunner(map, 0.000001, 1000);
    showResults("Scenario", runner.converge());
    System.out.println("\n Converged:"+map);
    System.out.println("\n***********\n Trust for Ent is:\n" + map.getConcepts().get("trust").getOutput() + "\n***********");
   
    dataset = FcmLearningIO.loadTrainingSetFromFile(new FileReader("./src/trust/jfcm/learning/examples/FCMTrainingSet.txt"), (FcmLearning) map);
   
    System.out.println("\n ********* TRAINING SET **********\n");
    System.out.println(dataset);
   
   
   
    map.setTrainingSet(dataset);
    trainer = new LinearRegression(map);
    trainer.train();
   
    map.resetConcepts();
   
    System.out.println("\n ********* Computing trust for Pediatrician AFTER training **********\n");
    map.getConcept("pediatrician").setOutput(1.0);
    map.getConcept("cautious").setOutput(1.0);
    map.getConcept("female").setOutput(1.0);
    map.getConcept("ent").setOutput(0.0);
    showResults("Scenario", runner.converge());
    System.out.println("\n Converged:"+map);
    System.out.println("\n***********\n Trust for Pedriatrician is:\n" + map.getConcepts().get("trust").getOutput() + "\n***********");
   
    map.resetConcepts();
   
    System.out.println("\n ********* Computing trust for Ent AFTER training **********\n");
    map.getConcept("ent").setOutput(1.0);
    map.getConcept("cautious").setOutput(1.0);
    map.getConcept("female").setOutput(1.0);
    map.getConcept("pediatrician").setOutput(0.0);
    showResults("Scenario", runner.converge());
    System.out.println("\n Converged:"+map);
    System.out.println("\n***********\n Trust for Ent is:\n" + map.getConcepts().get("trust").getOutput() + "\n***********");
   
    //System.out.println(dataset.size());
   
    System.out.println(map.adjacencyMatrix());
 
   
  }
 
 
  /*
   *  Agent class
  */
  public void run() {
    try {

     
      test_scenario(true);

    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.out.println("Some Problem Occurs in running the FCM...");
      e.printStackTrace();
    }
  }

  void showResults(String scenario, boolean converged) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
    nf.setMaximumFractionDigits(8);
    //System.out.print(scenario + "\t" + converged);
   
    /*
    for (Concept c : map.getConcepts().values()) {
      System.out.print("\t");
      System.out.print(c.getOutput() != null ? nf.format(c.getOutput()) : "");
    }*/
   
  }

  public static void main(String[] args) {
    Test example = new Test();
   
    example.run();
  }



}
TOP

Related Classes of trust.jfcm.learning.examples.Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.