override public void TramArrive(Tram tram)
        {
            if (firstOpenPos() == -1)
            {
                Console.WriteLine("Attempted to TramArrive at full endpoint. Crashing");
                Console.ReadLine(); Sim.EmergencyExit();;
            }
            else
            {
                tram.moveToNext();
                tram.busy      = false;
                tram.hasloaded = false;
                Previous.bumpTrams();
                tram.arrivaltime = Time.Now();
                tram.tramActivitylog.WriteLine(Time.Now() + " , Arrival , " + Name);

                logInterDepartureTime.WriteLine((Time.Now()).ToString() + " , " + (Time.Now() - lastDeparture).ToString());

                if (!TramRetire(tram))
                {
                    if (!tramloading)
                    {
                        new StartPassTransfer(Time.Now(), tram, this);
                        tramloading = true;
                    }
                }
            }
        }
/*
 *      Passenger Embarkation/debarkation methods
 *
 */

        override public void StartPassExchange(Tram tram)
        {
            int pin; int pout;

            tram.busy = true;

            transferPassNew       = 0;
            transferTimeLast      = lastpassupdate;
            transferPassWaitStart = PassWaiting;


            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , StartPassExchange , " + Name);

            int passnew = updatePassWaiting();

            pout         = PassDepart(tram.PassCurr);
            pin          = Math.Min(tram.PassMax - (tram.PassCurr - pout), PassWaiting);
            PassWaiting -= pin;

            transferPassNew += passnew;

            int extime = TimePassExchange(pin, pout);

            new EndPassTransfer(Time.Now() + extime, tram, this, pin, pout, true);
        }
        override public void ItPassEmbarking(Tram tram)
        {
            int pnew = updatePassWaiting();

            transferPassNew += pnew;

            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , ItPassExchange , " + Name);

            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("iterative passenger embarkation");
            }

            int pin = Math.Min(tram.PassMax - tram.PassCurr, PassWaiting);

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

            if ((!tram.cameFromDepot) && (Time.Now() - tram.arrivaltime < Dweltime))
            {
                new PassEmbarkWait(tram.arrivaltime + Dweltime, tram, pin, this);
            }
            else if (NextDeparture() > Time.Now() + 5)
            {
                new PassEmbarkWait(NextDeparture() - 5, tram, pin, this);
            }
            else
            {
                new EndPassTransfer((Time.Now() + deltat), tram, this, pin, 0, false);
            }
        }
Exemplo n.º 4
0
 public RecruitTram(int time, Tram tram, iHasRails place)
 {
     Priority = 0;
     ETime    = time; Subject = tram; Place = place;
     Name     = "RecruitTram";
     base.addToList();
 }
        public void Recruit()
        {
            int i = 0;

            while (i < depot.Length)
            {
                if (depot[i] != null)
                {
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Recruited tram.");
                    }

                    Tram tram = depot[i];
                    tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , Recruited , " + Name);
                    AddTram(tram);
                    depot[i]           = null;
                    tram.indepot       = false;
                    tram.cameFromDepot = true;
                    tram.arrivaltime   = Time.Now();
                    tramloading        = true;
                    new StartPassTransfer(Time.Now(), tram, this);
                    return;
                }
                i++;
            }
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("No Tram to recruit.");
            }
        }
 override public bool FirstinLine(Tram tram)
 {
     if (Spaces[0] == tram)
     {
         return(true);
     }
     return(false);
 }
        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);
        }
Exemplo n.º 8
0
 public StartPassTransfer(int time, Tram tram, iHasRails place)
 {
     EventList.checkForTram(tram);
     Priority  = 2;
     tram.busy = true;
     ETime     = time; Subject = tram; Place = place;
     Name      = "StartPassTransfer";
     base.addToList();
 }
