Example #1
0
        /// <summary>
        /// Generate a separating set combining generated Hi sets.
        /// </summary>
        public String[][][] GetHsiSet()
        {
            //if (this.hsiSet != null)
            //{
            //    return this.hsiSet;
            //}

            //Hi set stores families by state pair
            //this method will organize families by state, generating the HSI set.
            StatePair[] lZeroSet = this.GetStatePairGroup();
            // M[]xy => x(state pairs) by x(inputs)
            String[][] hiSet = this.GetHiSet();

            //result will be delivered as M[]ab, where M is a(State) by b(input)
            hsiSet = new String[this.fsm.States.Count][][];

            //sorting by state
            for (int i = 0; i < lZeroSet.Length; i++)
            {
                StatePair pair = lZeroSet[i];

                this.AddPrefixToState(pair.StateA, hiSet[i]);
                this.AddPrefixToState(pair.StateB, hiSet[i]);
            }

            return(hsiSet);
        }
Example #2
0
 public Scene(string folderName, Color backgroundColor)
 {
     this.folderName      = $"var/{folderName}";
     this.actors          = new List <Actor>();
     this.backgroundColor = backgroundColor;
     this.state           = new StatePair();
 }
Example #3
0
        public override void TrainModel(List <SARS> batch)
        {
            var inp  = new StatePair[batch.Count];
            var outp = new TargetIndexPair[batch.Count];
            int i    = 0;

            foreach (var sars in batch)
            {
                inp[i] = sars.State.Features;
                float target;
                if (!sars.NextState.IsTerminal)
                {
                    var a0max = QMax(sars.NextState);
                    target = sars.Reward + Discount * a0max;
                }
                else
                {
                    target = sars.Reward;
                }
                outp[i++] = new TargetIndexPair(target, _amap[sars.Action.ActionId]);
            }
            for (int j = 0; j < batch.Count; j++)
            {
                _net.SGD(inp[j], outp[j]);
            }
        }
Example #4
0
        /// <summary>
        /// Generate every combination of state-state for current fsm.
        /// </summary>
        public StatePair[] GetStatePairGroup()
        {
            //if (this.statePairGroup != null)
            //    return this.statePairGroup;

            //Pairs of X-X or Y-Y are not included. Left element must be different from the right one.
            //List size is equals to (N * (N -1)) / 2, where N is the fsm.States list size.
            int statesCount = this.fsm.States.Count;
            int indexer     = 0;

            StatePair[] statePairGroup = new StatePair[(statesCount * (statesCount - 1)) / 2];

            for (int i = 0; i < this.fsm.States.Count; i++)
            {
                for (int j = i + 1; j < this.fsm.States.Count; j++)
                {
                    State     s1 = this.fsm.States[i];
                    State     s2 = this.fsm.States[j];
                    StatePair sp = new StatePair();

                    sp.StateA = s1;
                    sp.StateB = s2;
                    statePairGroup[indexer++] = sp;
                }
            }
            this.statePairGroup = statePairGroup;

            return(statePairGroup);
        }
Example #5
0
        /// <summary>
        /// Gets the harmonized set for each state
        /// </summary>
        public String[][] GetHiSet()
        {
            //if (this.hiSet != null)
            //    return this.hiSet;

            List <FailnessRecord> failnessTable = this.GetFailnessTable();

            StatePair[] statePairGroup = this.GetStatePairGroup();
            // Jagged! Sequences have different sizes.
            // hiSet is a Matrix[mxn], where [m] is the StatePairCount (see formula below) and [n] is a unknow sequence length.
            String[][] hiSet = new String[statePairGroup.Length][];

            // for each StatePair, if pair is fail, use input which failed as HI, else
            // use failness from pointed state.
            for (int i = 0; i < statePairGroup.Length; i++)
            {
                StatePair sp  = statePairGroup[i];
                String[]  seq = this.FindShortestInputToFail(failnessTable, statePairGroup, sp);

                if (seq == null)
                {
                    seq = new String[] { };
                }
                hiSet[i] = seq;
            }
            this.hiSet = hiSet;

            return(hiSet);
        }
