Beispiel #1
0
        public static Path <SS, DS> ExtractPath(DS Sstart, DS SPgoal,
                                                ExtendedDictionary <DS, DS> tree, Operator <SS, DS>[] Ops, SS ss)
        {
            DS            Prev;
            DS            Cur  = SPgoal;
            Path <SS, DS> Path = new Path <SS, DS>((DS)null);

            do
            {
                Prev = Cur;
                Cur  = tree.Lookup(Prev);
                if (Path.StateHashTable.Contains(Cur))
                {
                    return(Path);
                }
                var Op = GetOp(Cur, Prev, Ops, ss);
                if (Op == null)
                {
                    return(Path);
                }
                Path.PushFirst(Op, Cur, Prev);
            } while (!Cur.Equals(Sstart));

            return(Path);
        }
Beispiel #2
0
 private static void Dijkstra(ref ExtendedDictionary <DS, Metric> G,
                              ExtendedDictionary <DS, Metric> H, DS Sstart, DS Sgoal,
                              ref ExtendedDictionary <DS, DS> tree,
                              ref PriorityQueue <Metric, DS> OPEN, ref HashSet <DS> CLOSED,
                              Operator <SS, DS>[] Ops, SS ss, Func <DS, DS, Metric> h0)
 {
     foreach (DS s in CLOSED)
     {
         H.Set(s, Metric.Infinity);
     }
     OPEN = OPEN.Reorder(s => H.Lookup(s));
     while (CLOSED.Count != 0 && !OPEN.IsEmpty( ))
     {
         DS s = OPEN.Dequeue( );
         if (CLOSED.Contains(s))
         {
             CLOSED.Remove(s);
         }
         foreach (DS sp in s.Expand(Ops, ss, false))
         {
             Metric cost = c(sp, s, h0) + H.Lookup(s);
             if (CLOSED.Contains(sp) && H.Lookup(sp) > cost)
             {
                 H.Set(sp, cost);
                 if (!OPEN.Contains(sp))
                 {
                     OPEN.Enqueue(cost, sp);
                 }
             }
         }
     }
 }
Beispiel #3
0
        public void Reload(Settings settings, string currentFilePath)
        {
            _mappingToDstFileContainerMap.Clear();
            _currentFilePath = null;

            var dstFilePathToDstWordsCache = new ExtendedDictionary <string, ISet <Word> >(); // dstFilePath -> ISet<dstWord>

            foreach (var mappingItem in settings.Mapping)
            {
                var dst         = mappingItem.Dst;
                var dstFilePath = dst.FullPath;

                var fileDstWords = dstFilePathToDstWordsCache.ComputeIfAbsent(dstFilePath, key => new HashSet <Word>());
                fileDstWords.Add(dst.Word);
            }

            IExtendedDictionary <DstLocation, DstFileContainer> dstContainerCache = new ExtendedDictionary <DstLocation, DstFileContainer>();

            foreach (var mappingItem in settings.Mapping)
            {
                var supportedWords   = dstFilePathToDstWordsCache[mappingItem.Dst.FullPath];
                var dstFileContainer = dstContainerCache.ComputeIfAbsent(mappingItem.Dst, dst => new DstFileContainer(_parser, dst.FullPath, supportedWords));
                _mappingToDstFileContainerMap[mappingItem] = dstFileContainer;
            }

            SwitchContext(currentFilePath);
        }
Beispiel #4
0
 public void TestBrackets_Expected3And6()
 {
     var a = new ExtendedDictionary<int, int, int>();
     a[1, 2] = 3;
     a[4, 5] = 6;
     Assert.AreEqual(3, a[1, 2]);
     Assert.AreEqual(6, a[4, 5]);
 }
Beispiel #5
0
        public void TestFastAccessById_ExpectedMas_2_5()
        {
            var a = new ExtendedDictionary<string, int, int>();
            a.Add("test", 1, 2);
            a.Add("test", 4, 5);
            a.Add("no_test", 6, 7);

            Assert.AreEqual(new int[]{2,5}, a.GetById("test"));
        }