/*
 *      Passenger embarkation/debarkation methods
 */

        override public void StartPassExchange(Tram tram)
        {
            tramloading = true;
            tram.busy   = true;
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("starting passenger exchange");
            }
            base.StartPassExchange(tram);
        }
 public void RemoveTram(Tram tram){
     int pos = findTram(tram);
     if (pos != -1){
         Spaces[pos] = null;
     }
     else {
         Console.WriteLine("Tram " + tram.TramNo.ToString() + " not found at " + this.Name + ". Quiting simulation.");
         Console.ReadLine(); Console.ReadLine(); Sim.EmergencyExit();;;
     }
 }
 protected int findTram(Tram tram)
 {
     for (int i = 0; i < Spaces.Length; i++)
     {
         if (Spaces[i] == tram)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemplo n.º 12
0
 //There shouldn't be two events for the same tram in the event queue
 public static void checkForTram(Tram tram)
 {
     foreach (Event e in list)
     {
         if (e.Subject == tram)
         {
             Console.WriteLine("Tram already has event in the list");
             Console.ReadLine(); Sim.EmergencyExit();
         }
     }
 }
Exemplo n.º 13
0
        public EndPassTransfer(int time, Tram tram, iHasRails place, int In, int Out, bool cont)
        {
            EventList.checkForTram(tram);

            Priority = 4;
            PassIn   = In; PassOut = Out;
            ETime    = time; Subject = tram; Place = place;
            Cont     = cont;
            Name     = "EndPassTransfer";
            base.addToList();
        }
 public bool AddTram (Tram tram) {
     int pos = firstOpenPos();
     if (pos != -1) {
         Spaces[pos] = tram;
         return true;
     }
     
     Console.WriteLine("Failure to add tram to station " + Name + ". Ending simulation" );
     Console.ReadLine(); Sim.EmergencyExit();;
     return false; 
 }
        public void RemoveTram(Tram tram)
        {
            int pos = findTram(tram);

            if (pos != -1)
            {
                Spaces[pos] = null;
            }
            else
            {
                Console.WriteLine("Tram " + tram.TramNo.ToString() + " not found at " + this.Name + ". Quiting simulation.");
                Console.ReadLine(); Console.ReadLine(); Sim.EmergencyExit();;;
            }
        }
        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);
        }
        public bool AddTram(Tram tram)
        {
            int pos = firstOpenPos();

            if (pos != -1)
            {
                Spaces[pos] = tram;
                return(true);
            }

            Console.WriteLine("Failure to add tram to station " + Name + ". Ending simulation");
            Console.ReadLine(); Sim.EmergencyExit();;
            return(false);
        }
Exemplo n.º 18
0
 public EndTransit(int time, Tram tram, iHasRails place)
 {
     EventList.checkForTram(tram);
     Priority  = 5;
     ETime     = time; Subject = tram; Place = place;
     tram.busy = true;
     if (!place.Name.Trim().Equals(tram.Position.Next.Name.Trim()))
     {
         Console.WriteLine("AT EndTransit: place and subject.position.next do not match. \"" + Place.Name + "\" vs \"" + Subject.Position.Next.Name + "\"");
         Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     Name = "Endtransit";
     base.addToList();
 }
        public Station(string name, TimeDistr inrate, TimeDistr outrate, TimeDistr indens, int pass,
                       double driveavg, double drivevar, string datafolder, Simulation sim)
        {
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Initialising station " + name);
                if (Simulation.WAIT)
                {
                    Console.ReadLine();
                }
            }
            Name           = name;
            Spaces         = new Tram[1];
            lastpassupdate = 0;
            PassWaiting    = 0;
            lastDeparture  = 0;
            TotalIn        = 0;
            TotalOut       = 0;
            DriveAvg       = driveavg;
            DriveVar       = drivevar;
            Sim            = sim;


            //reformulate distributions for incoming passengers to something sensible
            int[]    times      = indens.getTimes();
            double[] passengers = new double[times.Length];
            for (int i = 0; i < passengers.Length; i++)
            {
//                if (Simulation.EXPLICIT) Console.WriteLine("Calculating passengers at time " + times[i].ToString());
                passengers[i] = Convert.ToDouble(pass) * inrate.getRate(times[i]) * indens.getRate(times[i]);
//                if (Simulation.EXPLICIT) Console.WriteLine("Passengers from time " + times[i].ToString() + " : " + passengers[i].ToString());
            }
            PassArrival = new TimeDistr(times, passengers);

            PassArrival.WriteToFile(datafolder + Name + "PassDistr.csv");

            PassOut = outrate;

            logPassengersWaiting = new StreamWriter(datafolder + Name + "PassWaiting.csv");
            logPassengersWaiting.WriteLine("Time , Passengers waiting, passengers in tram");
            logInterDepartureTime = new StreamWriter(datafolder + Name + "IntDepTime.csv");
            logInterDepartureTime.WriteLine("Time , time since last Departure");
            logTramsWaiting = new StreamWriter(datafolder + Name + "TramsWaiting.csv");
            logTramsWaiting.WriteLine("Time , trams waiting , trams at station");
            logWaitingtime = new StreamWriter(datafolder + Name + "WaitingTime.csv");
            logWaitingtime.WriteLine("Time , passengers already waiting , time last departure , new passengers , time since last passenger update , passengers waiting after loading");
            logDwelltime = new StreamWriter(datafolder + name + "Dwelltime.csv");
            logDwelltime.WriteLine("Time , dwelltime of departing tram");
        }
