override public void TramDepart(Tram tram)
        {
            lastDeparture = Time.Now();

            tram.moveToNext();
            logPassengersWaiting.WriteLine(Time.Now().ToString() + " , " + PassWaiting.ToString() + " , " + tram.PassCurr.ToString());
            logDwelltime.WriteLine(Time.Now().ToString() + " , " + (Time.Now() - tram.arrivaltime).ToString());
            new EndTransit(Time.Now() + this.TimeTransit(), tram, Next.Next);
        }
Exemple #2
0
        public void changePass(int pin, int pout)
        {
            PassCurr += pin - pout;

/*            if (PassCurr < 0 || pin < 0 || pout < 0) {
 *              Console.WriteLine("Oh shit, we have anti-passengers!");
 *              Console.ReadLine(); Sim.EmergencyExit();
 *          }
 */
            TotIn  += pin;
            TotOut += pout;
            logPassengers.WriteLine(Time.Now().ToString() + " , " + Position.Name + " , " + PassCurr.ToString() + " , " + pin.ToString() + " , " + pout.ToString());
        }
        override public void ItPassEmbarking(Tram tram)
        {
            int pnew = updatePassWaiting();

            transferPassNew += pnew;
            tram.busy        = true;
            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , ItPassExchange , " + Name);
            int pin = Math.Min(tram.PassMax - tram.PassCurr, PassWaiting);

            PassWaiting -= pin;
            int deltat = (int)RNG.Gamma3(2, 0.2 * pin, 0.0);

            new EndPassTransfer((Time.Now() + deltat), tram, this, pin, 0, false);
        }
        override public void handleDepart(Tram tram, int slot)
        {
            int pos = findTram(tram);

            if (pos == 0)       // tram in line with departing rail
            {
                if (!raillock[0])
                {
                    tram.busy = true;
                    int time = TimeLocked();
                    new TrackLock(Time.Now() + time, tram, this, 0, false);
                    logPunctuality.WriteLine(Time.Now().ToString() + " , " + (Time.Now() - timetable[slot]).ToString());
                    counter++;
                    //Console.WriteLine(counter.ToString());

                    base.handleDepart(tram, 0);
                }
                else
                {
                    usedtimes[slot] = false;
                    tram.busy       = false;
                }
            }
            else if (pos == 1)       // tram not in line with departing rail
            {
                if ((!raillock[0]) && (!raillock[1]))
                {
                    tram.busy = true;
                    int time = TimeLocked();
                    logPunctuality.WriteLine(Time.Now().ToString() + " , " + (Time.Now() - timetable[slot]).ToString());
                    counter++;
                    //Console.WriteLine(counter.ToString());
                    new TrackLock(Time.Now() + time, tram, this, 2, false);

                    base.handleDepart(tram, 0);
                }
                else
                {
                    usedtimes[slot] = false;
                    tram.busy       = false;
                }
            }
            else
            {
                Console.WriteLine("Event handleDepart at station " + Name + " at time " + Time.Now().ToString() + " attempted on tram not at the station.");
                Console.ReadLine(); Sim.EmergencyExit();
            }
        }
 override public void EndPassExchange(Tram tram, int pin, int pout, bool iterate)
 {
     tram.busy = false;
     tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , EndPassExchange , " + Name);
     tram.changePass(pin, pout);
     if (iterate)
     {
         ItPassEmbarking(tram);
     }
     else
     {
         logWaitingtime.WriteLine(Time.Now() + " , " + transferPassWaitStart.ToString() + " , " + (Time.Now() - lastDeparture).ToString()
                                  + " , " + transferPassNew + " , " + (Time.Now() - transferTimeLast).ToString() + "  , " + PassWaiting);
         new StartTransit(Time.Now(), tram, this, 0);
     }
 }
        override public void TramRecruit()
        {
            if (NextDeparture() == -1)
            {
                return;
            }

/*
 *          //old recruitment. Retired because it caused high delays at high intensity scheduels.
 *          if (Spaces[1] != null || Spaces[0] != null) return;
 *          else Recruit();
 *
 */
            //new recruitment
            if (Time.Now() + 30 > NextDeparture() && firstOpenPos() != -1)
            {
                Recruit();
            }
            else if ((Spaces[0] != null || Spaces[1] != null))            //At least one tram there
            {
                if (firstOpenPos() != -1)
                {
                    int pos = 0;
                    if (Spaces[1] != null)
                    {
                        pos = 1;
                    }

                    if (Spaces[pos].arrivaltime + 4 * 60 > NextDeparture())     // the tram that's there can't possibly depart on time
                    {
                        Recruit();
                    }
                }
                else   // two trams at station, no possibility to recruit.
                {
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Recruitment unncessary, tram already there.");
                    }
                    return;
                }
            }
            else                                        // recruit tram from depot
            {
                Recruit();
            }
        }