Beispiel #6
0
        /// <summary>
        /// The compute method which uses greedy search.
        /// </summary>
        /// <param name="StaticState">A static state to work on.</param>
        /// <param name="DynamicState">A dynamic state to work on.</param>
        /// <param name="Goal">The goal state(s) to search for.</param>
        /// <returns>Computes a path.</returns>
        public override IEnumerable <Path <SS, DS> > Compute(SS StaticState,
                                                             DS DynamicState, Goal <SS, DS> Goal, Operator <SS, DS>[] Actions)
        {
            ExtendedDictionary <DS, DS>     tree = new ExtendedDictionary <DS, DS>(x => null);
            ExtendedDictionary <DS, Metric> G    = new ExtendedDictionary <DS, Metric>(k => Metric.Infinity);

            G.Set(DynamicState, Metric.Zero);
            uint IterationExpansions            = 0;
            PriorityQueue <Metric, DS> OpenList = new PriorityQueue <Metric, DS>( );
            HashSet <DS> ClosedList             = new HashSet <DS>( );

            OpenList.Enqueue(Metric.Zero, DynamicState);
            while (true)
            {
                if (OpenList.IsEmpty( ))
                {
                    throw new PathNotFoundException("Path Not Found");
                }
                DS Cur = OpenList.Dequeue( );
                ClosedList.Add(Cur);
                if (Goal.IsSatisfiedBy(StaticState, Cur))
                {
                    yield return(Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                 Cur, tree, Actions, StaticState));

                    yield break;
                }
                else
                {
                    this.Expansions++;
                    IterationExpansions++;
                    if (IterationExpansions > ComputationLimit)
                    {
                        yield return(Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                     Cur, tree, Actions, StaticState));

                        IterationExpansions = 1;
                    }
                    foreach (DS e in Cur.Expand(Actions, StaticState, true))
                    {
                        if (!ClosedList.Contains(e))
                        {
                            Metric cost = G.Lookup(Cur) + e.LastOperator.Cost(Cur);
                            if (G.Lookup(e) > cost)
                            {
                                G.Set(e, cost);
                                tree.Set(e, Cur);
                                OpenList.Enqueue(cost, e);
                            }
                        }
                        this.Generations++;
                    }
                }
            }
        }
Beispiel #7
0
        public void TestCount_Expected_5()
        {
            var a = new ExtendedDictionary<int, string, int>();
            a.Add(2, "test", 2);
            a.Add(3, "test", 5);
            a.Add(5, "no_test", 7);
            a.Add(6, "no_test", 7);
            a.Add(7, "no_test", 7);

            Assert.AreEqual(5, a.Count);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            //Лёгкая демонстрация.
            ExtendedDictionary<UserType, int, string> a = new ExtendedDictionary<UserType, int, string>();
            #region Заполнение базы

            a[ new UserType("Nikita", 23),  37]     = "Programmer";
            a[ new UserType("Nikita", 23),  666]    = "Clone";
            a[ new UserType("Andrey", 33),  38]     = "1st Programmer";
            a[ new UserType("Nikolay", 13), 39]     = "Bad Programmer";
            a[ new UserType("Alex", 93),    375]    = "Ex Programmer";
            a[ new UserType("Marina", 43),  371]    = "Seller";
            a[ new UserType("Daniil", 20),  370]    = "Dealer";
            a[ new UserType("Dima", 25),    3722]   = "Worker";

            a.Add(new UserType("Misha", 13), 37333, "Human");
            a.Add(new UserType("Vasya", 53), 3711,  "Slave");
            a.Add(new UserType("Yulia", 22), 377,   "Runer");
            a.Add(new UserType("Bruce", 40),  3766,  "Batman");
            #endregion

            Console.WriteLine("Какую должность занимает Юля с id=377? :\n" + a[new UserType("Yulia", 22), 377]);
            Console.WriteLine();

            Console.WriteLine("Люди, имеющие сигнатуру <'Nikita', 23> занимают должности:");
            foreach (var item in a.GetById(new UserType("Nikita", 23)))
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("Должности людей с id = 375:");
            foreach (var item in a.GetByName(375))
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            var b= new ExtendedDictionary<int, string, int>();
            b.Add(2, "test", 0);

            Console.WriteLine();
            Console.WriteLine("Выведем все значения в базе:");
            Console.WriteLine("Name/Age signature".PadRight(12) +"|   id".PadRight(8) + " | Possition\n");
            foreach (var item in a)
            {
                Console.WriteLine(item.Key + " " + item.Value);
            }

            Console.ReadLine();
        }
