Package statechum.analysis.learning.rpnicore

Examples of statechum.analysis.learning.rpnicore.LearnerGraph


  }

  @Test
  public final void testPairCompatible_general_F()
  {
    LearnerGraph fsm = FsmParser.buildLearnerGraph("I-a->A1-a->A2-b->A3-c->A4 / I-b->B1-a->B2-b->B3 / I-c->C1-a->C2-b->C3", "testPairCompatible_general_Fa",config,getLabelConverter());
    Collection<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
    fsm.pairscores.computePairCompatibilityScore_general(null,Arrays.asList(new StatePair[]{
        new StatePair(fsm.findVertex("I"),fsm.findVertex("A1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("B1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("C1"))
        }), verticesToMerge);
    LearnerGraph mergeOutcome =  MergeStates.mergeCollectionOfVertices(fsm, null, verticesToMerge);
    LearnerGraph expected = FsmParser.buildLearnerGraph("I-b->I-c->I / I-a->I", "testPairCompatible_general_Fb",config,getLabelConverter());
    DifferentFSMException diffEx = WMethod.checkM(expected, mergeOutcome);
    if (diffEx != null)
      throw diffEx;
  }
View Full Code Here


  }

  @Test
  public final void testPairCompatible_general_G()
  {
    LearnerGraph fsm = FsmParser.buildLearnerGraph("I-d->A1-a->A2-b->A3-c->A4 / I-b->B1-a->B2-b->B3 / I-c->C1-a->C2-b->C3 / A1-b->T1-c->T1 / A1-c->T2-a->A2 / B1-b->T2-e->B2 / C1-c->T3-a->T4", "testPairCompatible_general_Ga",config,getLabelConverter());
    Collection<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
    fsm.pairscores.computePairCompatibilityScore_general(null,Arrays.asList(new StatePair[]{
        new StatePair(fsm.findVertex("I"),fsm.findVertex("A1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("B1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("C1"))
        }), verticesToMerge);
    LearnerGraph mergeOutcome =  MergeStates.mergeCollectionOfVertices(fsm, null, verticesToMerge);
    LearnerGraph expected = FsmParser.buildLearnerGraph("I-d->I-b->I-c->I / I-a->B2-b->B3-c->C1 / I-e->B2", "testPairCompatible_general_Gb",config,getLabelConverter());
    DifferentFSMException diffEx = WMethod.checkM(expected, mergeOutcome);
    if (diffEx != null)
      throw diffEx;
  }
View Full Code Here

  }

  @Test
  public final void testPairCompatible_general_H()
  {
    LearnerGraph fsm = FsmParser.buildLearnerGraph("I-d->A1-a->A2-b->A3-c->A4 / I-b->B1-a->B2-b->B3 / I-c->C1-a->C2-b->C3 / A1-b->T1-c->T1 / A1-c->T2-a->A2 / B1-b->T2-d->B2 / C1-c->T3-a->T4", "testPairCompatible_general_Ha",config,getLabelConverter());
    Collection<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
    fsm.pairscores.computePairCompatibilityScore_general(null,Arrays.asList(new StatePair[]{
        new StatePair(fsm.findVertex("I"),fsm.findVertex("A1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("B1")),new StatePair(fsm.findVertex("I"),fsm.findVertex("C1"))
        }), verticesToMerge);
    LearnerGraph mergeOutcome =  MergeStates.mergeCollectionOfVertices(fsm, null, verticesToMerge);
    LearnerGraph expected = FsmParser.buildLearnerGraph("I-b->I-c->I / I-a->I-d->I", "testPairCompatible_general_Hb",config,getLabelConverter());
    DifferentFSMException diffEx = WMethod.checkM(expected, mergeOutcome);
    if (diffEx != null)
      throw diffEx;
  }
View Full Code Here

 
  @Test
  public final void testKtails1()
  {
    testConfig.setKlimit(0);
    LearnerGraph fsm = FsmParser.buildLearnerGraph("A-a->B-b->C-a->D-d->E / B-c->F-a->G-e->H / M-a-#N / U-a-#Q", "testKtails1",testConfig,getLabelConverter());
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("N")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("N")), false));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("M")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("M")), false));
   
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("H")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("H")), false));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("H"),fsm.findVertex("M")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("H"),fsm.findVertex("M")), false));
   
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("Q")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("Q")), false));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("Q"),fsm.findVertex("N")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("Q"),fsm.findVertex("N")), false));
  }
