private TspResult NtspRun(List <Location> tempTour)
        {
            var bestTspRes = new TspResult();

            bestTspRes.Distance = Double.MaxValue;
            double tempDistance = 0;
            double counter      = 0;
            var    sw           = new Stopwatch();

            sw.Start();
            while (sw.Elapsed.TotalSeconds < phaseOneTimeInSeconds)
            {
                counter++;
                tempTour.Shuffle();
                if ((tempDistance = Utils.DistanceSum(tempTour)) < bestTspRes.Distance)
                {
                    bestTspRes.BestTour = new List <Location>(tempTour);
                    bestTspRes.Distance = tempDistance;
                }
            }

            while (sw.Elapsed.TotalSeconds < phaseTwoTimeInSeconds)
            {
                counter++;
                tempTour.SwapEdges();
                if ((tempDistance = Utils.DistanceSum(tempTour)) < bestTspRes.Distance)
                {
                    bestTspRes.BestTour = new List <Location>(tempTour);
                    bestTspRes.Distance = tempDistance;
                }
            }

            return(bestTspRes);
        }
        private void SizeChanged(object obj)
        {
            var canvas = obj as Canvas;

            if (canvas != null)
            {
                double xDiff = Math.Abs(XSizeClass.MaxViewX - (canvas.ActualWidth - XSizeClass.Margin));
                double yDiff = Math.Abs(YSizeClass.MaxViewY - (canvas.ActualHeight - YSizeClass.Margin));
                if (xDiff > 10 || yDiff > 10)
                {
                    XSizeClass.MaxViewX = canvas.ActualWidth - XSizeClass.Margin;
                    YSizeClass.MaxViewY = canvas.ActualHeight - YSizeClass.Margin;

                    lock (lockObject)
                    {
                        if (bestTspResult != null)
                        {
                            var copyResult = new TspResult()
                            {
                                BestTour      = new List <Location>(BestTspResult.BestTour),
                                BestTourEdges = new List <Edge>(BestTspResult.BestTourEdges),
                                Distance      = BestTspResult.Distance
                            };
                            this.BestTspResult = copyResult;
                        }
                    }
                }
            }
        }
        private async void Start()
        {
            this.EnabledUI = false;

            this.solutionCount = -1;

            await Task.Factory.StartNew(() =>
            {
                var dataModel   = new DataModel(this.Filename);
                startTour       = new List <Location>(dataModel.Data);
                var startResult = new TspResult()
                {
                    BestTour = startTour, BestTourEdges = startTour.ConvertToEdgeList(), Distance = double.MaxValue
                };
                CalculateScale(startTour);
                UpdateResult(startResult);
                uint count = workersCount;

                if (TaskFlag)
                {
                    Task.Factory.StartNew(() => { InvokeWorkers(count); }).Wait();
                }
                else
                {
                    Task.Factory.StartNew(() => { InvokeProcessWorkers(count); }).Wait();
                }
            });
        }