Exemplo n.º 20
0
 public PassEmbarkWait(int time, Tram tram, int pin, iHasRails place)
 {
     EventList.checkForTram(tram);
     Priority     = 3;
     ETime        = time; Subject = tram; Place = place;
     Subject.busy = true;
     Pin          = pin;
     Name         = "PassEmbarkWait";
     if (!Place.Name.Trim().Equals(Subject.Position.Name.Trim()))
     {
         Console.WriteLine("At creation of passembarkwait: place and subject.position do not match. " + Place.Name + " vs " + Subject.Position.Name);
         Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     base.addToList();
 }
        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);
     }
 }
Exemplo n.º 23
0
        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();
        }
Exemplo n.º 24
0
        /*
         *  Instantiates the trams and places them in the Endstation containing the depot. The latter is currently hardcoded to be the first entry in railway
         */
        private void buildTrams()
        {
            if (EXPLICIT)
            {
                Console.WriteLine("Initialising trams");
                if (WAIT)
                {
                    Console.ReadLine();
                }
            }
            trams = new Tram[NoTrams];

            for (int i = 0; i < NoTrams; i++)
            {
                trams[i] = new Tram(i, (Endstation)railway[0], MaxPassTram, DataFolder, this);
            }
        }
Exemplo n.º 25
0
        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();
        }
        //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());
            }
        }
/*
 *  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);
        }
 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());
        }
    	override public bool FirstinLine(Tram tram){
	    	if (Spaces[0] == tram){
		    	return true;
		    }
		    return false;
	    }
    	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 ) ;
	    }
        public Endstation(string name, bool depot, int maxtrams, TimeDistr inrate, TimeDistr outrate,
                          TimeDistr indens, int pass, double driveavg, double drivevar, TimeDistr schedule,
                          int timefromstrt, string datafolder, Simulation sim)
            : base(name, inrate, outrate, indens, pass, driveavg, drivevar, datafolder, sim)
        {
            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Initialising Endstation " + name);
                if (Simulation.WAIT)
                {
                    Console.ReadLine();
                }
            }
            Spaces   = new Tram[2];
            raillock = new bool[2];
            if (depot)
            {
                this.depot = new Tram[maxtrams];
            }
            Name     = name;
            Dweltime = 4 * 60;  //Parameterize this.
            //counter = 0;

            logPunctuality = new StreamWriter(datafolder + Name + "Punctuality.csv");
            logPunctuality.WriteLine("Time , Deviation from schedule");

            //create schedule
            int[]    periods   = schedule.getTimes();
            double[] intensity = schedule.getRates();

            if (Simulation.EXPLICIT)
            {
                Console.WriteLine("Creating timetable at Endstation " + name);
                if (Simulation.WAIT)
                {
                    Console.ReadLine();
                }
            }
            Queue <int> temptable = new Queue <int>();

            for (int i = 0; i < periods.Length; i++)
            {
                if ((i + 1 == periods.Length + timefromstrt) || (intensity[i] == 0.0))
                {
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Scheduled leaving of tram at " + (periods[i] + timefromstrt).ToString());
                    }
                    temptable.Enqueue(periods[i] + timefromstrt);
                    continue;
                }
                int next = periods[i] + timefromstrt;

                int intdeptime = Convert.ToInt32(3600.0 / intensity[i]);
                while (next < periods[i + 1] + timefromstrt)
                {
                    if (Simulation.EXPLICIT)
                    {
                        Console.WriteLine("Scheduled event leaving at " + next.ToString());
                    }

                    temptable.Enqueue(next);
                    next += intdeptime;
                }
            }

            timetable = temptable.ToArray();
            StreamWriter logTimeSchedule = new StreamWriter(datafolder + Name + "schedule.txt");

            foreach (int i in timetable)
            {
                logTimeSchedule.WriteLine(i.ToString());
            }

            usedtimes = new bool[timetable.Length];
            for (int i = 0; i < usedtimes.Length; i++)
            {
                usedtimes[i] = false;
            }
            logTimeSchedule.Close();
        }
    	override public void TramArrive (Tram tram) { 
	    	if (firstOpenPos() == -1){
		    	Console.WriteLine("Attempted to TramArrive at full endpoint. Crashing");
			    Console.ReadLine(); Sim.EmergencyExit();;
    		}
	    	else {
		    	tram.moveToNext();
                tram.busy = false;
                tram.hasloaded = false;
                Previous.bumpTrams();
                tram.arrivaltime = Time.Now();
                tram.tramActivitylog.WriteLine(Time.Now() + " , Arrival , " + Name);

                logInterDepartureTime.WriteLine((Time.Now()).ToString() + " , " + (Time.Now() - lastDeparture).ToString());

			    if (!TramRetire(tram)) {
				    if (!tramloading) {
                        new StartPassTransfer(Time.Now(), tram, this);
                        tramloading = true;
                    }
			    }
		    }
	    }
/*
        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;
    	}
Exemplo n.º 37
0
        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();
        }
    	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(); 
            }
    	}
/*
        Passenger Embarkation/debarkation methods

*/
 
        override public void StartPassExchange(Tram tram) {
            int pin; int pout;
            tram.busy = true;

            transferPassNew = 0;
            transferTimeLast = lastpassupdate;
            transferPassWaitStart = PassWaiting;


            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , StartPassExchange , " + Name);

            int passnew = updatePassWaiting();
            pout = PassDepart(tram.PassCurr);
            pin = Math.Min(tram.PassMax - (tram.PassCurr - pout), PassWaiting);
            PassWaiting -= pin;

            transferPassNew += passnew;

            int extime = TimePassExchange(pin, pout);
            new EndPassTransfer(Time.Now() + extime, tram, this, pin, pout, true);
        }