View Full Code Here

   * @return trimmed reference graph
   */
  public static LearnerGraph trimUncoveredTransitions(LearnerGraph pta,LearnerGraph reference)
  {
    Configuration shallowCopy = reference.config.copy();shallowCopy.setLearnerCloneGraph(false);
    LearnerGraph outcome = new LearnerGraph(shallowCopy);AbstractLearnerGraph.copyGraphs(reference, outcome);

    for(Entry<CmpVertex,Set<Label>> entry:identifyUncoveredTransitions(pta, reference).entrySet())
    {
      Map<Label,CmpVertex> map = outcome.transitionMatrix.get(entry.getKey());
      for(Label lbl:entry.getValue()) map.remove(lbl);
View Full Code Here

    return outcome;
  }
 
  public static LearnerGraph buildFirstOrderMarkovGraph(MarkovClassifier ptaClassifier, LearnerGraph referenceGraph)
  {
    LearnerGraph outcome = new LearnerGraph(ptaClassifier.graph.config);
    if (ptaClassifier.model.getChunkLen() < 2)
      throw new IllegalArgumentException("not enough data for a first-order Markov model");
   
    LearnerGraph trimmedReference = trimUncoveredTransitions(ptaClassifier.graph,referenceGraph);
   
    ptaClassifier.updateMarkov(false);
 
    Map<Label,CmpVertex> states = new TreeMap<Label,CmpVertex>();
    for(Label l:ptaClassifier.graph.getCache().getAlphabet())
    {
      CmpVertex state = AbstractLearnerGraph.generateNewCmpVertex(VertexID.parseID(l.toString()), ptaClassifier.graph.config);states.put(l,state);
      outcome.transitionMatrix.put(state, outcome.createNewRow());
    }
    for(Entry<Label,CmpVertex> state:states.entrySet())
    {
      for(Label label:ptaClassifier.graph.getCache().getAlphabet())
      {
        MarkovOutcome transition = ptaClassifier.model.predictionsMatrix.get(new Trace(Arrays.asList(new Label[]{state.getKey(),label}),true));
        if (transition != null)
          if (transition == MarkovOutcome.positive) outcome.transitionMatrix.get(state.getValue()).put(label,states.get(label));
      }
    }   

    List<List<Label>> uniqueSequences = new LinkedList<List<Label>>();
    for(Label l1:ptaClassifier.graph.getCache().getAlphabet())
    {
      boolean nonUnique = false;
      Label unique = null;
      for(Label lbl:ptaClassifier.graph.getCache().getAlphabet())
      {
        if (ptaClassifier.model.predictionsMatrix.containsKey(new Trace(Arrays.asList(new Label[]{l1,lbl}),true)))
        {
          if (unique == null)
            unique = lbl;
          else
          {
            nonUnique = true;break;
          }
        }
      }
      if (unique != null && !nonUnique)
        uniqueSequences.add(Arrays.asList(new Label[]{l1,unique}));
     
    }
   
    List<List<Label>> sequencesUnique2=new LinkedList<List<Label>>();
    for(List<Label> prefix:uniqueSequences)
      {
        boolean nonUnique = false;
        List<Label> unique = null;
        for(Label lbl:ptaClassifier.graph.getCache().getAlphabet())
        {
          List<Label> seq = new LinkedList<Label>(prefix);seq.add(lbl);
          if (ptaClassifier.model.predictionsMatrix.containsKey(new Trace(seq,true)))
          {
            if (unique == null)
              unique = seq;
            else
            {
              nonUnique = true;break;
            }
          }
        }
       
        if (nonUnique == false && unique!= null)
          sequencesUnique2.add(unique);
      }
   
    for(List<Label> seq:sequencesUnique2)
    {
      System.out.println(seq);
      for(CmpVertex vert:trimmedReference.transitionMatrix.keySet())
      {
        CmpVertex target=trimmedReference.getVertex(vert,seq);
        if (target != null)
        {
          System.out.println("\t -> "+target);
          CmpVertex stateOfInterest = trimmedReference.getVertex(vert,seq.subList(0, 2));
          System.out.println("\t "+trimmedReference.transitionMatrix.get(stateOfInterest));
        }
       
      }
    }
View Full Code Here

  /** Similar to above, k limit is 1. */
  @Test
  public final void testKtails2()
  {
    testConfig.setKlimit(1);
    LearnerGraph fsm = FsmParser.buildLearnerGraph("A-a->B-b->C-a->D-d->E / B-c->F-a->G-e->H / M-a-#N / U-b-#Q", "testKtails2",testConfig,getLabelConverter());
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("N")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("N")), false));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("M")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("M")), false));
   
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("U")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("U")), false));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("U"),fsm.findVertex("M")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("U"),fsm.findVertex("M")), false));
   
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("Q")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("N"),fsm.findVertex("Q")), false));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("Q"),fsm.findVertex("N")), true));
    Assert.assertEquals(0,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("Q"),fsm.findVertex("N")), false));
  }
