Example #1
0
        private void TestPenaltyRule(BitMatrix input, PenaltyRules penaltyRule, int expected)
        {
            Penalty penalty = new PenaltyFactory().CreateByRule(penaltyRule);

            int result = penalty.PenaltyCalculate(input);

            AssertIntEquals(expected, result, input, penaltyRule);
        }
Example #2
0
 public PenaltyService(IActionHandler handler, IPenaltyRepository penaltyRepository, PenaltyDTOMapper penaltyMapper, PenaltyFactory penaltyFactory, ITelemetryEventService telemetry)
     : base(handler)
 {
     _penaltyRepository = penaltyRepository;
     _penaltyFactory    = penaltyFactory;
     _penaltyMapper     = penaltyMapper;
     _telemetry         = telemetry;
 }
 /// <summary>
 /// Penalties the score.
 /// </summary>
 /// <param name="matrix">The matrix.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 internal static int PenaltyScore(this BitMatrix matrix)
 {
     var penaltyFactory = new PenaltyFactory();
     return
         penaltyFactory
             .AllRules()
             .Sum(penalty => penalty.PenaltyCalculate(matrix));
 }
Example #4
0
        public void PerformanceTest(int rules, ByteMatrix bMatrix, BitMatrix bitMatrix)
        {
            Stopwatch sw          = new Stopwatch();
            int       timesofTest = 1000;

            Penalty penalty = new PenaltyFactory().CreateByRule((PenaltyRules)rules);


            string[] timeElapsed = new string[2];

            sw.Start();

            for (int i = 0; i < timesofTest; i++)
            {
                penalty.PenaltyCalculate(bitMatrix);
            }

            sw.Stop();

            timeElapsed[0] = sw.ElapsedMilliseconds.ToString();

            sw.Reset();

            sw.Start();

            for (int i = 0; i < timesofTest; i++)
            {
                switch (rules)
                {
                case 1:
                    MaskUtil.applyMaskPenaltyRule1(bMatrix);
                    break;

                case 2:
                    MaskUtil.applyMaskPenaltyRule2(bMatrix);
                    break;

                case 3:
                    MaskUtil.applyMaskPenaltyRule3(bMatrix);
                    break;

                case 4:
                    MaskUtil.applyMaskPenaltyRule4(bMatrix);
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Unsupport Rules {0}", rules.ToString()));
                }
            }
            sw.Stop();

            timeElapsed[1] = sw.ElapsedMilliseconds.ToString();


            Assert.Pass("Terminator performance {0} Tests~ QrCode.Net: {1} ZXing: {2}", timesofTest, timeElapsed[0], timeElapsed[1]);
        }
Example #5
0
        public static void Main(string[] args)
        {
            Logger.Info("Application Started");

            var categories =
                new CategoryCreator()
                    .SetRawPath ("Resources/eventexport.csv")
                    .SetOverrideFactory("Resources/events.json")
                    .Create();
            var athletes = new AthleteCreator().SetRawPath ("Resources/competitorexport.csv").SetOverrideFactory("Resources/athletes.json").Create ();

            var clubs = new ClubCreator(athletes).SetOverrideFactory("Resources/clubs.json").Create ();

            AthleteClubMapper.Map(athletes, clubs);

            var startpositions = new StartPositionFactory("Resources/startpositions.json").Create();
            var crews = new CrewCreator (categories, clubs, startpositions, athletes).SetRawPath ("Resources/crewexport.csv").SetOverrideFactory ("Resources/crews.json").Create ().OrderBy (cr => cr.StartNumber).ToList ();

            CategoryCrewMapper.Map(categories, crews);

            if(args.Count() == 0 || args[0].ToLowerInvariant() != "results")
            {
                StartPositionGenerator.Generate (crews);

                bool valid =
                    new CrewValidator().Validate (crews)
                    && new ClubValidator().Validate (clubs)
                    && new AthleteValidator().Validate (athletes)
                    && new CategoryValidator().Validate(categories);

                if (!valid)
                    return;
            }
            else
            {
                var starttimes = new SequenceItemFactory("Resources/start-times.json").Create();
                var finishtimes = new SequenceItemFactory("Resources/finish-times.json").Create();
                var penalties = new PenaltyFactory("Resources/penalties.json").Create();
                var adjustments = new AdjustmentFactory ("Resources/adjustments.json").Create ();
                // todo - is there a weighed in file?

                TimeMapper.Map (crews, starttimes, finishtimes);
                TimeMapper.Penalise (crews, penalties);
                TimeMapper.Adjust (crews, adjustments);

                CategoryResultsGenerator.Generate (categories);

                ResultsPrinter.Dump (crews);
            }

            Logger.Info ("Application stopped.");
        }
