Beispiel #1
0
        //
        // Adjusts the weights of the particle
        public void MoveParticle(double momentum, double globalWeight, double personalWeight, Neural_Network globalBestNetwork)
        {
            // Apply momentum to the last step
            Neural_Network step;

            step = stepNetwork.MultiplyConstant(momentum);

            // Add difference between current weights and group best
            Neural_Network tmpG = globalBestNetwork.SubtractNetworks(currNetwork);

            tmpG.MultiplyConstant(globalWeight);
            step.AddNetwork(tmpG);

            // Add difference between current weights and personal best
            Neural_Network tmpP = bestNetwork.SubtractNetworks(currNetwork);

            tmpP.MultiplyConstant(globalWeight);
            step.AddNetwork(tmpP);

            // Keep movement small
            if (Program.MAX_MOVEMENT > 0)
            {
                while (step.AverageWeights() > Program.MAX_MOVEMENT)
                {
                    step = step.MultiplyConstant(0.5);
                }
            }

            // Add to the current weights and set as last step
            currNetwork.AddNetwork(step);
            stepNetwork = step.SetWeights();

            // Find average movement in this step
            AverageMovement = stepNetwork.AverageWeights();
        }
Beispiel #2
0
        //
        // Find fitness of this particle for all training data
        //  - This is the summed squared error
        public double GetFitness(double[][] inputArray, double[][] outputArray)
        {
            double error = 0;   // Initialize error to 0

            // Get error
            int row = 0;

            foreach (double[] line in inputArray)
            {
                double[] output = currNetwork.Think(line);
                int      col    = 0;
                foreach (double val in output)
                {
                    error += Math.Abs((val - outputArray[row][col]) * (val - outputArray[row][col]));
                    col++;
                }
                row++;
            }
            ParticleFitness = error;

            // Check for personal best
            if (ParticleFitness < PersonalBest)
            {
                PersonalBest = ParticleFitness;
                bestNetwork  = currNetwork.SetWeights();
            }

            return(ParticleFitness);
        }
 //
 // Adds the values of another network to this
 public void AddNetwork(Neural_Network network)
 {
     for (int i = 0; i < Layers.Length; i++)
     {
         for (int j = 0; j < Layers[i].Nodes.Length; j++)
         {
             for (int k = 0; k < Layers[i].Nodes[j].Weights.Length; k++)
             {
                 Layers[i].Nodes[j].Weights[k] += network.Layers[i].Nodes[j].Weights[k];
             }
         }
     }
 }
Beispiel #4
0
 //
 // Updates all fitnesses and checks for new global best
 public void GetParticleFitnesses(int it)
 {
     for (int i = 0; i < Particles.Length; i++)
     {
         Particles[i].GetFitness(TrainingInputs, TrainingOutputs);   // get fitness
         if (Particles[i].PersonalBest < GlobalBest)                 // check for new global best
         {
             GlobalBest = Particles[i].PersonalBest;
             GlobalBestHistory.Add(GlobalBest);
             GlobalBestUpdateIteration.Add(it);
             bestNetwork = Particles[i].bestNetwork.SetWeights();
         }
     }
 }
        //
        // Sums returns the average value of the weights in this network
        public Neural_Network SetWeights()
        {
            Neural_Network output = new Neural_Network(LayerSizes, InputStats, OutputStats, UseOpponent, UseOffense, 0);

            for (int i = 0; i < Layers.Length; i++)
            {
                for (int j = 0; j < Layers[i].Nodes.Length; j++)
                {
                    for (int k = 0; k < Layers[i].Nodes[j].Weights.Length; k++)
                    {
                        output.Layers[i].Nodes[j].Weights[k] = Layers[i].Nodes[j].Weights[k];
                    }
                }
            }
            return(output);
        }
Beispiel #6
0
 //
 // Constructor
 public Particle(int[] LayerInfo, int[] inputStats, int[] outputStats, bool[] inUseOpponent, bool[] inUseOffense, Random random)
 {
     currNetwork = new Neural_Network(LayerInfo, inputStats, outputStats, inUseOpponent, inUseOffense, random);
     bestNetwork = currNetwork.SetWeights();
     stepNetwork = new Neural_Network(LayerInfo, inputStats, outputStats, inUseOpponent, inUseOffense, 0);
 }
