コード例 #1
0
 /// <summary>
 /// Starts a new route to a given goal StreetConnector.
 /// </summary>
 /// <param name="goal">The goal StreetConnector</param>
 public void StartNewRoute(StreetConnector goal)
 {
     goalConnector = goal;
     if (goal == currentConnector)
     {
         currentRoutingState = RoutingState.Finished;
         return;
     }
     baseRoute = RouteManager.GetBestBaseRoute(currentConnector, goal);
     if (baseRoute == null)
     {
         specialRoute        = RouteManager.GetSpecialRoute(currentConnector, goal);
         currentRoutingState = RoutingState.SpecialStart2;
     }
     else
     {
         if (baseRoute.Start == currentConnector)
         {
             currentRoutingState = RoutingState.BaseStart;
         }
         else
         {
             specialRoute        = RouteManager.GetSpecialRoute(currentConnector, baseRoute.Start);
             currentRoutingState = RoutingState.SpecialStart1;
         }
     }
 }
コード例 #2
0
 /// <summary>
 /// Creates a new SearchNode for a given StreetConnector that is the first one.
 /// </summary>
 /// <param name="connector">The StreetConnector this SearchNode belongs to</param>
 /// <param name="end">The end StreetConnector which should be found a route to</param>
 public SearchNode(StreetConnector connector, StreetConnector end)
 {
     parent         = null;
     this.connector = connector;
     cost           = 0;
     estimate       = Coordinate.GetDistance(connector.Coordinate, end.Coordinate);
     total          = cost + estimate;
 }
コード例 #3
0
 /// <summary>
 /// Creates a new SearchNode for a given StreetConnector that was found through another SearchNode.
 /// </summary>
 /// <param name="parent">The SearchNode that lead to this one</param>
 /// <param name="connector">The StreetConnector this SearchNode belongs to</param>
 /// <param name="end">The end StreetConnector which should be found a route to</param>
 public SearchNode(SearchNode parent, StreetConnector connector, StreetConnector end)
 {
     this.parent    = parent;
     this.connector = connector;
     cost           = parent.cost + Coordinate.GetDistance(parent.connector.Coordinate, connector.Coordinate);
     estimate       = Coordinate.GetDistance(connector.Coordinate, end.Coordinate);
     total          = cost + estimate;
 }
コード例 #4
0
        /// <summary>
        /// Creates a new SpecialRoute between the given StreetConnectors.
        /// </summary>
        /// <param name="start">The start StreetConnector</param>
        /// <param name="end">The end StreetConnector</param>
        public SpecialRoute(StreetConnector start, StreetConnector end)
        {
            this.start = start;
            this.end   = end;
            waypoints  = new List <StreetConnector>();

            CalcRoute();
        }
コード例 #5
0
 protected void CloneFrom(BaseRoute baseRoute)
 {
     id        = baseRoute.id;
     start     = baseRoute.start;
     end       = baseRoute.end;
     waypoints = new List <StreetConnector>(baseRoute.waypoints);
     isUsable  = baseRoute.isUsable;
     length    = baseRoute.length;
 }
コード例 #6
0
        /// <summary>
        /// Creates a new BaseRoute between two StreetConnectors.
        /// </summary>
        /// <param name="start">The StreetConnector from where to begin</param>
        /// <param name="end">The StreetConnector to where to end</param>
        protected BaseRoute(StreetConnector start, StreetConnector end)
        {
            id = NextID;

            this.start = start;
            this.end   = end;
            waypoints  = new List <StreetConnector>();

            isUsable = CalcRoute();
            length   = CalcLength();
        }
コード例 #7
0
        private double CalcLength()
        {
            double          length = 0;
            StreetConnector cur    = start;

            foreach (StreetConnector next in waypoints)
            {
                length += Coordinate.GetDistance(cur.Coordinate, next.Coordinate);
                cur     = next;
            }
            length += Coordinate.GetDistance(cur.Coordinate, end.Coordinate);

            return(length);
        }