Beispiel #9
0
        private static void AStar(ref ExtendedDictionary <DS, Metric> G,
                                  ExtendedDictionary <DS, Metric> H, DS Sstart, DS Sgoal,
                                  ExtendedDictionary <DS, DS> tree, ref PriorityQueue <Metric, DS> OPEN,
                                  ref HashSet <DS> CLOSED, int lookahead, Operator <SS, DS>[] Ops, SS ss,
                                  Func <DS, DS, Metric> h0, ref uint Expansions, StateVisualizer <SS, DS> sv)
        {
            G = new ExtendedDictionary <DS, Metric>(k => Metric.Infinity);
            G.Set(Sstart, Metric.Zero);
            OPEN   = new PriorityQueue <Metric, DS>( );
            CLOSED = new HashSet <DS>( );
            OPEN.Enqueue(H.Lookup(Sstart), Sstart);

            int expansions = 0;

            while (!OPEN.IsEmpty( ) && G.Lookup(Sgoal) > OPEN.TopKey( ) &&
                   expansions < lookahead)
            {
#if OpenGl
                if (sv != null)
                {
                    HashSet <DS> hs = new HashSet <DS>( );
                    OPEN.AddToHashSet(hs);
                    sv.VisualizeAlg(ss, ss.InitialDynamicState, new OpenGLStateVisualizer.OpenGlVisulizationData( )
                    {
                        List      = hs as HashSet <GenericGridWorldDynamicState>,
                        HAdjusted = H,
                    });
                }
#endif
                expansions++;
                DS s = OPEN.Dequeue( );
                CLOSED.Add(s);
                Expansions++;
                foreach (DS sp in s.Expand(Ops, ss, false))
                {
                    Metric cost = G.Lookup(s) + c(s, sp, h0);
                    if (G.Lookup(sp) > cost)
                    {
                        G.Set(sp, cost);
                        tree.Set(sp, s);
                        if (OPEN.Contains(sp))
                        {
                            OPEN.Remove(sp);
                        }
                        OPEN.Enqueue(G.Lookup(sp) + H.Lookup(sp), sp);
                    }
                }
            }
        }
Beispiel #10
0
        public override IEnumerable <Path <SS, DS> > Compute(SS ss, DS ds, Goal <SS, DS> Goal, Operator <SS, DS>[] Ops)
        {
            this.init(ss, ds, Ops, Goal);
            ExtendedDictionary <DS, Metric> H1   = new ExtendedDictionary <DS, Metric>(x => aS.h(x, aS.Sgoal));
            ExtendedDictionary <DS, DS>     tree = new ExtendedDictionary <DS, DS>(x => null);
            Path <SS, DS> Incumbant         = null;
            int           GlobalSearchLimit = GetGlobalSearchComputationLimit( );

            this.aS.ComputationLimit = GlobalSearchLimit;
            int LocalSearchLimit = GetLocalSearchComputationLimit( );

            foreach (Path <SS, DS> p in AnytimeDStarLiteLookahead(aS, GlobalSearchLimit))
            {
                this.Expansions  += aS.Expansions;
                this.Generations += aS.Generations;
                aS.Expansions     = 0;
                aS.Generations    = 0;
                if (p == null)
                {
                    switch (sm)
                    {
                    /*case ChooseMethodStep.LRTAStar:
                     * yield return LRTAStar<SS, DS>.AStarLookAhead( ss.InitialDynamicState, ss,
                     *    LocalSearchLimit, aS.sv,
                     *    ref this.Expansions, ref this.Generations, false,
                     *    Ops, H1, H, Goal, Metric.One, false );
                     * break;
                     * case ChooseMethodStep.RTAStar:
                     * yield return LRTAStar<SS, DS>.AStarLookAhead( ss.InitialDynamicState, ss,
                     *    LocalSearchLimit, aS.sv,
                     *    ref this.Expansions, ref this.Generations, false,
                     *    Ops, H1, H, Goal, Metric.One, true );
                     * break;*/
                    case ChooseMethodStep.LSSLRTAStar:
                        yield return(LSSLRTAStar <SS, DS> .LSSLRTAStarLookahead(LocalSearchLimit, ss, aS.Sstart,
                                                                                aS.Sgoal, Ops, aS.h, ref Incumbant, ref this.Expansions, aS.sv, tree, H1));

                        break;
                    }
                }
                else
                {
                    yield return(p);
                }
                aS.Sstart = aS.ss.InitialDynamicState;
            }
            yield break;
        }
Beispiel #11
0
        public void TestEnumirable_Expected_5()
        {
            var a = new ExtendedDictionary<int, string, int>();
            a.Add(2, "test", 2);
            a.Add(3, "test", 5);
            a.Add(5, "no_test", 7);
            a.Add(6, "no_test", 7);
            a.Add(7, "no_test", 7);

            int tmp = 0;
            foreach (var item in a)
            {
                tmp++;
            }

            Assert.AreEqual(5, tmp);
        }