Exemplo n.º 40
0
        public PassEmbarkWait(int time, Tram tram, int pin, iHasRails place){
            EventList.checkForTram(tram);
            Priority = 3;
			ETime = time; Subject = tram; Place= place;
            Subject.busy = true;
            Pin = pin;
            Name = "PassEmbarkWait";
            if ( !Place.Name.Trim().Equals( Subject.Position.Name.Trim())) {
                Console.WriteLine("At creation of passembarkwait: place and subject.position do not match. " +Place.Name + " vs " + Subject.Position.Name);
                Console.ReadLine(); EventList.Sim.EmergencyExit();
            }
			base.addToList();		
		}
	    override public void ItPassEmbarking(Tram tram) { 
		    int pnew = updatePassWaiting();
            transferPassNew += pnew;

            tram.tramActivitylog.WriteLine(Time.Now().ToString() + " , ItPassExchange , " + Name);

            if(Simulation.EXPLICIT) Console.WriteLine("iterative passenger embarkation");

		    int pin = Math.Min(tram.PassMax - tram.PassCurr, PassWaiting);
            PassWaiting -= pin;
            int deltat = (int)RNG.Gamma3(2, 0.2 * (double)pin, 0.0); 

            if (( !tram.cameFromDepot ) && (Time.Now() - tram.arrivaltime < Dweltime) ) {

                new PassEmbarkWait ( tram.arrivaltime + Dweltime, tram,  pin, this);
            }
            else if (NextDeparture() > Time.Now() + 5){
                new PassEmbarkWait ( NextDeparture() - 5 , tram, pin , this);
            }
            else {
                 new EndPassTransfer ( (Time.Now() + deltat), tram, this, pin, 0, false);
            }
        }
Exemplo n.º 42
0
        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();
        }
Exemplo n.º 43
0
 public StartPassTransfer(int time, Tram tram, iHasRails place) {
     EventList.checkForTram(tram);
     Priority = 2;
     tram.busy = true; 
     ETime = time; Subject = tram; Place = place;
     Name = "StartPassTransfer";
     base.addToList();            
 }
        //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 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);    
            }
        }