Exemple #7
0
        /*
         *  In addition to moving the tram: logs some data on tram activity.
         */
        private void moveTo(iHasRails place)
        {
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Moving tram " + TramNo.ToString() + " from " + Position.Name + " to " + place.Name);
            }

            if (!place.AddTram(this))
            {
                Console.WriteLine("Error trying to move Tram" + TramNo.ToString() + " to station " + place.Name);
                Console.ReadLine(); Sim.EmergencyExit();
            }
            Position.RemoveTram(this);
            logger.WriteLine(Time.Now().ToString() + " , " + Position.Name + " , " + place.Name);
            tramActivitylog.WriteLine(Time.Now().ToString() + " , Move , " + Position.Name + " , " + place.Name);
            Position = place;
        }
        //Handles actual arrival.
        override public void TramArrive(Tram tram)
        {
            if (this.firstOpenPos() == -1)
            {
                Console.WriteLine("Tried to move tram to station" + Name + " while there were no free spaces. Crashing");
                Console.ReadLine(); Sim.EmergencyExit();;
            }
            else
            {
                tram.moveToNext();
                tram.busy = false;
                Previous.bumpTrams();
                tram.arrivaltime = Time.Now();
                tram.tramActivitylog.WriteLine(Time.Now() + " , Arrival , " + Name);

                new StartPassTransfer(Time.Now(), tram, this);
                logInterDepartureTime.WriteLine((Time.Now()).ToString() + " , " + (Time.Now() - lastDeparture).ToString());
            }
        }
        override public void UnlockTrack(int track)
        {
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Releasing tracklocks at station " + Name);
            }
            if (track == 2)
            {
                raillock[0] = false;
                raillock[1] = false;
            }
            else
            {
                if (!raillock[track])
                {
                    Console.WriteLine("Attempt to unlock unlocked track. Crashing");
                    Console.ReadLine(); Sim.EmergencyExit();;
                }
                raillock[track] = false;
            }

            //Check if tram can enter/leave station.

            if (ReadyTram() != null && !raillock[0])
            {
                if (Simulation.EXPLICIT)
                {
                    Console.WriteLine("Planning departure of tram" + ReadyTram().TramNo.ToString() + " from station " + Name + " after lock has been released");
                }
                //Console.ReadLine();
                planDeparture(ReadyTram());
            }
            if (Previous.ReadyTram() != null && !raillock[1])
            {
                if (Simulation.EXPLICIT)
                {
                    Console.WriteLine("Planning arrival of tram after lock has been released");
                }
                //Console.ReadLine();
                new EndTransit(Time.Now(), Previous.ReadyTram(), this);
            }
        }
/*
 *  Arrival methods
 *
 */
        override public void handleArrival(Tram tram)
        {
            int spot = firstOpenPos();

            tram.tramActivitylog.WriteLine(Time.Now() + " , Arrival , " + Previous.Name);

            tram.busy = false;
            if (!Previous.FirstinLine(tram))
            {
                return;
            }
            if (spot == 0)
            {
                if ((!raillock[0]) && (!raillock[1]))
                {
                    int time = TimeLocked();
                    new TrackLock(Time.Now() + time, tram, this, 2, true);
                    TramArrive(tram);
                }
            }
            else if (spot == 1)
            {
                if (!raillock[1])
                {
                    int time = TimeLocked();
                    new TrackLock(Time.Now() + time, tram, this, 1, true);
                    TramArrive(tram);
                }
            }


            int trams = 0;

            foreach (Tram place in Spaces)
            {
                if (place != null)
                {
                    trams++;
                }
            }
            logTramsWaiting.WriteLine(Time.Now().ToString() + " , " + Previous.TramWaitingCount().ToString() + " , " + trams.ToString());
        }
/*
 *          Departure methods
 */


        //due to the scheduling at Termini which doesn't exist at regular stations, it's require to plan departures in advance.
        public void planDeparture(Tram tram)
        {
            int next = NextDeparture();

            if (next == -1)
            {
                TramRetire(tram);
                return;
            }

            int slot = ReserveSlot();



            if (next < Time.Now())
            {
                tram.busy = true;
                new StartTransit(Time.Now(), tram, this, slot);
            }
            else
            {
                tram.busy = true;
                new StartTransit(next, tram, this, slot);
            }

            if (tramloading && ReadyToLoad() != null)
            {
                Console.WriteLine("At planDeparture: tramloading == true, but also tram ready to load.");
                Console.ReadLine(); Sim.EmergencyExit();
            }
            if (ReadyToLoad() != null)
            {
                if (ReadyToLoad().TramNo == tram.TramNo)
                {
                    Console.WriteLine("Trying to start passenger loading departing tram");
                    Console.ReadLine(); Sim.EmergencyExit();
                }
                tramloading = true;
                new StartPassTransfer(Time.Now(), ReadyToLoad(), this);
            }
        }