Example #6
0
        private bool CanBeConcludedDistinguishable(StatePair pair)
        {
            var  state1       = pair.State1;
            var  state2       = pair.State2;
            bool isAccepting1 = acceptings.Contains(state1),
                 isAccepting2 = acceptings.Contains(state2);

            return(isAccepting1 != isAccepting2);
        }
Example #7
0
        /// <summary>
        /// Stores information about state pairs and its transitions.
        /// Used by HSI method to generate harmonized sets.
        /// </summary>


        /// <summary>
        /// Apply fsm inputs and set points from a state-pair to another one.
        /// </summary>
        public List <FailnessRecord> GetFailnessTable()
        {
            //if (this.failnessTable != null)
            //    return this.failnessTable;

            this.failnessTable = new List <FailnessRecord>();
            StatePair[] pairs = this.GetStatePairGroup();

            for (int i = 0; i < pairs.Length; i++)
            {
                StatePair pair = pairs[i];

                for (int j = 0; j < this.fsm.InputAlphabet.Count; j++)
                {
                    FailnessRecord record = new FailnessRecord();

                    record.SourcePair = pair;
                    record.Input      = this.fsm.InputAlphabet[j];

                    String outputA = String.Empty, outputB = String.Empty;
                    State  targetA = null, targetB = null;

                    //gets record target
                    foreach (Transition t in this.fsm.Transitions.Where(x => x.Input == record.Input))
                    {
                        if (t.SourceState == pair.StateA)
                        {
                            targetA = t.TargetState;
                            outputA = t.Output;
                        }
                        else if (t.SourceState == pair.StateB)
                        {
                            targetB = t.TargetState;
                            outputB = t.Output;
                        }
                    }

                    if (targetA == null || targetB == null)
                    {
                        record.Status = Failness.Invalid;
                    }
                    else if (outputA != outputB)
                    {
                        record.Status = Failness.Fail;
                    }
                    else
                    {
                        record.Status = Failness.Valid;
                    }
                    record.TargetPair = pairs.Where(x => (x.StateA == targetA && x.StateB == targetB) || (x.StateB == targetA && x.StateA == targetB)).FirstOrDefault();
                    this.failnessTable.Add(record);
                }
            }

            return(failnessTable);
        }
Example #8
0
        public void SGD(StatePair input, TargetIndexPair p)
        {
            _loss.Clear();
            _loss.At(p.Index, p.Target - Compute(input, true)[p.Index]);
            var split = _split.Visit(_outback.Visit(_loss, _params), _params);

            _backprop.BackPropagation(_unflatten.Visit(split.left, _params), _params);
            _hiddenBackprop.Visit(split.right, _params);
            IsOutputFromTraining = true;
        }
Example #9
0
        public void SGD(StatePair input, Vector <float> labels)
        {
            Compute(input, true);
            labels.CopyTo(_loss);
            _loss.Subtract(Output(), _loss);
            var split = _split.Visit(_outback.Visit(_loss, _params), _params);

            _backprop.BackPropagation(_unflatten.Visit(split.left, _params), _params);
            _hiddenBackprop.Visit(split.right, _params);
            IsOutputFromTraining = true;
        }
Example #10
0
 /// <summary>
 /// Checks for equality.
 /// </summary>
 /// <param name="obj"> Object to compare with. </param>
 /// <returns> <c>true</c> if <paramref name="obj"/> represents the same pair of states as this
 ///         pair. </returns>
 public override bool Equals(object obj)
 {
     if (obj is StatePair)
     {
         StatePair p = (StatePair)obj;
         return(p.S1 == S1 && p.S2 == S2);
     }
     else
     {
         return(false);
     }
 }