/*
            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);
            }
        }
 public InTransit (int maxTrams, Simulation sim, string name) {
     Spaces = new Tram[maxTrams];
     Name = "intransitafter" + name;
     Sim = sim;
 }
/*
    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());

	    }
 //Handles the actual departing of trams from the station. Then checks if a tram can be put into the station in its place.
 virtual public void TramDepart (Tram tram) {
     Console.WriteLine("Attempted to call TramDepart from nonstation object. Crashing.");
     Console.ReadLine(); Console.ReadLine(); Sim.EmergencyExit();;;
 }
        public Endstation (string name, bool depot, int maxtrams, TimeDistr inrate, TimeDistr outrate, 
                           TimeDistr indens, int pass, double driveavg, double drivevar, TimeDistr schedule,
                           int timefromstrt, string datafolder, Simulation sim ) 
                           : base(name,inrate,outrate,indens, pass, driveavg, drivevar, datafolder, sim) {
            if (Simulation.EXPLICIT) {
                Console.WriteLine("Initialising Endstation " + name);
                if (Simulation.WAIT) Console.ReadLine();
            } 
            Spaces = new Tram[2];
            raillock = new bool[2];
            if (depot) this.depot = new Tram[maxtrams];
            Name = name;
            Dweltime = 4* 60;  //Parameterize this.
            //counter = 0;

            logPunctuality = new StreamWriter(datafolder + Name + "Punctuality.csv");
            logPunctuality.WriteLine("Time , Deviation from schedule");

            //create schedule
            int[] periods = schedule.getTimes();
            double[] intensity = schedule.getRates();

            if (Simulation.EXPLICIT) {
                Console.WriteLine("Creating timetable at Endstation " + name);
                if (Simulation.WAIT) Console.ReadLine();
            } 
            Queue<int> temptable = new Queue<int>();

            for (int i = 0; i < periods.Length ; i++) {
                if ( (i+1 == periods.Length + timefromstrt) || (intensity[i] == 0.0) ){
                    if (Simulation.EXPLICIT) Console.WriteLine("Scheduled leaving of tram at " + (periods[i]+ timefromstrt).ToString() );
                    temptable.Enqueue(periods[i] + timefromstrt);
                    continue;
                }
                int next = periods[i] + timefromstrt;
                
                int intdeptime = Convert.ToInt32( 3600.0/intensity[i]);
                while (next < periods[i+1] + timefromstrt) {
                    if (Simulation.EXPLICIT) Console.WriteLine("Scheduled event leaving at " + next.ToString() );

                    temptable.Enqueue(next);
                    next += intdeptime;
                }
            }

            timetable = temptable.ToArray();
            StreamWriter logTimeSchedule = new StreamWriter(datafolder + Name + "schedule.txt");
            
            foreach (int i in timetable) {
                logTimeSchedule.WriteLine(i.ToString());
            }

            usedtimes = new bool[timetable.Length];
            for (int i = 0; i < usedtimes.Length; i++) {
                usedtimes[i] = false;
            }
            logTimeSchedule.Close();

        }
Exemplo n.º 51
0
        /*
            Instantiates the trams and places them in the Endstation containing the depot. The latter is currently hardcoded to be the first entry in railway
        */              
        private void buildTrams() {
            if (EXPLICIT) {
                Console.WriteLine("Initialising trams");
                if (WAIT) Console.ReadLine();
            }            trams = new Tram[NoTrams];

            for (int i = 0; i < NoTrams; i++) {
                trams[i] = new Tram(i, (Endstation)railway[0], MaxPassTram, DataFolder, this);
            }

        }
/*
        Tram departure methods

*/

        //Handles everything needed for departure. 
        override public void handleDepart(Tram tram, int slot) { 
            tram.busy = true;
            TramDepart(tram);
        }
Exemplo n.º 53
0
 public EndTransit(int time, Tram tram, iHasRails place) {
     EventList.checkForTram(tram);
     Priority = 5;
     ETime = time; Subject = tram; Place = place;
     tram.busy = true;
     if ( !place.Name.Trim().Equals( tram.Position.Next.Name.Trim())) {
         Console.WriteLine("AT EndTransit: place and subject.position.next do not match. \"" + Place.Name + "\" vs \"" + Subject.Position.Next.Name + "\"");
          Console.ReadLine(); EventList.Sim.EmergencyExit();
     }
     Name = "Endtransit";
     base.addToList();
 }
/*
        Passenger embarkation/debarkation methods
*/

	    override public void StartPassExchange(Tram tram) { 
	        tramloading = true; 
            tram.busy = true;
            if(Simulation.EXPLICIT) Console.WriteLine("starting passenger exchange");
            base.StartPassExchange(tram);
            
	    }
Exemplo n.º 55
0
        public EndPassTransfer(int time, Tram tram, iHasRails place, int In, int Out, bool cont) {
            EventList.checkForTram(tram);

            Priority = 4;
            PassIn = In; PassOut = Out;
            ETime = time; Subject = tram; Place = place;
            Cont = cont;
            Name = "EndPassTransfer";
            base.addToList();
        }
 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);
 }
Exemplo n.º 57
0
		public RecruitTram (int time, Tram tram, iHasRails place){
            Priority = 0;
			ETime = time; Subject = tram; Place= place;
            Name = "RecruitTram";
			base.addToList();		
		}
 protected int findTram(Tram tram) {
     for (int i = 0; i < Spaces.Length; i++) {
         if (Spaces[i] == tram) return i;
     }
     return -1;
 }
Exemplo n.º 59
0
 //There shouldn't be two events for the same tram in the event queue
 public static void checkForTram(Tram tram) {
     foreach (Event e in list) {
         if (e.Subject == tram) {
             Console.WriteLine("Tram already has event in the list");
             Console.ReadLine(); Sim.EmergencyExit();
         }
     }
 }
	    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); 
            }  
	    }