Exemple #1
0
        public MainWindow()
        {
            InitializeComponent();

            checkBoxRunning.Checked      += new RoutedEventHandler(checkBoxRunning_Checked);
            checkBoxRunning.Unchecked    += new RoutedEventHandler(checkBoxRunning_Unchecked);
            buttonResetSearch.Click      += new RoutedEventHandler(buttonResetSearch_Click);
            buttonNewDestinations.Click  += new RoutedEventHandler(buttonNewDestinations_Click);
            buttonMoreDestinations.Click += new RoutedEventHandler(buttonMoreDestinations_Click);
            buttonLessDestinations.Click += new RoutedEventHandler(buttonLessDestinations_Click);

            checkBoxMutateFailedCrossOvers.Checked   += new RoutedEventHandler(checkBoxMutateFailedCrossOvers_Checked);
            checkBoxMutateFailedCrossOvers.Unchecked += new RoutedEventHandler(checkBoxMutateFailedCrossOvers_Unchecked);
            checkBoxMutateDuplicates.Checked         += new RoutedEventHandler(checkBoxMutateDuplicates_Checked);
            checkBoxMutateDuplicates.Unchecked       += new RoutedEventHandler(checkBoxMutateDuplicates_Unchecked);
            checkBoxDoCrossover.Checked   += new RoutedEventHandler(checkBoxDoCrossover_Checked);
            checkBoxDoCrossover.Unchecked += new RoutedEventHandler(checkBoxDoCrossover_Unchecked);

            _randomLocations = RandomProvider.GetRandomDestinations(_destinationCount);
            _algorithm       = new TravellingSalesmanAlgorithm(_startLocation, _randomLocations, _populationCount);

            _bestSolutionSoFar = _algorithm.GetBestSolutionSoFar().ToArray();
            _DrawLines();

            var thread = new Thread(_Thread);

            thread.Start();
        }
Exemple #2
0
        void buttonResetSearch_Click(object sender, RoutedEventArgs e)
        {
            lock (_algorithmLock)
                _algorithm = new TravellingSalesmanAlgorithm(_startLocation, _randomLocations, _populationCount);

            _bestSolutionSoFar = _algorithm.GetBestSolutionSoFar().ToArray();
            _DrawLines();
        }
Exemple #3
0
        void buttonNewDestinations_Click(object sender, RoutedEventArgs e)
        {
            _randomLocations = RandomProvider.GetRandomDestinations(_destinationCount);

            lock (_algorithmLock)
                _algorithm = new TravellingSalesmanAlgorithm(_startLocation, _randomLocations, _populationCount);

            _bestSolutionSoFar = _algorithm.GetBestSolutionSoFar().ToArray();
            _DrawLines();
        }
Exemple #4
0
        private void _AdjustDestinations()
        {
            labelDestinationCount.Text       = _destinationCount.ToString() + " ";
            buttonMoreDestinations.IsEnabled = _destinationCount < 57;
            buttonLessDestinations.IsEnabled = _destinationCount > 6;

            _randomLocations = RandomProvider.GetRandomDestinations(_destinationCount);

            lock (_algorithmLock)
                _algorithm = new TravellingSalesmanAlgorithm(_startLocation, _randomLocations, _populationCount);

            _bestSolutionSoFar = _algorithm.GetBestSolutionSoFar().ToArray();
            _DrawLines();
        }
Exemple #5
0
        private void _Thread()
        {
            while (!_closing)
            {
                if (_paused)
                {
                    lock (_lock)
                    {
                        if (_closing)
                        {
                            return;
                        }

                        while (_paused)
                        {
                            Monitor.Wait(_lock);

                            if (_closing)
                            {
                                return;
                            }
                        }
                    }
                }

                lock (_algorithmLock)
                {
                    // Try setting the parameter to false to see the results.
                    _algorithm.MustMutateFailedCrossovers = _mutateFailedCrossovers;
                    _algorithm.MustDoCrossovers           = _mustDoCrossovers;
                    _algorithm.Reproduce();

                    if (_mutateDuplicates)
                    {
                        _algorithm.MutateDuplicates();
                    }

                    var newSolution = _algorithm.GetBestSolutionSoFar().ToArray();
                    if (!newSolution.SequenceEqual(_bestSolutionSoFar))
                    {
                        // Probably the lock is not necessary... yet I prefer to keep it.
                        lock (_lock)
                        {
                            _bestSolutionSoFar = newSolution;

                            if (!_messageWaitingToBeProcessed)
                            {
                                _messageWaitingToBeProcessed = true;

                                // Using Dispatcher.Invoke here will cause a dead-lock, as the other thread
                                // will try to get the lock that we are still holding.
                                Dispatcher.BeginInvoke(new Action(_DrawLines), DispatcherPriority.ApplicationIdle);
                            }
                        }

                        // In a real application we should not do this sleep. But here we want to show how the
                        // algorithm is discovering better solutions.
                        Thread.Sleep(100);
                    }
                }
            }
        }