コード例 #8
0
        public void TestGetConnectingStreetSegmentWithStreetHub()
        {
            List <StreetConnector> sc1 = new List <StreetConnector>();
            StreetConnector        c1  = new StreetConnector(0, 0);
            StreetConnector        c2  = new StreetConnector(0, 50);
            StreetConnector        c3  = new StreetConnector(0, 120);
            StreetConnector        ch1 = new StreetConnector(0, 200);
            StreetConnector        ch2 = new StreetConnector(0, 210);
            StreetConnector        ch3 = new StreetConnector(5, 205);
            StreetConnector        c4  = new StreetConnector(0, 300);
            StreetConnector        c5  = new StreetConnector(50, 205);

            sc1.Add(c1);
            sc1.Add(c2);
            sc1.Add(c3);
            sc1.Add(ch1);
            sc1.Add(ch2);
            sc1.Add(ch3);
            sc1.Add(c4);
            sc1.Add(c5);

            List <StreetConnector> h = new List <StreetConnector>();

            h.Add(ch1);
            h.Add(ch2);
            h.Add(ch3);

            List <StreetSegment> ss1 = new List <StreetSegment>();
            StreetSegment        s1  = new StreetSegment(c1, c2);
            StreetSegment        s2  = new StreetSegment(c2, c3);
            StreetSegment        s3  = new StreetSegment(c3, ch1);
            StreetSegment        s4  = new StreetSegment(ch2, c4);
            StreetSegment        s5  = new StreetSegment(ch3, c5);

            ss1.Add(s1);
            ss1.Add(s2);
            ss1.Add(s3);
            ss1.Add(s4);
            ss1.Add(s5);

            List <StreetSegment> sTest1 = ControlSystem.GetConnectingStreetSegments(sc1);

            Assert.AreEqual(5, sTest1.Count());
            Assert.AreEqual(true, sTest1.Contains(s1));
            Assert.AreEqual(true, sTest1.Contains(s2));
            Assert.AreEqual(true, sTest1.Contains(s3));
            Assert.AreEqual(true, sTest1.Contains(s4));
            Assert.AreEqual(true, sTest1.Contains(s5));
        }
コード例 #9
0
        /// <summary>
        /// Creates a new Participant.
        /// </summary>
        /// <param name="position">The initial position of this participant</param>
        /// <param name="maxSpeed">The max speed of this participant</param>
        /// <param name="accidentRisk">The risk that this participant causes an accident</param>
        /// <param name="size">The amount of space this participant uses up</param>
        protected Participant(StreetConnector position, double maxSpeed, double accidentRisk, double size)
        {
            id = NextID;
            currentConnector  = position;
            this.position     = ParticipantPosition.FromCoordinate(position.Coordinate);
            this.maxSpeed     = maxSpeed;
            this.accidentRisk = accidentRisk;
            this.size         = size;

            baseRoute           = null;
            specialRoute        = null;
            currentRoutingState = RoutingState.Idle;
            lastConnector       = null;
            nextConnector       = null;
            goalConnector       = null;
            timeBonus           = TimeSpan.Zero;
            claimedSpace        = false;
            isFirstClaim        = true;
        }
コード例 #10
0
        /// <summary>
        /// Returns the best BaseRoute for routing between the start StreetConnector and the end StreetConnector.
        /// </summary>
        /// <param name="start">The start StreetConnector</param>
        /// <param name="end">The end StreetConnector</param>
        /// <returns>The best BaseRoute or null if no BaseRoute is available</returns>
        public static BaseRoute GetBestBaseRoute(StreetConnector start, StreetConnector end)
        {
            SortedList <double, BaseRoute> routes = new SortedList <double, BaseRoute>();
            BaseRoute route;
            double    cost;

            foreach (BaseRouteInfo routeInfo in baseRoutes.Values)
            {
                route = routeInfo.BaseRoute;
                cost  = route.Length;
                cost += Coordinate.GetDistance(start.Coordinate, route.Start.Coordinate);
                cost += Coordinate.GetDistance(end.Coordinate, route.End.Coordinate);
                routes.Add(cost, route);
            }

            if (routes.Count == 0)
            {
                return(null);
            }

            return(routes.First().Value);
        }
