Ejemplo n.º 1
0
        static void trainNormal(TimeCanvas canvas, List <Sequence> trainSet, int iterations, bool bidirectional)
        {
            //canvas = new TimeCanvas(25, 7);
            //canvas.c2 = c2;

            for (int i = 0; i < iterations; i++)
            {
                foreach (Sequence s in trainSet)
                {
                    canvas.Reset();
                    foreach (double[] item in s)
                    {
                        canvas.Train(item);
                    }

                    if (bidirectional)
                    {
                        Sequence rS = ObjectCopier.Clone(s);
                        rS.Reverse();

                        canvas.Reset();
                        foreach (double[] item in rS)
                        {
                            canvas.Train(item);
                        }
                    }
                }
            }
            canvas.Save("debugNormal.csv");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //----------------------------------------------------------------------------------------------
            //Create the network
            int timelineSize = 50;

            TimeCells.TimeCanvas canvas = new TimeCanvas(2, timelineSize, null);
            //TimeCells.TimeCanvas canvas = new TimeCanvas(2, 50, HarvesineDistance);
            canvas.parameterLineCreationTreshold = 0.005;
            canvas.parameterFFLearningRate       = 0;

            bool   doTraining          = true;
            double avgTrajectoryLength = timelineSize;

            //----------------------------------------------------------------------------------------------
            //Load the training set
            if (doTraining)
            {
                double[] minBound = new double[2], maxBound = new double[2];
                //List<TaxiCourse> trainSet = LoadDataSet("C:\\Users\\Stephane\\Documents\\GitHub\\Myline\\Datasets\\Kaggle Taxis\\train.csv", ref minBound, ref maxBound, ref avgTrajectoryLength, -1);
                List <TaxiCourse> trainSet = LoadDataSet("D:\\robotology\\src\\Myline\\CDZ.NET\\Datasets\\Kaggle Taxis\\train.csv", canvas.parameterLineCreationTreshold, ref minBound, ref maxBound, ref avgTrajectoryLength, -1);
                Console.WriteLine("Range is: Lattitude=" + (maxBound[0] - minBound[0]) + "\t Longitude=" + (maxBound[1] - minBound[1]));
                Console.WriteLine("MinMax are: Lattitude=" + maxBound[0] + "/" + minBound[0] + "\t Longitude=" + maxBound[1] + "/" + minBound[1]);


                //List<TaxiCourse> testSet = LoadDataSet("C:\\Users\\Stephane\\Documents\\GitHub\\Myline\\Datasets\\Kaggle Taxis\\test.csv", ref minBoundTest, ref maxBoundTest, ref avgTrajectoryLengthTest, -1);

                StreamWriter stats = new StreamWriter("stats.txt");
                stats.WriteLine("Min " + minBound[0] + " " + minBound[1]);
                stats.WriteLine("Max " + maxBound[0] + " " + maxBound[1]);
                stats.WriteLine("Lines " + trainSet.Count);
                stats.Close();

                //----------------------------------------------------------------------------------------------
                //Prepare the data
                //ScaleTrajectoryData(trainSet, minBound, maxBound);
                //ScaleTrajectoryData(testSet, minBound, maxBound);
                AddTerminalSymbol(trainSet, new double[] { 999, 999 });

                //Try to imprint (fast training)
                int imprintIteration = 0;

                Stopwatch watch2 = new Stopwatch();
                watch2.Start();
                foreach (TaxiCourse course in trainSet)
                {
                    canvas.Imprint(course.polyline);
                    imprintIteration++;
                    if (imprintIteration % 1000 == 0)
                    {
                        Console.WriteLine("Epoch done : " + Math.Floor(100 * imprintIteration / (double)trainSet.Count) + "%");
                    }
                }
                watch2.Stop();
                Console.WriteLine("Training set (" + trainSet.Count + " elements) processed in " + watch2.Elapsed + ".");
                canvas.Save("SavedCanvas.csv");
            }
            else //if (false)
            {
                Console.WriteLine("Loading previously learnt canvas.");
                canvas.Load("SavedCanvas.csv");
                Console.WriteLine("Loading complete.");
            }

            //----------------------------------------------------------------------------------------------
            //Load the test set
            double[]          minBoundTest            = null, maxBoundTest = null;
            double            avgTrajectoryLengthTest = 0.0;
            List <TaxiCourse> testSet = LoadDataSet("D:\\robotology\\src\\Myline\\CDZ.NET\\Datasets\\Kaggle Taxis\\test.csv", canvas.parameterLineCreationTreshold, ref minBoundTest, ref maxBoundTest, ref avgTrajectoryLengthTest, -1);

            //----------------------------------------------------------------------------------------------
            //Generate the test results
            StreamWriter results = new StreamWriter("Results.csv");

            results.WriteLine("\"TRIP_ID\",\"LATITUDE\",\"LONGITUDE\"");
            foreach (TaxiCourse course in testSet)
            {
                canvas.Reset();

                //Load the begining of the sequence
                foreach (double[] point in course.polyline)
                {
                    canvas.PresentInput(point, false, false);
                    canvas.PropagateActivity();
                }

                double[] prediction     = new double[] { 0, 0 };
                double[] lastPrediction = new double[] { 0, 0 };
                Console.WriteLine("Predicting");
                int iterationCount = 0;
                while (prediction[0] != 999 && prediction[1] != 999 && iterationCount < 2 * avgTrajectoryLength)
                {
                    prediction.CopyTo(lastPrediction, 0);
                    canvas.PresentInput(prediction, false, false);
                    canvas.PropagateActivity();
                    //We take the best prediction after the actual spot
                    List <KeyValuePair <double[], double> > sortedPredictions = canvas.PredictAll();
                    foreach (KeyValuePair <double[], double> pair in sortedPredictions)
                    {
                        if (pair.Key[0] != prediction[0] || pair.Key[1] != prediction[1])
                        {
                            pair.Key.CopyTo(prediction, 0);
                            break;
                        }
                    }
                    Console.WriteLine(prediction[0] + "\t\t" + prediction[1]);
                    iterationCount++;
                }
                results.WriteLine("\"" + course.tripID + "\"," + lastPrediction[1] + "," + lastPrediction[0]);
            }
            results.Close();
        }