EuroJackpot with EnCog ANN and Stats

EuroJackpotYes, I know that it is impossible to predict lottery results. But if the results would be predictable? Of course I know they are not, but just hypothetically for a play with Perceptron Neural Network and prediction algorithms I want to show you that in 256 ;-) lines of code you are able to play with prediction thanks to EnCog 3.4 library. I bought recently 2 Jeff Heaton’s  books about neural networks and C# and his library. That inspired me to play with prediction, forecast, trends, classifications, regression and more… I do not want to bring you too much details and I wan to share that 256 lines of code. To make it working in Visual Studio you have to just download by NuGet encog-dotnet-core 3.4 library and include it to console application with that code, then last thing to prepare is recent results of euro jackpot lottery let’s said from last year and safe that as the TXT file. But please remember, predicted values are correct in math but it is impossible to predict truly random lottery results which is Power Ball, but who can stop me from trying ;-). There is one more thing, having good results can take some time and good deep parameter, too high takes too long, too low cannot find results, so play carefully. Here you have portion of the program output.

Try 01: (16 22 27 34 41 + 4 8)
16:100% 22:100% 27:100% 34:100% 41:100%
4:100% 8:100%
Try 02: (12 16 17 22 27 29 34 39 41 44 + 4 6 7 8)
12:50% 16:50% 17:50% 22:50% 27:50% 29:50% 34:50% 39:50% 41:50% 44:50%
4:50% 6:50% 7:50% 8:50%
Try 03: (12 13 14 16 17 22 24 27 29 34 39 40 41 44 45 + 3 4 6 7 8)
12:33,33% 13:33,33% 14:33,33% 16:33,33% 17:33,33% 22:33,33% 24:33,33% 27:33,33% 29:33,33% 34:33,33% 39:33,33% 40:33,33% 41:33,33% 44:33,33% 45:33,33%
7:66,67% 3:33,33% 4:33,33% 6:33,33% 8:33,33%
Try 04: (8 12 13 14 16 17 18 22 24 27 29 32 34 37 39 40 41 44 45 + 7 8)
44:50% 8:25% 12:25% 13:25% 14:25% 16:25% 17:25% 18:25% 22:25% 24:25% 27:25% 29:25% 32:25% 34:25% 37:25% 39:25% 40:25% 41:25% 45:25%
7:50% 8:50% 3:25% 4:25% 6:25% 9:25%
Try 05: (8 10 12 13 14 15 16 17 18 22 24 27 29 32 34 35 37 39 40 41 44 45 + 7 8)
12:40% 39:40% 44:40% 8:20% 10:20% 13:20% 14:20% 15:20% 16:20% 17:20% 18:20% 22:20% 24:20% 27:20% 29:20% 32:20% 34:20% 35:20% 37:20% 40:20% 41:20% 45:20%
7:60% 8:40% 3:20% 4:20% 5:20% 6:20% 9:20%
Try 06: (8 10 12 13 14 15 16 17 18 21 22 24 27 29 32 34 35 37 39 40 41 44 45 46 47 48 + 7 8)
12:33,33% 15:33,33% 39:33,33% 44:33,33% 8:16,67% 10:16,67% 13:16,67% 14:16,67% 16:16,67% 17:16,67% 18:16,67% 21:16,67% 22:16,67% 24:16,67% 27:16,67% 29:16,67% 32:16,67% 34:16,67% 35:16,67% 37:16,67% 40:16,67% 41:16,67% 45:16,67% 46:16,67% 47:16,67% 48:16,67%
7:66,67% 8:50% 3:16,67% 4:16,67% 5:16,67% 6:16,67% 9:16,67%
Try 07: (12 15 16 39 44 + 7 8)
12:28,57% 15:28,57% 16:28,57% 39:28,57% 44:28,57% 8:14,29% 10:14,29% 11:14,29% 13:14,29% 14:14,29% 17:14,29% 18:14,29% 21:14,29% 22:14,29% 24:14,29% 27:14,29% 29:14,29% 30:14,29% 32:14,29% 34:14,29% 35:14,29% 37:14,29% 38:14,29% 40:14,29% 41:14,29% 43:14,29% 45:14,29% 46:14,29% 47:14,29% 48:14,29%
7:57,14% 8:42,86% 6:28,57% 9:28,57% 3:14,29% 4:14,29% 5:14,29%

As you can see “Try 07” founded exactly 7 numbers, but I think, you should experiment with the program source code and execution by yourself. Thanks for reading!