Beispiel #12
0
        public override IEnumerable <Path <SS, DS> > Compute(SS ss,
                                                             DS Sstart, Goal <SS, DS> Goal, Operator <SS, DS>[] Ops)
        {
            DS Sgoal = Transformer.Transform(Goal, Sstart);
            Func <DS, DS, Metric>           h0   = (x, y) => Heuristic.H(ss, x, y, Ops);
            ExtendedDictionary <DS, DS>     tree = new ExtendedDictionary <DS, DS>(x => null);
            ExtendedDictionary <DS, Metric> H    = new ExtendedDictionary <DS, Metric>(
                x => h0(x, Sgoal));
            Path <SS, DS> Incumbant = null;

            while (!Sstart.Equals(Sgoal))
            {
                yield return(LSSLRTAStarLookahead(ComputationLimit, ss, Sstart, Sgoal,
                                                  Ops, h0, ref Incumbant, ref Expansions, sv, tree, H));

                Sstart = ss.InitialDynamicState;
            }
            yield break;
        }
        public override void Awake()
        {
            base.Awake();
            FilePath     = CombineFilePath(fileName);
            qLambdaTable = new ExtendedDictionary <State, EQValues>(new StateEqualityComparer());
            if (!File.Exists(FilePath) || new FileInfo(FilePath).Length == 0)
            {
                qLambdaTable = new ExtendedDictionary <State, EQValues>(new StateEqualityComparer());
            }
            else
            {
                qLambdaTable = new ReadQLambdaDictionaryFromFile(FilePath).QLambdaDictionary;
            }

            if (startLearning)
            {
                qLambdaTable = new ExtendedDictionary <State, EQValues>(new StateEqualityComparer());
                ResetETable();
            }
        }
Beispiel #14
0
 private void init(Goal <SS, DS> Goal, SS ss, DS Sstart,
                   Operator <SS, DS>[] Actions)
 {
     LockedList  = new HashSet <DS>( );
     RHS         = new Dictionary <string, Metric>( );
     G           = new Dictionary <string, Metric>( );
     HAdjusted   = new Dictionary <string, Metric>( );
     this.Sgoal  = Transformer.Transform(Goal, Sstart);
     this.Sstart = Sstart;
     this.ss     = ss;
     this.Ops    = Actions;
     us          = new USample( );
     switch (this.sm)
     {
     case ChooseMethodStep.LSSLRTAStar:
         h0        = (x, y) => H.H(ss, x, y, Actions);
         tree      = new ExtendedDictionary <DS, DS>(X => null);
         HLSS      = new ExtendedDictionary <DS, Metric>(x => h0(x, Sgoal));
         Incumbant = null;
         break;
     }
 }
