Skip to content

Commit

Permalink
sensor sensitivity corrected + test repaired + obsolete code removed
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Nov 23, 2024
1 parent 30a4d5e commit 16e23c2
Show file tree
Hide file tree
Showing 17 changed files with 21 additions and 266 deletions.
2 changes: 0 additions & 2 deletions source/EngineGpuKernels/ConstructorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,6 @@ ConstructorProcessor::constructCellIntern(
newConstructor.isReady = true;
} break;
case CellFunction_Sensor: {
result->cellFunctionData.sensor.mode = GenomeDecoder::readByte(constructor, genomeCurrentBytePosition) % SensorMode_Count;
result->cellFunctionData.sensor.angle = GenomeDecoder::readAngle(constructor, genomeCurrentBytePosition);
result->cellFunctionData.sensor.minDensity = (GenomeDecoder::readFloat(constructor, genomeCurrentBytePosition) + 1.0f) / 2;
result->cellFunctionData.sensor.restrictToColor = GenomeDecoder::readOptionalByte(constructor, genomeCurrentBytePosition, MAX_COLORS);
result->cellFunctionData.sensor.restrictToMutants =
Expand Down
2 changes: 0 additions & 2 deletions source/EngineGpuKernels/DataAccessKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ namespace
cellTO.cellFunctionData.constructor.constructionAngle2 = cell->cellFunctionData.constructor.constructionAngle2;
} break;
case CellFunction_Sensor: {
cellTO.cellFunctionData.sensor.mode = cell->cellFunctionData.sensor.mode;
cellTO.cellFunctionData.sensor.angle = cell->cellFunctionData.sensor.angle;
cellTO.cellFunctionData.sensor.minDensity = cell->cellFunctionData.sensor.minDensity;
cellTO.cellFunctionData.sensor.minRange = cell->cellFunctionData.sensor.minRange;
cellTO.cellFunctionData.sensor.maxRange = cell->cellFunctionData.sensor.maxRange;
Expand Down
2 changes: 0 additions & 2 deletions source/EngineGpuKernels/Object.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ struct ConstructorFunction

struct SensorFunction
{
SensorMode mode;
float angle;
float minDensity;
int8_t minRange; //< 0 = no restriction
int8_t maxRange; //< 0 = no restriction
Expand Down
4 changes: 0 additions & 4 deletions source/EngineGpuKernels/ObjectFactory.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ __inline__ __device__ void ObjectFactory::changeCellFromTO(DataTO const& dataTO,
cell->cellFunctionData.constructor.isReady = true;
} break;
case CellFunction_Sensor: {
cell->cellFunctionData.sensor.mode = cellTO.cellFunctionData.sensor.mode;
cell->cellFunctionData.sensor.angle = cellTO.cellFunctionData.sensor.angle;
cell->cellFunctionData.sensor.minDensity = cellTO.cellFunctionData.sensor.minDensity;
cell->cellFunctionData.sensor.minRange = cellTO.cellFunctionData.sensor.minRange;
cell->cellFunctionData.sensor.maxRange = cellTO.cellFunctionData.sensor.maxRange;
Expand Down Expand Up @@ -340,8 +338,6 @@ __inline__ __device__ Cell* ObjectFactory::createRandomCell(float energy, float2
cell->cellFunctionData.constructor.isReady = true;
} break;
case CellFunction_Sensor: {
cell->cellFunctionData.sensor.mode = _data->numberGen1.random(SensorMode_Count - 1);
cell->cellFunctionData.sensor.angle = _data->numberGen1.random(360.0f) - 180.0f;
cell->cellFunctionData.sensor.minDensity = _data->numberGen1.random(1.0f);
cell->cellFunctionData.sensor.minRange = -1;
cell->cellFunctionData.sensor.maxRange = -1;
Expand Down
97 changes: 7 additions & 90 deletions source/EngineGpuKernels/SensorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,8 @@ __inline__ __device__ void SensorProcessor::processCell(SimulationData& data, Si

if (abs(signal.channels[0]) > cudaSimulationParameters.cellFunctionSensorSignalThreshold) {
statistics.incNumSensorActivities(cell->color);
switch (cell->cellFunctionData.sensor.mode) {
case SensorMode_Neighborhood: {
searchNeighborhood(data, statistics, cell, signal);
signal.origin = SignalOrigin_Sensor;
} break;
//case SensorMode_FixedAngle: {
// searchByAngle(data, statistics, cell, signal);
//} break;
}
searchNeighborhood(data, statistics, cell, signal);
signal.origin = SignalOrigin_Sensor;
}
__syncthreads();

Expand Down Expand Up @@ -130,7 +123,7 @@ SensorProcessor::searchNeighborhood(SimulationData& data, SimulationStatistics&

if (threadIdx.x == 0) {
refScanAngle = Math::angleOfVector(CellFunctionProcessor::calcSignalDirection(data, cell));
minDensity = toInt(cell->cellFunctionData.sensor.minDensity * 100);
minDensity = toInt(cell->cellFunctionData.sensor.minDensity * 64);
minRange = cell->cellFunctionData.sensor.minRange;
restrictToColor = cell->cellFunctionData.sensor.restrictToColor;
restrictToMutants = cell->cellFunctionData.sensor.restrictToMutants;
Expand Down Expand Up @@ -193,11 +186,12 @@ SensorProcessor::searchNeighborhood(SimulationData& data, SimulationStatistics&
flagDetectedCells(data, cell, scanPos);

signal.channels[0] = 1; //something found
signal.channels[1] = toFloat((lookupResult >> 40) & 0xff) / 256; //density
signal.channels[1] = toFloat((lookupResult >> 40) & 0xff) / 64; //density

signal.channels[2] = 1.0f - min(1.0f, distance / 256); //distance: 1 = close, 0 = far away

auto legacyMode_unrestrictedMovements = !cudaSimulationParameters.cellFunctionMuscleMovementTowardTargetedObject;
signal.channels[3] = legacyMode_unrestrictedMovements ? angle / 360.0f : 0; //angle: between -0.5 and 0.5
auto movementTowardTargetedObject = !cudaSimulationParameters.cellFunctionMuscleMovementTowardTargetedObject;
signal.channels[3] = movementTowardTargetedObject ? angle / 360.0f : 0; //angle: between -0.5 and 0.5
cell->cellFunctionData.sensor.memoryChannel1 = signal.channels[1];
cell->cellFunctionData.sensor.memoryChannel2 = signal.channels[2];
cell->cellFunctionData.sensor.memoryChannel3 = signal.channels[3];
Expand All @@ -221,83 +215,6 @@ SensorProcessor::searchNeighborhood(SimulationData& data, SimulationStatistics&
__syncthreads();
}

__inline__ __device__ void
SensorProcessor::searchByAngle(SimulationData& data, SimulationStatistics& statistics, Cell* cell, Signal& signal)
{
__shared__ uint32_t minDensity;
__shared__ uint8_t restrictToColor;
__shared__ SensorRestrictToMutants restrictToMutants;
__shared__ float2 searchDelta;
__shared__ uint64_t lookupResult;

if (threadIdx.x == 0) {
minDensity = toInt(cell->cellFunctionData.sensor.minDensity * 255);
restrictToColor = cell->cellFunctionData.sensor.restrictToColor;
restrictToMutants = cell->cellFunctionData.sensor.restrictToMutants;
searchDelta = CellFunctionProcessor::calcSignalDirection(data, cell);
searchDelta = Math::rotateClockwise(searchDelta, cell->cellFunctionData.sensor.angle);

lookupResult = 0xffffffffffffffff;
}
__syncthreads();

auto const partition = calcPartition(NumScanPoints, threadIdx.x, blockDim.x);
auto const startRadius = calcStartDistanceForScanning(restrictToColor, restrictToMutants, cell->color);
auto const& densityMap = data.preprocessedSimulationData.densityMap;
for (int distanceIndex = partition.startIndex; distanceIndex <= partition.endIndex; ++distanceIndex) {
auto distance = startRadius + cudaSimulationParameters.cellFunctionSensorRange[cell->color] / NumScanPoints * distanceIndex;
auto scanPos = cell->pos + searchDelta * distance;
data.cellMap.correctPosition(scanPos);

uint32_t density = getCellDensity(data.timestep, cell, restrictToColor, restrictToMutants, densityMap, scanPos);

if (density < minDensity) {
continue;
}

float preciseDistance = distance;
uint64_t combined = static_cast<uint64_t>(preciseDistance) << 48 | static_cast<uint64_t>(density) << 40;
alienAtomicMin64(&lookupResult, combined);
}
__syncthreads();

if (threadIdx.x == 0) {
if (lookupResult != 0xffffffffffffffff) {

auto distance = toFloat(lookupResult >> 48);
auto scanPos = cell->pos + searchDelta * distance;
flagDetectedCells(data, cell, scanPos);

signal.channels[0] = 1; //something found
signal.channels[1] = static_cast<float>((lookupResult >> 40) & 0xff) / 256; //density
signal.channels[2] = distance / 256; //distance

auto legacyMode_unrestrictedMovements = !cudaSimulationParameters.cellFunctionMuscleMovementTowardTargetedObject;
if (!legacyMode_unrestrictedMovements) {
signal.channels[3] = 0;
}
cell->cellFunctionData.sensor.memoryChannel1 = signal.channels[1];
cell->cellFunctionData.sensor.memoryChannel2 = signal.channels[2];
cell->cellFunctionData.sensor.memoryChannel3 = signal.channels[3];

statistics.incNumSensorMatches(cell->color);

auto delta = data.cellMap.getCorrectedDirection(scanPos - cell->pos);
signal.targetX = delta.x;
signal.targetY = delta.y;
cell->cellFunctionData.sensor.memoryTargetX = delta.x;
cell->cellFunctionData.sensor.memoryTargetY = delta.y;
} else {
signal.channels[0] = 0; //nothing found
signal.channels[1] = cell->cellFunctionData.sensor.memoryChannel1;
signal.channels[2] = cell->cellFunctionData.sensor.memoryChannel2;
signal.channels[3] = cell->cellFunctionData.sensor.memoryChannel3;
signal.targetX = cell->cellFunctionData.sensor.memoryTargetX;
signal.targetY = cell->cellFunctionData.sensor.memoryTargetY;
}
}
}

__inline__ __device__ void SensorProcessor::flagDetectedCells(SimulationData& data, Cell* cell, float2 const& scanPos)
{
auto const& restrictToColor = cell->cellFunctionData.sensor.restrictToColor;
Expand Down
2 changes: 0 additions & 2 deletions source/EngineGpuKernels/TOs.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ struct ConstructorTO

struct SensorTO
{
SensorMode mode;
float angle;
float minDensity;
int8_t minRange; //< 0 = no restriction
int8_t maxRange; //< 0 = no restriction
Expand Down
5 changes: 0 additions & 5 deletions source/EngineImpl/DescriptionConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,6 @@ CellDescription DescriptionConverter::createCellDescription(DataTO const& dataTO
} break;
case CellFunction_Sensor: {
SensorDescription sensor;
if (cellTO.cellFunctionData.sensor.mode == SensorMode_FixedAngle) {
sensor.fixedAngle = cellTO.cellFunctionData.sensor.angle;
}
sensor.minDensity = cellTO.cellFunctionData.sensor.minDensity;
sensor.minRange = cellTO.cellFunctionData.sensor.minRange >= 0 ? std::make_optional(cellTO.cellFunctionData.sensor.minRange) : std::nullopt;
sensor.maxRange = cellTO.cellFunctionData.sensor.maxRange >= 0 ? std::make_optional(cellTO.cellFunctionData.sensor.maxRange) : std::nullopt;
Expand Down Expand Up @@ -608,13 +605,11 @@ void DescriptionConverter::addCell(
case CellFunction_Sensor: {
auto const& sensorDesc = std::get<SensorDescription>(*cellDesc.cellFunction);
SensorTO sensorTO;
sensorTO.mode = sensorDesc.getSensorMode();
sensorTO.restrictToColor = sensorDesc.restrictToColor.value_or(255);
sensorTO.restrictToMutants = sensorDesc.restrictToMutants;
sensorTO.minDensity = sensorDesc.minDensity;
sensorTO.minRange = static_cast<int8_t>(sensorDesc.minRange.value_or(-1));
sensorTO.maxRange = static_cast<int8_t>(sensorDesc.maxRange.value_or(-1));
sensorTO.angle = sensorDesc.fixedAngle.value_or(0);
sensorTO.memoryChannel1 = sensorDesc.memoryChannel1;
sensorTO.memoryChannel2 = sensorDesc.memoryChannel2;
sensorTO.memoryChannel3 = sensorDesc.memoryChannel3;
Expand Down
8 changes: 0 additions & 8 deletions source/EngineInterface/CellFunctionConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ enum NeuronActivationFunction_
NeuronActivationFunction_Count
};

using SensorMode = int;
enum SensorMode_
{
SensorMode_Neighborhood,
SensorMode_FixedAngle,
SensorMode_Count
};

using SensorRestrictToMutants = int;
enum SensorRestrictToMutants_
{
Expand Down
7 changes: 0 additions & 7 deletions source/EngineInterface/Descriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ struct ConstructorDescription

struct SensorDescription
{
std::optional<float> fixedAngle; //nullopt = entire neighborhood
float minDensity = 0.05f;
std::optional<int> minRange;
std::optional<int> maxRange;
Expand All @@ -191,12 +190,6 @@ struct SensorDescription

auto operator<=>(SensorDescription const&) const = default;

SensorMode getSensorMode() const { return fixedAngle.has_value() ? SensorMode_FixedAngle : SensorMode_Neighborhood; }
SensorDescription& setFixedAngle(float value)
{
fixedAngle = value;
return *this;
}
SensorDescription& setColor(int value)
{
restrictToColor = value;
Expand Down
2 changes: 1 addition & 1 deletion source/EngineInterface/GenomeConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Const
auto constexpr NeuronBytes = 64 + 8 + 8;
auto constexpr TransmitterBytes = 1;
auto constexpr ConstructorFixedBytes = 5;
auto constexpr SensorBytes = 7;
auto constexpr SensorBytes = 5;
auto constexpr NerveBytes = 2;
auto constexpr AttackerBytes = 1;
auto constexpr InjectorFixedBytes = 1;
Expand Down
7 changes: 0 additions & 7 deletions source/EngineInterface/GenomeDescriptionService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ std::vector<uint8_t> GenomeDescriptionService::convertDescriptionToBytes(GenomeD
} break;
case CellFunction_Sensor: {
auto const& sensor = std::get<SensorGenomeDescription>(*cell.cellFunction);
writeByte(result, sensor.fixedAngle.has_value() ? SensorMode_FixedAngle : SensorMode_Neighborhood);
writeAngle(result, sensor.fixedAngle.has_value() ? *sensor.fixedAngle : 0.0f);
writeDensity(result, sensor.minDensity);
writeOptionalByte(result, sensor.restrictToColor);
writeByte(result, sensor.restrictToMutants);
Expand Down Expand Up @@ -339,11 +337,6 @@ namespace
} break;
case CellFunction_Sensor: {
SensorGenomeDescription sensor;
auto mode = readByte(data, bytePosition) % SensorMode_Count;
auto angle = readAngle(data, bytePosition);
if (mode == SensorMode_FixedAngle) {
sensor.fixedAngle = angle;
}
sensor.minDensity = readDensity(data, bytePosition);
sensor.restrictToColor = readOptionalByte(data, bytePosition, MAX_COLORS);
sensor.restrictToMutants = readByte(data, bytePosition) % SensorRestrictToMutants_Count;
Expand Down
9 changes: 0 additions & 9 deletions source/EngineInterface/GenomeDescriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ struct ConstructorGenomeDescription

struct SensorGenomeDescription
{
std::optional<float> fixedAngle; //nullopt = entire neighborhood
float minDensity = 0.05f;
std::optional<int> minRange;
std::optional<int> maxRange;
Expand All @@ -89,14 +88,6 @@ struct SensorGenomeDescription

auto operator<=>(SensorGenomeDescription const&) const = default;

SensorMode getSensorMode() const { return fixedAngle.has_value() ? SensorMode_FixedAngle : SensorMode_Neighborhood; }

SensorGenomeDescription& setFixedAngle(float const& value)
{
fixedAngle = value;
return *this;
}

SensorGenomeDescription& setMinDensity(float const& value)
{
minDensity = value;
Expand Down
4 changes: 1 addition & 3 deletions source/EngineTests/ConstructorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ TEST_F(ConstructorTests, constructMuscleCell)

TEST_F(ConstructorTests, constructSensorCell)
{
auto sensorDesc = SensorGenomeDescription().setFixedAngle(90.0f).setColor(2).setMinDensity(0.5f).setRestrictToMutants(SensorRestrictToMutants_RestrictToFreeCells);
auto sensorDesc = SensorGenomeDescription().setColor(2).setMinDensity(0.5f).setRestrictToMutants(SensorRestrictToMutants_RestrictToFreeCells);
auto genome = GenomeDescriptionService::get().convertDescriptionToBytes(GenomeDescription().setCells({CellGenomeDescription().setCellFunction(sensorDesc)}));

DataDescription data;
Expand All @@ -1047,8 +1047,6 @@ TEST_F(ConstructorTests, constructSensorCell)
EXPECT_EQ(CellFunction_Sensor, actualConstructedCell.getCellFunctionType());

auto actualSensor = std::get<SensorDescription>(*actualConstructedCell.cellFunction);
EXPECT_EQ(sensorDesc.fixedAngle.has_value(), actualSensor.fixedAngle.has_value());
EXPECT_TRUE(lowPrecisionCompare(*sensorDesc.fixedAngle, *actualSensor.fixedAngle));
EXPECT_TRUE(lowPrecisionCompare(sensorDesc.minDensity, actualSensor.minDensity));
EXPECT_EQ(sensorDesc.restrictToColor, actualSensor.restrictToColor);
EXPECT_EQ(sensorDesc.restrictToMutants, actualSensor.restrictToMutants);
Expand Down
Loading

0 comments on commit 16e23c2

Please sign in to comment.