social_learning.World.calculateForagingAgentSensors C# (CSharp) Method

calculateForagingAgentSensors() public method

public calculateForagingAgentSensors ( IAgent agent ) : double[]
agent IAgent
return double[]
        public double[] calculateForagingAgentSensors(IAgent agent)
        {
            // Each plant type has its own set of sensors, plus we have one sensor for the velocity input.
            double[] sensors = new double[PlantTypes.Count() * SENSORS_PER_OBJECT_TYPE + (DistinguishPredators ? PredatorTypes : 1) * SENSORS_PER_OBJECT_TYPE + 1];

            sensors[0] = agent.Velocity / agent.MaxVelocity;

            // For every plant
            foreach (var plant in Plants)
            {
                // if the plant isn't available for eating then we do not activate the sensors
                if (!plant.AvailableForEating(agent))
                    continue;

                // Calculate the distance to the object from the teacher
                int[] distanceAndOrientation = _sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)plant.X, (int)plant.Y);
                int dist = distanceAndOrientation[0];
                int pos = distanceAndOrientation[1];

                // If it's too far away for the pred to see
                if (dist > AgentHorizon)
                    continue;

                // Identify the appropriate sensor
                int sIdx = getSensorIndex(agent, plant.Species.SpeciesId * SENSORS_PER_OBJECT_TYPE + 1, pos);

                if (sIdx == -1)
                    continue;

                // Add the signal strength for this plant to the sensor
                sensors[sIdx] += 1.0 - dist / AgentHorizon;
            }

            // For every predator
            foreach (var predator in Predators)
            {
                // Calculate the distance to the predator from the pred
                int[] distanceAndOrientation = _sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)predator.X, (int)predator.Y);
                int dist = distanceAndOrientation[0];
                int pos = distanceAndOrientation[1];

                // If it's too far away for the teacher to see
                if (dist > AgentHorizon)
                    continue;

                // Identify the appropriate sensor
                int sIdx = getSensorIndex(agent, PlantTypes.Count() * SENSORS_PER_OBJECT_TYPE + (DistinguishPredators ? predator.AttackType-1 : 0), pos);

                if (sIdx == -1)
                    continue;

                // Add the signal strength for this plant to the sensor
                sensors[sIdx] += 1.0 - dist / AgentHorizon;
            }

            return sensors;
        }

Usage Example

        public double[][] getSensorReadings()
        {
            IAgent a = _world.Agents.First();

            double[][] readings = new double[numlocations][];
            for (int i = 0; i < readings.Length; i++)
            {
                readings[i] = new double[3];
            }
            int j = 0;

            foreach (int[] location in locations)
            {
                a.X           = location[0];
                a.Y           = location[1];
                a.Orientation = orientations[j];
                a.Velocity    = velocities[j];
                readings[j]   = _world.calculateForagingAgentSensors(a);
                j++;
            }
            return(readings);
        }