private StarterInfo CreateStarterInfo(Horse horse, Dictionary<Horse, StarterInfo> map)
        {
            if (horse.Scratched)
            {
                return null;
            }

            var dummyStarterInfo = new StarterInfo();

            dummyStarterInfo.RacingDate = horse.Parent.Parent.Date;
            dummyStarterInfo.Distance = horse.Parent.CorrespondingBrisRace.DistanceInYards;

            StarterInfo currentStarterInfo = dummyStarterInfo;

            foreach (var pp in GetPastPerformancesWithValidGoldenFigures(horse))
            {
                StarterInfo si = StarterInfo.CreateFromBrisPastPerformance(pp);
                currentStarterInfo.Previous = si;
                si.Next = currentStarterInfo;
                currentStarterInfo = si;
            }

            if (dummyStarterInfo.CountPreceding < 3)
                return null;
            map.Add(horse, dummyStarterInfo);
            return dummyStarterInfo;
        }
        public double GetOpinion(StarterInfo si)
        {
            if (si.CountPreceding < _numberOfPastPerformances)
                return 0;

            var v1 = StarterInfo.NormalizedInputForMemoryLearning(si, _numberOfPastPerformances);
            _patterns.ForEach(p=>p.CalculateEuclidianDistances(v1));
            _patterns.Sort(CompareEuclidianDistances);

            double positive = 0, negative =0;
            int desiredOutcomeIndex = _patterns[0].X.Count - 1;
            for(int i = 0; i < 2; ++i)
            {
                if(_patterns[i].X[desiredOutcomeIndex] == 1)
                {
                    ++positive;
                }
                else
                {
                    ++negative;
                }
            }

            return positive >0 ? 1.0 : 0.0;
        }
        internal static List<double> NormalizedInputForMemoryLearning(StarterInfo si, int ppCount)
        {
            var input = new List<double>();

            if (si.CountPreceding >= ppCount)
            {
                si.GetPreviousPerformances(ppCount).ToList().ForEach(si2 => input.Add(si2.GoldenFigure));

                double min = input.Min();
                double max = input.Max();

                for (int i = 0; i < input.Count; ++i)
                {
                    input[i] = (input[i] - min) / max;
                }

                PopulateNormalizedDataForMemoryLearning(si, input, ppCount);
                input.Add(NormalizeDaysOff(si));
                input.Add(NormalizePostPosition(si));
                input.Add(NormalizeDistance(si));
                input.Add(NormalizeBoolean(si.RestrictedToFemalesOnly));
                input.Add(NormalizeBoolean(si.RestrictedToStateBredsOnly));
                input.Add(NormalizeBoolean(si.WasClaimed));
                input.Add(NormalizeBoolean(si.BlinkersOff));
                input.Add(NormalizeBoolean(si.FirstTimeLasix));
                input.Add(NormalizeBoolean(si.RestrictedToTwoYearsOnly));
                input.Add(NormalizeBoolean(si.RestrictedToThreeYearsOnly));
                double desiredValue = si.GoldenFigure > max ? 1.0 : 0.0;
                input.Add(desiredValue);
            }

            return input;
        }
        public static bool CheckIfStarterWillImprove(StarterInfo si)
        {
            if(null == _singleton)
            {
                _singleton = new MemoryBasedNeuronNetwork(4);
                _singleton.AddTrainingPatterns(StarterInfo.LoadStartersFromFile(@"C:\Users\John\Desktop\neural_network.csv", 4));
            }

            return _singleton.GetOpinion(si) > 0;
        }
 internal static List<double> NormalizedInputForImprovementDetection(StarterInfo si, int ppCount)
 {
     var input = new List<double>();
     var gf = new List<double>();
     si.GetPreviousPerformances(ppCount).ToList().ForEach(si2 => gf.Add(si2.GoldenFigure));
     double min = gf.Min();
     double max = gf.Max();
     PopulateNormalizedData(si, input, min, max, ppCount);
     input.Add(NormalizeDaysOff(si));
     input.Add(NormalizePostPosition(si));
     input.Add(NormalizeDistance(si));
     return input;
 }
