MCPcopy Create free account
hub / github.com/davidrmiller/biosim4 / getSensor

Method getSensor

src/getSensor.cpp:197–370  ·  view source on GitHub ↗

Returned sensor values range SENSOR_MIN..SENSOR_MAX

Source from the content-addressed store, hash-verified

195
196// Returned sensor values range SENSOR_MIN..SENSOR_MAX
197float Indiv::getSensor(Sensor sensorNum, unsigned simStep) const
198{
199 float sensorVal = 0.0;
200
201 switch (sensorNum) {
202 case Sensor::AGE:
203 // Converts age (units of simSteps compared to life expectancy)
204 // linearly to normalized sensor range 0.0..1.0
205 sensorVal = (float)age / p.stepsPerGeneration;
206 break;
207 case Sensor::BOUNDARY_DIST:
208 {
209 // Finds closest boundary, compares that to the max possible dist
210 // to a boundary from the center, and converts that linearly to the
211 // sensor range 0.0..1.0
212 int distX = std::min<int>(loc.x, (p.sizeX - loc.x) - 1);
213 int distY = std::min<int>(loc.y, (p.sizeY - loc.y) - 1);
214 int closest = std::min<int>(distX, distY);
215 int maxPossible = std::max<int>(p.sizeX / 2 - 1, p.sizeY / 2 - 1);
216 sensorVal = (float)closest / maxPossible;
217 break;
218 }
219 case Sensor::BOUNDARY_DIST_X:
220 {
221 // Measures the distance to nearest boundary in the east-west axis,
222 // max distance is half the grid width; scaled to sensor range 0.0..1.0.
223 int minDistX = std::min<int>(loc.x, (p.sizeX - loc.x) - 1);
224 sensorVal = minDistX / (p.sizeX / 2.0);
225 break;
226 }
227 case Sensor::BOUNDARY_DIST_Y:
228 {
229 // Measures the distance to nearest boundary in the south-north axis,
230 // max distance is half the grid height; scaled to sensor range 0.0..1.0.
231 int minDistY = std::min<int>(loc.y, (p.sizeY - loc.y) - 1);
232 sensorVal = minDistY / (p.sizeY / 2.0);
233 break;
234 }
235 case Sensor::LAST_MOVE_DIR_X:
236 {
237 // X component -1,0,1 maps to sensor values 0.0, 0.5, 1.0
238 auto lastX = lastMoveDir.asNormalizedCoord().x;
239 sensorVal = lastX == 0 ? 0.5 :
240 (lastX == -1 ? 0.0 : 1.0);
241 break;
242 }
243 case Sensor::LAST_MOVE_DIR_Y:
244 {
245 // Y component -1,0,1 maps to sensor values 0.0, 0.5, 1.0
246 auto lastY = lastMoveDir.asNormalizedCoord().y;
247 sensorVal = lastY == 0 ? 0.5 :
248 (lastY == -1 ? 0.0 : 1.0);
249 break;
250 }
251 case Sensor::LOC_X:
252 // Maps current X location 0..p.sizeX-1 to sensor range 0.0..1.0
253 sensorVal = (float)loc.x / (p.sizeX - 1);
254 break;

Callers

nothing calls this directly

Calls 13

longProbePopulationFwdFunction · 0.85
longProbeBarrierFwdFunction · 0.85
visitNeighborhoodFunction · 0.85
getSignalDensityFunction · 0.85
genomeSimilarityFunction · 0.85
sensorNameFunction · 0.85
asNormalizedCoordMethod · 0.80
isOccupiedAtMethod · 0.80
rotate90DegCWMethod · 0.80

Tested by

no test coverage detected