Example #11
0
        private IEnumerable <StatePair> GetAllPairs(IEnumerable <State> states)
        {
            var stateArray  = states.ToArray();
            int statesCount = stateArray.Length;

            for (int i = 0; i < statesCount - 1; i++)
            {
                for (int j = statesCount - 1; j > i; j--)
                {
                    var pair = new StatePair(stateArray[i], stateArray[j]);
                    yield return(pair);
                }
            }
        }
Example #12
0
        private StatePair GetNextPair(StatePair current, char character)
        {
            State state1 = current.State1,
                  state2 = current.State2;

            var nextStates1 = table.GetNextStates(state1, character);
            var nextStates2 = table.GetNextStates(state2, character);

            Debug.Assert(nextStates1.Count() == 1);
            Debug.Assert(nextStates2.Count() == 1);

            State next1 = nextStates1.First(),
                  next2 = nextStates2.First();

            return(new StatePair(next1, next2));
        }
Example #13
0
        public static StatePair<IEnumerable<Token>> ExplodeOptionList(
            StatePair<IEnumerable<Token>> tokens,
            Func<string, Maybe<char>> optionSequenceWithSeparatorLookup)
        {
            var replaces = tokens.Value.Select((t,i) =>
                optionSequenceWithSeparatorLookup(t.Text)
                    .Return(sep => Tuple.Create(i + 1, sep),
                        Tuple.Create(-1, '\0'))).SkipWhile(x => x.Item1 < 0);

            var exploded = tokens.Value.Select((t, i) =>
                        replaces.FirstOrDefault(x => x.Item1 == i).ToMaybe()
                            .Return(r => t.Text.Split(r.Item2).Select(Token.Value),
                                Enumerable.Empty<Token>().Concat(new[]{ t })));

            var flattened = exploded.SelectMany(x => x);

            return StatePair.Create(flattened, tokens.Errors);
        }
Example #14
0
            public override bool Equals(object obj)
            {
                if (!(obj is StatePair))
                {
                    return(false);
                }

                StatePair s = (StatePair)obj;

                if (this.StateA.Equals(s.StateA) && this.StateB.Equals(s.StateB))
                {
                    return(true);
                }
                if (this.StateB.Equals(s.StateA) && this.StateA.Equals(s.StateB))
                {
                    return(true);
                }
                return(false);
            }
Example #15
0
        public Vector <float> Compute(StatePair input, bool training)
        {
            // Forward propagate.
            var img = InputLayer.Compute(input.Spatial);

            for (int i = 0; i < ConvolutionalLayers.Length; i++)
            {
                img = SubSampleLayers[i].Compute(ConvolutionalLayers[i].Compute(img));
            }
            _vecp.left           = FlattenLayer.Compute(img);
            _vecp.right          = LinearHiddenLayer.Compute(VectorInput.Compute(input.Linear));
            IsOutputFromTraining = false;
            var res = OutputLayer.Compute(CombinationLayer.Compute(_vecp));

            if (ValuesComputed != null)
            {
                ValuesComputed(res, training);
            }
            return(res);
        }
Example #16
0
        public async Task <string> ReturnFromAuth(StatePair pair)
        {
            System.Console.WriteLine("Code: " + pair.Code + "\n");
            System.Console.WriteLine("State: " + pair.State + "\n");
            System.Console.WriteLine("RealmId: " + pair.RealmId + "\n");

            var tokenResponse = await auth2Client.GetBearerTokenAsync(pair.Code);

            //retrieve access_token and refresh_token
            access_token = tokenResponse.AccessToken;
            idToken      = tokenResponse.IdentityToken;

            var isTokenValid = await auth2Client.ValidateIDTokenAsync(idToken);

            await SetApiAuthToken(access_token, pair.RealmId);

            System.Console.WriteLine(isTokenValid);

            return(redirectUrl);
        }
