public Tuple <CoverNode <T>, SpatialObj <T> > IncrementalNNFindNext()
        {
            if (incrNN_queue is null)
            {
                return(null);
            }

            while (incrNN_queue.Count > 0)
            {
                NodeOrObj current_element = incrNN_queue.Dequeue();
                while (incrNN_queue.Count > 0 && incrNN_queue.First().Equals(current_element))
                {
                    incrNN_queue.Dequeue();
                }

                if (current_element.IsObj())
                {
                    return(Tuple.Create(current_element.GetNode(), current_element.GetObj()));
                }
                else
                {
                    CoverNode <T> current_node = current_element.GetNode();
                    double        current_dist = current_node.GetDistanceToPointSq(incrNN_origin);

                    if (!current_node.IsEmpty())
                    {
                        foreach (SpatialObj <T> obj in current_node.GetAllObjects())
                        {
                            double distance = obj.boundingBox.GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj obj_nodeOrObj = new NodeOrObj();
                                obj_nodeOrObj.SetNode(current_node);
                                obj_nodeOrObj.SetObj(obj);
                                incrNN_queue.Enqueue(obj_nodeOrObj, (float)distance);
                            }
                        }
                    }
                    if (current_node.HasChildren())
                    {
                        foreach (CoverNode <T> child_node in current_node.GetChildren())
                        {
                            double distance = child_node.GetDistanceToPointSq(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj node_nodeOrObj = new NodeOrObj();
                                node_nodeOrObj.SetNode(child_node);
                                incrNN_queue.Enqueue(node_nodeOrObj, (float)distance);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        public Dictionary <int, int> MainAlgorithm(int source)
        {
            SimplePriorityQueue <int> priorityQueue = new SimplePriorityQueue <int>();

            InitiateLenghts(source);
            priorityQueue.Enqueue(source, 0);
            while (priorityQueue.Any())
            {
                int operatedVertex = priorityQueue.First();
                priorityQueue.Dequeue();
                foreach (var item in calculatedGraph.vertices[operatedVertex].outgoingEdges)
                {
                    int newLenght = lenghts[operatedVertex] + item.Value;
                    if (lenghts[item.Key] > newLenght)
                    {
                        lenghts[item.Key] = newLenght;
                        priorityQueue.Enqueue(item.Key, newLenght);
                    }
                }
            }
            return(lenghts);
        }
    public void refershSimilarMapsBatch()
    {
        int i = 0;

        TileObject[,] tmpStrAlias;
        Vector2Int startMainMap = ParameterManager.Instance.StartCell;

        GeneratorUIManager.Instance.deleteMapOnUI(AliasDragAreas[1].GetChild(0));
        AliasDragAreas[1].GetComponent <MapListManager>().dictionaryMap.Clear();

        if (SimilarMapsQueue.Count <= 0)
        {
            GenerateAndTestAliasMaps();
        }

        while (i < BatchAliasNumber)
        {
            float dst = SimilarMapsQueue.GetPriority(SimilarMapsQueue.First());
            tmpStrAlias = SimilarMapsQueue.Dequeue();

            Utility.renderAliasOnUI(AliasDragAreas[1].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true);
            i++;
        }
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Complexifies the tessellation so that no edge is longer than provided the maximum edge length
        /// or for adding the provided number of faces - whichever comes first
        /// </summary>
        /// <param name="ts">The ts.</param>
        /// <param name="numberOfFaces">The number of new faces to add.</param>
        /// <param name="maxLength">The maximum length.</param>
        public static void Complexify(TessellatedSolid ts, int numberOfFaces, double maxLength)
        {
            var edgeQueue = new SimplePriorityQueue <Edge, double>(new ReverseSort());

            foreach (var e in ts.Edges)
            {
                edgeQueue.Enqueue(e, e.Length);
            }
            var addedEdges    = new List <Edge>();
            var addedVertices = new List <Vertex>();
            var addedFaces    = new List <PolygonalFace>();
            var edge          = edgeQueue.Dequeue();
            var iterations    = numberOfFaces > 0 ? (int)Math.Ceiling(numberOfFaces / 2.0) : numberOfFaces;

            while (iterations-- != 0 && edge.Length >= maxLength)
            {
                var origLeftFace   = edge.OtherFace;
                var origRightFace  = edge.OwnedFace;
                var leftFarVertex  = origLeftFace.OtherVertex(edge);
                var rightFarVertex = origRightFace.OtherVertex(edge);
                var fromVertex     = edge.From;
                var toVertex       = edge.To;
                var addedVertex    = new Vertex(DetermineIntermediateVertexPosition(fromVertex, toVertex));
                // modify original faces with new intermediate vertex
                var index = origLeftFace.Vertices.IndexOf(toVertex);
                origLeftFace.Vertices[index] = addedVertex;
                origLeftFace.Update();
                addedVertex.Faces.Add(origLeftFace);
                index = origRightFace.Vertices.IndexOf(toVertex);
                origRightFace.Vertices[index] = addedVertex;
                origRightFace.Update();
                addedVertex.Faces.Add(origRightFace);

                var newLeftFace  = new PolygonalFace(new[] { toVertex, addedVertex, leftFarVertex });
                var newRightFace = new PolygonalFace(new[] { addedVertex, toVertex, rightFarVertex });
                toVertex.Faces.Remove(origLeftFace);
                toVertex.Faces.Remove(origRightFace);

                var inlineEdge = new Edge(addedVertex, toVertex, newRightFace, newLeftFace, true);
                toVertex.Edges.Remove(edge);
                edge.To = addedVertex;
                addedVertex.Edges.Add(edge);
                edge.Update();
                var newLeftEdge  = new Edge(leftFarVertex, addedVertex, origLeftFace, newLeftFace, true);
                var newRightEdge = new Edge(rightFarVertex, addedVertex, newRightFace, origRightFace, true);
                origLeftFace.AddEdge(newLeftEdge);
                origRightFace.AddEdge(newRightEdge);
                var bottomEdge = toVertex.Edges.First(e => e.OtherVertex(toVertex) == leftFarVertex);
                if (bottomEdge.OwnedFace == origLeftFace)
                {
                    bottomEdge.OwnedFace = newLeftFace;
                }
                else
                {
                    bottomEdge.OtherFace = newLeftFace;
                }
                newLeftFace.AddEdge(bottomEdge);
                bottomEdge.Update();

                bottomEdge = toVertex.Edges.First(e => e.OtherVertex(toVertex) == rightFarVertex);
                if (bottomEdge.OwnedFace == origRightFace)
                {
                    bottomEdge.OwnedFace = newRightFace;
                }
                else
                {
                    bottomEdge.OtherFace = newRightFace;
                }
                newRightFace.AddEdge(bottomEdge);
                bottomEdge.Update();


                // need to re-add the edge. It was modified in the SplitEdge function (now, half the lenght), but
                // it may still be met by this criteria
                edgeQueue.Enqueue(edge, edge.Length);
                edgeQueue.Enqueue(inlineEdge, inlineEdge.Length);
                addedEdges.Add(inlineEdge);
                edgeQueue.Enqueue(newLeftEdge, newLeftEdge.Length);
                addedEdges.Add(newLeftEdge);
                edgeQueue.Enqueue(newRightEdge, newRightEdge.Length);
                addedEdges.Add(newRightEdge);
                addedFaces.Add(newLeftFace);
                addedFaces.Add(newRightFace);
                addedVertices.Add(addedVertex);
                edge = edgeQueue.First();
            }
            ts.AddVertices(addedVertices);
            ts.AddEdges(addedEdges);
            ts.AddFaces(addedFaces);
        }
Ejemplo n.º 5
0
 public void Unsubscribe(Object subscriber, Action <Object, TEventArgs> a)
 {
     subscribers.Remove(subscribers.First(x => x.Key.Equals(subscriber) && x.Value.Equals(a)));
 }
Ejemplo n.º 6
0
        public static LinkedList <T> AStar <T>(T fromVertice, T toVertice) where T : IVertice
        {
            if (fromVertice == null || toVertice == null)
            {
                Debug.LogError("fromVertice: " + (fromVertice == null ? "null" : "ref") + " toVertice: " +
                               (toVertice == null ? "null" : "ref"));
                return(new LinkedList <T>());
            }

            var closedSet = new List <T>();
            var openSet   = new SimplePriorityQueue <T, float>(); // new List<T>();

            openSet.Enqueue(fromVertice, 0f);
            var cameFrom = new Dictionary <T, T>();

            var gScore = new Dictionary <T, float> {
                [fromVertice] = 0f
            };

            var fScore = new Dictionary <T, float> {
                [fromVertice] = HeuristicCostEstimate(fromVertice, toVertice)
            };

            while (openSet.Count > 0)
            {
                var currentVertice = openSet.First();
                if (currentVertice.Equals(toVertice))
                {
                    return(ReconstructPath(cameFrom, currentVertice));
                }

                openSet.Remove(currentVertice);
                closedSet.Add(currentVertice);

                foreach (T neighborVertice in currentVertice.Neighbors)
                {
                    if (closedSet.Contains(neighborVertice))
                    {
                        continue;
                    }

                    var tentativeGScore = gScore[currentVertice] + Vector3.Distance(currentVertice.Position, neighborVertice.Position);

                    if (openSet.Contains(neighborVertice) && tentativeGScore >= gScore[neighborVertice])
                    {
                        continue;
                    }

                    cameFrom[neighborVertice] = currentVertice;
                    gScore[neighborVertice]   = tentativeGScore;
                    fScore[neighborVertice]   = gScore[neighborVertice] +
                                                HeuristicCostEstimate(neighborVertice, toVertice);

                    if (openSet.Contains(neighborVertice))
                    {
                        openSet.Remove(neighborVertice);
                    }

                    openSet.Enqueue(neighborVertice, fScore[neighborVertice]);
                }
            }

            return(new LinkedList <T>());
        }
Ejemplo n.º 7
0
 private bool AreOperationsFinishing(int startTime, Assay assay)
 {
     return(CurrentlyRunningOpertions.Count > 0 && (assay.IsEmpty() || startTime >= CurrentlyRunningOpertions.First().EndTime));
 }
    public Dictionary <int, StructuredAlias> GenerateNRandomAliasFromRealMap(StructuredAlias realMap, int N)
    {
        mainMap  = realMap.AliasMap;
        gridType = ParameterManager.Instance.GridType;
        Vector2Int startMainMap = realMap.start;
        int        width        = realMap.AliasMap.GetLength(0);
        int        height       = realMap.AliasMap.GetLength(1);

        //Map initialization.
        int i = 0;

        Dictionary <int, StructuredAlias> AliasBatch = new Dictionary <int, StructuredAlias>();

        Vector2Int startAlias = realMap.start;
        Vector2Int endAlias   = realMap.end;
        SimplePriorityQueue <TileObject[, ]> tmpPQ = new SimplePriorityQueue <TileObject[, ]>();
        bool isAliasSameAsReal = true;

        while (i < N)
        {
            //define here the width, height, start and end  of the chosen map
            TileObject[,] aliasMap = new TileObject[width, height];

            genMan.connectedGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0?1:-1) * RNG_Alias.Next(), false);
            genMan.cellularAutomataGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0 ? 1 : -1) * RNG_Alias.Next(), false);
            genMan.primGenerator.setBaseGeneratorParameters(gridType, width, height, startAlias, endAlias, (i % 2 == 0 ? 1 : -1) * RNG_Alias.Next(), false);

            switch (i % 3)
            {
            case 0:
                aliasMap = genMan.connectedGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)RNG_Alias.NextDouble());
                break;

            case 1:
                float prcObsCA = (float)RNG_Alias.NextDouble();
                aliasMap = genMan.cellularAutomataGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, prcObsCA, 1 + (prcObsCA <= .5f && !(i % 2 == 1)?0:RNG_Alias.Next(0, 6)), 5, i % 2 == 1, 0, 0);
                break;

            case 2:
                aliasMap = genMan.primGenerator.generateMapGeneral(ParameterManager.Instance.IsTrapsOnMapBorder, (float)RNG_Alias.NextDouble());
                break;

            default:
                ErrorManager.ManageError(ErrorManager.Error.HARD_ERROR, "AliasManager: no map generator found.");
                break;
            }

            foreach (var m in BaseAliasCollisionMask)
            {
                if (Utility.in_bounds_General(new Vector2Int(startMainMap.x + m.x, startMainMap.y + m.y), mainMap.GetLength(0), mainMap.GetLength(1)) &&
                    Utility.in_bounds_General(new Vector2Int(startAlias.x + m.x, startAlias.y + m.y), aliasMap.GetLength(0), aliasMap.GetLength(1)))
                {
                    aliasMap[startAlias.x + m.x, startAlias.y + m.y].type = realMap.AliasMap[startMainMap.x + m.x, startMainMap.y + m.y].type;
                }
            }

            for (int h = 0; h < width; h++)
            {
                for (int k = 0; k < height; k++)
                {
                    isAliasSameAsReal &= (aliasMap[h, k].type == realMap.AliasMap[h, k].type);

                    if (!isAliasSameAsReal)
                    {
                        break;
                    }
                }
                if (!isAliasSameAsReal)
                {
                    break;
                }
            }

            if (MapEvaluator.isEndReachable(aliasMap, gridType, startAlias, endAlias, ParameterManager.Instance.allowAutosolverForAlias).First() == endAlias && !isAliasSameAsReal)
            {
                //if the map has a path from start to end, add it
                float dst = MapEvaluator.BinaryMapSimilarity(mainMap, aliasMap, startMainMap, startAlias);
                tmpPQ.Enqueue(aliasMap, dst);


                i++;
            }
        }

        i = 0;
        TileObject[,] tmpStrAlias;
        while (i < N)
        {
            if (ParameterManager.Instance.considerSimilar)
            {
                float dst = tmpPQ.GetPriority(tmpPQ.First());
                tmpStrAlias = tmpPQ.Dequeue();

                AliasBatch.Add(Guid.NewGuid().GetHashCode(), new StructuredAlias(tmpStrAlias, startAlias, endAlias, dst));

                i++;
            }

            if (ParameterManager.Instance.considerNovelty && i < N)
            {
                tmpStrAlias = tmpPQ.Last();
                float dst = tmpPQ.GetPriority(tmpStrAlias);
                tmpPQ.Remove(tmpStrAlias);

                AliasBatch.Add(Guid.NewGuid().GetHashCode(), new StructuredAlias(tmpStrAlias, startAlias, endAlias, dst));

                i++;
            }
        }

        return(AliasBatch);
    }
    public void CollabGameGeneration()
    {
        mainMap          = ParameterManager.Instance.MapToPlay;
        gridType         = ParameterManager.Instance.GridType;
        SimilarMapsQueue = new SimplePriorityQueue <TileObject[, ]>();

        K_CollisionSet = MapEvaluator.BuildKCollisionVec(mainMap, gridType, ParameterManager.Instance.StartCell, Mathf.Max(ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.maxStepsSolution));

        if (ParameterManager.Instance.isOptimizerOn)
        {
            List <TileObject[, ]> alises = new List <TileObject[, ]>();
            BaseAliasCollisionMask = getMainMapKMaxMinCells(mainMap, gridType, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.minStepsSolution, ParameterManager.Instance.StartCell, 0f);

            int experimentsNum = 1;
            while (experimentsNum > 0)
            {
                foreach (var a in AliasGeneratorManager.Instance.GetComponent <AliasGameEvaluator>().AliasGameOptimizerHandler())
                {
                    SimilarMapsQueue.Enqueue(a.Value.AliasMap, a.Value.similarityDistance);
                }
                experimentsNum--;
            }
        }
        else
        {
            GenerateAndTestAliasMaps();
        }


        int        i            = 0;
        Vector2Int startMainMap = ParameterManager.Instance.StartCell;

        TileObject[,] tmpStrAlias;
        while (i < ParameterManager.Instance.aliasNum)
        {
            if (ParameterManager.Instance.considerSimilar)
            {
                float dst = SimilarMapsQueue.GetPriority(SimilarMapsQueue.First());
                tmpStrAlias = SimilarMapsQueue.Dequeue();

                Utility.renderAliasOnUI(AliasDragAreas[0].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true);
                i++;
            }

            if (ParameterManager.Instance.considerNovelty && i < ParameterManager.Instance.aliasNum)
            {
                tmpStrAlias = SimilarMapsQueue.Last();
                float dst = SimilarMapsQueue.GetPriority(tmpStrAlias);
                SimilarMapsQueue.Remove(tmpStrAlias);

                Utility.renderAliasOnUI(AliasDragAreas[0].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true);
                i++;
            }
        }

        i = 0;
        while (SimilarMapsQueue.Count > 0 && i < BatchAliasNumber)
        {
            float dst = SimilarMapsQueue.GetPriority(SimilarMapsQueue.First());
            tmpStrAlias = SimilarMapsQueue.Dequeue();

            Utility.renderAliasOnUI(AliasDragAreas[1].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true);
            i++;
        }

        i = 0;
        while (SimilarMapsQueue.Count > 0 && i < BatchAliasNumber)
        {
            tmpStrAlias = SimilarMapsQueue.Last();
            float dst = SimilarMapsQueue.GetPriority(tmpStrAlias);
            SimilarMapsQueue.Remove(tmpStrAlias);

            Utility.renderAliasOnUI(AliasDragAreas[2].GetChild(0).GetComponent <RectTransform>(), ParameterManager.Instance.GridType, new StructuredAlias(tmpStrAlias, startMainMap, ParameterManager.Instance.EndCell, dst), AliasPrefab, true);

            i++;
        }

        //reset horizontal and vertical bars if exists
        ScrollRect sR = AliasDragAreas[0].GetComponent <ScrollRect>();

        if (sR != null)
        {
            Scrollbar hSb = sR.horizontalScrollbar;
            Scrollbar vSb = sR.verticalScrollbar;

            if (hSb != null)
            {
                hSb.value = .99f;
            }

            if (vSb != null)
            {
                vSb.value = .99f;
            }
        }

        gameObject.GetComponent <AliasGameEvaluator>().AliasGameEvaluatorHandler();
    }