Example #6
0
        internal static StarterInfo CreateFromBrisPastPerformance(BrisPastPerformance pp)
        {
            if (null == pp || pp.GoldenFigureForThisHorse == -999)
                return null;

            var si = new StarterInfo { Previous = null, Next = null };

            si.HorseName = pp.Parent.CorrespondingHorse.Name;
            si.RaceId = -9999;
            si.TrackCode = pp.Parent.CorrespondingHorse.Parent.Parent.TrackCode;
            si.RacingDate = Utilities.GetDateInYYYYMMDD(pp.Date);
            si.FinishPosition = Convert.ToInt32(pp.FinalPosition);
            si.Odds = pp.OddsAsDouble;
            si.GoldenFigure = (int)pp.GoldenFigureForThisHorse;

            if (si.GoldenFigure < 0)
            {
                si.GoldenFigure = 1;
            }

            si.WinnersGoldenFigure = (int)pp.GoldenFigureForTheWinner;

            si.TrackCondition = pp.TrackCondition;
            si.Distance = pp.DistanceInYards;

            int scp;
            int.TryParse(pp.SecondCallPosition, out scp);
            si.SecondCallPosition = scp;

            return si;
        }
        private StarterInfo[] GetPreviousPerformances(int count)
        {
            var array = new StarterInfo[count];

            var si = Previous;

            for (int i = 0; i < count; ++i)
            {
                array[i] = si;

                if (null != si)
                {
                    si = si.Previous;
                }
            }

            return array;
        }
 private static void PopulateNormalizedDataForMemoryLearning(StarterInfo starterInfo, List<double> input, int count)
 {
     starterInfo.GetPreviousPerformances(count).ToList().ForEach(
                     si =>
                     {
                         input.Add(NormalizeTrackCondition(si));
                         input.Add(NormalizeOdds(si));
                         input.Add(NormalizeDistance(si));
                         input.Add(NormalizePosition(si.FinishPosition));
                         input.Add(NormalizePosition(si.SecondCallPosition));
                         input.Add(NormalizeDaysOff(si));
                         input.Add(NormalizeRaceType(si));
                         input.Add(NormalizePostPosition(si));
                         input.Add(NormalizeBoolean(si.RestrictedToFemalesOnly));
                         input.Add(NormalizeBoolean(si.RestrictedToStateBredsOnly));
                         input.Add(NormalizeBoolean(si.WasClaimed));
                         input.Add(NormalizeBoolean(si.BlinkersOff));
                         input.Add(NormalizeBoolean(si.FirstTimeLasix));
                         input.Add(NormalizeBoolean(si.RestrictedToTwoYearsOnly));
                         input.Add(NormalizeBoolean(si.RestrictedToThreeYearsOnly));
                     });
 }
        internal static List<double> NormalizedInputToChooseBetweenFirstAndSecondFavorite(StarterInfo favorite, StarterInfo secondFavorite, StarterInfo thirdFavorite, int ppCount)
        {
            var input = new List<double>();
            var gf = new List<double>();
            favorite.GetPreviousPerformances(ppCount).ToList().ForEach(si => gf.Add(si.GoldenFigure));
               // favorite.GetPreviousPerformances(ppCount).ToList().ForEach(si => gf.Add(si.WinnersGoldenFigure));
            secondFavorite.GetPreviousPerformances(ppCount).ToList().ForEach(si => gf.Add(si.GoldenFigure));
            //secondFavorite.GetPreviousPerformances(ppCount).ToList().ForEach(si => gf.Add(si.WinnersGoldenFigure));

            thirdFavorite.GetPreviousPerformances(ppCount).ToList().ForEach(si => gf.Add(si.GoldenFigure));
            double min = gf.Min();
            double max = gf.Max();
            PopulateNormalizedData(favorite, input, min, max, gf.Average(),ppCount);
            PopulateNormalizedData(secondFavorite, input, min, max, gf.Average(), ppCount);
            PopulateNormalizedData(thirdFavorite, input, min, max, gf.Average(), ppCount);

            input.Add(NormalizeDaysOff(favorite));
            input.Add(NormalizeDaysOff(secondFavorite));
            input.Add(NormalizeDaysOff(thirdFavorite));

            //input.Add(NormalizeOdds(favorite));
            //input.Add(NormalizeOdds(secondFavorite));
            //input.Add(NormalizeOdds(thirdFavorite));

            //input.Add(NormalizePostPosition(favorite));
            //input.Add(NormalizePostPosition(secondFavorite));
            //input.Add(NormalizePostPosition(thirdFavorite));
            //input.Add(NormalizeDistance(favorite));
            return input;
        }
 private static double NormalizeTrackCondition(StarterInfo si)
 {
     return si.TrackCondition.Trim().ToUpper() == "FT" ? 0.3 : 0.7;
 }
 static double NormalizeRaceType(StarterInfo si)
 {
     switch(si.RaceType)
     {
         case RaceType.GRADE_1:
         case RaceType.GRADE_2:
         case RaceType.GRADE_3:
         case RaceType.STAKES:
             return 0.9;
         case RaceType.ALW:
             return 0.67;
         case RaceType.MSW:
             return 0.52;
         case RaceType.CLM:
             return 0.17;
         case RaceType.MCL:
             return 0.02;
         default:
             return 0.02;
     }
 }
 static double NormalizePostPosition(StarterInfo si)
 {
     if(si.PostPosition == 1)
     {
         return 0.8;
     }
     else if (si.PostPosition == 2)
     {
         return 0.7;
     }
     else if (si.PostPosition >= 10)
     {
         return 0.1;
     }
     else if (si.PostPosition >= 8)
     {
         return 0.2;
     }
     else
     {
         return 0.3;
     }
 }
        private static double NormalizeOdds(StarterInfo si)
        {
            if (si.WasTheFavorite)
                return 0.9;

            if (si.Odds <= 3.0)
            {
                return 0.6;
            }
            else if (si.Odds <= 5.0)
            {
                return 0.2;
            }
            else
            {
                return 0.02;
            }
        }
 private static double NormalizeDistance(StarterInfo si)
 {
     return si.Distance < 1780 ? 0.3 : 0.7;
 }
        private static double NormalizeDaysOff(StarterInfo si)
        {
            int layoff = si.LayoffInDays;

            if (layoff == 0)
                return 0.05;
            else if (layoff < 10)
                return 0.8;
            else if (layoff < 30)
                return 0.5;
            else if (layoff < 80)
                return 0.25;
            else if (layoff < 120)
                return 0.15;
            else
                return 0.10;
        }