Example #17
0
        public void Explode_scalar_with_separator_in_even_args_input_returns_sequence()
        {
            // Fixture setup
            var expectedTokens = new[] { Token.Name("x"), Token.Name("string-seq"),
                                         Token.Value("aaa"), Token.Value("bb"), Token.Value("cccc"), Token.Name("switch") };
            var specs = new[] { new OptionSpecification(string.Empty, "string-seq",
                                                        false, string.Empty, Maybe.Nothing <int>(), Maybe.Nothing <int>(), ',', null, typeof(IEnumerable <string>), TargetType.Sequence, string.Empty, string.Empty, new List <string>()) };

            // Exercize system
            var result =
                Tokenizer.ExplodeOptionList(
                    StatePair.Create(
                        Enumerable.Empty <Token>().Concat(new[] { Token.Name("x"),
                                                                  Token.Name("string-seq"), Token.Value("aaa,bb,cccc"), Token.Name("switch") }),
                        Enumerable.Empty <Error>()),
                    optionName => NameLookup.HavingSeparator(optionName, specs, StringComparer.InvariantCulture));

            // Verify outcome
            Assert.True(expectedTokens.SequenceEqual(result.Value));

            // Teardown
        }
Example #18
0
 public Vector <float> Compute(StatePair input)
 {
     return(Compute(input, false));
 }
Example #19
0
        /// <summary>
        /// Returns an automaton that accepts the intersection of the languages of the given automata.
        /// Never modifies the input automata languages.
        /// </summary>
        /// <param name="a1">The a1.</param>
        /// <param name="a2">The a2.</param>
        /// <returns></returns>
        public static Automaton Intersection(Automaton a1, Automaton a2)
        {
            if (a1.IsSingleton)
            {
                if (a2.Run(a1.Singleton))
                {
                    return a1.CloneIfRequired();
                }

                return BasicAutomata.MakeEmpty();
            }

            if (a2.IsSingleton)
            {
                if (a1.Run(a2.Singleton))
                {
                    return a2.CloneIfRequired();
                }

                return BasicAutomata.MakeEmpty();
            }

            if (a1 == a2)
            {
                return a1.CloneIfRequired();
            }

            Transition[][] transitions1 = Automaton.GetSortedTransitions(a1.GetStates());
            Transition[][] transitions2 = Automaton.GetSortedTransitions(a2.GetStates());
            var c = new Automaton();
            var worklist = new LinkedList<StatePair>();
            var newstates = new Dictionary<StatePair, StatePair>();
            var p = new StatePair(c.Initial, a1.Initial, a2.Initial);
            worklist.AddLast(p);
            newstates.Add(p, p);
            while (worklist.Count > 0)
            {
                p = worklist.RemoveAndReturnFirst();
                p.S.Accept = p.FirstState.Accept && p.SecondState.Accept;
                Transition[] t1 = transitions1[p.FirstState.Number];
                Transition[] t2 = transitions2[p.SecondState.Number];
                for (int n1 = 0, b2 = 0; n1 < t1.Length; n1++)
                {
                    while (b2 < t2.Length && t2[b2].Max < t1[n1].Min)
                    {
                        b2++;
                    }

                    for (int n2 = b2; n2 < t2.Length && t1[n1].Max >= t2[n2].Min; n2++)
                    {
                        if (t2[n2].Max >= t1[n1].Min)
                        {
                            var q = new StatePair(t1[n1].To, t2[n2].To);
                            StatePair r;
                            newstates.TryGetValue(q, out r);
                            if (r == null)
                            {
                                q.S = new State();
                                worklist.AddLast(q);
                                newstates.Add(q, q);
                                r = q;
                            }

                            char min = t1[n1].Min > t2[n2].Min ? t1[n1].Min : t2[n2].Min;
                            char max = t1[n1].Max < t2[n2].Max ? t1[n1].Max : t2[n2].Max;
                            p.S.Transitions.Add(new Transition(min, max, r.S));
                        }
                    }
                }
            }

            c.IsDeterministic = a1.IsDeterministic && a2.IsDeterministic;
            c.RemoveDeadTransitions();
            c.CheckMinimizeAlways();
            return c;
        }