コード例 #11
0
        public void TestShortestPath()
        {
            List <StreetConnector> sc = new List <StreetConnector>();
            StreetConnector        c1 = new StreetConnector(0, 0);
            StreetConnector        c2 = new StreetConnector(0, 50);
            StreetConnector        c3 = new StreetConnector(0, 120);
            StreetConnector        c4 = new StreetConnector(20, 0);
            StreetConnector        c5 = new StreetConnector(20, 40);
            StreetConnector        c6 = new StreetConnector(20, 100);

            sc.Add(c1);
            sc.Add(c2);
            sc.Add(c3);
            sc.Add(c4);
            sc.Add(c5);
            sc.Add(c6);

            List <StreetSegment> ss = new List <StreetSegment>();
            StreetSegment        s1 = new StreetSegment(c1, c2);
            StreetSegment        s2 = new StreetSegment(c2, c3);
            StreetSegment        s3 = new StreetSegment(c1, c4);
            StreetSegment        s4 = new StreetSegment(c4, c5);
            StreetSegment        s5 = new StreetSegment(c5, c6);
            StreetSegment        s6 = new StreetSegment(c6, c3);

            ss.Add(s1);
            ss.Add(s2);
            ss.Add(s3);
            ss.Add(s4);
            ss.Add(s5);
            ss.Add(s6);

            BaseRoute b = new ShortestBaseRoute(c1, c6);

            Assert.AreEqual(2, b.Waypoints.Count);
            Assert.AreEqual(true, b.Waypoints[0] == c4);
            Assert.AreEqual(true, b.Waypoints[1] == c5);
        }
コード例 #12
0
        public void TestGetConnectingStreetSegments()
        {
            List <StreetConnector> sc1 = new List <StreetConnector>();
            StreetConnector        c1  = new StreetConnector(0, 0);
            StreetConnector        c2  = new StreetConnector(0, 50);
            StreetConnector        c3  = new StreetConnector(0, 120);

            sc1.Add(c1);
            sc1.Add(c2);
            sc1.Add(c3);

            List <StreetSegment> ss1 = new List <StreetSegment>();
            StreetSegment        s1  = new StreetSegment(c1, c2);
            StreetSegment        s2  = new StreetSegment(c2, c3);

            ss1.Add(s1);
            ss1.Add(s2);

            List <StreetSegment> sTest1 = ControlSystem.GetConnectingStreetSegments(sc1);

            Assert.AreEqual(2, sTest1.Count());
            Assert.AreEqual(true, sTest1.Contains(s1));
            Assert.AreEqual(true, sTest1.Contains(s2));
        }
コード例 #13
0
 /// <summary>
 /// Creates a new GoodsTransport.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 protected GoodsTransport(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
 }
コード例 #14
0
 /// <summary>
 /// Creates a new MaintenanceCar.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 public MaintenanceCar(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
 }
コード例 #15
0
 public Node(Node pre, StreetConnector con, StreetConnector end)
 {
     this.pre = pre;
     this.con = con;
     cost     = pre.cost + Coordinate.GetDistance(pre.con.Coordinate, con.Coordinate);
 }
コード例 #16
0
 public Node(StreetConnector con, StreetConnector end)
 {
     pre      = null;
     this.con = con;
     cost     = 0;
 }
コード例 #17
0
 public ShortestBaseRoute(StreetConnector start, StreetConnector end) : base(start, end)
 {
 }
コード例 #18
0
 /// <summary>
 /// Creates a new Bus.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 public Bus(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
 }
コード例 #19
0
 /// <summary>
 /// Returns a new SpecialRoute between the start StreetConnector and the end StreetConnector.
 /// </summary>
 /// <param name="start">The start StreetConnetor</param>
 /// <param name="end">The end StreetConnector</param>
 /// <returns>The new SpecialRoute</returns>
 public static SpecialRoute GetSpecialRoute(StreetConnector start, StreetConnector end)
 {
     return(new SpecialRoute(start, end));
 }