Example #16
0
        private static StarterInfo CreateFromString(string[] s, int stringArrayIndex, StarterInfo previous)
        {
            if (stringArrayIndex >= s.Count())
                return null;

            var si = new StarterInfo {Previous = previous, Next = null};

            si.HorseName = (null == previous) ? s[stringArrayIndex++] : previous.HorseName ;
            si.RaceId = Convert.ToInt32(s[stringArrayIndex++]);
            si.TrackCode = s[stringArrayIndex++];
            si.RacingDate = s[stringArrayIndex++];
            si.FinishPosition = Convert.ToInt32(s[stringArrayIndex++]);
            si.Odds = Convert.ToDouble(s[stringArrayIndex++]);
            si.GoldenFigure = Convert.ToInt32(s[stringArrayIndex++]);

            if (si.GoldenFigure < 0)
            {
                si.GoldenFigure = 1;
            }

            si.WinnersGoldenFigure = Convert.ToInt32(s[stringArrayIndex++]);

            si.TrackCondition = s[stringArrayIndex++];
            si.Distance = Convert.ToInt32(s[stringArrayIndex++]);
            si.SecondCallPosition = Convert.ToInt32(s[stringArrayIndex++]);
            si.RaceType = DeserializeRaceType(s[stringArrayIndex++]);
            si.PostPosition = Convert.ToInt32(s[stringArrayIndex++]);
            DeserializeRestrictions(si, s[stringArrayIndex++]);
            si.FieldSize = Convert.ToInt32(s[stringArrayIndex++]);
            si.WasTheFavorite = Convert.ToInt32(s[stringArrayIndex++]) == 1;

            var next = CreateFromString(s, stringArrayIndex, si);
            si.Next = next;

            return si;
        }
Example #17
0
 private static void DeserializeRestrictions(StarterInfo si, string s)
 {
     s = s.Trim();
     if (s.Length == 7)
     {
         si.RestrictedToFemalesOnly = (s[0] == '1');
         si.RestrictedToStateBredsOnly = (s[1] == '1');
         si.WasClaimed = (s[2] == '1');
         si.BlinkersOff = (s[3] == '1');
         si.FirstTimeLasix = (s[4] == '1');
         si.RestrictedToTwoYearsOnly = (s[5] == '1');
         si.RestrictedToThreeYearsOnly = (s[6] == '1');
     }
 }
        private static void PopulateNormalizedData(StarterInfo starterInfo, List<double> normalizedData, double min, double max, double average, int count)
        {
            starterInfo.GetPreviousPerformances(count).ToList().ForEach(
                si =>
                {
                    normalizedData.Add(NormalizeGoldenFigure(si.GoldenFigure, min, max, average));
                   // normalizedData.Add(NormalizeGoldenFigure(si.WinnersGoldenFigure, min, max, average));
                    normalizedData.Add(NormalizeTrackCondition(si));
                   // normalizedData.Add(NormalizeOdds(si));
                    normalizedData.Add(NormalizeDistance(si));
                    //normalizedData.Add(NormalizePosition(si.FinishPosition));

                    /*
                    normalizedData.Add(NormalizePosition(si.SecondCallPosition));
                    normalizedData.Add(NormalizeDaysOff(si));
                    normalizedData.Add(NormalizeRaceType(si));
                    normalizedData.Add(NormalizePostPosition(si));

                   normalizedData.Add(NormalizeBoolean(si.RestrictedToFemalesOnly));
                   normalizedData.Add(NormalizeBoolean(si.RestrictedToStateBredsOnly));
                   normalizedData.Add(NormalizeBoolean(si.WasClaimed));
                   normalizedData.Add(NormalizeBoolean(si.BlinkersOff));
                   normalizedData.Add(NormalizeBoolean(si.FirstTimeLasix));
                   normalizedData.Add(NormalizeBoolean(si.RestrictedToTwoYearsOnly));
                   normalizedData.Add(NormalizeBoolean(si.RestrictedToThreeYearsOnly));
                    */
                });
        }