Beispiel #15
0
        public static Path <SS, DS> LSSLRTAStarLookahead(int ComputationLimit, SS ss,
                                                         DS Sstart, DS Sgoal, Operator <SS, DS>[] Ops, Func <DS, DS, Metric> h0,
                                                         ref Path <SS, DS> Incumbant, ref uint Expansions, StateVisualizer <SS, DS> sv,
                                                         ExtendedDictionary <DS, DS> tree, ExtendedDictionary <DS, Metric> H)
        {
            if (!PathGood(Incumbant, ss))
            {
                ExtendedDictionary <DS, Metric> G    = null;
                PriorityQueue <Metric, DS>      OPEN = null;
                HashSet <DS> CLOSED = null;
                AStar(ref G, H, Sstart, Sgoal, tree, ref OPEN, ref CLOSED, ComputationLimit, Ops, ss, h0, ref Expansions, sv);
                if (OPEN.IsEmpty( ))
                {
                    return(null);
                }
                DS SPgoal = OPEN.Top( );
                Dijkstra(ref G, H, Sstart, Sgoal, ref tree, ref OPEN, ref CLOSED, Ops, ss, h0);

                Incumbant = ExtractPath(Sstart, SPgoal, tree, Ops, ss);
            }
            if (Incumbant == null || Incumbant.ActionCount == 0)
            {
                return(null);
            }
            else
            {
                var IHead = Incumbant.PopHead( );
                if (IHead.Actions.First.Value.IsValidOn(ss.InitialDynamicState, ss, false))
                {
                    return(IHead);
                }
                else
                {
                    Incumbant = null;
                    return(null);
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// The compute method which uses greedy search.
        /// </summary>
        /// <param name="StaticState">A static state to work on.</param>
        /// <param name="DynamicState">A dynamic state to work on.</param>
        /// <param name="Goal">The goal state(s) to search for.</param>
        /// <returns>Computes a path.</returns>
        public override IEnumerable <Path <SS, DS> > Compute(SS StaticState,
                                                             DS DynamicState, Goal <SS, DS> Goal, Operator <SS, DS>[] Actions)
        {
            HashSet <DS>         DeadEndList = new HashSet <DS>( );
            InStateGoal <SS, DS> FakeGoal    = new InStateGoal <SS, DS>(DynamicState);
            DS   GoalState                       = Transformer.Transform(Goal, DynamicState);
            uint IterationExpansions             = 0;
            ExtendedDictionary <DS, DS>     tree = new ExtendedDictionary <DS, DS>(x => null);
            ExtendedDictionary <DS, Metric> G    = new ExtendedDictionary <DS, Metric>(k => Metric.Infinity);

            G.Set(DynamicState, Metric.Zero);
            PriorityQueue <Metric, DS> OpenList = new PriorityQueue <Metric, DS>( );
            HashSet <DS> ClosedList             = new HashSet <DS>( );

            OpenList.Enqueue(Metric.Zero, GoalState);
            while (true)
            {
                if (OpenList.IsEmpty( ))
                {
                    throw new PathNotFoundException("Path Not Found");
                }
                var Cur = OpenList.DequeueNode( );
                ClosedList.Add(Cur.Second);
                if (FakeGoal.IsSatisfiedBy(StaticState, Cur.Second))
                {
                    LinkedList <Operator <SS, DS> > Operators = new LinkedList <Operator <SS,
                                                                                          DS> >( );
                    foreach (var a in Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                      Cur.Second, tree, Actions, StaticState).Actions)
                    {
                        Operators.AddFirst(new ReverseOperator <SS, DS>(a));
                    }
                    yield return(new Path <SS, DS>((DS)null)
                    {
                        Actions = Operators
                    });

                    yield break;
                }
                else
                {
                    this.Expansions++;
                    if (++IterationExpansions >= ComputationLimit)
                    {
                        IEnumerable <DS> dss = DynamicState.Expand(Actions, StaticState,
                                                                   true).Where(
                            ds => !DeadEndList.Contains(ds));
                        if (dss.All(ds => HeuristicSS.H(StaticState, ds, Cur.Second,
                                                        Actions) >
                                    HeuristicSS.H(StaticState, DynamicState, Cur.Second, Actions))
                            )
                        {
                            DeadEndList.Add(DynamicState);
                        }
                        if (!dss.Any( ))
                        {
                            dss = DynamicState.Expand(Actions, StaticState, true);
                        }
                        DS s = dss.ArgMin(
                            ds => HeuristicSS.H(StaticState, ds, Cur.Second, Actions));
                        var p = Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                Cur.Second, tree, Actions, StaticState);

                        yield return(p);

                        DynamicState = p.Actions.First( ).PerformOn(DynamicState,
                                                                    StaticState).First( );
                        if (OpenList.Contains(DynamicState))
                        {
                            DS st;
                            do
                            {
                                st = OpenList.Dequeue( );
                            } while (!st.Equals(DynamicState)); //TODO add method for this.
                            LinkedList <Operator <SS, DS> > Operators =
                                new LinkedList <Operator <SS, DS> >( );
                            foreach (var a in Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                              st, tree, Actions, StaticState).Actions)
                            {
                                Operators.AddFirst(new ReverseOperator <SS, DS>(a));
                            }
                            yield return(new Path <SS, DS>((DS)null)
                            {
                                Actions = Operators
                            });

                            yield break;
                        }
                        if (ClosedList.Contains(DynamicState))
                        {
                            LinkedList <Operator <SS, DS> > Operators =
                                new LinkedList <Operator <SS, DS> >( );
                            var pgot = Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                                       ClosedList.First(ds => ds.Equals(
                                                                                            DynamicState)), tree, Actions, StaticState);

                            foreach (var a in pgot.Actions)
                            {
                                Operators.AddFirst(new ReverseOperator <SS, DS>(a));
                            }
                            yield return(new Path <SS, DS>((DS)null)
                            {
                                Actions = Operators
                            });

                            yield break;
                        }
                        FakeGoal            = new InStateGoal <SS, DS>(DynamicState);
                        IterationExpansions = 0;
                        OpenList.Enqueue(Cur.First, Cur.Second);
                        if (Sort)
                        {
                            /*PriorityQueue<Metric, DS> sortedOpen =
                             * new PriorityQueue<Metric, DS>( );
                             * DS x;
                             * while ( ( x = OpenList.Dequeue( ) ) !=  null ) {
                             * sortedOpen.Enqueue( G.Lookup( Cur.Second )  + e.LastOperator.Cost( Cur.Second ), x );
                             * }
                             * OpenList = sortedOpen;
                             */
                        }
                    }
                    else
                    {
                        foreach (DS e in Cur.Second.Expand(Actions, StaticState, true
                                                           ))
                        {
                            if (!ClosedList.Contains(e) && !OpenList.Contains(e))
                            {
                                Metric cost = G.Lookup(Cur.Second) + e.LastOperator.Cost(Cur.Second);
                                if (G.Lookup(e) > cost)
                                {
                                    G.Set(e, cost);
                                    tree.Set(e, Cur.Second);
                                    OpenList.Enqueue(cost, e);
                                }
                            }
                            this.Generations++;
                        }
                    }
                }
            }
        }