Ejemplo n.º 10
0
        public List <RectangleObj> IncrementalNNFindNext()
        {
            // 1st entry is the object found, the rest are the bounding boxes of circular search steps.
            List <RectangleObj> answer = new List <RectangleObj>();
            Random rand = new Random();

            while (incrNN_queue.Count > 0)
            {
                NodeOrObj current_element = incrNN_queue.Dequeue();
                while (incrNN_queue.Count > 0 && incrNN_queue.First().Equals(current_element))
                {
                    incrNN_queue.Dequeue();
                }

                if (current_element.IsObj())
                {
                    double       dist      = current_element.GetObj().GetDistanceSqToPoint(incrNN_origin);
                    RectangleObj circleObj = new RectangleObj();
                    int          radius    = (int)(Math.Sqrt(dist) * 2.0);
                    circleObj.SetRectangleByCenter(incrNN_origin, radius, radius);
                    circleObj.rect_color = Color.Orange;
                    answer.Add(circleObj);

                    answer.Insert(0, current_element.GetObj());
                    return(answer);
                }
                else
                {
                    NodeType     current_node = current_element.GetNode();
                    double       current_dist = current_node.GetBounds().GetDistanceSqToPoint(incrNN_origin);
                    RectangleObj circleObj    = new RectangleObj();
                    int          radius       = (int)(Math.Sqrt(current_dist) * 2.0);
                    circleObj.SetRectangleByCenter(incrNN_origin, radius, radius);
                    Color col = Color.FromArgb(0, rand.Next(50, 256), rand.Next(50, 256));
                    circleObj.rect_color = col;

                    answer.Add(circleObj);
                    if (!current_node.IsEmpty())
                    {
                        foreach (RectangleObj obj in current_node.GetAllStoredObjs())
                        {
                            double distance = obj.GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj obj_nodeOrObj = new NodeOrObj();
                                obj_nodeOrObj.SetObj(obj);
                                incrNN_queue.Enqueue(obj_nodeOrObj, (float)distance);
                            }
                        }
                    }
                    if (current_node.HasChildren())
                    {
                        foreach (KeyValuePair <int, NodeType> child_node in current_node.getChildern())
                        {
                            double distance = child_node.Value.GetBounds().GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj node_nodeOrObj = new NodeOrObj();
                                node_nodeOrObj.SetNode(child_node.Value);
                                incrNN_queue.Enqueue(node_nodeOrObj, (float)distance);
                            }
                        }
                    }
                }
            }
            return(answer);
        }