Exemplo n.º 1
0
        public DataSerie1D Forecast(DataSerie1D initialSerie, bool addMonths, int startingMonth)
        {
            int         futureCount = FutureHorizon;
            DataSerie1D result      = new DataSerie1D();

            if (Equals(NeuralNetEngine, null))
            {
                return(null);
            }
            int neuralDimension = NeuralNetEngine.InputsCount - 1;
            int dataDimension   = initialSerie.Count;

            if (addMonths)
            {
                try
                {
                    if (neuralDimension > dataDimension)
                    {
                        throw new Exception("Err in input data.");
                    }

                    for (int k = (dataDimension - neuralDimension); k < dataDimension; k++)
                    {
                        result.Add(initialSerie.Data[k].Title, initialSerie.Data[k].X_Value);
                    }

                    //------------------------------------
                    int    monthIndex = startingMonth + neuralDimension;
                    double monthStand;
                    //------------------------------------

                    double[] inputs      = new double[neuralDimension + 1];
                    double   resultvalue = 0;

                    for (int i = 0; i < futureCount; i++)
                    {
                        for (int j = 0; j < neuralDimension; j++)
                        {
                            inputs[j] = result.Data[(j + i)].X_Value;
                        }
                        //------------------------------------------------
                        monthStand = (GetMonthIndex(monthIndex) * 0.9) / 12;
                        inputs[neuralDimension] = monthStand;
                        monthIndex += 1;
                        //------------------------------------------------
                        resultvalue = NeuralNetEngine.Compute(inputs)[0];
                        result.Add(i.ToString(), resultvalue);
                    }
                }
                catch (Exception ex)
                { throw ex; }
            }
            else
            {
                result = Forecast(initialSerie);
            }
            return(result);
        }
Exemplo n.º 2
0
        public void GetLayersStructTest1()
        {
            DataSerie1D hidenLayersStruct = new DataSerie1D();

            hidenLayersStruct.Add("HL1", 4);
            hidenLayersStruct.Add("HL2", 5);
            hidenLayersStruct.Add("HL3", 2);
            int inp = 2;

            int[] AnnStruct = new int[] { 4, 5, 2, 1 };
            Assert.Equal(AnnStruct, _tst.GetLayersStruct(hidenLayersStruct, inp, 1));
        }
Exemplo n.º 3
0
        public DataSerie1D Forecast(DataSerie1D initialSerie)
        {
            int         futureCount = FutureHorizon;
            DataSerie1D result      = new DataSerie1D();

            if (Equals(NeuralNetEngine, null))
            {
                return(null);
            }
            int neuralDimension = NeuralNetEngine.InputsCount;
            int dataDimension   = initialSerie.Count;

            try
            {
                if (neuralDimension > dataDimension)
                {
                    throw new Exception("Err in input data.");
                }

                for (int k = (dataDimension - neuralDimension); k < dataDimension; k++)
                {
                    result.Add(initialSerie.Data[k].Title, initialSerie.Data[k].X_Value);
                }

                double[] inputs      = new double[neuralDimension];
                double   resultvalue = 0;


                for (int i = 0; i < futureCount; i++)
                {
                    for (int j = 0; j < neuralDimension; j++)
                    {
                        inputs[j] = result.Data[(j + i)].X_Value;
                    }

                    resultvalue = NeuralNetEngine.Compute(inputs)[0];
                    result.Add(i.ToString(), resultvalue);
                }
            }
            catch (Exception ex)
            { throw ex; }

            return(result);
        }
Exemplo n.º 4
0
        static void LaunchANN(double[][] matrix, double[] vector)
        {
            //double[] vector = new double[] { 0.2, 0.2, 0.6, 0.9, 0.5};
            //double[][] matrix = new double[][]
            //{
            //    new double[]{0.1, 0.1},
            //    new double[]{0.2, 0.0},
            //    new double[]{0.3, 0.3},
            //    new double[]{0.4, 0.5},
            //    new double[]{0.25, 0.25}
            //};

            DataSerie1D annStrct = new DataSerie1D();

            annStrct.Add("HL1", 4);
            annStrct.Add("HL2", 2);

            NeuralNetworkEngineEO ann = new NeuralNetworkEngineEO(matrix, vector, annStrct);

            ann.LearningMaxIterations    = 100;
            ann.LearningError            = 0.0001;
            ann.LearningAlgorithm        = LearningAlgorithmEnum.LevenbergMarquardtLearning;
            ann.LearningAlgorithm_Params = new double[] { 0.1, 10 };
            ann.ActivationFunction       = ActivationFunctionEnum.BipolarSigmoidFunction;
            ann.ActiveFunction_Params    = new double[] { 2.00 };


            ann.Learn();
            //-0.5440211109;-0.2720105555
            var ans = ann.Compute(new double[] { 0.28, 0.29 });

            foreach (var valu in ans)
            {
                Console.WriteLine("ans = {0}", Math.Round(valu, 3));
            }

            Console.WriteLine("Final teaching err = {0}; MaxIter ={1}.", ann.FinalTeachingError, ann.FinalIterationsCount);
        }