コード例 #20
0
        /// <summary>
        /// Follows the current route as long as the time suffice.
        /// </summary>
        public void ExecuteRouting()
        {
            bool executing = true;

            while (executing)
            {
                switch (currentRoutingState)
                {
                case RoutingState.Idle:
                case RoutingState.Starting: return;

                case RoutingState.SpecialStart1:
                    if (specialRoute.Waypoints.Count == 0)
                    {
                        currentRoutingState = RoutingState.SpecialEnd1;
                        continue;
                    }
                    nextConnector = specialRoute.Waypoints.First();
                    if (executing = AdvanceRoute())
                    {
                        currentRoutingState  = RoutingState.SpecialWayspoints1;
                        currentWaypointIndex = 1;
                    }
                    break;

                case RoutingState.SpecialWayspoints1:
                    if (specialRoute.Waypoints.Count == currentWaypointIndex)
                    {
                        currentRoutingState = RoutingState.SpecialEnd1;
                        continue;
                    }
                    nextConnector = specialRoute.Waypoints.ElementAt(currentWaypointIndex);
                    if (executing = AdvanceRoute())
                    {
                        currentWaypointIndex++;
                    }
                    break;

                case RoutingState.SpecialEnd1:
                    nextConnector = specialRoute.End;
                    if (executing = AdvanceRoute())
                    {
                        currentRoutingState = RoutingState.BaseStart;
                    }
                    break;

                case RoutingState.BaseStart:
                    if (baseRoute.Waypoints.Count == 0)
                    {
                        currentRoutingState = RoutingState.BaseEnd;
                        continue;
                    }
                    nextConnector = baseRoute.Waypoints.First();
                    if (executing = AdvanceRoute())
                    {
                        currentRoutingState  = RoutingState.BaseWaypoints;
                        currentWaypointIndex = 1;
                    }
                    break;

                case RoutingState.BaseWaypoints:
                    if (baseRoute.Waypoints.Count == currentWaypointIndex)
                    {
                        currentRoutingState = RoutingState.BaseEnd;
                        continue;
                    }
                    nextConnector = baseRoute.Waypoints.ElementAt(currentWaypointIndex);
                    if (executing = AdvanceRoute())
                    {
                        currentWaypointIndex++;
                    }
                    break;

                case RoutingState.BaseEnd:
                    nextConnector = baseRoute.End;
                    if (executing = AdvanceRoute())
                    {
                        if (currentConnector == goalConnector)
                        {
                            currentRoutingState = RoutingState.Finished;
                        }
                        else
                        {
                            specialRoute        = new SpecialRoute(currentConnector, goalConnector);
                            currentRoutingState = RoutingState.SpecialStart2;
                        }
                    }
                    break;

                case RoutingState.SpecialStart2:
                    if (specialRoute.Waypoints.Count == 0)
                    {
                        currentRoutingState = RoutingState.SpecialEnd2;
                        continue;
                    }
                    nextConnector = specialRoute.Waypoints.First();
                    if (executing = AdvanceRoute())
                    {
                        currentRoutingState  = RoutingState.SpecialWayspoints2;
                        currentWaypointIndex = 1;
                    }
                    break;

                case RoutingState.SpecialWayspoints2:
                    if (specialRoute.Waypoints.Count == currentWaypointIndex)
                    {
                        currentRoutingState = RoutingState.SpecialEnd2;
                        continue;
                    }
                    nextConnector = specialRoute.Waypoints.ElementAt(currentWaypointIndex);
                    if (executing = AdvanceRoute())
                    {
                        currentWaypointIndex++;
                    }
                    break;

                case RoutingState.SpecialEnd2:
                    nextConnector = specialRoute.End;
                    if (executing = AdvanceRoute())
                    {
                        currentRoutingState = RoutingState.Finished;
                    }
                    break;

                case RoutingState.Finished:
                    if (!isFirstClaim)
                    {
                        StreetType typeLast;
                        if (currentConnector.EP1.FindNeighbours().Contains(lastConnector))
                        {
                            typeLast = currentConnector.EP1.Self;
                        }
                        else
                        {
                            typeLast = currentConnector.EP2.Self;
                        }
                        FreeSpace(typeLast);
                    }
                    isFirstClaim = true;
                    return;

                default: throw new Exception("Unknown RoutingState");
                }
            }
        }
