示例#1
0
        /*
         * public void ShowGrid(int steps, ref DataGrid grid)
         * {
         *  DataTable table = new DataTable();
         *  initGrid(ref table);
         *  for (int i = 0; i < steps; i++)
         *  {
         *      setOutputsToGrid(ref table, i + 1);
         *      feedForward();
         *      setInputsAsOutputs();
         *      storeOutputs();
         *  }
         *  grid.DataSource = table;
         * }
         */

        public void loadTrainingInstance(TrainingInstance instance)
        {
            for (int i = 0; i < instance.NoOfInputs; i++)
            {
                _input[i].value = instance.getInput(i);
            }
            for (int i = 0; i <= instance.NoOfOutputs - 1; i++)
            {
                _output[i].desiredValue = instance.getOutput(i);
            }
        }
示例#2
0
 public void addInstance(TrainingInstance newInstance)
 {
     instances.Add(newInstance);
     NoOfInstances = instances.Count;
 }
示例#3
0
        /*
         * public void Save(ref string filename)
         * {
         *  int i;
         *  classTrainingInstance inst;
         *  int FileNumber;
         *  FileNumber = FreeFile;
         *  FileOpen(FileNumber, filename, OpenMode.Output);
         *  PrintLine(FileNumber, "[Training Set]");
         *  PrintLine(FileNumber, instances.Count());
         *  for (int i = 0; i <= instances.Count() - 1; i++)
         *  {
         *      inst = getInstance(i);
         *      inst.Save(FileNumber);
         *  }
         *  FileClose(FileNumber);
         * }
         *
         * public void Load(ref string filename)
         * {
         *  int i;
         *  TrainingInstance inst;
         *  string dummy;
         *  int NoOfInstances;
         *  int FileNumber;
         *  Clear();
         *  FileNumber = FreeFile;
         *  FileOpen(FileNumber, filename, OpenMode.Input);
         *  Input(FileNumber, dummy);
         *  PrintLine(FileNumber, NoOfInstances);
         *  for (int i = 0; i <= NoOfInstances - 1; i++)
         *  {
         *      inst = new classTrainingInstance();
         *      inst.Load(FileNumber);
         *  }
         *  FileClose(FileNumber);
         * }
         */
        /*
         * public void Train(Network bp)
         * {
         *  TrainingInstance inst;
         *  for (int i = 1; i <= instances.Count; i++)
         *  {
         *      inst = (TrainingInstance)instances[i];
         *      bp.loadTrainingInstance(inst);
         *      bp.update();
         *  }
         * }
         */

        public void ImportTimeSeries(string filename, int Dimensions, int TimeDelay)
        {
            //int n;
            double dv;

            string[]  dataArray = new string[7];
            Regex     rex       = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
            ArrayList _data     = new ArrayList();

            TrainingInstance inst;

            double[] currentData = new double[Dimensions];
            double[,] prevHistory = new double[Dimensions, TimeDelay + 1];


            System.IO.StreamReader file_reader     = null;
            System.IO.StreamReader buffered_reader = null;

            if ((filename != ""))
            {
                MinValue = new double[Dimensions];
                MaxValue = new double[Dimensions];
                for (int i = 0; i < Dimensions; i++)
                {
                    MinValue[i] = 999999999;
                    MaxValue[i] = -999999999;
                }

                Clear();

                if (File.Exists(filename))
                {
                    try
                    {
                        file_reader     = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
                        buffered_reader = new System.IO.StreamReader(file_reader.BaseStream, file_reader.CurrentEncoding);
                        string line = null;
                        buffered_reader.ReadLine();
                        System.Collections.ArrayList _temp = new System.Collections.ArrayList();


                        while ((line = buffered_reader.ReadLine()) != null)
                        {
                            dataArray = rex.Split(line);

                            for (int i = 0; i < dataArray.Length - 1; i++)
                            {
                                if ((double.Parse(dataArray[i + 1]) < MinValue[i]))
                                {
                                    MinValue[i] = double.Parse(dataArray[i + 1]);
                                }

                                if ((double.Parse(dataArray[i + 1]) > MaxValue[i]))
                                {
                                    MaxValue[i] = double.Parse(dataArray[i + 1]);
                                }

                                dv = MaxValue[i] - MinValue[i];
                                if ((dv > 0))
                                {
                                    currentData[i] = System.Math.Abs(((double.Parse(dataArray[i + 1]) - MinValue[i]) * 0.6) / dv) + 0.2;
                                }
                                else
                                {
                                    currentData[i] = 0;
                                }

                                inst = new TrainingInstance();

                                inst.init(Dimensions * TimeDelay, Dimensions);

                                int n = 0;
                                for (int k = 0; k < TimeDelay; k++)
                                {
                                    for (int j = 0; j < Dimensions; j++)
                                    {
                                        inst.setInput(n, prevHistory[j, k]);
                                        n = n + 1;
                                    }
                                }

                                for (int j = 0; j < Dimensions; j++)
                                {
                                    inst.setOutput(j, currentData[j]);
                                }

                                addInstance(inst);

                                for (int j = 0; j < Dimensions; j++)
                                {
                                    for (int k = TimeDelay - 1; k > 0; k--)
                                    {
                                        prevHistory[j, k] = prevHistory[j, k - 1];
                                    }
                                }

                                for (int j = 0; j < Dimensions; j++)
                                {
                                    prevHistory[j, 0] = currentData[j];
                                }
                            }
                        }
                    }
                    finally
                    {
                        buffered_reader.Close();
                        file_reader.Close();
                    }
                }
            }
        }