Beispiel #7
0
        static void Main(string[] args)
        {
            // ================================================================================
            //   BUILD SEASON DATA   ==========================================================
            // ================================================================================
            Season season2012 = new Season(2012);
            Season season2013 = new Season(2013);

            Season[] pastSeasons = { season2012 };
            season2013.AddPastSeasons(pastSeasons);

            // Make a list of training games
            Game[]      trainGames1 = GetGoodGames(season2012, ALL_DATES);
            Game[]      trainGames2 = GetGoodGames(season2013, TRAIN_DATES);
            List <Game> trainList   = new List <Game>();

            //foreach (Game g in trainGames1)
            //trainList.Add(g);
            foreach (Game g in trainGames2)
            {
                trainList.Add(g);
            }

            // Finalize training and accuracy games
            Game[] trainGames = trainList.ToArray();
            Game[] accGames   = GetGoodGames(season2013, ACCRY_DATES);

            // Input statistics
            // ft = this team's for stats
            // tt = opp team's for stats
            // tf = opp team's against stats
            // ff = this team's against stats
            int[] inputStat =
            {
                TOTAL_YARDS,   TOTAL_YARDS,
                ADJ_PASS_AVG,  ADJ_PASS_AVG,
                ADJ_RUSH_AVG,  ADJ_RUSH_AVG,
                POINTS,        POINTS,
                //YARD_RATIO,         YARD_RATIO,
                IS_HOME,
                HV_PY_EXPECT,  HV_PY_EXPECT,
                //HOME_TIMES_HVPE,
                OOC_PYTHAG,    OOC_PYTHAG,
                //OOC_PYTHAG_RATIO,
                PYTHAG_EXPECT, PYTHAG_EXPECT,
                PENALTY_YARD,  PENALTY_YARD,
                //TO_NET,             TO_NET,
                //TO_FOR,             TO_FOR,
                //TO_AGAINST,         TO_AGAINST,
                //TIME_OF_POS,
            };
            bool[] inUseOpponent =
            {
                false, true,                          // TOTAL_YARDS
                false, true,                          // ADJ_PASS_AVG
                false, true,                          // ADJ_RUSH_AVG
                false, true,                          // POINTS
                //false, true,     // YARD_RATIO
                false,                                // IS_HOME
                false, true,                          // HV_PY_EXPECT
                //false,           // HOME_TIMES_HVPE
                false, true,                          // OOC_PYTHAG
                //false,           // OOC_PYTHAG_RATIO
                false, true,                          // PYTHAG_EXPECT
                false, true,                          // PENALTY
                //false, true,     // TO_NET
                //false, true,     // TO_FOR
                //false, true,     // TO_AGAINST
                //false,           // TIME_OF_POS
            };
            bool[] inUseOffense =
            {
                true, false,                          // TOTAL_YARDS
                true, false,                          // ADJ_PASS_AVG
                true, false,                          // ADJ_RUSH_AVG
                true, false,                          // POINTS
                //true, false,      // YARD_RATIO
                true,                                 // IS_HOME
                true, true,                           // HV_PY_EXPECT
                //true,             // HOME_TIMES_HVPE
                true, true,                           // OOC_PYTHAG
                //true,             // OOC_PYTHAG_RATIO
                true, true,                           // PYTHAG_EXPECT
                true, true,                           // PENALTY
                //true, true,       // TO_NET
                //true, true,       // TO_FOR
                //true, true,       // TO_AGAINST
                //true,             // TIME_OF_POS
            };

            int[] LayerSizes = { inputStat.Length, 2, 4, 2, 1 };   // 1st layer is # of inputs, last is # of outputs

            int[] outputStat = { POINTS };

            // Accuracy variables
            double correct = 0;
            int    nGames  = 0;

            string[] predictions = new string[accGames.Length];

            // ================================================================================
            //   TRAIN NEW NETWORK   ==========================================================
            // ================================================================================
            if (RUN == TRAIN)
            {
                // Get training maximums
                double[] maximumData = FindMaximums(trainGames);

                // Train network
                int            nTrainingGames     = (int)Math.Ceiling((1 - TEST_RATIO) * trainGames.Length);
                int            nTestGames         = (int)Math.Floor(TEST_RATIO * trainGames.Length);
                double[][]     trainingInputData  = new double[nTrainingGames][];
                double[][]     trainingOutputData = new double[nTrainingGames][];
                double[][]     testInputData      = new double[nTestGames][];
                double[][]     testOutputData     = new double[nTestGames][];
                Neural_Network oracle             = TrainNetwork(maximumData, trainGames, LayerSizes, inUseOpponent, inUseOffense, inputStat,
                                                                 outputStat, ref trainingInputData, ref trainingOutputData, ref testInputData, ref testOutputData, SAVE_TO);

                // Get accuracy maximums
                maximumData = FindMaximums(accGames);

                // Get accuracy
                nGames  = accGames.Length;
                correct = AnalyzeAccuracy(accGames, LayerSizes, inUseOpponent, inUseOffense, inputStat, outputStat, maximumData,
                                          oracle, ref nGames, ref predictions);
            }
            // ================================================================================
            //   RUN DUMB TEST  ===============================================================
            // ================================================================================
            else if (RUN == DUMB_ACC)
            {
                // Read in networks
                List <Neural_Network> networks = new List <Neural_Network>();
                for (int i = BEGIN_NETWORKS; i <= END_NETWORKS; i++)
                {
                    string         networkFile = "../../Networks/Network_" + i.ToString() + ".csv";
                    Neural_Network newNetwork  = RememberNetwork(networkFile);
                    networks.Add(newNetwork);
                }

                // Get accuracy maximums
                double[] maximumData = FindMaximums(accGames);

                // Get accuracy
                nGames  = accGames.Length;
                correct = AnalyzeAccuracy(accGames, maximumData, networks.ToArray(), ref nGames, ref predictions);
            }

            // ================================================================================
            //   SIMULATE RANGE OF DATES   ====================================================
            // ================================================================================
            else if (RUN == SIM_DATES)
            {
                // Read in networks
                List <Neural_Network> networkList = new List <Neural_Network>();
                for (int i = BEGIN_NETWORKS; i <= END_NETWORKS; i++)
                {
                    string         path        = "../../Networks/";
                    string         networkFile = path + "Network_" + i.ToString() + ".csv";
                    Neural_Network newNetwork  = RememberNetwork(networkFile);
                    networkList.Add(newNetwork);
                }
                Neural_Network[] networks = networkList.ToArray();

                // Get accuracy
                int[] dates = ACCRY_DATES;
                correct = SimulateWeek(season2013, dates, networks, ref predictions, ref nGames, trainGames);
            }

            double pctCorrect = 100 * correct / nGames;

            Console.WriteLine("Percent games predicted: {0}", pctCorrect);

            // Write predictions
            string       predictionFileName   = "../../Output/Point_Predictions.csv";
            StreamWriter predictionFileStream = new StreamWriter(predictionFileName);

            foreach (string s in predictions)
            {
                predictionFileStream.WriteLine(s);
            }
            predictionFileStream.Close();

            Console.WriteLine("\nPress ENTER to continue...");
            Console.ReadLine();
        }