示例#1
0
        private Task CreateTask(List <GISDataPoint>[] partitions, InverseDistanceWeightedExtension idwFunction, KDTree tree, int indx, int numOutputs)
        {
            var task = Task.Factory.StartNew(() =>
            {
                _outputLists[indx] = new List <string>();

                int partSize = partitions[indx].Count;
                for (int i = 0; i < partSize; i++)
                {
                    GISDataPoint dp = partitions[indx][i];
                    dp.measurement  = idwFunction.Interpolate(tree, dp, _listGISDataPoints);

                    //_outputLists[indx].Add(dp.ToString());
                    _outputLists[indx].Add(dp.ToStringForOutputFile());

                    // Increment Progress Bar here.
                    percentComplete = ((double)progress / (double)numOutputs) * 100;
                    //Console.Write("\r{0}" + " calculations done. [{1:#.##}% Complete]", progress, percentComplete);
                    GISLocationProcessed(string.Format("\r{0}" + " calculations done. [{1:#.##}% Complete]", progress, percentComplete));
                    progress++;
                }
            });

            return(task);
        }
示例#2
0
        public void Validate()
        {
            int size       = listGISDataPoints.Count;
            int numOutputs = numNeighbors.Length * exponent.Length * size;

            idwFunction = new InverseDistanceWeightedExtension();

            //Get numThreads.
            if (Environment.ProcessorCount >= 4)
            {
                numThreads = Environment.ProcessorCount - 2;
            }
            else if (Environment.ProcessorCount >= 2 && Environment.ProcessorCount < 4)
            {
                numThreads = Environment.ProcessorCount - 1;
            }
            else
            {
                numThreads = 1;
            }

            using (FileStream fs = new FileStream(System.IO.Path.Combine(_outputDirectory, "loocv_idw.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(string.Format("{0,-10}", "ORIGINAL"));
                    for (int n = 0; n < numNeighbors.Length; n++)
                    {
                        for (int e = 0; e < exponent.Length; e++)
                        {
                            sw.Write(string.Format("{0,-1}{1,-1:D}{2,-1}{3,-1:F1}    ", "N", numNeighbors[n], "E", exponent[e]));
                        }
                    }
                    sw.WriteLine("");

                    List <GISDataPoint>[] partitions = ProcessingEngine.PartitionDataSet(listGISDataPoints, numThreads); // Partition our data.

                    looData = new double[partitions.Length][][];
                    for (int indx = 0; indx < partitions.Length; indx++)
                    {
                        looData[indx] = RectangularArrays.ReturnRectangularDoubleArray(partitions[indx].Count, numNeighbors.Length * exponent.Length);
                    }

                    List <Task> tasks = new List <Task>(); // Put thread tasks in a container to iterate over for Task.Wait() ... makeshift WaitAll() threads.

                    // Create Threads
                    for (int indx = 0; indx < partitions.Length; indx++)
                    {
                        // Must methodize the task creation. Otherwise above for loop with get out of range while threads are created and give exception.
                        tasks.Add(CreateTask(partitions, indx, numOutputs));
                    }

                    // Make Main thread wait for background/worker task threads.
                    for (int i = 0; i < tasks.Count; i++)
                    {
                        tasks[i].Wait();
                    }

                    // Output Results Outside of Threads (StreamWriter is not thread safe.)
                    //Console.WriteLine("\n\nOutputting Validation Results...\n");
                    MileStoneEvent("Writing Validation Results");

                    progress = 1;

                    for (int indx = 0; indx < partitions.Length; indx++)
                    {
                        int partSize = partitions[indx].Count;
                        for (int i = 0; i < partSize; i++)
                        {
                            sw.Write(string.Format("{0,-10:F1}", partitions[indx][i].measurement));
                            for (int n = 0; n < numNeighbors.Length; n++)
                            {
                                for (int e = 0; e < exponent.Length; e++)
                                {
                                    sw.Write(string.Format("{0,-10:F1}", looData[indx][i][n * exponent.Length + e]));

                                    // Increment Progress Bar here.
                                    percentComplete = ((double)progress / (double)numOutputs) * 100;
                                    //Console.Write("\r{0}" + " outputs written. [{1:#.##}% Complete]", progress, percentComplete);
                                    ItemProcessed(string.Format("\r{0}" + " outputs written. [{1:#.##}% Complete]", progress, percentComplete));
                                    progress++;
                                }
                            }
                            sw.WriteLine("");
                            //sw.Flush();
                        }
                    }
                    sw.Flush();
                }
            }
        }