Exemple #1
0
    //search delegate
    protected bool EdgeWithinLoop(LinkedGraphEdge theEdge)
    {
        if (!(theEdge is EdgeType))
        {
            return(false);
        }
        EdgeType castEdge = (EdgeType)theEdge;
        Polygon  poly     = GetPolygon();

        Vector2 edgeCenter = (theEdge.a.pt + theEdge.b.pt) / 2f;

        if (edges.Contains(castEdge) || poly.ContainsPoint(edgeCenter))
        {
            return(true);
        }
        else
        {
            return(false);
        }

        //if ((poly.PermiterContainsPoint(theEdge.a.pt, Segment.defaultAccuracy*2) || poly.ContainsPoint(theEdge.a.pt)) &&
        //    (poly.PermiterContainsPoint(theEdge.b.pt, Segment.defaultAccuracy*2) || poly.ContainsPoint(theEdge.b.pt)))
        //{
        //    return true;
        //}
        //return false;
    }
Exemple #2
0
        /// <summary>
        ///  Initializes the solver so that the optimization can be run.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// If not all target nodes are connected to the start node.
        /// </exception>
        public void Initialize()
        {
            BuildSearchGraph();

            _searchSpace =
                _searchGraph.NodeDict.Values.Where(n => IncludeNode(n) && n != StartNodes && !TargetNodes.Contains(n))
                .ToList();

            var consideredNodes = SearchSpace.Concat(TargetNodes).ToList();

            consideredNodes.Add(StartNodes);
            Distances.CalculateFully(consideredNodes);

            if (_targetNodes.Any(node => !Distances.AreConnected(StartNodes, node)))
            {
                throw new InvalidOperationException("The graph is disconnected.");
            }

            // Saving the leastSolution as initial solution. Makes sure there is always a
            // solution even if the search space is empty or MaxGeneration is 0.
            BestSolution = SpannedMstToSkillnodes(CreateLeastSolution());

            var removedNodes   = new List <GraphNode>();
            var newSearchSpace = new List <GraphNode>();

            foreach (var node in SearchSpace)
            {
                if (IncludeNodeUsingDistances(node))
                {
                    newSearchSpace.Add(node);
                }
                else
                {
                    removedNodes.Add(node);
                }
            }
            _searchSpace = newSearchSpace;

            var remainingNodes = SearchSpace.Concat(TargetNodes).ToList();

            remainingNodes.Add(StartNodes);
            Distances.RemoveNodes(removedNodes, remainingNodes);

            if (_targetNodes.Count / (double)remainingNodes.Count >= PreFilledSpanThreshold)
            {
                var prioQueue = new LinkedListPriorityQueue <LinkedGraphEdge>(100);
                for (var i = 0; i < remainingNodes.Count; i++)
                {
                    for (var j = i + 1; j < remainingNodes.Count; j++)
                    {
                        prioQueue.Enqueue(new LinkedGraphEdge(i, j), Distances[i, j]);
                    }
                }
                _firstEdge = prioQueue.First;
            }

            InitializeGa();

            _isInitialized = true;
        }
Exemple #3
0
 //add functionality to ensure that children edges inheret our id and type
 public override void OnEdgeSplitCustom(LinkedGraphEdge edge1, LinkedGraphEdge edge2)
 {
     if (edge1 is CityEdge)
     {
         CityEdge ce1 = (CityEdge)edge1;
         ce1.id    = id;
         ce1.type  = type;
         ce1.width = width;
     }
     else
     {
         Debug.Log("couldn't assign data to sub edge on split.");
     }
     if (edge2 is CityEdge)
     {
         CityEdge ce2 = (CityEdge)edge2;
         ce2.id    = id;
         ce2.type  = type;
         ce2.width = width;
     }
     else
     {
         Debug.Log("couldn't assign data to sub edge on split.");
     }
 }
Exemple #4
0
    public void AddEdge(LinkedGraphEdge edge, Color color)
    {
        if (edges.Count > 1)
        {
            foreach (LinkedGraphEdge edgeIter in edges)
            {
                if (edge == edgeIter)
                {
                    print("HARD DUP");
                }
                else if (edge.IsEqual(edgeIter))
                {
                    print("SOFT DUP");
                }
            }
            //print("DUPLICATE HARD");
        }
        //if (edges.Count > 1 && edge.IsEqual(edges[edges.Count - 1]))
        //{
        //    print("DUPLICATE SOFT");
        //}

        edges.Add(edge);
        if (color != null)
        {
            colors.Add(color);
        }
        else
        {
            colors.Add(Color.white);
        }
        elevations.Add(edges.Count / 10f);

        //print(edge.a.pt + ", " + edge.b.pt);
    }
    public List <EdgeType> CollectEdges <EdgeType> (bool allConsecutive, SearchFilter filter) where EdgeType : LinkedGraphEdge
    {
        List <EdgeType>           collectedEdges = new List <EdgeType>();
        HashSet <LinkedGraphEdge> seenEdges      = new HashSet <LinkedGraphEdge>();

        //List<LinkedGraphEdge> frontier = new List<LinkedGraphEdge>();

        Stack <LinkedGraphEdge> frontier = new Stack <LinkedGraphEdge>();

        frontier.Push(this);

        while (frontier.Count > 0)
        {
            LinkedGraphEdge next = frontier.Pop();

            EdgeType thisInstance = null;
            bool     spread       = true;
            if (next is EdgeType)
            {
                thisInstance = (EdgeType)next;
            }

            bool passesFilter = filter == null || filter(next);

            if (thisInstance == null)
            {
                if (allConsecutive)
                {
                    spread = false;
                }
            }
            else if (passesFilter)
            {
                collectedEdges.Add(thisInstance);
            }
            else if (allConsecutive)//if all the passing edge should be attached, or consecutive, then if this fails, return.
            {
                spread = false;
            }

            seenEdges.Add(next);

            if (spread)
            {
                next.EnumerateNeighborEdges((LinkedGraphEdge edge) =>
                {
                    if (!seenEdges.Contains(edge) && !frontier.Contains(edge))
                    {
                        frontier.Push(edge);
                    }
                });
            }
        }

        //CollectEdgesR(collectedEdges, seenEdges, allConsecutive, filter, mask);

        return(collectedEdges);
    }
 public bool IsEqual(LinkedGraphEdge other)
 {
     if ((a == other.a || a == other.b) &&
         (b == other.a || b == other.b))
     {
         return(true);
     }
     return(false);
 }
