Ejemplo n.º 1
0
    public double Satisfy (State s, Finally @finally)
    {
        // Search states such that enclosed in finally is true
        var validStates = States.Where (x => Satisfy(x, @finally.Enclosed) > 0);
        if (validStates.Contains (s)) 
            return 1;
        else if (validStates.Count () == 0)
            return 0;
        
        Console.WriteLine ("---------- [States satisfying enclosed]");
        Console.WriteLine (string.Join (", ", validStates.Select (x => x.Identifier)));
        Console.WriteLine ("----------");
        
        var smToValidStates = new MarkovChain ();
        foreach (var state in validStates) {
            DFS (validStates, smToValidStates);
        }
        
        Console.WriteLine ("---------- [States in S tilde]");
        Console.WriteLine (string.Join (", ", smToValidStates.States.Select (x => x.Identifier)));
        Console.WriteLine ("----------");
        
        var table = smToValidStates.BuildConversionTable ();
        var transitions = smToValidStates.GetTransitionMatrix(table);
        
        /*
            Console.WriteLine ("---------- [Conversion Table]");
            Console.WriteLine (string.Join ("\n", table.Select (x => x.Key + ":" + x.Value)));
            Console.WriteLine ("----------");

            Console.WriteLine ("---------- [Transitions]");
            Console.WriteLine (transitions);
            Console.WriteLine ("----------");
            */
        
        // Build the transition matrix for this sub state-machine
        
        var system = new DiagonalMatrix (transitions.RowCount, transitions.ColumnCount, 1)
            - transitions;
        
        /*
            Console.WriteLine ("---------- [System]");
            Console.WriteLine (system);
            Console.WriteLine ("----------");
            */
        
        var vector = new DenseVector (
            smToValidStates.States.Select (x => 
                                       Transitions.Where (y => y.From.Equals (x))
                                       .Where (y => validStates.Contains (y.To))
                                       .Select (y => y.Probability).Sum ()
                                       ).ToArray ()
            );
        /*
            Console.WriteLine ("---------- [Vector]");
            Console.WriteLine (vector);
            Console.WriteLine ("----------");
            */
        
        var result = system.LU ().Solve (vector);
        
        /*
            Console.WriteLine ("---------- [Result]");
            Console.WriteLine (result);
            Console.WriteLine ("----------");
            */
        
        return table.ContainsKey (s.Identifier) ? result[table[s.Identifier]] : 0;
    }
Ejemplo n.º 2
0
 public double Satisfy (State currentState, Until until)
 {
     if (caching.ContainsKey (until)) {
         return caching[until][currentState];
     }
     
     caching[until] = new Dictionary<State, double> ();
     
     var B = until.Left;
     var C = until.Right;
     
     // Compute SO
     var s0 = States.Except (ComputeNotS0 (States.Where (s => Satisfy (s, B) == 1), C));
     Console.WriteLine ("S0 : " + string.Join (",", s0.Select (s => s.Identifier)));
     
     foreach (var s in s0) 
         caching[until].Add (s, 0);
     
     // Compute S1
     var s1 = ComputeS1 (B, C);
     Console.WriteLine ("S1 : " + string.Join (",", s1.Select (s => s.Identifier)));
     
     foreach (var s in s1) 
         caching[until].Add (s, 1);
     
     // Compute S?
     var spending = States.Except (s0.Union(s1));
     Console.WriteLine ("S? : " + string.Join (",", spending.Select (s => s.Identifier)));
     
     if (spending.Count() > 0) {
         // Build a SM with only states from S?
         var sm = new MarkovChain ();
         foreach (var s in spending) {
             sm.Add (s);
             foreach (var t in spending) {
                 var transition = Transitions.Where (trans => trans.From.Equals (s)
                                                     & trans.To.Equals (t)).SingleOrDefault ();
                 if (transition != null) 
                     sm.Add (transition);
             }
         }
         
         var table = sm.BuildConversionTable ();
         var A = sm.GetTransitionMatrix (table);
         var b = new DenseVector (
             sm.States.Select (x => 
                           Transitions.Where (y => y.From.Equals (x))
                           .Where (y => s1.Contains (y.To))
                           .Select (y => y.Probability).Sum ()
                           ).ToArray ()
             );
         
         Console.WriteLine ("---------- [Conversion Table]");
         Console.WriteLine (string.Join ("\n", table.Select (x => x.Key + ":" + x.Value)));
         Console.WriteLine ("----------");
         
         Console.WriteLine ("---------- [A]");
         Console.WriteLine (A);
         Console.WriteLine ("----------");
         
         Console.WriteLine ("---------- [b]");
         Console.WriteLine (b);
         Console.WriteLine ("----------");
         
         var epsilon = 0.001;
         
         MathNet.Numerics.LinearAlgebra.Generic.Vector<double> prevsol = new DenseVector (b.Count, 0);
         MathNet.Numerics.LinearAlgebra.Generic.Vector<double> currentsol = new DenseVector (b.Count, 0);
         do {
             prevsol = currentsol;
             currentsol = A.Multiply (prevsol) + b;
             
         } while ((prevsol - currentsol).AbsoluteMaximum() > epsilon);
         
         Console.WriteLine ("---------- [solution]");
         Console.WriteLine (currentsol);
         Console.WriteLine ("----------");
         
         foreach (var s in spending) 
             caching[until].Add(s, currentsol[table[s.Identifier]]);
         
         
         Console.WriteLine ("---------- [Final result]");
         Console.WriteLine (string.Join ("\n", caching[until].Select (x => x.Key.Identifier + " : " + x.Value)));
         Console.WriteLine ("----------");
     }
     
     return caching[until][currentState];
 }