Package unbbayes.prs.mebn.ssbn

Examples of unbbayes.prs.mebn.ssbn.SSBN


      OVInstanceFaultException, InvalidParentException {

   
    long time0 = System.currentTimeMillis();
   
    SSBN ssbn = null;
   
    //Step 1:
    if(Boolean.valueOf(parameters.getParameterValue(LaskeyAlgorithmParameters.DO_INITIALIZATION))){
      ssbn = initialization(queryList, knowledgeBase);
      ssbn.setParameters(parameters);
    }
   
   
    ISSBNLogManager logManager = ssbn.getLogManager();
   
    //Step 2:
    if(Boolean.valueOf(parameters.getParameterValue(LaskeyAlgorithmParameters.DO_BUILDER))){
     
      if (logManager != null) {
        logManager.skipLine();
        logManager.printText(null, false, resourceLog.getString("004_Step2_BuildingGrandBN"));
        logManager.skipLine();
      }
     
      getBuilderStructure().buildStructure(ssbn);
     
      if (logManager != null) {
        logManager.printText(null, false, resourceLog.getString("010_StepFinished"));
        logManager.skipLine();
        logManager.printBox2Bar();
        logManager.printBox2("List of nodes: ");
       
        printSimpleSSBNNodeList(ssbn);
       
        logManager.printBox2Bar();
        logManager.skipLine();
        logManager.printSectionSeparation();
      }
     
    }
   
    //Step 3:
    if(Boolean.valueOf(parameters.getParameterValue(LaskeyAlgorithmParameters.DO_PRUNE))){
     
      if (logManager != null) {
        logManager.skipLine();
        ssbn.getLogManager().printText(null, false, resourceLog.getString("005_Step3_PruneGrandBN"));
        ssbn.getLogManager().skipLine();
      }
     
      getPruneStructure().pruneStructure(ssbn);
     
      if (logManager != null) {
        logManager.printText(null, false, resourceLog.getString("010_StepFinished"));
        logManager.skipLine();
        logManager.printBox2Bar();
        logManager.printBox2("List of nodes: ");
       
        printSimpleSSBNNodeList(ssbn);
       
        logManager.printBox2Bar();
        logManager.skipLine();
        logManager.printSectionSeparation();
      }
    }
   
    //Step 4:
    if(Boolean.valueOf(parameters.getParameterValue(LaskeyAlgorithmParameters.DO_CPT_GENERATION))){
     
      if (logManager != null) {
        logManager.skipLine();
        logManager.printText(null, false, resourceLog.getString("006_Step4_BuildCPT"));
        logManager.skipLine();
      }
     
      getBuildLocalDistribution().buildLocalDistribution(ssbn);
     
      if (logManager != null) {
        logManager.printText(null, false, resourceLog.getString("010_StepFinished"));
        logManager.skipLine();
        logManager.printSectionSeparation();
      }
    }
   
    // adjust position of nodes
    try {
      PositionAdjustmentUtils.adjustPositionProbabilisticNetwork(ssbn.getProbabilisticNetwork());
    } catch (Exception e) {
      Debug.println(getClass(), e.getMessage(), e);
    }
   
   
    if (logManager != null) {
      long time1 = System.currentTimeMillis();
      long deltaTime = time1 - time0;
     
      SSBNDebugInformationUtil.printAndSaveCurrentNetwork(ssbn);
      logManager.printBox1Bar();
      logManager.printBox1(resourceLog.getString("007_ExecutionSucces"));
      logManager.printBox1(resourceLog.getString("009_Time") + ": " + deltaTime + " ms");
      logManager.printBox1Bar();
      logManager.skipLine();
    }
   
    this.cleanUpSSBN(ssbn);
   
    if (ssbn.getNetwork().getNodeCount() != ssbn.getNetwork().getNodeIndexes().keySet().size()) {
      // inconsistency on the quantity of indexed nodes and actual nodes
      // force synchronization
      ssbn.getNetwork().getNodeIndexes().clear();
      for (int i = 0; i < ssbn.getNetwork().getNodes().size(); i++) {
        ssbn.getNetwork().getNodeIndexes().put(ssbn.getNetwork().getNodes().get(i).getName(), i);
      }
    }
   
    try {
      this.compileSSBNAndPropagateFindings(ssbn);
View Full Code Here


   * @param knowledgeBase
   * @return
   */
  protected SSBN initialization(List<Query> queryList, KnowledgeBase knowledgeBase){
   
    SSBN ssbn = new SSBN();
    if (!isLogEnabled()) {
      ssbn.setLogManager(null);
    }
   
    MultiEntityBayesianNetwork mebn = null;

    //log
    ISSBNLogManager logManager = ssbn.getLogManager();
    if (logManager != null) {
      logManager.printBox1Bar();
      logManager.printBox1(getResourceForHybridAlgorithm().getString("001_Title"));
      logManager.printBox1("Queries:");
      for(int i = 0; i < queryList.size(); i++){
        logManager.printBox1("    (" + i + ") " + queryList.get(i).toString());
      }
      logManager.printBox1(getResourceForHybridAlgorithm().getString("002_Algorithm") + ": " +
          getResourceForHybridAlgorithm().getString("002_001_LaskeyAlgorithm"));
      logManager.printBox1Bar();
     
      logManager.skipLine();
      logManager.printText(null, false, getResourceForHybridAlgorithm().getString("003_Step1_Initialization"));
      logManager.skipLine();
    }
   
    ssbn.setKnowledgeBase(knowledgeBase);
   
    //We assume that all the queries is referent to the same MEBN
    mebn = queryList.get(0).getMebn();
   
    //Parameters:

    IdentationLevel in = new IdentationLevel(null);
   
    //Add queries to the list of nodes
    IdentationLevel in1 = new IdentationLevel(in);
    if (logManager != null) {
      logManager.printText(in1, true,
        getResourceForHybridAlgorithm().getString("011_BuildingSSBNForQueries"));
    }
   
    for(Query query: queryList){
      SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(query.getResidentNode());
      query.setSSBNNode(ssbnNode);
     
      for(OVInstance argument : query.getArguments()){
        ssbnNode.setEntityForOv(argument.getOv(), argument.getEntity());  
      }
     
      ssbnNode.setFinished(false);
      ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
      ssbn.addQueryToTheQueryList(query);
     
      if (logManager != null) {
        logManager.printText(in1, false, " - " + ssbnNode);
      }
                                                                         
    }
   
    if (logManager != null) {
      logManager.skipLine();
    }
   
    //Add findings to the list of nodes
   
    in1 = new IdentationLevel(in);
    if (logManager != null) {
      logManager.printText(in1, true,
        getResourceForHybridAlgorithm().getString("012_BuildingSSBNForFindings"));
    }
   
    for(MFrag mFrag: mebn.getMFragList()){
      for(ResidentNode residentNode: mFrag.getResidentNodeList()){
        for(RandomVariableFinding finding: residentNode.getRandomVariableFindingList()){
         
          if (logManager != null) {
            logManager.printText(in1, false, " - " + finding);
          }
         
          ObjectEntityInstance arguments[] = finding.getArguments();
          List<OVInstance> ovInstanceList = new ArrayList<OVInstance>();
          for(int i = 0; i < arguments.length ; i++){
            OrdinaryVariable ov = residentNode.getArgumentNumber(i + 1).getOVariable();
            LiteralEntityInstance lei = LiteralEntityInstance.getInstance(arguments[i].getName() , ov.getValueType());
            ovInstanceList.add(OVInstance.getInstance(ov, lei));
          }
         
          SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(residentNode);
         
          for(OVInstance argument : ovInstanceList){
            ssbnNode.setEntityForOv(argument.getOv(), argument.getEntity());  
          }
          ssbnNode = ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
         
          ssbnNode.setState(finding.getState());
          ssbnNode.setFinished(false);
         
          ssbn.addFindingToTheFindingList(ssbnNode);
        }
      }
    }
   
    if (logManager != null) {
      logManager.skipLine();
      logManager.printText(null, false, getResourceForHybridAlgorithm().getString("010_StepFinished"));
      logManager.skipLine();
      logManager.printSectionSeparation();
    }
   
    // add continuous nodes to the list of initial nodes
    if (isToAddAllContinuousNodes()) {
      for(MFrag mfrag: mebn.getMFragList()){
        // we are creating one continuous SSBN node per each possible combination of arguments in a continuous resident node
        for (Node node : mfrag.getNodes()) {
          if (node instanceof ContinuousResidentNode) {
           
            ContinuousResidentNode continuousResidentNode = (ContinuousResidentNode) node;
            if (continuousResidentNode.getOrdinaryVariableList() == null
                || continuousResidentNode.getOrdinaryVariableList().isEmpty()) {
              continue// go to next node
            }
           
            // prepare all possible combinations of arguments. The ovs are synchronized by index
            boolean hasAtLeastOneCombination = true// true if all Lists in combinator were filled
            List<List<LiteralEntityInstance>> combinator = new ArrayList<List<LiteralEntityInstance>>();
            for (OrdinaryVariable ov : continuousResidentNode.getOrdinaryVariableList()) {
              List<LiteralEntityInstance> contentOfCombinator = new ArrayList<LiteralEntityInstance>();
              // get all values for ov
              List<String> ovValues = knowledgeBase.getEntityByType(ov.getValueType().getName());
              if (ovValues.isEmpty()) {
                // this iterator cannot be filled, because ov has no possible values
                hasAtLeastOneCombination = false;
                break;
              }
              // add all possible values for ov
              for (String ovValue : ovValues) {
                contentOfCombinator.add(LiteralEntityInstance.getInstance(ovValue, ov.getValueType()));
              }
              combinator.add(contentOfCombinator);
            }
           
            // do not add node if there is some ov with no possible value (that means, no combination can be created)
            if (!hasAtLeastOneCombination) {
              continue// go to next continuous node
            }
           
            // list of indexes of combinator
            List<Integer> indexes = new ArrayList<Integer>(combinator.size());
            for (int i = 0; i < combinator.size(); i++) {
              // Note that at this point, each list in combinator has at least 1 element, because hasAtLeastOneCombination == true (so, 0 is a valid index)
              indexes.add(0);
            }
           
            /*
             * iterate on combinator using indexes.
             * E.g.:  combinator = {{a,b},{1,2}};
             *       then indexes must iterate like:
             *       {0,0}  (means (a,1));
             *       {1,0}  (means (b,1));
             *       {0,1}  (means (a,2));
             *       {1,1}  (means (b,2));
             *  
             */
            while (hasAtLeastOneCombination) {
             
              // create a continuous ssbn node for each possible combination of combinator
              SimpleSSBNNode ssbnNode = SimpleSSBNNode.getInstance(continuousResidentNode);
              ssbnNode.setFinished(false);
             
              // fill arguments of ssbnNode
              for (int i = 0; i < continuousResidentNode.getOrdinaryVariableList().size(); i++) {
                ssbnNode.setEntityForOv(
                    continuousResidentNode.getOrdinaryVariableList().get(i),
                    combinator.get(i).get(indexes.get(i))
                );
              }
             
              if (logManager != null) {
                logManager.printText(in1, false, " - " + ssbnNode);
              }
             
              ssbn.addSSBNNodeIfItDontAdded(ssbnNode);
             
              // update indexes and check condition to end loop (it ends when all possible combinations were visited)
              for (int i = 0; i < combinator.size(); i++) { 
                // OBS. combinator, indexes and continuousResidentNode.getOrdinaryVariableList() have same size and are synchronized.
                Integer newIndex = indexes.get(i) + 1// obtain current step + 1
View Full Code Here

//    mebn.getObjectEntityContainer().addEntityInstance(new ObjectEntityInstance("T1", mebn.getObjectEntityContainer().getObjectEntityByName("TimeStep")));
    mebn.getObjectEntityContainer().addEntityInstance(new ObjectEntityInstance("T2", mebn.getObjectEntityContainer().getObjectEntityByName("TimeStep")));
   
   
    probabilisticNetwork = new ProbabilisticNetwork(mebn.getName());
    SSBN ssbn = new SSBN();
    ssbn.setNetwork(probabilisticNetwork);
   
    speedOBJ1T2 = new ProbabilisticNode();
    speedOBJ1T2.setName(residentSpeed.getName() + "_OBJ1_T2");
    probabilisticNetwork.addNode(speedOBJ1T2);
    SSBNNode ssbnNodeSpeedOBJ1T2 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T2);
    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T2);
    ssbnNodeSpeedOBJ1T2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("t"), "T2");
   
    // Speed(OBJ1, T0),
//    ProbabilisticNode speedOBJ1T0 = new ProbabilisticNode();
//    speedOBJ1T0.setName(residentSpeed.getName() + "_OBJ1_T0");
//    probabilisticNetwork.addNode(speedOBJ1T0);
//    SSBNNode ssbnNodeSpeedOBJ1T0 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T0);
//    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T0);
//    ssbnNodeSpeedOBJ1T0.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
//    ssbnNodeSpeedOBJ1T0.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("tPrev"), "T0");
//    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeSpeedOBJ1T0, false);  // this should also add the edge
   
    // Speed(OBJ1, T1),
    ProbabilisticNode speedOBJ1T1 = new ProbabilisticNode();
    speedOBJ1T1.setName(residentSpeed.getName() + "_OBJ1_T1");
    probabilisticNetwork.addNode(speedOBJ1T1);
    SSBNNode ssbnNodeSpeedOBJ1T1 = SSBNNode.getInstance(probabilisticNetwork, residentSpeed, speedOBJ1T1);
    ssbn.getSsbnNodeList().add(ssbnNodeSpeedOBJ1T1);
    ssbnNodeSpeedOBJ1T1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("tPrev"), "T1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeSpeedOBJ1T1, false)// this should also add the edge
   
    // TerrainType(RGN1),
    ResidentNode terrainTypeResident = mebn.getDomainResidentNode("TerrainType");
    ProbabilisticNode terrainTypeRGN1 = new ProbabilisticNode();
    terrainTypeRGN1.setName(terrainTypeResident.getName() + "_RGN1");
    probabilisticNetwork.addNode(terrainTypeRGN1);
    SSBNNode ssbnNodeTerrainTypeRGN1 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN1);
    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN1);
    ssbnNodeTerrainTypeRGN1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN1,false)// this should also add the edge
   
    // TerrainType(RGN2),
    ProbabilisticNode terrainTypeRGN2 = new ProbabilisticNode();
    terrainTypeRGN2.setName(terrainTypeResident.getName() + "_RGN2");
    probabilisticNetwork.addNode(terrainTypeRGN2);
    SSBNNode ssbnNodeTerrainTypeRGN2 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN2);
    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN2);
    ssbnNodeTerrainTypeRGN2.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN2");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN2,false)// this should also add the edge
   
    // TerrainType(RGN3),
//    ProbabilisticNode terrainTypeRGN3 = new ProbabilisticNode();
//    terrainTypeRGN3.setName(terrainTypeResident.getName() + "_RGN3");
//    probabilisticNetwork.addNode(terrainTypeRGN3);
//    SSBNNode ssbnNodeTerrainTypeRGN3 = SSBNNode.getInstance(probabilisticNetwork, terrainTypeResident, terrainTypeRGN3);
//    ssbn.getSsbnNodeList().add(ssbnNodeTerrainTypeRGN3);
//    ssbnNodeTerrainTypeRGN3.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("rgn"), "RGN3");
//    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeTerrainTypeRGN3,false);  // this should also add the edge
   
    // ObjectType(OBJ1)
    ResidentNode objectTypeResident = mebn.getDomainResidentNode("ObjectType");
    ProbabilisticNode objectTypeOBJ1 = new ProbabilisticNode();
    objectTypeOBJ1.setName(objectTypeResident.getName() + "_OBJ1");
    probabilisticNetwork.addNode(objectTypeOBJ1);
    SSBNNode ssbnNodeObjectTypeOBJ1 = SSBNNode.getInstance(probabilisticNetwork, objectTypeResident, objectTypeOBJ1);
    ssbn.getSsbnNodeList().add(ssbnNodeObjectTypeOBJ1);
    ssbnNodeObjectTypeOBJ1.addArgument(residentSpeed.getMFrag().getOrdinaryVariableByName("obj"), "OBJ1");
    ssbnNodeSpeedOBJ1T2.addParent(ssbnNodeObjectTypeOBJ1,false)// this should also add the edge
   
    try {
      tableParser = (ContinuousResidentNodeLPDCompiler)ContinuousResidentNodeLPDCompiler.newInstance(residentSpeed, ssbnNodeSpeedOBJ1T2);
View Full Code Here

TOP

Related Classes of unbbayes.prs.mebn.ssbn.SSBN

Copyright © 2018 www.massapicom. 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.