Beispiel #17
0
 public QLambdaDictionaryToFile(string filePath, ExtendedDictionary <State, EQValues> qLambdaDictionary) : base(filePath)
 {
     FilePath    = filePath;
     QLambdaDict = qLambdaDictionary;
     DictionaryToFile();
 }
 // Use this for initialization
 private void Awake()
 {
     m_Character = GetComponent <PlatformerCharacter2D>();
     dic         = new ExtendedDictionary <State, float[]>(new StateEqualityComparer());
 }
Beispiel #19
0
        public ExpressionParserTestFixture()
        {
            AssignmentOperator      = ("=", 1, AssociativityType.Right, (lhs, rhs) => (((Variable)lhs).Value = rhs));
            EscapeSequenceFormatter = new ExtendedNativeEscapeSequenceFormatter();
            CustomVariables         = new Dictionary <string, Variable>();
            Results = new List <object>();

            ArithmeticUnaryOperators = new ExtendedDictionary <char, UnaryOperator>((value) => value.Identifier)
            {
                ('+', 1, AssociativityType.Right, (value) => + System.Convert.ToDouble(value)),
                ('-', 1, AssociativityType.Right, (value) => - System.Convert.ToDouble(value)),

                ('!', 1, AssociativityType.Right, (value) => { double tmp = System.Convert.ToDouble(value); return(tmp == 0D || tmp == double.NaN); })
            };

            ArithmeticBinaryOperators = new ExtendedDictionary <string, BinaryOperator>((value) => value.Identifier)
            {
                ("+", 4, AssociativityType.Left, (lhs, rhs) => lhs is string || rhs is string?(object)$"{lhs}{rhs}" : (object)(System.Convert.ToDouble(lhs) + System.Convert.ToDouble(rhs))),
                ("-", 4, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) - System.Convert.ToDouble(rhs)),
                ("*", 3, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) * System.Convert.ToDouble(rhs)),
                ("/", 3, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) / System.Convert.ToDouble(rhs)),
                ("%", 3, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) % System.Convert.ToDouble(rhs)),
                ("^", 2, AssociativityType.Right, (lhs, rhs) => Exponentiation(lhs, rhs)),

                ("//", 3, AssociativityType.Right, (lhs, rhs) => Math.Truncate(System.Convert.ToDouble(lhs) / System.Convert.ToDouble(rhs))),
                ("**", 2, AssociativityType.Right, (lhs, rhs) => Exponentiation(lhs, rhs)),

                ("==", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) == System.Convert.ToDouble(lhs)),
                ("!=", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) != System.Convert.ToDouble(lhs)),
                ("<", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) < System.Convert.ToDouble(lhs)),
                (">", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) > System.Convert.ToDouble(lhs)),
                ("<=", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) <= System.Convert.ToDouble(lhs)),
                (">=", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToDouble(lhs) >= System.Convert.ToDouble(lhs))
            };

            ArithmeticVariables = new ExtendedDictionary <string, Variable>((value) => value.Identifier)
            {
                ("T", 1000000000000.0),
                ("G", 1000000000.0),
                ("M", 1000000.0),
                ("k", 1000.0),
                ("h", 100.0),
                ("da", 10.0),
                ("d", 0.1),
                ("c", 0.01),
                ("m", 0.001),
                ("u", 0.000001),
                ("n", 0.000000001),
                ("p", 0.000000000001),

                ("math.pi", Math.PI),
                ("math.e", Math.E),
                ("phys.c", 299792458),
                ("phys.au", 149597870700)
            };

            ArithmeticFunctions = new ExtendedDictionary <string, Function>((value) => value.Identifier)
            {
                ("ans", 0, 1, Ans),
                ("pi", 0, PI),
                ("abs", 1, (args) => Math.Abs(System.Convert.ToDouble(args[0]))),
                ("neg", 1, (args) => - Math.Abs(System.Convert.ToDouble(args[0]))),
                ("pow", 2, Exponentiation),
                ("root", 2, Root),
                ("sqrt", 1, (args) => Math.Sqrt(System.Convert.ToDouble(args[0]))),
                ("cbrt", 1, (args) => Root(args[0], 3)),
                ("log", 1, (args) => Math.Log(System.Convert.ToDouble(args[0]))),
                ("log10", 1, (args) => Math.Log10(System.Convert.ToDouble(args[0]))),
                ("sin", 1, (args) => Math.Sin(System.Convert.ToDouble(args[0]))),
                ("cos", 1, (args) => Math.Cos(System.Convert.ToDouble(args[0]))),
                ("tan", 1, (args) => Math.Tan(System.Convert.ToDouble(args[0]))),
                ("asin", 1, (args) => Math.Asin(System.Convert.ToDouble(args[0]))),
                ("acos", 1, (args) => Math.Acos(System.Convert.ToDouble(args[0]))),
                ("atan", 1, (args) => Math.Atan(System.Convert.ToDouble(args[0]))),
                ("sinh", 1, (args) => Math.Sinh(System.Convert.ToDouble(args[0]))),
                ("cosh", 1, (args) => Math.Cosh(System.Convert.ToDouble(args[0]))),
                ("tanh", 1, (args) => Math.Tanh(System.Convert.ToDouble(args[0]))),
                ("asinh", 1, (args) => Math.Asinh(System.Convert.ToDouble(args[0]))),
                ("acosh", 1, (args) => Math.Acosh(System.Convert.ToDouble(args[0]))),
                ("atanh", 1, (args) => Math.Atanh(System.Convert.ToDouble(args[0]))),
                ("min", 1, -1, Min),
                ("max", 1, -1, Max),
                ("strlen", 1, StringLength)
            };

            ArithmeticShorthandOperator = ("*", 3, AssociativityType.Right, (lhs, rhs) => System.Convert.ToDouble(lhs) * System.Convert.ToDouble(rhs));
            ArithmeticExpressionParser  = new ExpressionParser(ArithmeticUnaryOperators, ArithmeticBinaryOperators, ArithmeticVariables, ArithmeticFunctions, CustomVariables, AssignmentOperator)
            {
                ShorthandOperator       = ArithmeticShorthandOperator,
                EscapeSequenceFormatter = EscapeSequenceFormatter
            };

            BitwiseVariables = new ExtendedDictionary <string, Variable>((value) => value.Identifier)
            {
                ("false", 0),
                ("true", 1)
            };

            BitwiseFunctions = new ExtendedDictionary <string, Function>((value) => value.Identifier)
            {
            };

            BitwiseUnaryOperators = new ExtendedDictionary <char, UnaryOperator>((value) => value.Identifier)
            {
                ('!', 1, AssociativityType.Right, (value) => System.Convert.ToInt32(value) != 0 ? 0 : 1),
                ('~', 1, AssociativityType.Right, (value) => ~System.Convert.ToInt32(value))
            };

            BitwiseBinaryOperators = new ExtendedDictionary <string, BinaryOperator>((value) => value.Identifier)
            {
                ("+", 5, AssociativityType.Left, (lhs, rhs) => System.Convert.ToInt32(lhs) | System.Convert.ToInt32(rhs)),
                ("*", 3, AssociativityType.Left, (lhs, rhs) => System.Convert.ToInt32(lhs) & System.Convert.ToInt32(rhs)),
                ("^", 4, AssociativityType.Left, (lhs, rhs) => System.Convert.ToInt32(lhs) ^ System.Convert.ToInt32(rhs)),

                ("<<", 2, AssociativityType.Right, (lhs, rhs) => System.Convert.ToInt32(lhs) << System.Convert.ToInt32(rhs)),
                (">>", 2, AssociativityType.Left, (lhs, rhs) => System.Convert.ToInt32(lhs) >> System.Convert.ToInt32(rhs))
            };

            BitwiseShorthandOperator = ("*", 3, AssociativityType.Right, (lhs, rhs) => System.Convert.ToInt32(lhs) & System.Convert.ToInt32(rhs));

            BitwiseExpressionParser = new ExpressionParser(BitwiseUnaryOperators, BitwiseBinaryOperators, BitwiseVariables, BitwiseFunctions, CustomVariables, AssignmentOperator)
            {
                ShorthandOperator       = BitwiseShorthandOperator,
                EscapeSequenceFormatter = EscapeSequenceFormatter
            };
        }
