コード例 #1
0
        public static List <JamInstant> GetJams(this SlowToStop source)
        {
            var CarArray             = source.Cars;
            List <JamInstant> output = new List <JamInstant>();

            //create jams
            for (uint i = 0; i < CarArray.Length;)
            {
                uint start, length;
                for (; i < CarArray.Length && CarArray[i].velocity > 0; i++)
                {
                    ;
                }
                start = i;
                for (; i < CarArray.Length && CarArray[i].velocity == 0; i++)
                {
                    ;
                }
                length = i - start;
                output.Add(new JamInstant(start, length));
            }

            //Combine jam if loops around edge of road
            if (output.Count > 1)
            {
                if (output.Last().Last == CarArray.Length - 1 && output.First().First == 0)
                {
                    output[0].Prefix(output.Last());
                    output.RemoveAt(output.Count - 1);
                }
            }

            return(output);
        }
コード例 #2
0
        public static string GetHistogramOutput(this SlowToStop input)
        {
            var Cars = input.Cars;

            char[,] output = new char[input.MaxVelocity, input.NumberOfCars];
            for (var j = 0; j < input.NumberOfCars; j++)
            {
                var v = 0;
                for (; v < Cars[j].velocity; v++)
                {
                    output[v, j] = '*';
                }
                for (; v < input.MaxVelocity; v++)
                {
                    output[v, j] = ' ';
                }
            }
            StringBuilder sb = new StringBuilder();

            for (int i = output.GetLength(0) - 1; i >= 0; i--)
            {
                for (var j = 0; j < output.GetLength(1); j++)
                {
                    sb.Append(output[i, j]);
                }
                sb.Append('\n');
            }
            return(sb.ToString());
        }
コード例 #3
0
        public static uint GetThrouputMeasure(uint cars, uint roadLength, uint steps, uint throwAwaySteps, Func <uint, uint, uint[]> initializer, Func <IEnumerable <SlowToStop>, uint> measure)
        {
            SlowToStop road = new SlowToStop(initializer(roadLength, cars), roadLength);

            road.Step(throwAwaySteps);

            return(measure(road.Take((int)steps).Cast <SlowToStop>()));
        }
コード例 #4
0
 /// <summary>
 /// Copy constructor, makes complete clone
 /// </summary>
 /// <param name="other">Object to copy</param>
 public SlowToStop(SlowToStop other)
 {
     MaxVelocity      = other.MaxVelocity;
     cars             = (Car[])other.cars.Clone();
     RoadLength       = other.RoadLength;
     FaultProbability = other.FaultProbability;
     SlowProbability  = other.SlowProbability;
     Time             = other.Time;
 }
コード例 #5
0
        static void Main(string[] args)
        {
            const uint numb = 1000, steps = 10000, dump = 100, simul = 100000;
            Stopwatch  timer = new Stopwatch();

            timer.Start();
            Console.WriteLine(SlowToStop.OptimalDensity(numb, SlowToStop.StandardInitilizer, SlowToStop_Additions.PasspointMeasure, steps, dump, simul));
            timer.Stop();
            Console.WriteLine("Time 1: " + timer.Elapsed);
        }
コード例 #6
0
        public static uint TotalVelocityMeasure(this SlowToStop source)
        {
            uint sum = 0;

            foreach (var item in source.Cars)
            {
                sum += item.velocity;
            }
            return(sum);
        }
コード例 #7
0
        public static string GetMathematicaOutput(this SlowToStop input)
        {
            StringBuilder output = new StringBuilder("{");

            foreach (var item in input.Cars)
            {
                output.Append($"{{{item.position},{item.velocity}}},");
            }
            if (input.NumberOfCars != 0)
            {
                output.Length--;
            }
            output.Append('}');

            return(output.ToString());
        }
コード例 #8
0
 public static string GetPositionlesVelocities(this SlowToStop input)
 {
     if (input.MaxVelocity < 10)
     {
         var    Cars   = input.Cars;
         char[] output = new char[Cars.Length];
         for (int i = 0; i < Cars.Length; i++)
         {
             output[i] = (char)('0' + Cars[i].velocity);
         }
         return(new string(output));
     }
     else
     {
         throw new NotImplementedException("Does not support cars with greater then 10 velocity");
     }
 }
コード例 #9
0
 /// <summary>
 /// Get velocity printout of a single state as a string
 /// </summary>
 /// <param name="input">SlowToStop object to print</param>
 /// <returns>String form with velocity of each car in it's position</returns>
 public static string GetStandardOutput(this SlowToStop input)
 {
     if (input.MaxVelocity < 10)
     {
         char[] road = new char[input.RoadLength];
         for (int i = 0; i < road.Length; i++)
         {
             road[i] = '_';
         }
         foreach (var car in input.Cars)
         {
             road[car.position] = (char)('0' + car.velocity);
         }
         return(new string(road));
     }
     else
     {
         throw new NotImplementedException("Does not support cars with greater then 10 velocity");
     }
 }