Skip to content

Commit

Permalink
allow reconnectors to establish only connections to less or more comp…
Browse files Browse the repository at this point in the history
…lex mutants
  • Loading branch information
chrxh committed Jul 6, 2024
1 parent eaefe5f commit 73c6296
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 20 deletions.
61 changes: 51 additions & 10 deletions source/EngineGpuKernels/DensityMap.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public:
CudaMemoryManager::getInstance().acquireMemory<uint32_t>(_densityMapSize.x * _densityMapSize.y, _specificMutantDensityMap);
CudaMemoryManager::getInstance().acquireMemory<uint64_t>(_densityMapSize.x * _densityMapSize.y, _lessGenomeComplexityDensityMap1);
CudaMemoryManager::getInstance().acquireMemory<uint64_t>(_densityMapSize.x * _densityMapSize.y, _lessGenomeComplexityDensityMap2);
CudaMemoryManager::getInstance().acquireMemory<uint64_t>(_densityMapSize.x * _densityMapSize.y, _moreGenomeComplexityDensityMap1);
CudaMemoryManager::getInstance().acquireMemory<uint64_t>(_densityMapSize.x * _densityMapSize.y, _moreGenomeComplexityDensityMap2);
_slotSize = slotSize;
}

Expand All @@ -30,6 +32,8 @@ public:
CudaMemoryManager::getInstance().freeMemory(_specificMutantDensityMap);
CudaMemoryManager::getInstance().freeMemory(_lessGenomeComplexityDensityMap1);
CudaMemoryManager::getInstance().freeMemory(_lessGenomeComplexityDensityMap2);
CudaMemoryManager::getInstance().freeMemory(_moreGenomeComplexityDensityMap1);
CudaMemoryManager::getInstance().freeMemory(_moreGenomeComplexityDensityMap2);
}

__device__ __inline__ void clear()
Expand All @@ -43,6 +47,8 @@ public:
_specificMutantDensityMap[index] = 0;
_lessGenomeComplexityDensityMap1[index] = 0;
_lessGenomeComplexityDensityMap2[index] = 0;
_moreGenomeComplexityDensityMap1[index] = 0;
_moreGenomeComplexityDensityMap2[index] = 0;
}
}

Expand Down Expand Up @@ -108,7 +114,7 @@ public:
return 0ul;
}