Beispiel #20
0
        public void TestParallelModificationSafity_Expected_4950()
        {
            var a = new ExtendedDictionary<int, string, int>();
            a.Add(2, "test", 0);

            Parallel.For(0, 100,
                (i) =>
                {
                    a.EnterUpgradeableReadLock();
                    a[2, "test"] += i;
                    a.ExitUpgradeableReadLock();
                });

            Assert.AreEqual(4950, a[2, "test"]);
        }
Beispiel #21
0
 public ReadQLambdaDictionaryFromFile(string filePath) : base(filePath)
 {
     FilePath          = filePath;
     QLambdaDictionary = new ExtendedDictionary <State, EQValues>(new StateEqualityComparer());
     Read();
 }
Beispiel #22
0
        public void TestFastAccessByName_ExpectedMas_2_5()
        {
            var a = new ExtendedDictionary<int,string, int>();
            a.Add(2,"test", 2);
            a.Add(3,"test", 5);
            a.Add(5,"no_test", 7);

            Assert.AreEqual(new int[] { 2, 5 }, a.GetByName("test"));
        }
 public ReadQ0DictionaryFromFile(string filePath) : base(filePath)
 {
     FilePath     = filePath;
     Q0Dictionary = new ExtendedDictionary <State, float[]>(new StateEqualityComparer());
     Read();
 }