Example #6
0
        public static void Main(string[] args)
        {
            Logger.Info("Application Started");

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["csvpath"].ToString()))
            {
                CsvImporter.CsvPath = ConfigurationManager.AppSettings["csvpath"].ToString();
            }
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["jsonpath"].ToString()))
            {
                JsonOverrideFactory.JsonPath = ConfigurationManager.AppSettings["jsonpath"].ToString();
            }

            var categories =
                new CategoryCreator()
                .SetRawPath("Events.csv")
                .SetOverrideFactory("events.json")
                .Create();
            var athletes =
                new AthleteCreator()
                .SetRawPath("Competitors.csv")
                .SetOverrideFactory("athletes.json")
                .Create();

            var clubs = new ClubCreator(athletes).SetOverrideFactory("clubs.json").Create();

            AthleteClubMapper.Map(athletes, clubs);

            var startpositions = new StartPositionFactory("startpositions.json").Create();
            var crews          =
                new CrewCreator(categories, clubs, startpositions, athletes)
                .SetRawPath("Crews.csv")
                .SetOverrideFactory("crews.json")
                .Create()
                .OrderBy(cr => cr.StartNumber)
                .ToList();

            // todo - this is almost certainly not the best place to do this
            var bandProportions = ConfigurationManager.AppSettings["OpenBands"].Split(',').Select(i => Int32.Parse(i)).ToList();

            foreach (Gender gender in (Gender[])Enum.GetValues(typeof(Gender)))
            {
                var openBands = categories.Where(cat => cat is EventCategory).Select(c => (EventCategory)c).Where(c => c.Gender == gender && c.UseForCRI).Select(c => c.EventId).ToList();

                IList <Func <List <int> > > del = new List <Func <List <int> > >
                {
                    () => crews.Where(cr => cr.Gender == gender).Select(cr => cr.CRI(false)).ToList(),
                    () => crews.Where(crew => categories.Where(cat => cat is EventCategory).Select(c => (EventCategory)c).Where(c => c.Gender == gender && c.UseForCRI).Select(c => c.EventId).Contains(crew.EventCategory.EventId)).Select(cr => cr.CRI(false)).ToList()
                };

                foreach (var fn in del)
                {
                    var crisToInclude = fn(); // crews.Where(crew => openBands.Contains(crew.EventCategory.EventId)).Select(cr => cr.CRI).ToList();
                    crisToInclude.Sort();
                    if (crisToInclude.Count > 0)
                    {
                        Logger.DebugFormat("{0} crews [#{2}] {1}", gender, crisToInclude.Select(c => c.ToString()).Aggregate((h, t) => String.Format("{0}, {1}", h, t)), crisToInclude.Count);

                        int tally = 0;
                        int lower = 0;
                        for (int i = 0; i < bandProportions.Count; i++)
                        {
                            tally += bandProportions[i];
                            int upper = i + 1 == bandProportions.Count ? crisToInclude.Count - 1 : 1 + (int)Math.Floor((decimal)crisToInclude.Count * tally / 100);
                            Logger.DebugFormat("Band {0} [{1}, {2}{4}. #{3} ", i + 1, crisToInclude[lower], crisToInclude[upper], (i + 1 == bandProportions.Count ? 1 : 0) + upper - lower, i + 1 < bandProportions.Count ? ")" : "]"); // crisToInclude[mag-1], crisToInclude[mag]);
                            lower = upper;
                        }
                        foreach (var b in new List <bool> {
                            true, false
                        })
                        {
                            Logger.InfoFormat("Correlation between CRI{2} and points ({1}): {0}",
                                              PearsonCorrelation(crews.Where(cr => cr.Gender == gender).Select(cr => (double)cr.CRI(b)).ToList(), crews.Where(cr => cr.Gender == gender).Select(cr => (double)cr.Points).ToList()),
                                              gender,
                                              b ? "Max" : "");
                        }
                    }
                }
            }
            foreach (var b in new List <bool> {
                true, false
            })
            {
                Logger.InfoFormat("Correlation between CRI{1} and points (all): {0}",
                                  PearsonCorrelation(crews.Select(cr => (double)cr.CRI(b)).ToList(), crews.Select(cr => (double)cr.Points).ToList()),
                                  b ? "Max" : "");
            }

            CategoryCrewMapper.Map(categories, crews);

            // TODO: move results into the config file?
            if (args.Count() == 0 || args[0].ToLowerInvariant() != "results")
            {
                StartPositionGenerator.Generate(crews);



                bool valid =
                    new CrewValidator(athletes).Validate(crews) &&
                    new ClubValidator().Validate(clubs) &&
                    new AthleteValidator().Validate(athletes) &&
                    new CategoryValidator().Validate(categories);

                if (!valid)
                {
                    return;
                }
            }
            else
            {
                var starttimes  = new SequenceItemFactory("start-times.json").Create();
                var finishtimes = new SequenceItemFactory("finish-times.json").Create();
                var penalties   = new PenaltyFactory("penalties.json").Create();
                var adjustments = new AdjustmentFactory("adjustments.json").Create();
                // todo - is there a weighed in file?

                TimeMapper.Map(crews, starttimes, finishtimes);
                TimeMapper.Penalise(crews, penalties);
                TimeMapper.Adjust(crews, adjustments);

                CategoryResultsGenerator.Generate(categories);

                ResultsPrinter.Dump(crews);
            }

            Logger.Info("Application stopped.");
        }