namespace EuroJackpotPredictor
{
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using Encog.Neural.Networks;
 using Encog.ML.Data.Basic;
 using Encog.Neural.Networks.Layers;
 using Encog.Engine.Network.Activation;
 using Encog.Neural.Networks.Training.Propagation.Resilient;
 class Result
 {
  public int V1 { get; private set; }
  public int V2 { get; private set; }
  public int V3 { get; private set; }
  public int V4 { get; private set; }
  public int V5 { get; private set; }
  public int V6 { get; private set; }
  public int V7 { get; private set; }
  public Result(
      int v1, int v2, int v3, int v4, int v5, int v6, int v7)
  {
   V1 = v1; V2 = v2;
   V3 = v3; V4 = v4;
   V5 = v5;
   V6 = v6; V7 = v7;
  }
  public Result(double[] values)
  {
   V1 = (int)Math.Round(values[0]);
   V2 = (int)Math.Round(values[1]);
   V3 = (int)Math.Round(values[2]);
   V4 = (int)Math.Round(values[3]);
   V5 = (int)Math.Round(values[4]);
   V6 = (int)Math.Round(values[5]);
   V7 = (int)Math.Round(values[6]);
  }
  public bool IsValid()
  {
   return
   V1 >= 1 && V1 <= 50 &&
   V2 >= 1 && V2 <= 50 &&
   V3 >= 1 && V3 <= 50 &&
   V4 >= 1 && V4 <= 50 &&
   V5 >= 1 && V5 <= 50 &&
   V6 >= 1 && V6 <= 10 &&
   V7 >= 1 && V7 <= 10 &&
   V1 != V2 &&
   V1 != V3 &&
   V1 != V4 &&
   V1 != V5 &&
   V2 != V3 &&
   V2 != V4 &&
   V2 != V5 &&
   V3 != V4 &&
   V3 != V5 &&
   V4 != V5 &&
   V6 != V7;
  }
  public bool IsOut()
  {
   return
   !(V1 >= 1 && V1 <= 50 &&
     V2 >= 1 && V2 <= 50 &&
     V3 >= 1 && V3 <= 50 &&
     V4 >= 1 && V4 <= 50 &&
     V5 >= 1 && V5 <= 50 &&
     V6 >= 1 && V6 <= 10 &&
     V7 >= 1 && V7 <= 10);
  }
  public override string ToString()
  {
   return string.Format(
   "{0},{1},{2},{3},{4},{5},{6}",
   V1, V2, V3, V4, V5, V6, V7);
  }
 }
 class ListResults : List<Result> { }
 class Program
 {
  static void Main(string[] args)
  {
   var count = 0;
   var countMax = args.Length == 1 ? int.Parse(args[0]) : 27;
   var fileDB = @"C:\Projects\PredictorSandbox\db\ejpdb.txt";
   try
   {
    ListResults dbl = null;
    if (CreateDatabases(fileDB, out dbl))
    {
     var deep = countMax;
     var network = new BasicNetwork();
     network.AddLayer(
     new BasicLayer(null, true, 7 * deep));
     network.AddLayer(
     new BasicLayer(
     new ActivationSigmoid(), true, 5 * 7 * deep));
     network.AddLayer(
     new BasicLayer(
     new ActivationSigmoid(), true, 5 * 7 * deep));
     network.AddLayer(
     new BasicLayer(new ActivationLinear(), true, 7));
     network.Structure.FinalizeStructure();
     var learningInput = new double[deep][];
     for (int i = 0; i < deep; ++i)
     {
      learningInput[i] = new double[deep * 7];
      for (int j = 0, k = 0; j < deep; ++j)
      {
       var idx = 2 * deep - i - j;
       var data = dbl[idx];
       learningInput[i][k++] = data.V1;
       learningInput[i][k++] = data.V2;
       learningInput[i][k++] = data.V3;
       learningInput[i][k++] = data.V4;
       learningInput[i][k++] = data.V5;
       learningInput[i][k++] = data.V6;
       learningInput[i][k++] = data.V7;
      }
     }
     var learningOutput = new double[deep][];
     for (int i = 0; i < deep; ++i)
     {
      var idx = deep - 1 - i;
      var data = dbl[idx];
      learningOutput[i] = new double[7]
      {
       data.V1,
       data.V2,
       data.V3,
       data.V4,
       data.V5,
       data.V6,
       data.V7,
      };
     }
     var trainingSet = new BasicMLDataSet(
     learningInput,
     learningOutput);
     var train = new ResilientPropagation(
     network, trainingSet);
     train.NumThreads = Environment.ProcessorCount;
    START:
     System.Threading.Thread.Sleep(new Random().Next(10, 100));
     network.Reset();
    RETRY:
     var step = 0;
     do
     {
      train.Iteration();
      ++step;
     }
     while (train.Error > (0.0001 * countMax) && step < countMax);
     var passedCount = 0;
     for (var i = 0; i < deep; ++i)
     {
      var should =
      new Result(learningOutput[i]);
      var inputn = new BasicMLData(7 * deep);
      Array.Copy(
      learningInput[i],
      inputn.Data,
      inputn.Data.Length);
      var comput =
      new Result(
      ((BasicMLData)network.
      Compute(inputn)).Data);
      var passed = should.ToString() == comput.ToString();
      if (passed)
      {
       ++passedCount;
      }
     }
     var input = new BasicMLData(7 * deep);
     for (int i = 0, k = 0; i < deep; ++i)
     {
      var idx = deep - 1 - i;
      var data = dbl[idx];
      input.Data[k++] = data.V1;
      input.Data[k++] = data.V2;
      input.Data[k++] = data.V3;
      input.Data[k++] = data.V4;
      input.Data[k++] = data.V5;
      input.Data[k++] = data.V6;
      input.Data[k++] = data.V7;
     }
     var perfect = dbl[0];
     var predict = new Result(
     ((BasicMLData)network.Compute(input)).Data);
     Console.ResetColor();
     if (predict.IsOut())
      goto START;
     if (passedCount < (deep * 9d / 10) ||
         !predict.IsValid())
      goto RETRY;
     if (PrintPredicts(predict, ++count, --countMax))
      goto START;
    }
   }
   catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  }
  static bool CreateDatabases(
  string fileDB,
  out ListResults dbl)
  {
   dbl = new ListResults();
   using (var reader = File.OpenText(fileDB))
   {
    var line = string.Empty;
    var separator = new string[] { " " };
    while ((line = reader.ReadLine()) != null)
    {
     var values = line.Split(separator, StringSplitOptions.None);
     var res = new Result(
     int.Parse(values[2]), int.Parse(values[3]),
     int.Parse(values[4]), int.Parse(values[5]),
     int.Parse(values[6]), int.Parse(values[7]),
     int.Parse(values[8]));
     dbl.Add(res);
    }
   }
   return true;
  }
  static IDictionary<int, int> vs = new Dictionary<int, int>(50);
  static IDictionary<int, int> cs = new Dictionary<int, int>(10);
  static bool PrintPredicts(Result r, int count = 1, int countMax = 0)
  {
   if (!vs.ContainsKey(r.V1)) vs.Add(r.V1, 1); else vs[r.V1]++;
   if (!vs.ContainsKey(r.V2)) vs.Add(r.V2, 1); else vs[r.V2]++;
   if (!vs.ContainsKey(r.V3)) vs.Add(r.V3, 1); else vs[r.V3]++;
   if (!vs.ContainsKey(r.V4)) vs.Add(r.V4, 1); else vs[r.V4]++;
   if (!vs.ContainsKey(r.V5)) vs.Add(r.V5, 1); else vs[r.V5]++;
   if (!cs.ContainsKey(r.V6)) cs.Add(r.V6, 1); else cs[r.V6]++;
   if (!cs.ContainsKey(r.V7)) cs.Add(r.V7, 1); else cs[r.V7]++;
   var vss = from p in vs
   orderby p.Value descending, p.Key ascending
   select string.Format("{0}:{1}%", p.Key, Math.Round(100m*p.Value/count,2));
   var vtp =
   (from p in vs orderby p.Value descending select p).Take(5).Last().Value;
   var css = from p in cs
   orderby p.Value descending, p.Key ascending
   select string.Format("{0}:{1}%", p.Key, Math.Round(100m*p.Value/count,2));
   var ctp =
   (from p in cs orderby p.Value descending select p).Take(2).Last().Value;
   Console.WriteLine("Try {0}: ({1} + {2})", count.ToString().PadLeft(2, '0'),
   string.Join(" ",
   from p in vs where p.Value >= vtp orderby p.Key ascending
   select p.Key),
   string.Join(" ",
   from p in cs where p.Value >= ctp orderby p.Key ascending select p.Key));
   Console.WriteLine(string.Join(" ", vss) + "\n" + string.Join(" ", css));
   return countMax > 0;
  }
 }
}

p ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.