Beispiel #24
0
        /// <summary>
        /// The compute method which uses greedy search.
        /// </summary>
        /// <param name="StaticState">A static state to work on.</param>
        /// <param name="DynamicState">A dynamic state to work on.</param>
        /// <param name="Goal">The goal state(s) to search for.</param>
        /// <returns>Computes a path.</returns>
        public override Path <SS, DS> ComputeComplete(SS StaticState,
                                                      DS DynamicState, Goal <SS, DS> Goal, Operator <SS, DS>[] Actions)
        {
            PriorityQueue <Metric, DS>      OpenList = new PriorityQueue <Metric, DS>( );
            ExtendedDictionary <DS, DS>     tree     = new ExtendedDictionary <DS, DS>(x => null);
            ExtendedDictionary <DS, Metric> G        = new ExtendedDictionary <DS, Metric>(k => Metric.Infinity);

            G.Set(DynamicState, Metric.Zero);
            HashSet <DS> ClosedList = new HashSet <DS>( );

            OpenList.Enqueue(Metric.Zero, DynamicState);
            while (true)
            {
#if OpenGl
                if (sv != null)
                {
                    this.sv.VisualizeAlg(StaticState, StaticState.InitialDynamicState,
                                         new OpenGLStateVisualizer.OpenGlVisulizationData( )
                    {
                        OL = OpenList as PriorityQueue <Metric, GenericGridWorldDynamicState>,
                    });
                }
#endif
                if (OpenList.IsEmpty( ))
                {
                    throw new PathNotFoundException("Path Not Found");
                }
                DS Cur = OpenList.Dequeue( );
                if (UseClosedList)
                {
                    ClosedList.Add(Cur);
                }
                if (Goal.IsSatisfiedBy(StaticState, Cur))
                {
                    return(Algorithm <SS, DS> .ExtractPath(StaticState.InitialDynamicState,
                                                           Cur, tree, Actions, StaticState));
                }
                else
                {
                    this.Expansions++;
                    foreach (DS e in Cur.Expand(Actions, StaticState, true))
                    {
                        e.BookKeeping = new ParentBookKeeping <SS, DS>( );
                        if (!UseClosedList)
                        {
                            (e.BookKeeping as ParentBookKeeping <SS, DS>).Parent = Cur;
                        }
                        if ((UseClosedList && !ClosedList.Contains(e) && !OpenList.Contains(e)) ||
                            (!UseClosedList && !CheckReversePathForDuplicates(e)))
                        {
                            Metric cost = G.Lookup(Cur) + e.LastOperator.Cost(Cur);
                            if (G.Lookup(e) > cost)
                            {
                                G.Set(e, cost);
                                tree.Set(e, Cur);
                                OpenList.Enqueue(cost, e);
                            }
                        }
                        this.Generations++;
                    }
                }
            }
        }
Beispiel #25
0
 public Q0DictionaryToFile(string filePath, ExtendedDictionary <State, float[]> q0Dictionary) : base(filePath)
 {
     FilePath     = filePath;
     Q0Dictionary = q0Dictionary;
     DictionaryToFile();
 }