Exemplo n.º 5
0
            public DataSerie1D Compute(DataSerieTD inputsDs)
            {
                if (Equals(inputsDs, null))
                {
                    return(null);
                }
                if (Equals(inputsDs.Data, null))
                {
                    return(null);
                }
                if (inputsDs.Data.Count < 1)
                {
                    return(null);
                }

                if (Equals(Network, null))
                {
                    return(null);
                }

                StandardizeData(inputsDs, this.mActivationFunction);

                DataSerie1D resultDs = new DataSerie1D();

                double[] result = null;

                int jCount = inputsDs.Data[0].List.Count();

                if (jCount < 1)
                {
                    return(null);
                }

                double[] input = new double[jCount];

                foreach (DataItemTD dItem in inputsDs.Data)
                {
                    for (int j = 0; j < jCount; j++)
                    {
                        input[j] = dItem.List[j];
                    }

                    result = Network.Compute(input);

                    resultDs.Add(dItem.Title, result[0]);
                }

                return(resultDs);
            }
Exemplo n.º 6
0
        public DataSerie1D Forecast2(DataSerie1D initialSerie)
        {
            int         futureCount     = FutureHorizon;
            DataSerie1D result          = new DataSerie1D();
            int         neuralDimension = NeuralNetEngine.InputsCount;
            int         dataDimension   = 1;
            int         dataCount       = initialSerie.Count;

            try
            {
                if (neuralDimension != dataDimension)
                {
                    throw new Exception("Err in input data.");
                }

                double[] inputs      = new double[neuralDimension];
                double   resultvalue = 0;

                //for (int k = 0; k < neuralDimension; k++)
                //{ result.Add(initialSerie.Data[k].Title, initialSerie.Data[k].X_Value, -100); }

                for (int i = 0; i < futureCount; i++)
                {
                    for (int j = 0; j < neuralDimension; j++)
                    {
                        inputs[j] = initialSerie.Data[i].X_Value;
                    }

                    resultvalue = NeuralNetEngine.Compute(inputs)[0];
                    result.Add(initialSerie.Data[i].Title, resultvalue);
                }
            }
            catch (Exception ex)
            { throw ex; }

            return(result);
        }
Exemplo n.º 7
0
        public void Formate(bool includeMonths, int startingMonth)
        {
            if (includeMonths == false)
            {
                Formate();
            }
            else
            {
                if (object.Equals(Data, null))
                {
                    return;
                }
                if (object.Equals(Data.Data, null))
                {
                    return;
                }
                int dataCount = Data.Count;
                if (dataCount < 1)
                {
                    return;
                }

                TrainingCount = (int)((TrainingRate * dataCount) / 100);
                TestingCount  = (dataCount - TrainingCount);

                mTrainingInputs = new DataSerieTD()
                {
                    Name = "Training inputs"
                };
                mTrainingOutputs = new DataSerie1D()
                {
                    Name = "Training outputs"
                };

                int monthIndex = startingMonth + mAnnInputLayerCount;

                double monthStand = 0;

                for (int i = 0; i < (TrainingCount - mAnnInputLayerCount); i++)
                {
                    double[] qj = new double[mAnnInputLayerCount + 1];

                    for (int j = 0; j < mAnnInputLayerCount; j++)
                    {
                        qj[j] = Data.Data[(i + j)].X_Value;
                    }

                    //------------------------------------
                    monthStand = (GetMonthIndex(monthIndex) * 0.9) / 12;
                    qj[mAnnInputLayerCount] = monthStand;
                    monthIndex += 1;
                    //------------------------------------
                    mTrainingInputs.Add(i.ToString(), qj);
                    mTrainingOutputs.Add(i.ToString(), Data.Data[(i + mAnnInputLayerCount)].X_Value);
                }

                mTestingInputs = new DataSerieTD()
                {
                    Name = "Testing inputs"
                };

                mTestingOutputs = new DataSerie1D()
                {
                    Name = "Testing outputs"
                };

                int k = TrainingCount;

                for (int i = 0; i < (TestingCount - mAnnInputLayerCount); i++)
                {
                    double[] qj = new double[mAnnInputLayerCount + 1];

                    for (int j = 0; j < mAnnInputLayerCount; j++)
                    {
                        qj[j] = Data.Data[(k + i + j)].X_Value;
                    }
                    //------------------------------------
                    monthStand = (GetMonthIndex(monthIndex) * 0.9) / 12;
                    qj[mAnnInputLayerCount] = monthStand;
                    monthIndex += 1;
                    //------------------------------------

                    mTestingInputs.Add((i + k).ToString(), qj);
                    mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + mAnnInputLayerCount)].X_Value);
                }
            }
        }