Example #20
0
        /// <summary>
        /// Adds epsilon transitions to the given automaton. This method adds extra character interval
        /// transitions that are equivalent to the given set of epsilon transitions.
        /// </summary>
        /// <param name="a">The automaton.</param>
        /// <param name="pairs">A collection of <see cref="StatePair"/> objects representing pairs of
        /// source/destination states where epsilon transitions should be added.</param>
        public static void AddEpsilons(Automaton a, ICollection<StatePair> pairs)
        {
            a.ExpandSingleton();
            var forward = new Dictionary<State, HashSet<State>>();
            var back = new Dictionary<State, HashSet<State>>();
            foreach (StatePair p in pairs)
            {
                HashSet<State> to = forward[p.FirstState];
                if (to == null)
                {
                    to = new HashSet<State>();
                    forward.Add(p.FirstState, to);
                }

                to.Add(p.SecondState);
                HashSet<State> from = back[p.SecondState];
                if (from == null)
                {
                    from = new HashSet<State>();
                    back.Add(p.SecondState, from);
                }

                from.Add(p.FirstState);
            }

            var worklist = new LinkedList<StatePair>(pairs);
            var workset = new HashSet<StatePair>(pairs);
            while (worklist.Count != 0)
            {
                StatePair p = worklist.RemoveAndReturnFirst();
                workset.Remove(p);
                HashSet<State> to = forward[p.SecondState];
                HashSet<State> from = back[p.FirstState];
                if (to != null)
                {
                    foreach (State s in to)
                    {
                        var pp = new StatePair(p.FirstState, s);
                        if (!pairs.Contains(pp))
                        {
                            pairs.Add(pp);
                            forward[p.FirstState].Add(s);
                            back[s].Add(p.FirstState);
                            worklist.AddLast(pp);
                            workset.Add(pp);
                            if (from != null)
                            {
                                foreach (State q in from)
                                {
                                    var qq = new StatePair(q, p.FirstState);
                                    if (!workset.Contains(qq))
                                    {
                                        worklist.AddLast(qq);
                                        workset.Add(qq);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Add transitions.
            foreach (StatePair p in pairs)
            {
                p.FirstState.AddEpsilon(p.SecondState);
            }

            a.IsDeterministic = false;
            a.ClearHashCode();
            a.CheckMinimizeAlways();
        }
Example #21
0
        /// <summary>
        /// Returns input which made state pair failed. If current
        /// State pair is not fail, locate the shortest sequence to
        /// reach a failed state pair.
        /// </summary>
        private String[] FindShortestInputToFail(List <FailnessRecord> failnessTable, StatePair[] statePairGroup, StatePair sp)
        {
            IEnumerable <FailnessRecord> foundRecords = from FailnessRecord f in failnessTable
                                                        where f.SourcePair.Equals(sp) && f.Status == Failness.Fail
                                                        select f;

            //it is fail. get input which made it fail
            if (foundRecords.Count() > 0)
            {
                FailnessRecord record = foundRecords.First(); //will never be null

                return(new String[] { record.Input });
            }
            else //is invalid or valid
            {
                //make a new query, so we can proceed on the valid way
                foundRecords = from FailnessRecord f in failnessTable
                               where f.SourcePair.Equals(sp) && f.Status == Failness.Valid
                               select f;
                //find available sequences to fail
                List <String[]> foundSequences = new List <String[]>();

                foreach (FailnessRecord f in foundRecords)
                {
                    String[] sequence = FindShortestInputToFail(failnessTable, statePairGroup, f.TargetPair);

                    if (sequence != null)
                    {
                        List <String> newSequence = new List <String>();

                        newSequence.Add(f.Input);
                        newSequence.AddRange(sequence);
                        foundSequences.Add(newSequence.ToArray());
                    }
                }

                //return the shortest sequence
                String[] bestFit = null;

                foreach (String[] sequence in foundSequences)
                {
                    if (sequence.Length < 2)
                    {
                        continue;
                    }

                    if (bestFit == null || bestFit.Length > sequence.Length)
                    {
                        bestFit = sequence;
                    }
                }

                return(bestFit);
            }
        }