Exemple #7
0
    public LinkedGraphEdge GetLeftConnection(LinkedGraphEdge edge)
    {
        int index = connections.IndexOf(edge);

        if (index != -1)
        {
            return(connections[(index - 1 + connections.Count) % connections.Count]);
        }
        return(null);
    }
 //called on the instance ofwhen it is subdivided
 //Ensure that edge1.a is equal to this.a and edge2.b is equal to this.b
 public void OnEdgeSplit(LinkedGraphEdge edge1, LinkedGraphEdge edge2)
 {
     foreach (IEdgeSplitListener listener in listeners)
     {
         listener.SplitEdge(this, edge1, edge2);
         edge1.AddEdgeSplitListener(listener);
         edge2.AddEdgeSplitListener(listener);
     }
     OnEdgeSplitCustom(edge1, edge2);
 }
    public LinkedGraphVertex GetSharedVertex(LinkedGraphEdge other)
    {
        if (a == other.a || a == other.b)
        {
            return(a);
        }
        else if (b == other.a || b == other.b)
        {
            return(b);
        }

        return(null);
    }
Exemple #10
0
    //split edge into a and b.
    public void SplitEdge(LinkedGraphEdge edge, LinkedGraphEdge a, LinkedGraphEdge b)
    {
        if (!(edge is EdgeType && a is EdgeType && b is EdgeType))
        {
            Debug.LogWarning("Bad split. one of the params is not the same type defined edgeLoop type");
            return;
        }
        EdgeType castEdge = (EdgeType)edge;
        EdgeType castA    = (EdgeType)a;
        EdgeType castB    = (EdgeType)b;

        if (a.a != edge.a || b.b != edge.b)
        {
            Debug.LogWarning("Bad split. A or B don't represent a split of edge");
            return;
        }
        int ind = edges.IndexOf(castEdge);

        if (ind == -1)
        {
            Debug.Log("Could not split edge as this loop does not contain it.");
        }
        else
        {
            bool edgeFollowsWinding = EdgeFollowsWinding(castEdge);
            edges.RemoveAt(ind);

            //make sure we add the two replacing edges in correct order
            if (edgeFollowsWinding)
            {
                edges.Insert(ind, castA);
                edges.Insert(ind + 1, castB);
            }
            else
            {
                edges.Insert(ind, castB);
                edges.Insert(ind + 1, castA);
            }
        }
    }
Exemple #11
0
    private void Update()
    {
        LinkedGraph <LinkedGraphEdge> .DebugDraw(edges, colors, elevations);

        if (edges.Count > 0)
        {
            LinkedGraphEdge higlightedEdge = edges[edgeHighlightIndex];
            if (vertHighlight)
            {
                LinkedGraphVertex vert;
                if (vertIndex == 0)
                {
                    vert = higlightedEdge.a;
                }
                else
                {
                    vert = higlightedEdge.b;
                }

                foreach (LinkedGraphEdge edge in vert.GetConnections())
                {
                    Debug.DrawLine(HelperFunctions.projVec2(edge.a.pt) + Vector3.up * 1.1f, HelperFunctions.projVec2(edge.b.pt) + Vector3.up * 1.1f, Color.green);
                }

                Debug.Log(vert.NumConnections() + " " + Time.time);
            }
            else
            {
                Debug.DrawLine(HelperFunctions.projVec2(higlightedEdge.a.pt) + Vector3.up * 1.1f, HelperFunctions.projVec2(higlightedEdge.b.pt) + Vector3.up * 1.1f, Color.green);
            }
        }

        if (displaySimpleEdges)
        {
            foreach (Vector4 edge in simpleEdges)
            {
                Debug.DrawLine(new Vector3(edge.x, -1f, edge.y), new Vector3(edge.z, -1f, edge.w), Color.yellow);
            }
        }
    }
Exemple #12
0
    public void AddConnection(LinkedGraphEdge connection)
    {
        int insertIndex             = 0;
        LinkedGraphVertex otherVert = connection.GetOppositeVertex(this);
        float             newAngle  = HelperFunctions.AngleBetween(otherVert.pt - pt, Vector2.right) % (Mathf.PI * 2);

        for (int i = 0; i < connections.Count; i++)
        {
            LinkedGraphVertex thisOppositeVert = connections[i].GetOppositeVertex(this);
            float             existingAngle    = HelperFunctions.AngleBetween(thisOppositeVert.pt - pt, Vector2.right) % (Mathf.PI * 2);

            if (newAngle < existingAngle)
            {
                insertIndex++;
            }
            else
            {
                break;
            }
        }

        connections.Insert(insertIndex, connection);
    }
Exemple #13
0
 public bool RemoveConnection(LinkedGraphEdge connection)
 {
     return(connections.Remove(connection));
 }
 public bool isConnectedTo(LinkedGraphEdge other)
 {
     return(GetSharedVertex(other) != null);
 }
 public virtual void OnEdgeSplitCustom(LinkedGraphEdge edge1, LinkedGraphEdge edge2)
 {
 }