//step 2 /// <summary> /// Computes the destinations in a parallel thread /// </summary> /// <param name="externalDepth">The external depth of view.</param> /// <param name="desiredNumber">The desired number of destinations to keep after simplification.</param> /// <param name="cellularFloor">The cellular floor.</param> /// <param name="staticCost">The static cost.</param> /// <param name="Tolerance">The tolerance.</param> public void ComputeDestinations(double externalDepth, int desiredNumber, CellularFloor cellularFloor, Dictionary <Cell, double> staticCost, double Tolerance = OSMDocument.AbsoluteTolerance) { AgentEscapeRoutes agentScapeRoutes = null; try { agentScapeRoutes = CellularIsovistCalculator.GetAgentEscapeRoutes(this.VantageCell, externalDepth, desiredNumber, cellularFloor, staticCost, Tolerance); } catch (Exception er) { this.Error = er.Report(); } if (agentScapeRoutes == null) { this._destinations = null; } else { this._destinations = agentScapeRoutes.Destinations; } }
private void updateDestination() { try { //update the destination AgentEscapeRoutes escapeRoute = null; var vantageCell = this._cellularFloor.FindCell(this.CurrentState.Location); if (!this._escapeRouts.ContainsKey(vantageCell)) { try { escapeRoute = CellularIsovistCalculator.GetAgentEscapeRoutes( vantageCell, this._isovistExternalRadius, this._destinationCount, this._cellularFloor, this._staticCost, 0.0000001d); } catch (Exception) { //throw new ArgumentException("Escape Routes cannot be calculated for the agent training!"); //MessageBox.Show(e.Report()); //try //{ // escapeRoute = CellularIsovistCalculator.GetAgentEscapeRoutes( // vantageCell, // this._isovistRadius, // this._destinationCount, // this._cellularFloor, // this._staticCost, // 0.0000001d); //} //catch (Exception) { } } if (escapeRoute == null) { return; } this._escapeRouts.Add(vantageCell, escapeRoute); } else { escapeRoute = this._escapeRouts[vantageCell]; } this.DecisionMakingPeriod = this.DecisionMakingPeriodDistribution.Sample(); this.WalkTime = 0.0d; //updating desired state //filtering the destinations to those in the cone of vision var visibleDestinationList = new List <AgentCellDestination>(); foreach (var item in escapeRoute.Destinations) { UV direction = item.Destination - this.CurrentState.Location; direction.Unitize(); if (direction.DotProduct(this.CurrentState.Direction) >= this.VisibilityCosineFactor) { visibleDestinationList.Add(item); } } AgentCellDestination[] destinations; if (visibleDestinationList.Count > 0) { destinations = visibleDestinationList.ToArray(); visibleDestinationList.Clear(); visibleDestinationList = null; } else { destinations = escapeRoute.Destinations; } double[] normalizedAngleCost = new double[destinations.Length]; //between zero and 1 for (int i = 0; i < destinations.Length; i++) { UV direction = destinations[i].Destination - this.CurrentState.Location; direction.Unitize(); normalizedAngleCost[i] = (direction.DotProduct(this.CurrentState.Direction) + 1) / 2; } double[] weighted = new double[destinations.Length]; double sum = 0; for (int i = 0; i < destinations.Length; i++) { weighted[i] = this.AngleDistributionLambdaFactor * Math.Exp(-this.AngleDistributionLambdaFactor * normalizedAngleCost[i]) + this.DesirabilityDistributionLambdaFactor * Math.Exp(-this.DesirabilityDistributionLambdaFactor * destinations[i].DesirabilityCost); sum += weighted[i]; } double selected = this._random.NextDouble() * sum; sum = 0; int selectedIndex = 0; for (int i = 0; i < destinations.Length; i++) { sum += weighted[i]; if (sum > selected) { selectedIndex = i; break; } } this.Destination = destinations[selectedIndex].Destination; } catch (Exception) { //MessageBox.Show(e.Report()); } }