Example #1
0
        void Update()
        {
            if (!IsSimulating.Value)
            {
                return;
            }

            CheckSimulationComplete();

            if (!eventQueue.Any())
            {
                return;
            }

            simulationDuration += Time.deltaTime;

            var nextEvent = eventQueue.First();

            if (nextEvent.Delay <= simulationDuration)
            {
                var activeEvent = eventQueue.DeleteMin();
                activeEvents.Add(activeEvent);

                activeEvent.Start().Then(() =>
                {
                    activeEvents.Remove(activeEvent);
                    completeEvents.Add(activeEvent);
                    CheckSimulationComplete();
                });
            }
        }
Example #2
0
        private void GetTopDistances(IEnumerable <string> threadDictionary)
        {
            var comparer = new WordDistanceComparer();
            var heap     = new IntervalHeap <WordDistance>(HeapCapacity, comparer);

            foreach (var entry in threadDictionary)
            {
                var threshold = heap.Any() ? heap.FindMax().Distance : int.MaxValue;
                var distance  = Value.GetDistance(entry, threshold);

                if (heap.Count < HeapCapacity)
                {
                    heap.Add(new WordDistance(entry, distance));
                    continue;
                }

                if (distance >= threshold)
                {
                    continue;
                }

                heap.DeleteMax();
                heap.Add(new WordDistance(entry, distance));
            }

            foreach (var wordDistance in heap)
            {
                results.Add(wordDistance);
            }
        }
Example #3
0
        // Converts the Cytoscape nodes into animation nodes and pushes them onto the list that tracks frontiers
        // We have to clone the frontier and then delete the elements from the copy because there is no way
        // to iterate through the frontier ordered by priority.
        private static void storeFrontierOverTime(AStarSpecificAnimation frontierOverTime, IntervalHeap <CytoscapeNode> frontier)
        {
            IntervalHeap <CytoscapeNode> frontierCopy = cloneFrontier(frontier);
            AStarAnimationNode           animationNode;
            List <AStarAnimationNode>    currentFrontier = new List <AStarAnimationNode>();
            CytoscapeNode cyNode;

            while (frontierCopy.Any())
            {
                cyNode             = frontierCopy.DeleteMax();
                animationNode      = new AStarAnimationNode(cyNode.id);
                animationNode.name = cyNode.name;
                animationNode.f    = cyNode.f;
                currentFrontier.Add(animationNode);
            }

            frontierOverTime.frontierOverTime.Add(currentFrontier);
        }
Example #4
0
        public IEnumerable <Timetable> SolveSssp(System.Collections.Generic.IList <Course> courses, ConstraintsCollection constraints)
        {
            var allClassTypes = GetAllClassTypes(courses);
            var variables     = ApplyMrvHeuristic(allClassTypes);

            //allSolutions = MaxHeap
            var backtrackingSolutions = new IntervalHeap <Timetable>(200);

            // BT(Variables, {}, Domains, allSolutions)
            var cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(20));
            var cancellationToken = cancellationTokenSource.Token;
            var backtrackingTask  = Task.Factory.StartNew(
                () =>
            {
                BacktrackingAllSolutions(variables, new Timetable(), backtrackingSolutions, 0, constraints, cancellationToken);
                return(backtrackingSolutions);
            }, cancellationToken);

            // Generic algorithm
            var geneticAlgorithmRunner = new TimetablerGeneticAlgorithmRunner(variables, constraints);
            var geneticTask            = geneticAlgorithmRunner.RunAsync();

            Task.WaitAll(backtrackingTask);

            var timetables = new List <Timetable>();

            if (cancellationToken.IsCancellationRequested)
            {
                geneticTask.Wait();
                timetables.Add(geneticAlgorithmRunner.GeneticSolution);
            }

            if (backtrackingSolutions.Any())
            {
                timetables.AddRange(backtrackingSolutions);
            }

            var sortedSolutions = SortByRate(timetables);

            //Return allSolutions
            return(sortedSolutions);
        }
Example #5
0
        public void IntervalHeap_NoComparer_OrderIsAscendingByDefaultComparer()
        {
            var priorityQueue = new IntervalHeap <int> {
                3, 4, 5, 1, 2
            };

            var orderedResult = new List <int>();

            while (priorityQueue.Any())
            {
                orderedResult.Add(priorityQueue.FindMin());
                priorityQueue.DeleteMin();
            }

            var expectedResult = new List <int> {
                1, 2, 3, 4, 5
            };

            orderedResult.Should().BeEquivalentTo(expectedResult, config => config.WithStrictOrderingFor(number => number));
        }
Example #6
0
        public List<Node> AStarSearch(Vector3 start, Vector3 end)
        {
            var frontier = new IntervalHeap<Node>(new NodeComparer());
            var cameFrom = new Dictionary<string, Node>();
            //var costSoFar = new Dictionary<string, int>();

            var startNode = FindNode(start);
            var endNode = FindNode(end);

            frontier.Add(startNode);
            cameFrom.Add(startNode.ToString(), null);

            while (frontier.Any())
            {
                var currentNode = frontier.FindMin();
                frontier.DeleteMin();

                if (currentNode == endNode)
                    break;

                foreach (var neighbor in currentNode.Neighboors)
                {
                    var neighborName = neighbor.ToString();

                    if (cameFrom.ContainsKey(neighborName))
                        continue;

                    var newCost = Heuristic(endNode, neighbor);

                    neighbor.Cost = newCost;

                    frontier.Add(neighbor);
                    cameFrom.Add(neighborName, currentNode);
                }
            }

            var current = endNode;
            var path = new List<Node> { current };

            if (!cameFrom.ContainsKey(endNode.ToString())) return path;

            while (current != startNode)
            {
                if (!cameFrom.ContainsKey(current.ToString())) continue;

                current = cameFrom[current.ToString()];
                path.Add(current);
            }

            return path;
        }