//function for coordinating junction public void CoordinateJunctions(int ToD, int[] PreviousStage, Returner ReturnerVariables) { foreach (JunctionAgent JA in Junctions) { JA.MediateAuction(ToD, PreviousStage, ReturnerVariables); } Strat.ProcessZone(this, ToD, PreviousStage, ReturnerVariables); //WriteBidsDataBase(ToD); //WriteSITDataBase(ToD); }
//*Function for mediating the auction public void MediateAuction(int ToD, int[] PreviousStage, Returner ReturnerVariables) { AllPhases.Clear(); foreach (StageAgent SA in Stages) { SA.GenerateBid(ToD); foreach (double PhaseBid in SA.LanePhases) { AllPhases.Add(PhaseBid); } } Strat.ProcessJunction(this, PreviousStage, ReturnerVariables); }
//function public virtual List <int[]> EvaluationProcess(int ModelTimeofDay, int[] PreviousStage, Returner ReturnerVariables) { TimeSpan TS = new TimeSpan(0, 0, ModelTimeofDay / 100); string TimeOfDay = TS.ToString(); bool Stochastic = false; string[] Source = { "default" }; double[] Xsig = { 0 }; double[] Ysig = { 0 }; double[] SenPC = { 0 }; FindStoch(ref Stochastic, ref Source, ref Xsig, ref Ysig, ref SenPC); if (Stochastic == false) { NetDat.IVPextract(); } else { NetDat.IVPextractWiggle(Source, Xsig, Ysig, SenPC); } MainZone.CoordinateJunctions(ModelTimeofDay, PreviousStage, ReturnerVariables); WriteBidsDatabase(ModelTimeofDay); /*foreach (int item in MainZone.NextStages) * { * Console.WriteLine(item); * Console.Read(); * }*/ ReturnerVariables.CyclePlan = MainZone.NextStages; return(MainZone.NextStages); //AH - this is where the next stage is finalised }
public override void ProcessJunction(JunctionAgent JA, int[] PreviousStage, Returner ReturnerVariables) { //test that number of stages and agents match JA.AllRoadStates.Clear(); if (JA.Stages.Count != JA.NoOfStages) { throw (new Exception("The number of stages does not match the number of agents")); } for (int i = 0; i < JA.Stages.Count; i++) { JA.AllRoadStates.Add(JA.Stages[i].RoadStates); } int WinningStage = 1; //This is where AH adds his stage selector... AllStages AS = new AllStages(); List <List <int> > AllPossibleStages = new List <List <int> >(); //Number of Stages - Reads a file to generate stage list //AllPossibleStages = AS.ReturnAllCrossroadStages(); //AllPossibleStages = AS.StagesGenerator(@"OptimalCrossroad17Stages.txt"); //This is for the 17 stage option //AllPossibleStages = AS.StagesGenerator(@"OptimalCrossroad16Stages.txt"); //This is for the 16 stage option //AllPossibleStages = AS.StagesGenerator(@"OptimalCrossroad12Stages.txt"); //This is for the 12 stage option //AllPossibleStages = AS.StagesGenerator(@"OptimalCrossroad8Stages.txt"); //This is for the 8 stage option //Console.WriteLine(PreviousStage); //Four stage road state model [Stage1RoadState, State2RoadState...] //List<double[]> FourStageRoadStates = new List<double[]>(); //FourStageRoadStates = ReturnFourStageSolution(JA.AllRoadStates); //12 Phase road state model [Phase1RoadState, Phase2RoadState...] List <double[]> TwelvePhaseRoadStates = new List <double[]>(); TwelvePhaseRoadStates = Return12PhaseRoadState(JA.AllRoadStates); List <int[]> CyclePlan = new List <int[]>(); CyclePlan.Clear(); double NumberOfVehicles = ReturnNumberOfVehicles(TwelvePhaseRoadStates); /*if (NumberOfVehicles >= FixedVariables.MaxNumberOfVehiclesAtJunction) //This is a way of using a fixed time cycle if the number of cars exceeds a certain value.... * { * Console.WriteLine("There are this many cars..." + NumberOfVehicles); * CyclePlan = FixedTimePlan(PreviousStage[0]); * Console.WriteLine("Stage " + CyclePlan[0][0] + " called for " + CyclePlan[0][1] + "seconds"); * }*/ if (NumberOfVehicles <= FixedVariables.MinNumberOfVehiclesAtJunction) { FixedVariables.MaximumGreenTime = FixedVariables.MaxGreenTimeLowFlow; } else { FixedVariables.MaximumGreenTime = FixedVariables.MaxGreenTimeNormalFlow; } if (false) { } else { FixedVariables FV = new FixedVariables(); RunnerCyclePlan RunCyclePlan = new RunnerCyclePlan(); //4 Stage Model //CyclePlan = RunCyclePlan.RunAlgorithm(FV.StartingSeeds, FV.StepsClimbed, FV.MutationsAroundAPoint, TwelvePhaseRoadStates, RunCyclePlan.PopulatePhasesFourStageModel()); //8 Stage Model //CyclePlan = RunCyclePlan.RunAlgorithm(FV.StartingSeeds, FV.StepsClimbed, FV.MutationsAroundAPoint, TwelvePhaseRoadStates, RunCyclePlan.PopulatePhasesEightStageModel()); //17 Stage Model //CyclePlan = RunCyclePlan.RunAlgorithm(FV.StartingSeeds, FV.StepsClimbed, FV.MutationsAroundAPoint, TwelvePhaseRoadStates, RunCyclePlan.PopulatePhasesSeventeenStageModel()); //RunnerSingleStage RunSS = new RunnerSingleStage(); RunnerAllSingleStageOptions RunAllSS = new RunnerAllSingleStageOptions(); //8 Stage - Single Stage Selection Model //CyclePlan = RunSS.RunAlgorithm(FV.StartingSeeds, FV.StepsClimbed, FV.MutationsAroundAPoint, TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesEightStageModel()); //CyclePlan = RunAllSS.RunAlgorithm(TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesEightStageModel()); //NB. In 'RunAllSS' there is a function which would not need to be used in 4 stage model - UpdateDischargeRate8Stage //4 Stage - Single Stage Selection Model - All Combinations checked CyclePlan = RunAllSS.RunAlgorithm(TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesFourStageModel(), ReturnerVariables.TimeSinceReleased); UpdateTimeSinceReleased(CyclePlan, RunCyclePlan.PopulatePhasesFourStageModel(), ReturnerVariables); //NB. In 'RunAllSS' there is a function which would not need to be used in 4 stage model - UpdateDischargeRate8Stage //8 Stage - Single Stage Selection Model - All Combinations checked if demand //CyclePlan = RunAllSS.RunAlgorithm(TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesEightStageModel(), ReturnerVariables.TimeSinceReleased); //UpdateTimeSinceReleased(CyclePlan, RunCyclePlan.PopulatePhasesEightStageModel(), ReturnerVariables); //17 Stage - Single Stage Selection Model - All Combinations checked //CyclePlan = RunAllSS.RunAlgorithm(TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesSeventeenStageModel()); //2 Stage - Single Stage Selection Model - All Combinations checked //CyclePlan = RunAllSS.RunAlgorithm(TwelvePhaseRoadStates, PreviousStage[0], RunCyclePlan.PopulatePhasesTwoStageModel()); } List <int[]> FinalAnswer = new List <int[]>(); //This is to remove the (99,5) Intergreen Phase FinalAnswer.Clear(); FinalAnswer = ReturnCyclePlan(CyclePlan); JA.NextStage = FinalAnswer; //This all works for the highbid model... /*int LastStage = 2; * LastStage = PreviousStage[0]; * * //Stage Choice - Select the 'logic' for choosing the best stage * //Console.WriteLine(LastStage); * //WinningStage = AS.WithinXPercentStage(AllPossibleStages, JA.AllPhases, LastStage, 15.0); //This is for the within "X" percent bid * * WinningStage = AS.SimpleHighestStage(AllPossibleStages, JA.AllPhases); //This is for the basic highest bid * * List<int[]> Answer = new List<int[]>(); * int[] SingleStage = new int[2]; * int StageLength = 10; * SingleStage[0] = WinningStage; * SingleStage[1] = StageLength; * Answer.Add(SingleStage); * * JA.NextStage = Answer; * * /*int Stage = 1; * double BidHolder = 0; * foreach (StageAgent SA in JA.Stages) * { * Bid B = SA.StageBid; * if (B.Scalar > BidHolder) //This is Simon's original stage selection process * { * WinningStage = Stage; * BidHolder = B.Scalar; * } * Stage++; * }*/ //JA.NextStage = WinningStage;*/ }
public override void ProcessZone(ZoneAgent ZA, int ToD, int[] PreviousStage, Returner ReturnerVariables) { base.ProcessZone(ZA, ToD, PreviousStage, ReturnerVariables); }
private void UpdateTimeSinceReleased(List <int[]> CyclePlan, List <int[]> PhaseCombinations, Returner ReturnerVariables) { foreach (int[] StageAndTime in CyclePlan) { if (StageAndTime[0] != FixedVariables.IntergreenStageNumber) { int[] ActivePhases = PhaseCombinations[StageAndTime[0] - 1]; for (int i = 1; i < 13; i++) { if (ActivePhases.Contains(i)) //This checks to see if the phase is active or not { ReturnerVariables.TimeSinceReleased[i - 1] = 0; } else { ReturnerVariables.TimeSinceReleased[i - 1] += StageAndTime[1]; } } } else { for (int i = 0; i < 12; i++) { ReturnerVariables.TimeSinceReleased[i] += StageAndTime[1]; //This adds the intergreen time to the Time since released } } } }
public override void ProcessZone(ZoneAgent ZA, int ToD, int[] PreviousStage, Returner ReturnerVariables) { GreedyNextStages(ZA); }
public abstract void ProcessZone(ZoneAgent ZA, int ToD, int[] PreviousStage, Returner ReturnerVariables);
public abstract void ProcessJunction(JunctionAgent JA, int[] PreviousStage, Returner ReturnerVariables);
public void SynchRun(int Duration) { Returner ReturnerVariables = new Returner(); int SynchDelay = 1000; //This is in centi-seconds - ie. 10 second delay is 1000. //Connector.PCont.SetModelSyncTrapRate(ref SynchDelay); //AH moved this to within the 'do' loop to ensure we can vary the time Connector.PCont.OnSynch += new __PController_OnSynchEventHandler(PCont_OnSynch); ExitSim = false; /*int[] LastStageList = new int[Processor.MainZone.Junctions.Count]; * for (int i = 0; i < LastStageList.Length; i++) * { * LastStageList[i] = 1; * }*/ //int iterations = 0; int iterations = 10; int[] PreviousStage = new int[1]; PreviousStage[0] = 0; //AH copied this bit of code so that the first timestep would still work! Connector.PCont.SetModelSyncTrapRate(ref SynchDelay); SynchDone = false; Connector.PCont.SimRunAndLogAndPause(); while (SynchDone == false) { ; } Thread.Sleep(1000);//TODO This is to give paramics time to save the snapfile should make this explicit in the future FixedVariables FV = new FixedVariables(); int IntergreenTime = FV.IntergreenTimeVariable; do { List <int[]> Jstages = Processor.EvaluationProcess(ModelTimeofDay, PreviousStage, ReturnerVariables); //AH this is where the next stage is determined for each junction foreach (int[] StageAndLength in Jstages) { Processor.SigSet.SetStages(StageAndLength[0], 0); //Junction 0 is the junction number for the single junction PreviousStage[0] = StageAndLength[0]; //This is so that when the calculations are made, the current stage is known ReturnerVariables.PreviousStage[0] = StageAndLength[0]; SynchDelay = (StageAndLength[1] + IntergreenTime) * 100; //This is how long the stage will run for multiplied by 100 to express it in centi-seconds Connector.PCont.SetModelSyncTrapRate(ref SynchDelay); SynchDone = false; Connector.PCont.SimRunAndLogAndPause(); while (SynchDone == false) { ; } //Thread.Sleep(1000);//TODO This is to give paramics time to save the snapfile should make this explicit in the future //Trial to make any simulation time work... iterations += (SynchDelay / 100); //NB. SynchDelay is recorded in Centi-seconds //NB. That I have commented out the iterations++ below for this to work } //This function needs to be editted to be able to use a cycle plan rather than a single stage number... //int[] Jstages = Processor.EvaluationProcess(ModelTimeofDay, PreviousStage); //AH this is where the next stage is determined for each junction /*for (int i = 0; i < Jstages.Length; i++) * { * /*if (LastStageList[i] != Jstages[i]) * { * LastStageList[i] = Jstages[i];*/ /*Processor.SigSet.SetStages(Jstages[i], i); * PreviousStage = Jstages; * //} * }*/ if (iterations >= Duration) { ExitSim = true; } //iterations++; } while (ExitSim == false); //Connector.PCont.Disconnect(); Connector.PCont.Kill(); }