__device__ __inline__ uint32_t getLowerComplexMutantDensity(float2 const& pos, uint32_t genomeComplexity) const
__device__ __inline__ uint32_t getLessComplexMutantDensity(float2 const& pos, uint32_t genomeComplexity) const
{
auto index = toInt(pos.x) / _slotSize + toInt(pos.y) / _slotSize * _densityMapSize.x;
if (index >= 0 && index < _densityMapSize.x * _densityMapSize.y) {
Expand All @@ -122,6 +128,20 @@ public:
return 0ul;
}

__device__ __inline__ uint32_t getMoreComplexMutantDensity(float2 const& pos, uint32_t genomeComplexity) const
{
auto index = toInt(pos.x) / _slotSize + toInt(pos.y) / _slotSize * _densityMapSize.x;
if (index >= 0 && index < _densityMapSize.x * _densityMapSize.y) {
auto bucket = min(16, max(0, 33 - __clz(genomeComplexity)));
if (bucket < 8) {
return (_moreGenomeComplexityDensityMap1[index] >> (bucket * 8)) & 0xff;
} else {
return (_moreGenomeComplexityDensityMap2[index] >> ((bucket - 8) * 8)) & 0xff;
}
}
return 0ul;
}

__device__ __inline__ void addCell(uint64_t const& timestep, Cell* cell)
{
auto index = toInt(cell->pos.x) / _slotSize + toInt(cell->pos.y) / _slotSize * _densityMapSize.x;
Expand All @@ -148,18 +168,37 @@ public:
{
auto bucket = 32 - __clz(cell->genomeComplexity);
if (bucket < 8) {
auto bitset = static_cast<uint64_t>(1ull << bucket * 8);
for (int i = 0; i < 7; ++i) {
bitset |= (bitset << 8);
{
auto bitset = static_cast<uint64_t>(1ull << bucket * 8);
for (int i = 0; i < 7; ++i) {
bitset |= (bitset << 8);
}
alienAtomicAdd64(&_lessGenomeComplexityDensityMap1[index], bitset);
alienAtomicAdd64(&_lessGenomeComplexityDensityMap2[index], static_cast<uint64_t>(0x0101010101010101ull));
}
{
auto bitset = static_cast<uint64_t>(1ull << bucket * 8);
for (int i = 0; i < 7; ++i) {
bitset |= (bitset >> 8);
}
alienAtomicAdd64(&_moreGenomeComplexityDensityMap1[index], bitset);
}
alienAtomicAdd64(&_lessGenomeComplexityDensityMap1[index], bitset);
alienAtomicAdd64(&_lessGenomeComplexityDensityMap2[index], static_cast<uint64_t>(0x0101010101010101ull));
} else if (bucket < 16) {
auto bitset = static_cast<uint64_t>(1ull << ((bucket - 8) * 8));
for (int i = 0; i < 7; ++i) {
bitset |= (bitset << 8);
{
auto bitset = static_cast<uint64_t>(1ull << ((bucket - 8) * 8));
for (int i = 0; i < 7; ++i) {
bitset |= (bitset << 8);
}
alienAtomicAdd64(&_lessGenomeComplexityDensityMap2[index], bitset);
}
{
auto bitset = static_cast<uint64_t>(1ull << ((bucket - 8) * 8));
for (int i = 0; i < 7; ++i) {
bitset |= (bitset >> 8);
}
alienAtomicAdd64(&_moreGenomeComplexityDensityMap2[index], bitset);
alienAtomicAdd64(&_moreGenomeComplexityDensityMap1[index], static_cast<uint64_t>(0x0101010101010101ull));
}
alienAtomicAdd64(&_lessGenomeComplexityDensityMap2[index], bitset);
}
}
}
Expand All @@ -183,5 +222,7 @@ private:
uint32_t* _specificMutantDensityMap;
uint64_t* _lessGenomeComplexityDensityMap1;
uint64_t* _lessGenomeComplexityDensityMap2;
uint64_t* _moreGenomeComplexityDensityMap1;
uint64_t* _moreGenomeComplexityDensityMap2;
};

10 changes: 9 additions & 1 deletion source/EngineGpuKernels/ReconnectorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ __inline__ __device__ void ReconnectorProcessor::tryCreateConnection(SimulationD
auto const& reconnector = cell->cellFunctionData.reconnector;
Cell* closestCell = nullptr;
float closestDistance = 0;
data.cellMap.executeForEach(cell->pos, cudaSimulationParameters.cellFunctionReconnectorRadius[cell->color], cell->detached, [&](auto const& otherCell) {
data.cellMap.executeForEach(cell->pos, cudaSimulationParameters.cellFunctionReconnectorRadius[cell->color], cell->detached, [&](Cell* const& otherCell) {
if (cell->creatureId != 0 && otherCell->creatureId == cell->creatureId) {
return;
}
Expand All @@ -76,6 +76,14 @@ __inline__ __device__ void ReconnectorProcessor::tryCreateConnection(SimulationD
if (reconnector.restrictToMutants == ReconnectorRestrictToMutants_RestrictToRespawnedMutants && otherCell->mutationId != 1) {
return;
}
if (reconnector.restrictToMutants == ReconnectorRestrictToMutants_RestrictToLessComplexMutants
&& otherCell->genomeComplexity >= cell->genomeComplexity) {
return;
}
if (reconnector.restrictToMutants == ReconnectorRestrictToMutants_RestrictToMoreComplexMutants
&& otherCell->genomeComplexity <= cell->genomeComplexity) {
return;
}
if (CellConnectionProcessor::isConnectedConnected(cell, otherCell)) {
return;
}
Expand Down
11 changes: 9 additions & 2 deletions source/EngineGpuKernels/SensorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ __inline__ __device__ uint32_t SensorProcessor::getCellDensity(
result = densityMap.getZeroMutantDensity(scanPos);
}
if (restrictToMutants == SensorRestrictToMutants_RestrictToLessComplexMutants) {
result = densityMap.getLowerComplexMutantDensity(scanPos, cell->genomeComplexity);
result = densityMap.getLessComplexMutantDensity(scanPos, cell->genomeComplexity);
}
if (restrictToMutants == SensorRestrictToMutants_RestrictToMoreComplexMutants) {
result = densityMap.getMoreComplexMutantDensity(scanPos, cell->genomeComplexity);
}
if (restrictToColor != 255) {
result = min(result, densityMap.getColorDensity(scanPos, restrictToColor));
Expand Down Expand Up @@ -273,7 +276,11 @@ __inline__ __device__ void SensorProcessor::flagDetectedCells(SimulationData& da
continue;
}
if (restrictToMutants == SensorRestrictToMutants_RestrictToLessComplexMutants
&& (otherCell->genomeComplexity >= cell->genomeComplexity || otherCell->mutationId == 0 && otherCell->mutationId == 1)) {
&& (otherCell->genomeComplexity >= cell->genomeComplexity || otherCell->mutationId == 0 || otherCell->mutationId == 1)) {
continue;
}
if (restrictToMutants == SensorRestrictToMutants_RestrictToMoreComplexMutants
&& (otherCell->genomeComplexity <= cell->genomeComplexity || otherCell->mutationId == 0 || otherCell->mutationId == 1)) {
continue;
}
//if (restrictToOtherMutants && otherCell->mutationId != 0
Expand Down
3 changes: 3 additions & 0 deletions source/EngineInterface/CellFunctionConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ enum SensorRestrictToMutants_
SensorRestrictToMutants_RestrictToRespawnedMutants,
SensorRestrictToMutants_RestrictToZeroMutants,
SensorRestrictToMutants_RestrictToLessComplexMutants,
SensorRestrictToMutants_RestrictToMoreComplexMutants,
SensorRestrictToMutants_Count
};

Expand Down Expand Up @@ -148,6 +149,8 @@ enum ReconnectorRestrictToMutants_
ReconnectorRestrictToMutants_RestrictToOtherMutants,
ReconnectorRestrictToMutants_RestrictToRespawnedMutants,
ReconnectorRestrictToMutants_RestrictToZeroMutants,
ReconnectorRestrictToMutants_RestrictToLessComplexMutants,
ReconnectorRestrictToMutants_RestrictToMoreComplexMutants,
ReconnectorRestrictToMutants_Count
};

Expand Down
124 changes: 124 additions & 0 deletions source/EngineTests/ReconnectorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,130 @@ TEST_F(ReconnectorTests, establishConnection_restrictToRespawned_failed)
EXPECT_EQ(0, actualTargetCell.connections.size());
}

TEST_F(ReconnectorTests, establishConnection_restrictToLessComplexMutants_success)
{
DataDescription data;
data.addCells({
CellDescription()
.setId(1)
.setPos({10.0f, 10.0f})
.setMutationId(5)
.setGenomeComplexity(1000)
.setMaxConnections(2)
.setExecutionOrderNumber(0)
.setInputExecutionOrderNumber(5)
.setCellFunction(ReconnectorDescription().setRestrictToMutants(ReconnectorRestrictToMutants_RestrictToLessComplexMutants)),
CellDescription()
.setId(2)
.setPos({11.0f, 10.0f})
.setMaxConnections(1)
.setExecutionOrderNumber(5)
.setCellFunction(NerveDescription())
.setActivity({1, 0, 0, 0, 0, 0, 0, 0}),
CellDescription().setId(3).setPos({9.0f, 10.0f}).setMutationId(1).setGenomeComplexity(999),
});
data.addConnection(1, 2);

_simController->setSimulationData(data);
_simController->calcTimesteps(1);

auto actualData = _simController->getSimulationData();
EXPECT_TRUE(hasConnection(actualData, 1, 3));
}

TEST_F(ReconnectorTests, establishConnection_restrictToLessComplexMutants_failed)
{
DataDescription data;
data.addCells({
CellDescription()
.setId(1)
.setPos({10.0f, 10.0f})
.setMutationId(5)
.setGenomeComplexity(1000)
.setMaxConnections(2)
.setExecutionOrderNumber(0)
.setInputExecutionOrderNumber(5)
.setCellFunction(ReconnectorDescription().setRestrictToMutants(ReconnectorRestrictToMutants_RestrictToLessComplexMutants)),
CellDescription()
.setId(2)
.setPos({11.0f, 10.0f})
.setMaxConnections(1)
.setExecutionOrderNumber(5)
.setCellFunction(NerveDescription())
.setActivity({1, 0, 0, 0, 0, 0, 0, 0}),
CellDescription().setId(3).setPos({9.0f, 10.0f}).setMutationId(1).setGenomeComplexity(1001),
});
data.addConnection(1, 2);

_simController->setSimulationData(data);
_simController->calcTimesteps(1);

auto actualData = _simController->getSimulationData();
EXPECT_FALSE(hasConnection(actualData, 1, 3));
}

TEST_F(ReconnectorTests, establishConnection_restrictToMoreComplexMutants_success)
{
DataDescription data;
data.addCells({
CellDescription()
.setId(1)
.setPos({10.0f, 10.0f})
.setMutationId(5)
.setGenomeComplexity(1000)
.setMaxConnections(2)
.setExecutionOrderNumber(0)
.setInputExecutionOrderNumber(5)
.setCellFunction(ReconnectorDescription().setRestrictToMutants(ReconnectorRestrictToMutants_RestrictToMoreComplexMutants)),
CellDescription()
.setId(2)
.setPos({11.0f, 10.0f})
.setMaxConnections(1)
.setExecutionOrderNumber(5)
.setCellFunction(NerveDescription())
.setActivity({1, 0, 0, 0, 0, 0, 0, 0}),
CellDescription().setId(3).setPos({9.0f, 10.0f}).setMutationId(1).setGenomeComplexity(1001),
});
data.addConnection(1, 2);

_simController->setSimulationData(data);
_simController->calcTimesteps(1);

auto actualData = _simController->getSimulationData();
EXPECT_TRUE(hasConnection(actualData, 1, 3));
}

TEST_F(ReconnectorTests, establishConnection_restrictToMoreComplexMutants_failed)
{
DataDescription data;
data.addCells({
CellDescription()
.setId(1)
.setPos({10.0f, 10.0f})
.setMutationId(5)
.setGenomeComplexity(1000)
.setMaxConnections(2)
.setExecutionOrderNumber(0)
.setInputExecutionOrderNumber(5)
.setCellFunction(ReconnectorDescription().setRestrictToMutants(ReconnectorRestrictToMutants_RestrictToMoreComplexMutants)),
CellDescription()
.setId(2)
.setPos({11.0f, 10.0f})
.setMaxConnections(1)
.setExecutionOrderNumber(5)
.setCellFunction(NerveDescription())
.setActivity({1, 0, 0, 0, 0, 0, 0, 0}),
CellDescription().setId(3).setPos({9.0f, 10.0f}).setMutationId(1).setGenomeComplexity(1000),
});
data.addConnection(1, 2);

_simController->setSimulationData(data);
_simController->calcTimesteps(1);

auto actualData = _simController->getSimulationData();
EXPECT_FALSE(hasConnection(actualData, 1, 3));
}

TEST_F(ReconnectorTests, deleteConnections_success)
{
DataDescription data;
Expand Down
Loading

0 comments on commit 73c6296

Please sign in to comment.