예제 #1
0
        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());
            }
        }
        private void updateDestination()
        {
            try
            {
                //update the destination
                AgentEscapeRoutes escapeRoute = null;
                var vantageCell = this._host.cellularFloor.FindCell(this.CurrentState.Location);
                if (!this._escapeRouts.ContainsKey(vantageCell))
                {
                    Index vantageIndex = this._host.cellularFloor.FindIndex(vantageCell);
                    //var cells = new SortedDictionary<double, Cell>();
                    var cells = new SortedSet <Cell>(new CellComparer(this.CurrentState));
                    for (int i = -2; i <= 2; i++)
                    {
                        for (int j = -2; j <= 2; j++)
                        {
                            if (i != 0 && j != 0)
                            {
                                Index neighbor = vantageIndex + new Index(i, j);
                                if (this._host.cellularFloor.ContainsCell(neighbor))
                                {
                                    if (this._escapeRouts.ContainsKey(this._host.cellularFloor.Cells[neighbor.I, neighbor.J]))
                                    {
                                        cells.Add(this._host.cellularFloor.Cells[neighbor.I, neighbor.J]);
                                    }
                                }
                            }
                        }
                    }
                    if (cells.Count > 0)
                    {
                        escapeRoute = this._escapeRouts[cells.Min];
                    }
                    else
                    {
                        // AgentEscapeRoutes cannot be found!
                        return;
                        //throw new ArgumentException("The agent and all of its surrounding cells are inside barrier buffer");
                    }
                }
                else
                {
                    escapeRoute = this._escapeRouts[vantageCell];
                }
                // escape route exists and is not null
                this.WalkTime             = 0.0d;
                this.DecisionMakingPeriod = this.DecisionMakingPeriodDistribution.Sample();
                //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];
                }

                //selecting the destination
                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 e)
            {
                //MessageBox.Show(e.Message);
            }
        }