public void stopAnimation()
 {
     if (this._animationInProgress)
     {
         this._host.FreeNavigationAgentCharacter = FreeNavigationAgent.Create(this.agent.CurrentState);// new FreeNavigationAgent(this.agent);
         this._getWalkingTrailDataMenu.IsEnabled = true;
         this._settings._walkThrough.IsEnabled   = false;
         this._settings._walkThrough.IsChecked   = false;
         this._settings._walkThrough.Checked    -= _walkThrough_Checked;
         this._settings._walkThrough.Unchecked  -= _walkThrough_Unchecked;
         this._settings._hide.Visibility         = System.Windows.Visibility.Collapsed;
         this._settings._hide.Click    -= _hide_Click;
         this._settings.init_Title.Text = "Start Animation";
         this.agent.StopAnimation();
         this._settings._init.Click += _initStart_Click;
         this._settings._init.Click -= _initStop_Click;
         this._animationInProgress   = false;
     }
 }
示例#2
0
        private void _run_Click(object sender, RoutedEventArgs e)
        {
            #region Validate simulates annealing parameters
            int iterationCount = 0;
            if (!int.TryParse(this._numberOfIterations.Text, out iterationCount))
            {
                this.invalidInput("Number of Iterations");
                return;
            }
            if (iterationCount <= 0)
            {
                this.valueSmallerThanZero("Number of Iterations");
                return;
            }
            this._iterationCount = iterationCount;
            double minimumEnergy = 0;
            if (!double.TryParse(this._minimumTemperature.Text, out minimumEnergy))
            {
                this.invalidInput("Minimum Temperature");
                return;
            }
            if (minimumEnergy < 0)
            {
                MessageBox.Show("'Minimum Temperature' should not be smaller than zero!",
                                "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            double maximumEnergy = 0;
            if (!double.TryParse(this._maximumTemperature.Text, out maximumEnergy))
            {
                this.invalidInput("Maximum Temperature");
                return;
            }
            if (maximumEnergy <= 0)
            {
                this.valueSmallerThanZero("Maximum Temperature");
                return;
            }
            #endregion

            #region validation of training subset
            //double trainingRatio = 0;
            //if (!double.TryParse(this._ratioOfTrainingSubset.Text, out trainingRatio))
            //{
            //    MessageBox.Show("Invalied 'Ratio of Training Subset'", "Invalid Input",
            //        MessageBoxButton.OK, MessageBoxImage.Error);
            //    return;
            //}
            //else
            //{
            //    if (trainingRatio <= 0 || trainingRatio > 1.0)
            //    {
            //        MessageBox.Show("'Ratio of Training Subset' must be larger than zero and smaller than or equal to 1",
            //            "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
            //        return;
            //    }
            //}
            #endregion

            #region Validate duration and timeStep parameters
            double timeStep = 0;
            if (!double.TryParse(this._timeStep.Text, out timeStep))
            {
                MessageBox.Show("Invalid input for 'Time Step'!",
                                "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                if (timeStep <= 0)
                {
                    MessageBox.Show("'Time Step' must be larger than zero!",
                                    "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            this._timeStepValue = timeStep;

            this._durationValue = this._host.trailVisualization.AgentWalkingTrail.TimeIntervalBetweenInterpolatedStates;//convert seconds to hours
            #endregion

            #region Check to see if spatial data has been included
            int spatialDataCount = 0;
            foreach (Function function in this._host.cellularFloor.AllSpatialDataFields.Values)
            {
                SpatialDataField dataField = function as SpatialDataField;
                if (dataField != null)
                {
                    if (dataField.IncludeInActivityGeneration)
                    {
                        spatialDataCount++;
                    }
                }
            }
            if (spatialDataCount == 0)
            {
                var res = MessageBox.Show("There is no a spatial data field included to calculate cost/desirability!");
                return;
            }
            #endregion

            #region extracting the ralated parameters and checking to see if number of parameter is not zero
            this.AllParameters = new HashSet <Parameter>();
            if (this._isoExternalDepth.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.OPT_IsovistExternalDepth.ToString()]);
            }
            if (this._numberOfDestinations.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.OPT_NumberOfDestinations.ToString()]);
            }
            if (this._velocityMagnetude.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_VelocityMagnitude.ToString()]);
            }
            if (this._angularVelocity.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_AngularVelocity.ToString()]);
            }
            if (this._bodySize.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_BodySize.ToString()]);
            }
            if (this._accelerationMagnitude.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_AccelerationMagnitude.ToString()]);
            }
            if (this._barrierRepulsionRange.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_BarrierRepulsionRange.ToString()]);
            }
            if (this._repulsionChangeRate.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_MaximumRepulsion.ToString()]);
            }
            if (this._barrierFriction.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_BarrierFriction.ToString()]);
            }
            if (this._bodyElasticity.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_AgentBodyElasticity.ToString()]);
            }
            if (this._visibilityAngle.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.GEN_VisibilityAngle.ToString()]);
            }
            if (this._decisionMakingPeriodLamdaFactor.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.OPT_DecisionMakingPeriodLambdaFactor.ToString()]);
            }
            if (this._angleDistributionLambdaFactor.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.OPT_AngleDistributionLambdaFactor.ToString()]);
            }
            if (this._desirabilityDistributionLambdaFactor.IsChecked.Value)
            {
                this.AllParameters.Add(this._host.Parameters[AgentParameters.OPT_DesirabilityDistributionLambdaFactor.ToString()]);
            }
            foreach (var item in this._host.Parameters)
            {
                foreach (var function in item.Value.LinkedFunctions)
                {
                    if (function.IncludeInActivityGeneration)
                    {
                        this.AllParameters.Add(item.Value);
                        break;
                    }
                }
            }
            if (this.AllParameters.Count == 0)
            {
                MessageBox.Show("No parameter is included to account for the variability in the optimization process",
                                "Parameter Not Assigned", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            #endregion

            //execute visibility changes in the interface
            Dispatcher.Invoke(new Action(() =>
            {
                this._run_close.SetValue(Button.VisibilityProperty, System.Windows.Visibility.Collapsed);
                this._run_close.SetValue(Button.ContentProperty, "Close");
                this._mainInterface.SetValue(Grid.VisibilityProperty, System.Windows.Visibility.Collapsed);
                this._progressPanel.SetValue(StackPanel.VisibilityProperty, System.Windows.Visibility.Visible);
            }), DispatcherPriority.ContextIdle);
            //unregister run button event
            this._run_close.Click -= this._run_Click;
            //create annealing
            SimulatedAnnealingSolver solver = new SimulatedAnnealingSolver(this.AllParameters);
            //register UI update event for best fitness changes
            solver.BestFitnessUpdated += solver_BestFitnessUpdated;
            //register UI update event for fitness changes
            solver.FitnessUpdated += solver_FitnessUpdated;
            //Setting the initial iteration to zero
            this._counter = 0;
            //initializing the trial visualization
            this._host.trailVisualization.InitiateTrainingVisualization();

            //running the annealing process
            solver.Solve(minimumEnergy, maximumEnergy, iterationCount, this.measureFitnessWithInterfaceUpdate);

            //unregister UI update event for fitness changes
            solver.BestFitnessUpdated -= solver_BestFitnessUpdated;
            //unregister UI update event for fitness changes
            solver.FitnessUpdated -= solver_FitnessUpdated;
            //showing the close btn. The close event is not registered yet
            Dispatcher.Invoke(new Action(() =>
            {
                this._run_close.SetValue(Button.VisibilityProperty, System.Windows.Visibility.Visible);
            }), DispatcherPriority.ContextIdle);
            //updating the FreeNavigationAgentCharacters
            this._host.FreeNavigationAgentCharacter = FreeNavigationAgent.Create(this._host.trailVisualization.AgentWalkingTrail.InterpolatedStates[0]);
            //Show the last parameter setting on UI
            this._sb.Clear();
            foreach (var item in this.AllParameters)
            {
                this._sb.AppendLine(item.ToString());
            }
            this._updateMessage.SetValue(TextBlock.TextProperty, this._sb.ToString());
            //set Window closing events
            this._run_close.Click += (s, e1) =>
            {
                var result = MessageBox.Show("Do you want to clear the training data from screen?",
                                             "", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.Yes)
                {
                    this._host.trailVisualization.TerminateTrainingVisualization();
                }
                this.Close();
            };
        }