/*
 *      Tram Recruitment/retiring
 */
        private bool TramRetire(Tram tram)
        {
            if (depot != null)            //wrong endstation
            {
                if (((Time.Now() >= RetireTime) && (firstOpenPos() == -1)) || NextDeparture() == -1)
                {
                    RemoveTram(tram);
                    int i = 0;
                    while (i < depot.Length)
                    {
                        if (depot[i] == null)
                        {
                            EventList.checkForTram(tram);
                            int pout = PassDepart(tram.PassCurr);
                            TotalOut += pout;
                            tram.changePass(0, pout);
                            if (Simulation.EXPLICIT)
                            {
                                Console.WriteLine("Retired tram to depot");
                            }
                            depot[i] = tram;


                            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , Retired , " + Name);
                            tram.indepot = true;
                            return(true);
                        }
                        i++;
                    }
                }

/*          This was an idea, but not really necessary, since it ends after the even this belongs to is done anyhow.
 *          bool full = true;
 *          for (int i = 0; i < depot.Length; i++) {
 *              if (depot[i] == null) { full = false; break; }
 *          }
 *          if (full) //Find some way to end simulation from here...
 */
            }
            return(false);
        }
/*
 *  All RNG-based functions
 *
 */

        override protected int updatePassWaiting()
        {
            //get intervals from the PassArival which fall within lastupdate to Now, from there calculate the passengers waiting.

            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Updating waiting passengers");
            }
            int[] times = PassArrival.getIntervals(lastpassupdate, Time.Now());

            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Calculating passengers at station " + Name + "over interval " + Time.Now().ToString() + " from " + lastpassupdate.ToString());
            }

            int newpass = 0;

            for (int i = 0; i < times.Length - 1; i++)
            {
                int time = times[i + 1] - times[i];
                if (Simulation.EXPLICIT)
                {
                    Console.WriteLine("Calculatign for subinterval " + times[i].ToString() + " to " + times[i + 1].ToString() + ". Passengers per second: " + PassArrival.getRate(times[i]).ToString());
                }
                newpass += Math.Max(0, RNG.Poisson(PassArrival.getRate(times[i]) * Convert.ToDouble(time)));
                //newpass += Convert.ToInt32(PassArrival.getRate(Time.Now()) * Convert.ToDouble (Time.Now() - lastpassupdate)); //HOTFIX: TODO: FIX
            }


//            Console.WriteLine("passengers per second: " + PassArrival.getRate(Time.Now()).ToString() + " time since update: " + (Time.Now() - lastpassupdate).ToString() );
//            newpass = Convert.ToInt32(PassArrival.getRate(Time.Now()) * Convert.ToDouble (Time.Now() - lastpassupdate)); //hotfic, ugle as f**k.
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("New passengers: " + newpass.ToString());
            }
            lastpassupdate = Time.Now();
            PassWaiting   += newpass;
            TotalIn       += newpass;
            return(newpass);
        }
 override public void EndPassExchange(Tram tram, int pin, int pout, bool iterate)
 {
     tram.changePass(pin, pout);
     tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , EndPassExchange , " + Name);
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("ending passenger exchange");
     }
     if (iterate)
     {
         ItPassEmbarking(tram);
     }
     else
     {
         tramloading        = false;
         tram.busy          = true;
         tram.cameFromDepot = false;
         tram.hasloaded     = true;
         logWaitingtime.WriteLine(Time.Now() + " , " + transferPassWaitStart.ToString() + " , " + (Time.Now() - lastDeparture).ToString()
                                  + " , " + transferPassNew + " , " + (Time.Now() - transferTimeLast).ToString() + "  , " + PassWaiting);
         planDeparture(tram);
     }
 }