Exemplo n.º 8
0
        public void FormateDefault()
        {
            if (object.Equals(Data, null))
            {
                return;
            }
            if (object.Equals(Data.Data, null))
            {
                return;
            }
            int dataCount = Data.Count;

            if (dataCount < 1)
            {
                return;
            }

            TrainingCount = (int)((TrainingRate * dataCount) / 100);
            TestingCount  = (dataCount - TrainingCount);

            mTrainingInputs = new DataSerieTD()
            {
                Name = "Training inputs"
            };
            mTrainingOutputs = new DataSerie1D()
            {
                Name = "Training outputs"
            };


            for (int i = 0; i < (TrainingCount - mAnnInputLayerCount); i++)
            {
                double[] qj = new double[mAnnInputLayerCount];

                for (int j = 0; j < mAnnInputLayerCount; j++)
                {
                    qj[j] = Data.Data[(i + j)].X_Value;
                }
                mTrainingInputs.Add(i.ToString(), qj);
                mTrainingOutputs.Add(i.ToString(), Data.Data[(i + mAnnInputLayerCount)].X_Value);
            }

            mTestingInputs = new DataSerieTD()
            {
                Name = "Testing inputs"
            };
            mTestingOutputs = new DataSerie1D()
            {
                Name = "Testing outputs"
            };
            int k = TrainingCount;

            for (int i = 0; i < (TestingCount - mAnnInputLayerCount); i++)
            {
                double[] qj = new double[mAnnInputLayerCount];
                for (int j = 0; j < mAnnInputLayerCount; j++)
                {
                    qj[j] = Data.Data[(k + i + j)].X_Value;
                }
                mTestingInputs.Add((i + k).ToString(), qj);
                mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + mAnnInputLayerCount)].X_Value);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Formate Training and Testing (inputs-outputs) by specific time series indexes.
        /// </summary>
        /// <param name="annInputModel"> The ANN input model </param>
        private void Formate(DataSerie1D annInputModel)
        {
            try
            {
                if (object.Equals(Data, null))
                {
                    return;
                }
                if (object.Equals(Data.Data, null))
                {
                    return;
                }
                int dataCount = Data.Count;
                if (dataCount < 1)
                {
                    return;
                }

                if (object.Equals(annInputModel, null))
                {
                    throw new Exception("No time series indexes pattern are found.");
                }
                if (object.Equals(annInputModel.Data, null))
                {
                    throw new Exception("No time series indexes pattern are found.");
                }
                if (annInputModel.Count < 1)
                {
                    throw new Exception("No input model is specified.");
                }

                if (annInputModel.Max > dataCount)
                {
                    throw new Exception("Index great then data serie count.");
                }

                TrainingCount = (int)((TrainingRate * dataCount) / 100);
                TestingCount  = (dataCount - TrainingCount);

                mTrainingInputs = new DataSerieTD()
                {
                    Name = "Training inputs"
                };
                mTrainingOutputs = new DataSerie1D()
                {
                    Name = "Training outputs"
                };

                int annInputCount = annInputModel.Count;

                int[] indexes = new int[annInputCount];

                for (int i = 0; i < annInputCount; i++)
                {
                    if (annInputModel.Data[i].X_Value > TrainingCount || annInputModel.Data[i].X_Value > TestingCount)
                    {
                        throw new Exception("ANN Input indexes must be lower than Training and Testing data serie lenght.");
                    }
                    indexes[i] = (int)(annInputModel.Data[i].X_Value - 1);
                }

                int indexMax = (indexes.Max() - 1);

                for (int i = 0; i < (TrainingCount - indexMax); i++)
                {
                    double[] qj = new double[annInputCount];

                    for (int j = 0; j < annInputCount; j++)
                    {
                        qj[j] = Data.Data[(i + indexes[j])].X_Value;
                    }
                    mTrainingInputs.Add(i.ToString(), qj);
                    mTrainingOutputs.Add(i.ToString(), Data.Data[(i + indexMax)].X_Value);
                }

                mTestingInputs = new DataSerieTD()
                {
                    Name = "Testing inputs"
                };
                mTestingOutputs = new DataSerie1D()
                {
                    Name = "Testing outputs"
                };
                int k = TrainingCount;

                for (int i = 0; i < (TestingCount - indexMax); i++)
                {
                    double[] qj = new double[annInputCount];
                    for (int j = 0; j < annInputCount; j++)
                    {
                        qj[j] = Data.Data[(k + i + indexes[j])].X_Value;
                    }
                    mTestingInputs.Add((i + k).ToString(), qj);
                    mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + indexMax)].X_Value);
                }
            }
            catch (Exception ex) { throw ex; }
        }