Beispiel #4
0
        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            //GUI
            BtnStart.Visibility         = Visibility.Collapsed;
            BtnExit.Visibility          = Visibility.Hidden;
            BtnChooseFile.IsEnabled     = false;
            LblStatus.Content           = "Processing";
            TasksCountControl.IsEnabled = false;

            //variables
            phase1Time = (int)Phase1TimeControl.Value;
            phase2Time = (int)Phase2TimeControl.Value;
            isWorking  = true;

            //Get locations from file
            bestTspRes = new TspResult();
            var dataModel = new DataModel(FileNameTextBox.Text);

            //Start timer
            sw = new Stopwatch();
            sw.Start();

            //Set initial values
            bestTspRes.Distance      = Double.MaxValue;
            bestTspRes.SolutionCount = 1;

            //Init parents
            parent1 = new List <Location>(dataModel.Data);
            parent2 = new List <Location>(dataModel.Data);

            //Shuffle parents
            Task taskA = Task.Run(() => parent1.Shuffle());
            Task taskB = Task.Run(() => parent2.Shuffle());

            taskA.Wait();
            taskB.Wait();
            //Set one parent as best
            checkAndHandleIfBetter(parent1);
            checkAndHandleIfBetter(parent2);

            tasksArray = new Task[(int)TasksCountControl.Value];
            for (int i = 0; i < tasksArray.Length; i++)
            {
                if (i % 2 == 0)
                {
                    tasksArray[i] = Task.Run(() => phase1());
                }
                else
                {
                    tasksArray[i] = Task.Run(() => phase2());
                }
            }
            Refresher          = Task.Run(() => refresh());
            BtnStop.Visibility = Visibility.Visible;
        }
        private void UpdateResult(TspResult result)
        {
            Application.Current?.Dispatcher.Invoke(
                () =>
            {
                lock (lockObject)
                {
                    if (bestTspResult == null ||
                        result.Distance < bestTspResult.Distance)
                    {
                        this.BestTspResult = result;
                    }

                    this.SolutionCount++;
                }
            });
        }
 private void InvokeWorkers(uint count)
 {
     for (var i = 0; i < count; i++)
     {
         var tokenSource = new CancellationTokenSource();
         Task.Factory.StartNew(() =>
         {
             var copy = new List <Location>(startTour);
             while (!tokenSource.Token.IsCancellationRequested)
             {
                 TspResult result     = NtspRun(copy);
                 result.BestTourEdges = result.BestTour.ConvertToEdgeList();
                 UpdateResult(result);
             }
         }, TaskCreationOptions.LongRunning);
         tasksToken.Add(tokenSource);
     }
 }
Beispiel #7
0
        public Task Consume(ConsumeContext <IAlgortihmsInfo> ctx)
        {
            _numberOfTasks++;
            Console.WriteLine("Number of task = " + _numberOfTasks);
            var             bestTspRes              = new TspResult();
            var             tspDataFilePath         = ctx.Message.TspDataFilePath;
            var             dataModel               = new DataModel(tspDataFilePath);
            double          bestDistance            = Double.MaxValue;
            List <Location> tempTour                = new List <Location>(dataModel.Data);
            List <Location> tempTourSec             = new List <Location>(dataModel.Data);
            double          tempDistance            = 0;
            int             counter                 = 0;
            var             phaseFirstTimeInSeconds = ctx.Message.PhaseFirstTimeInSeconds;
            var             phaseSecTimeInSeconds   = ctx.Message.PhaseSecTimeInSeconds;

            tempTour.Shuffle();    // first parent
            tempTourSec.Shuffle(); // sec parent

            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (sw.Elapsed.TotalSeconds < phaseFirstTimeInSeconds)
            {
                counter++;
                if (counter % 2 == 0)
                {
                    tempTour.Shuffle();
                    if ((tempDistance = Utils.DistanceSum(tempTour)) < bestDistance)
                    {
                        bestTspRes.BestTour = new List <Location>(tempTour);
                        bestDistance        = tempDistance;
                        RespondResults(ctx, bestTspRes.BestTour, bestDistance, counter);
                    }
                }
                else
                {
                    tempTourSec.Shuffle();
                    if ((tempDistance = Utils.DistanceSum(tempTourSec)) < bestDistance)
                    {
                        bestTspRes.BestTour = new List <Location>(tempTourSec);
                        bestDistance        = tempDistance;
                        RespondResults(ctx, bestTspRes.BestTour, bestDistance, counter);
                    }
                }
            }

            Console.WriteLine("Second stage started ...");
            sw.Restart();
            while (sw.Elapsed.TotalSeconds < phaseSecTimeInSeconds)
            {
                counter++;

                tempTour.SwapEdges();
                if ((tempDistance = Utils.DistanceSum(tempTour)) < bestDistance)
                {
                    bestTspRes.BestTour = new List <Location>(tempTour);
                    bestDistance        = tempDistance;
                    RespondResults(ctx, bestTspRes.BestTour, bestDistance, counter);
                }
            }

            Console.WriteLine("the shortest found traveling salesman route:");
            Console.WriteLine("Distance = " + bestDistance);
            Console.WriteLine("Solution Count = " + counter);
            Console.WriteLine("Task ID = " + ctx.Message.TaskId);

            RespondResults(ctx, bestTspRes.BestTour, bestDistance, counter);

            return(Task.FromResult(0));
        }