コード例 #21
0
 /// <summary>
 /// Creates a new Vehicle.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 protected Vehicle(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
 }
コード例 #22
0
ファイル: Adult.cs プロジェクト: k11719302/CityTrafficControl
 /// <summary>
 /// Creates a new Adult.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 public Adult(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
     isWorking = false;
     worktime  = TimeSpan.Zero;
 }
コード例 #23
0
 /// <summary>
 /// Creates a new Child.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 public Child(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
     isLearning = false;
     learntime  = TimeSpan.Zero;
 }
コード例 #24
0
 /// <summary>
 /// Creates a new Pedestrian.
 /// </summary>
 /// <param name="position">The initial position of this participant</param>
 /// <param name="maxSpeed">The max speed of this participant</param>
 /// <param name="accidentRisk">The risk that this participant causes an accident</param>
 /// <param name="size">The amount of space this participant uses up</param>
 protected Pedestrian(StreetConnector position, double maxSpeed, double accidentRisk, double size) : base(position, maxSpeed, accidentRisk, size)
 {
 }
コード例 #25
0
        protected bool AdvanceRoute()
        {
            StreetType typeCur, typeLast;
            TimeSpan   requiredTime;

            if (currentConnector.EP1.FindNeighbours().Contains(nextConnector))
            {
                typeCur  = currentConnector.EP1.Self;
                typeLast = currentConnector.EP2.Self;
            }
            else
            {
                typeCur  = currentConnector.EP2.Self;
                typeLast = currentConnector.EP1.Self;
            }

            if (typeCur is StreetSegment)
            {
                StreetSegment segment = (StreetSegment)typeCur;

                if (!claimedSpace)
                {
                    int direction = segment.EP1.Connector == currentConnector ? 1 : 2;
                    if (!segment.ClaimSpace(direction, size))
                    {
                        timeBonus = TimeSpan.Zero;
                        return(false);
                    }
                    if (isFirstClaim)
                    {
                        isFirstClaim = false;
                    }
                    else
                    {
                        FreeSpace(typeLast);
                    }
                    claimedSpace = true;
                }

                requiredTime = maxSpeed < segment.SpeedLimit ? TimeSpan.FromSeconds(segment.Length / maxSpeed) : segment.MinDriveTime;
                if (requiredTime.CompareTo(timeBonus) > 0)
                {
                    return(false);
                }

                timeBonus        = timeBonus.Subtract(requiredTime);
                lastConnector    = currentConnector;
                currentConnector = nextConnector;
                nextConnector    = null;
                claimedSpace     = false;
                ReportManager.PrintDebug(this + " advanced to " + currentConnector + " over " + segment + ".");
                return(true);
            }
            else if (typeCur is StreetHub)
            {
                StreetHub hub = (StreetHub)typeCur;
                bool      isGreen;

                hub.IsGreenList.TryGetValue(currentConnector.ID, out isGreen);
                if (!isGreen)
                {
                    timeBonus = TimeSpan.Zero;
                    return(false);
                }

                if (!claimedSpace)
                {
                    if (!hub.ClaimSpace(size))
                    {
                        timeBonus = TimeSpan.Zero;
                        return(false);
                    }
                    if (isFirstClaim)
                    {
                        isFirstClaim = false;
                    }
                    else
                    {
                        FreeSpace(typeLast);
                    }
                    claimedSpace = true;
                }

                double speed = maxSpeed < hub.SpeedLimit ? maxSpeed : hub.SpeedLimit;
                requiredTime = TimeSpan.FromSeconds(Coordinate.GetDistance(currentConnector.Coordinate, nextConnector.Coordinate) / speed);
                if (requiredTime.CompareTo(timeBonus) > 0)
                {
                    return(false);
                }

                timeBonus        = timeBonus.Subtract(requiredTime);
                lastConnector    = currentConnector;
                currentConnector = nextConnector;
                nextConnector    = null;
                claimedSpace     = false;
                ReportManager.PrintDebug(this + " advanced to " + currentConnector + " over " + hub + ".");
                return(true);
            }
            else
            {
                throw new StreetMapException("Unknown StreetType");
            }
        }