Ejemplo n.º 1
0
 public Traveler(int startIndex, int endIndex, Starfield starfield, List <int[]> permutations)
 {
     this.startIndex   = startIndex;
     this.endIndex     = endIndex;
     this.starfield    = starfield;
     this.permutations = permutations;
 }
Ejemplo n.º 2
0
 public TravelerV2(int startIndex, int endIndex, Starfield starfield, int[] points, int ID)
 {
     this.startIndex = startIndex;
     this.endIndex   = endIndex;
     this.starfield  = starfield;
     this.points     = (int[])points.Clone();
     this.ID         = ID;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The distance between two points, given as indices into the Starfield's list
        /// </summary>
        private double Distance(int p1, int p2, Starfield starfield)
        {
            double dist = Vector.Distance(starfield.Points[p1], starfield.Points[p2]);

            lock (locker)
            {
                starfield.Distances[p1, p2] = dist;
                starfield.Distances[p2, p1] = dist;
            }
            return(dist);
        }
Ejemplo n.º 4
0
        static void Level1(int nPoints, Starfield starfield, int numThreads)
        {
            // Create an array in order
            var points = new int[nPoints];

            for (int i = 0; i < nPoints; ++i)
            {
                points[i] = i;
            }
            //int numPermutations = 1;
            //int count = 1;
            //while (count <= nPoints)
            //{
            //    numPermutations *= count;
            //    count += 1;
            //}
            //List<int[]> permutations = AllPermutations(points, numPermutations);
            List <TravelerV2> travelerV2s = new List <TravelerV2>();
            List <Thread>     threads     = new List <Thread>();
            var    watch   = Stopwatch.StartNew();
            double spacing = (double)nPoints / numThreads;

            for (int i = 0; i < numThreads; i++)
            {
                //If you had 10 points and 3 threads, spacing = 3.33
                //[0,3.33->3], [3,6.66->7], [7, 10]
                travelerV2s.Add(new TravelerV2((int)Math.Round(spacing * i), (int)Math.Round(spacing * (i + 1)), starfield, points, i));
                threads.Add(new Thread(travelerV2s[i].computeDistance));
                threads[i].Start();
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }
            var time = watch.ElapsedMilliseconds;

            Console.WriteLine(time / 1000.0 + "s");
            double minDistance = double.MaxValue;
            Path   bestPath    = null;

            foreach (TravelerV2 traveler in travelerV2s)
            {
                if (traveler.minDistance < minDistance)
                {
                    minDistance = traveler.minDistance;
                    bestPath    = traveler.bestPath;
                }
            }
            Console.WriteLine(minDistance);
            Console.WriteLine(bestPath);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The total length of the path
        /// </summary>
        /// <param name="starfield">Needed because the path stores only integers, not vectors</param>
        public double TotalDistance(Starfield starfield)
        {
            double total = 0;

            //for (int i = 0; i < Points.Count - 1; i++)
            //    total += Distance(Points[i], Points[i + 1], starfield);
            for (int i = 0; i < Points.Count - 1; i++)
            {
                if (starfield.Distances[Points[i], Points[i + 1]] == null)
                {
                    total += Distance(Points[i], Points[i + 1], starfield);
                }
                else
                {
                    total += (double)starfield.Distances[Points[i], Points[i + 1]];
                }
            }

            return(total);
        }