View Full Code Here

  public static LearnerGraph checkIfSingleStateLoopsCanBeFormed(MarkovClassifier ptaClassifier,LearnerGraph referenceGraph,final Collection<List<Label>> pathsOfInterest)
  {
    List<StatePair> pairsList = ptaClassifier.buildVerticesToMergeForPath(pathsOfInterest);
    LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
    ptaClassifier.graph.pairscores.computePairCompatibilityScore_general(null, pairsList, verticesToMerge);
    LearnerGraph merged = MergeStates.mergeCollectionOfVertices(ptaClassifier.graph, null, verticesToMerge);// after merging all paths of interest, we get this graph.
    ConsistencyChecker checker = new MarkovClassifier.DifferentPredictionsInconsistency();
    final long genScoreThreshold = 1;
    int nrOfMergers=0;
    List<StatePair> pairsToMerge = new LinkedList<StatePair>();
    for(Entry<CmpVertex,Map<Label,CmpVertex>> entry:merged.transitionMatrix.entrySet())
      for(Entry<Label,CmpVertex> transition:entry.getValue().entrySet())
        if (merged.transitionMatrix.get(transition.getValue()).containsKey(transition.getKey()))
        {// we have a potential loop
          PairScore p = new PairScore(entry.getKey(),transition.getValue(),0,0);
          ArrayList<PairScore> pairOfInterest = new ArrayList<PairScore>(1);pairOfInterest.add(p);
          List<PairScore> correctPairs = new ArrayList<PairScore>(1), wrongPairs = new ArrayList<PairScore>(1);
          SplitSetOfPairsIntoRightAndWrong(ptaClassifier.graph, referenceGraph, pairOfInterest, correctPairs, wrongPairs);
         
          verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
          long genScore = ptaClassifier.graph.pairscores.computePairCompatibilityScore_general(p, null, verticesToMerge);
          LearnerGraph mergedForThisPair = MergeStates.mergeCollectionOfVertices(ptaClassifier.graph, null, verticesToMerge);
          long value = MarkovClassifier.computeInconsistency(mergedForThisPair, ptaClassifier.model, checker,false);
         
          boolean decidedToMerge= (value == 0 && genScore >= genScoreThreshold);
          if (decidedToMerge)
          {
View Full Code Here

 
  public static void showInconsistenciesForDifferentMergers(LearnerGraph referenceGraph,MarkovClassifier ptaClassifier, Collection<Set<CmpVertex>> verticesToMergeBasedOnInitialPTA)
  {
    LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
    int genScore = ptaClassifier.graph.pairscores.computePairCompatibilityScore_general(null, constructPairsToMergeBasedOnSetsToMerge(ptaClassifier.graph.transitionMatrix.keySet(),verticesToMergeBasedOnInitialPTA), verticesToMerge);
    LearnerGraph graph = MergeStates.mergeCollectionOfVertices(ptaClassifier.graph, null, verticesToMerge);
   
    Set<CmpVertex> tr=graph.transform.trimGraph(10, graph.getInit()).transitionMatrix.keySet();
    ConsistencyChecker checker = new MarkovClassifier.DifferentPredictionsInconsistency();

    constructPairsToMergeBasedOnSetsToMerge(graph.transitionMatrix.keySet(),verticesToMergeBasedOnInitialPTA);   
    for(CmpVertex v0:tr)
      for(CmpVertex v1:tr)
        if (v0 != v1)
        {
          PairScore p = new PairScore(v0,v1,0,0);
          ArrayList<PairScore> pairOfInterest = new ArrayList<PairScore>(1);pairOfInterest.add(p);
          List<PairScore> correctPairs = new ArrayList<PairScore>(1), wrongPairs = new ArrayList<PairScore>(1);
          SplitSetOfPairsIntoRightAndWrong(graph, referenceGraph, pairOfInterest, correctPairs, wrongPairs);
         
          verticesToMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
          genScore = graph.pairscores.computePairCompatibilityScore_general(p, null, verticesToMerge);
          LearnerGraph merged = MergeStates.mergeCollectionOfVertices(graph, null, verticesToMerge);
          long value = MarkovClassifier.computeInconsistency(merged, ptaClassifier.model, checker,false);
          if ( (wrongPairs.isEmpty() && value > 0) ||  (!wrongPairs.isEmpty() && value == 0))
          {
            System.out.println( p.toString()+(wrongPairs.isEmpty()?"valid, ":"invalid:")+value+ "(score "+genScore+")");
            System.out.println( "R: " + graph.transitionMatrix.get(p.getR())+" B: "+graph.transitionMatrix.get(p.getQ()));
View Full Code Here

  /** Only matches for any path. */
  @Test
  public final void testKtails11()
  {
    testConfig.setKlimit(2);
    LearnerGraph fsm = FsmParser.buildLearnerGraph("A-a->B-b->C-a->D-d->E / B-c->F-a->G-e->H / M-b->N-a->Q / M-c-#P", "testKtails11",testConfig,getLabelConverter());
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("B")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("M"),fsm.findVertex("B")), false));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("B"),fsm.findVertex("M")), true));
    Assert.assertEquals(-1,fsm.pairscores.computeStateScoreKTails(new StatePair(fsm.findVertex("B"),fsm.findVertex("M")), false));
 
View Full Code Here

TOP

Related Classes of statechum.analysis.learning.rpnicore.LearnerGraph

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.