/*
 *      Tram arrival methods
 */

        //Executes on endTransit event. Checks if tram is allow to enter the station
        override public void handleArrival(Tram tram)
        {
            tram.busy = false;
            tram.tramActivitylog.WriteLine(Time.Now() + " , Arrival , " + Previous.Name);

            if (firstOpenPos() != -1)
            {
                if (Previous.FirstinLine(tram))
                {
                    TramArrive(tram);
                }
            }

            int trams = 0;

            foreach (Tram place in Spaces)
            {
                if (place != null)
                {
                    trams++;
                }
            }
            logTramsWaiting.WriteLine(Time.Now().ToString() + " , " + Previous.TramWaitingCount().ToString() + " , " + trams.ToString());
        }
 public override void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Waiting further passenger embarkation of tram " + Subject.TramNo.ToString() + " at station " + Place.Name + " at time " + Time.Now().ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     if (!Place.Name.Trim().Equals(Subject.Position.Name.Trim()))
     {
         Console.WriteLine("At Execution of passembarkwait: place and subject.position do not match. " + Place.Name + " vs " + Subject.Position.Name);
         Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     Place.EndPassExchange(Subject, Pin, 0, true);
 }
 public override void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Recruiting tram at station " + Place.Name + " at time " + Time.Now().ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     Place.TramRecruit();
 }
 public override void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Ending tracklock at station " + Place.Name + " at time " + Time.Now().ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     Place.UnlockTrack(Track);
 }
        public TrackLock(int time, Tram tram, iHasRails place, int track, bool incoming)
        {
            Priority = 6;
            ETime    = time; Place = place; Track = track; Incoming = incoming;
            Place.LockTrack(track);
            Name = "TrackLock";
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Starting tracklock of  at station " + Place.Name + " at time " + Time.Now().ToString());
                if (Simulation.WAIT)
                {
                    Console.ReadLine();
                }
            }

            base.addToList();
        }
 public override void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Ending passenger transfer of tram " + Subject.TramNo.ToString() + " at station " + Place.Name + " at time " + Time.Now().ToString());
         Console.WriteLine(" In : " + PassIn.ToString() + " out : " + PassOut.ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     if (!Place.Name.Trim().Equals(Subject.Position.Name.Trim()))
     {
         Console.WriteLine("At Execution of endpasstransfer: place and subject.position do not match. " + Place.Name + " vs " + Subject.Position.Name);
         Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     Place.EndPassExchange(Subject, PassIn, PassOut, Cont);
 }
 override public void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Ending of tram " + Subject.TramNo.ToString() + " at station " + Place.Name + " at time " + Time.Now().ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     Place.handleArrival(Subject); //handle crossover locking for endstations here. For other stations: just start loading passengers and shit.
 }
        public static void addEvent(Event happening)
        {
            foreach (Event oud in list)
            {
                if ((happening.Subject != null) && oud.Subject != null && (happening.Name == oud.Name) && (happening.Subject.TramNo == oud.Subject.TramNo))
                {
                    Console.WriteLine("Duplicate event " + happening.Name + " detected at station " + happening.Place.Name + " for tram " + happening.Subject.TramNo.ToString());
                    Console.ReadLine(); Sim.EmergencyExit();
                }
                if ((happening.Subject != null) && oud.Subject != null && (happening.Subject.TramNo == oud.Subject.TramNo))
                {
                    Console.WriteLine("Dual event found for tram" + happening.Subject.TramNo.ToString() + ": " + happening.Name + " at " + happening.Place.Name + "  and " + oud.Name + " at " + oud.Place.Name);
                    Console.ReadLine(); Sim.EmergencyExit();
                }
            }


            int time = happening.ETime;

            for (int i = 0; i < list.Count(); i++)
            {
                if (time < list[i].ETime)
                {
                    list.Insert(i, happening);
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Inserted event " + happening.Name + " at station " + happening.Place.Name + " for time " + happening.ETime.ToString() + " at time " + Time.Now().ToString());
                    }
                    return;
                }
                else if (time == list[i].ETime && happening.Priority > list[i].Priority)
                {
                    list.Insert(i, happening);
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Inserted event " + happening.Name + " at station " + happening.Place.Name + " for time " + happening.ETime.ToString() + " at time " + Time.Now().ToString());
                    }
                    return;
                }
            }
            list.Add(happening);
        }
 override public void execEvent()
 {
     if (Simulation.EXPLICIT)
     {
         Console.WriteLine("Starting Transit of tram " + Subject.TramNo.ToString() + " at station " + Place.Name + "planned for" + ETime.ToString() + " at time " + Time.Now().ToString());
         if (Simulation.WAIT)
         {
             Console.ReadLine();
         }
     }
     if (!Place.Name.Trim().Equals(Subject.Position.Name.Trim()))
     {
         Console.WriteLine("At Execution of starttransit: place and subject.position do not match. " + Place.Name + " vs " + Subject.Position.Name);
         Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     Place.handleDepart(Subject, number);
 }
        public StartTransit(int time, Tram tram, iHasRails place, int timenumber)
        {
            EventList.checkForTram(tram);
            Priority     = 5;
            ETime        = time; Subject = tram; Place = place; number = timenumber;
            Subject.busy = true;
            if (Place != tram.Position)
            {
                Console.WriteLine("At StartTransit event: place and subject.Position did not match. " + Place.Name + " vs " + Subject.Position.Name);
                Console.ReadLine(); EventList.Sim.EmergencyExit();
            }

            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Planned Starttransit of tram" + Subject.TramNo.ToString() + " at station " + Place.Name + " at time " + Time.Now().ToString());
            }
            Name = "StartTransit";
            base.addToList();
        }