Skip to content

Commit

Permalink
+ offspringConstructionId is generated and passed to offspring when s…
Browse files Browse the repository at this point in the history
…elf-replication starts

+ origGenomeSize introduced
+ construction bug fixed: genomeReadPosition is set correctly for offspring
  • Loading branch information
chrxh committed Jun 29, 2023
1 parent 458ce19 commit 76c2cfe
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 22 deletions.
13 changes: 8 additions & 5 deletions source/EngineGpuKernels/AttackerProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ __device__ __inline__ void AttackerProcessor::processCell(SimulationData& data,

Cell* someOtherCell = nullptr;
data.cellMap.executeForEach(cell->absPos, cudaSimulationParameters.cellFunctionAttackerRadius[cell->color], cell->detached, [&](auto const& otherCell) {
if (!isConnectedConnected(cell, otherCell) && !otherCell->barrier /*&& otherCell->livingState != LivingState_UnderConstruction*/) {
if (otherCell->constructionId != cell->constructionId && !otherCell->barrier /*&& otherCell->livingState != LivingState_UnderConstruction*/) {
auto energyToTransfer = (atomicAdd(&otherCell->energy, 0) - cellMinEnergy) * cudaSimulationParameters.cellFunctionAttackerStrength[cell->color];
if (energyToTransfer < 0) {
return;
}
//if (otherCell->origGenomeSize > cell->origGenomeSize) {
// return;
//}

auto velocityPenalty = Math::length(cell->vel) * 20 * cudaSimulationParameters.cellFunctionAttackerVelocityPenalty[cell->color] + 1.0f;
energyToTransfer /= velocityPenalty;
Expand Down Expand Up @@ -205,14 +208,14 @@ __device__ __inline__ void AttackerProcessor::distributeEnergy(SimulationData& d

for (int i = 0; i < cell->numConnections; ++i) {
auto connectedCell = cell->connections[i].cell;
if (cudaSimulationParameters.cellFunctionAttackerEnergyDistributionSameColor && connectedCell->color != cell->color) {
if (connectedCell->constructionId != cell->constructionId) {
continue;
}
atomicAdd(&connectedCell->energy, energyPerReceiver);
energyDelta -= energyPerReceiver;
for (int i = 0; i < connectedCell->numConnections; ++i) {
auto connectedConnectedCell = connectedCell->connections[i].cell;
if (cudaSimulationParameters.cellFunctionAttackerEnergyDistributionSameColor && connectedConnectedCell->color != cell->color) {
if (connectedConnectedCell->constructionId != cell->constructionId) {
continue;
}
atomicAdd(&connectedConnectedCell->energy, energyPerReceiver);
Expand All @@ -229,7 +232,7 @@ __device__ __inline__ void AttackerProcessor::distributeEnergy(SimulationData& d
}
if (otherCell->cellFunction == CellFunction_Constructor) {
auto isFinished = GenomeDecoder::isFinishedSingleConstruction(otherCell->cellFunctionData.constructor);
if (!isFinished && (!cudaSimulationParameters.cellFunctionAttackerEnergyDistributionSameColor || otherCell->color == cell->color)) {
if (!isFinished && otherCell->constructionId == cell->constructionId) {
return true;
}
}
Expand All @@ -240,7 +243,7 @@ __device__ __inline__ void AttackerProcessor::distributeEnergy(SimulationData& d
return false;
}
if (otherCell->cellFunction == CellFunction_Transmitter) {
if (!cudaSimulationParameters.cellFunctionAttackerEnergyDistributionSameColor || otherCell->color == cell->color) {
if (otherCell->constructionId == cell->constructionId) {
return true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions source/EngineGpuKernels/Cell.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct ConstructorFunction

//process data
uint64_t genomeReadPosition;
int offspringConstructionId; //will be filled when self-replication starts

//temp
bool isComplete;
Expand Down Expand Up @@ -146,6 +147,7 @@ struct Cell
CellFunctionData cellFunctionData;
Activity activity;
int activationTime;
int origGenomeSize;

CellMetadataDescription metadata;

Expand Down
26 changes: 17 additions & 9 deletions source/EngineGpuKernels/ConstructorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace
return false;
}
auto constructor = cell->cellFunctionData.constructor;
return constructor.genomeReadPosition < constructor.genomeSize;
return constructor.genomeReadPosition >= Const::GenomeHeaderSize && constructor.genomeReadPosition < constructor.genomeSize;
}
}

Expand Down Expand Up @@ -198,7 +198,8 @@ __inline__ __device__ bool ConstructorProcessor::isConstructionAtBeginning(Cell*

__inline__ __device__ bool ConstructorProcessor::isConstructionFinished(Cell* cell)
{
return cell->cellFunctionData.constructor.genomeReadPosition >= cell->cellFunctionData.constructor.genomeSize;
return cell->cellFunctionData.constructor.genomeReadPosition >= cell->cellFunctionData.constructor.genomeSize
|| cell->cellFunctionData.constructor.genomeSize <= Const::GenomeHeaderSize;
}

__inline__ __device__ bool
Expand Down Expand Up @@ -310,6 +311,8 @@ __inline__ __device__ Cell* ConstructorProcessor::getFirstCellOfConstructionSite
__inline__ __device__ bool
ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStatistics& statistics, Cell* hostCell, ConstructionData const& constructionData)
{
auto& constructor = hostCell->cellFunctionData.constructor;

if (!isConnectable(hostCell->numConnections, hostCell->maxConnections, true)) {
return false;
}
Expand All @@ -323,24 +326,28 @@ ConstructorProcessor::startNewConstruction(SimulationData& data, SimulationStati
return false;
}

if (cudaSimulationParameters.cellFunctionConstructorCheckCompletenessForSelfReplication
&& !hostCell->cellFunctionData.constructor.isComplete) {
if (cudaSimulationParameters.cellFunctionConstructorCheckCompletenessForSelfReplication && !constructor.isComplete) {
return false;
}

if (!checkAndReduceHostEnergy(data, hostCell, constructionData)) {
return false;
}

hostCell->constructionId = data.numberGen1.random(65535);
if (GenomeDecoder::containsSelfReplication(constructor)) {
constructor.offspringConstructionId = data.numberGen1.random(65535);
hostCell->origGenomeSize = GenomeDecoder::getNumNodesRecursively(constructor.genome, toInt(constructor.genomeSize));
} else {
hostCell->cellFunctionData.constructor.offspringConstructionId = hostCell->constructionId;
}

Cell* newCell = constructCellIntern(data, hostCell, newCellPos, 0, constructionData);

if (!newCell->tryLock()) {
return false;
}

if (!GenomeDecoder::isFinished(hostCell->cellFunctionData.constructor) || !constructionData.genomeHeader.separateConstruction) {
auto const& constructor = hostCell->cellFunctionData.constructor;
auto distance =
GenomeDecoder::isFinished(constructor) && !constructionData.genomeHeader.separateConstruction && constructionData.genomeHeader.singleConstruction
? 1.0f
Expand Down Expand Up @@ -392,7 +399,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
hostCell->detached,
[&](Cell* const& otherCell) {
if (otherCell == underConstructionCell || otherCell == hostCell || otherCell->livingState != LivingState_UnderConstruction
|| otherCell->constructionId != hostCell->constructionId) {
|| otherCell->constructionId != hostCell->cellFunctionData.constructor.offspringConstructionId) {
return false;
}
return true;
Expand Down Expand Up @@ -589,13 +596,14 @@ ConstructorProcessor::constructCellIntern(
result->numConnections = 0;
result->executionOrderNumber = constructionData.executionOrderNumber;
result->livingState = true;
result->constructionId = hostCell->constructionId;
result->constructionId = constructor.offspringConstructionId;
result->cellFunction = constructionData.cellFunction;
result->color = constructionData.color;
result->inputExecutionOrderNumber = constructionData.inputExecutionOrderNumber;
result->outputBlocked = constructionData.outputBlocked;

result->activationTime = constructor.constructionActivationTime;
result->origGenomeSize = hostCell->origGenomeSize;

switch (constructionData.cellFunction) {
case CellFunction_Neuron: {
Expand All @@ -614,7 +622,7 @@ ConstructorProcessor::constructCellIntern(
auto& newConstructor = result->cellFunctionData.constructor;
newConstructor.activationMode = GenomeDecoder::readByte(constructor);
newConstructor.constructionActivationTime = GenomeDecoder::readWord(constructor);
newConstructor.genomeReadPosition = 0;
newConstructor.genomeReadPosition = Const::GenomeHeaderSize;
newConstructor.constructionAngle1 = GenomeDecoder::readAngle(constructor);
newConstructor.constructionAngle2 = GenomeDecoder::readAngle(constructor);
GenomeDecoder::copyGenome(data, constructor, newConstructor);
Expand Down
2 changes: 2 additions & 0 deletions source/EngineGpuKernels/DataAccessKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace
cellTO.executionOrderNumber = cell->executionOrderNumber;
cellTO.livingState = cell->livingState;
cellTO.constructionId = cell->constructionId;
cellTO.origGenomeSize = cell->origGenomeSize;
cellTO.inputExecutionOrderNumber = cell->inputExecutionOrderNumber;
cellTO.outputBlocked = cell->outputBlocked;
cellTO.cellFunction = cell->cellFunction;
Expand Down Expand Up @@ -88,6 +89,7 @@ namespace
*dataTO.numAuxiliaryData,
dataTO.auxiliaryData);
cellTO.cellFunctionData.constructor.genomeReadPosition = cell->cellFunctionData.constructor.genomeReadPosition;
cellTO.cellFunctionData.constructor.offspringConstructionId = cell->cellFunctionData.constructor.offspringConstructionId;
cellTO.cellFunctionData.constructor.genomeGeneration = cell->cellFunctionData.constructor.genomeGeneration;
} break;
case CellFunction_Sensor: {
Expand Down
3 changes: 3 additions & 0 deletions source/EngineGpuKernels/ObjectFactory.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ __inline__ __device__ void ObjectFactory::changeCellFromTO(DataTO const& dataTO,
cell->age = cellTO.age;
cell->color = cellTO.color;
cell->activationTime = cellTO.activationTime;
cell->origGenomeSize = cellTO.origGenomeSize;

createAuxiliaryData(cellTO.metadata.nameSize, cellTO.metadata.nameDataIndex, dataTO.auxiliaryData, cell->metadata.nameSize, cell->metadata.name);

Expand Down Expand Up @@ -138,6 +139,7 @@ __inline__ __device__ void ObjectFactory::changeCellFromTO(DataTO const& dataTO,
cell->cellFunctionData.constructor.genomeSize,
cell->cellFunctionData.constructor.genome);
cell->cellFunctionData.constructor.genomeReadPosition = cellTO.cellFunctionData.constructor.genomeReadPosition;
cell->cellFunctionData.constructor.offspringConstructionId = cellTO.cellFunctionData.constructor.offspringConstructionId;
cell->cellFunctionData.constructor.genomeGeneration = cellTO.cellFunctionData.constructor.genomeGeneration;
cell->cellFunctionData.constructor.constructionAngle1 = cellTO.cellFunctionData.constructor.constructionAngle1;
cell->cellFunctionData.constructor.constructionAngle2 = cellTO.cellFunctionData.constructor.constructionAngle2;
Expand Down Expand Up @@ -246,6 +248,7 @@ __inline__ __device__ Cell* ObjectFactory::createRandomCell(float energy, float2
cell->barrier = false;
cell->age = 0;
cell->activationTime = 0;
cell->origGenomeSize = 0;
cell->inputExecutionOrderNumber = _data->numberGen1.random(cudaSimulationParameters.cellNumExecutionOrderNumbers - 1);
cell->outputBlocked = _data->numberGen1.randomBool();
for (int i = 0; i < MAX_CHANNELS; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/ParticleProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ __inline__ __device__ void ParticleProcessor::radiate(SimulationData& data, floa
}
pos += delta;
if (source.useAngle) {
vel = Math::unitVectorOfAngle(source.angle);
vel = Math::unitVectorOfAngle(source.angle) * data.numberGen1.random(0.5f, 1.0f);
} else {
vel = Math::normalized(delta) * data.numberGen1.random(0.5f, 1.0f);
}
Expand All @@ -174,7 +174,7 @@ __inline__ __device__ void ParticleProcessor::radiate(SimulationData& data, floa
delta.y = data.numberGen1.random() * rectangle.height - rectangle.height / 2;
pos += delta;
if (source.useAngle) {
vel = Math::unitVectorOfAngle(source.angle);
vel = Math::unitVectorOfAngle(source.angle) * data.numberGen1.random(0.5f, 1.0f);
} else {
auto roundSize = min(rectangle.width, rectangle.height) / 2;
float2 corner1{-rectangle.width / 2, -rectangle.height / 2};
Expand Down
2 changes: 2 additions & 0 deletions source/EngineGpuKernels/TOs.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct ConstructorTO

//process data
uint64_t genomeReadPosition;
int offspringConstructionId;
};

struct SensorTO
Expand Down Expand Up @@ -147,6 +148,7 @@ struct CellTO
CellFunctionTO cellFunctionData;
ActivityTO activity;
int activationTime;
int origGenomeSize;

CellMetadataTO metadata;

Expand Down
10 changes: 6 additions & 4 deletions source/EngineGpuKernels/TransmitterProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ __device__ __inline__ void TransmitterProcessor::distributeEnergy(SimulationData

for (int i = 0; i < cell->numConnections; ++i) {
auto connectedCell = cell->connections[i].cell;
if (cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor && connectedCell->color != cell->color) {
if (cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor && connectedCell->constructionId != cell->constructionId) {
continue;
}
atomicAdd(&connectedCell->energy, energyPerReceiver);

energyDelta -= energyPerReceiver;
for (int i = 0; i < connectedCell->numConnections; ++i) {
auto connectedConnectedCell = connectedCell->connections[i].cell;
if (cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor && connectedConnectedCell->color != cell->color) {
if (cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor
&& connectedConnectedCell->constructionId != cell->constructionId) {
continue;
}
atomicAdd(&connectedConnectedCell->energy, energyPerReceiver);
Expand All @@ -107,7 +108,8 @@ __device__ __inline__ void TransmitterProcessor::distributeEnergy(SimulationData
}
if (otherCell->cellFunction == CellFunction_Constructor) {
auto isFinished = GenomeDecoder::isFinishedSingleConstruction(otherCell->cellFunctionData.constructor);
if (!isFinished && (!cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor || otherCell->color == cell->color)) {
if (!isFinished
&& (!cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor || otherCell->constructionId == cell->constructionId)) {
return true;
}
}
Expand All @@ -118,7 +120,7 @@ __device__ __inline__ void TransmitterProcessor::distributeEnergy(SimulationData
return false;
}
if (otherCell->cellFunction == CellFunction_Transmitter) {
if (!cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor || otherCell->color == cell->color) {
if (!cudaSimulationParameters.cellFunctionTransmitterEnergyDistributionSameColor || otherCell->constructionId == cell->constructionId) {
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions source/EngineImpl/DescriptionConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ CellDescription DescriptionConverter::createCellDescription(DataTO const& dataTO
result.barrier = cellTO.barrier;
result.age = cellTO.age;
result.color = cellTO.color;
result.origGenomeSize = cellTO.origGenomeSize;

auto const& metadataTO = cellTO.metadata;
auto metadata = CellMetadataDescription();
Expand Down Expand Up @@ -423,6 +424,7 @@ CellDescription DescriptionConverter::createCellDescription(DataTO const& dataTO
constructor.constructionActivationTime = cellTO.cellFunctionData.constructor.constructionActivationTime;
convert(dataTO, cellTO.cellFunctionData.constructor.genomeSize, cellTO.cellFunctionData.constructor.genomeDataIndex, constructor.genome);
constructor.genomeReadPosition = toInt(cellTO.cellFunctionData.constructor.genomeReadPosition);
constructor.offspringConstructionId = cellTO.cellFunctionData.constructor.offspringConstructionId;
constructor.genomeGeneration = cellTO.cellFunctionData.constructor.genomeGeneration;
constructor.constructionAngle1 = cellTO.cellFunctionData.constructor.constructionAngle1;
constructor.constructionAngle2 = cellTO.cellFunctionData.constructor.constructionAngle2;
Expand Down Expand Up @@ -534,6 +536,7 @@ void DescriptionConverter::addCell(
constructorTO.constructionActivationTime = constructorDesc.constructionActivationTime;
convert(dataTO, constructorDesc.genome, constructorTO.genomeSize, constructorTO.genomeDataIndex);
constructorTO.genomeReadPosition = constructorDesc.genomeReadPosition;
constructorTO.offspringConstructionId = constructorDesc.offspringConstructionId;
constructorTO.genomeGeneration = constructorDesc.genomeGeneration;
constructorTO.constructionAngle1 = constructorDesc.constructionAngle1;
constructorTO.constructionAngle2 = constructorDesc.constructionAngle2;
Expand Down Expand Up @@ -598,6 +601,7 @@ void DescriptionConverter::addCell(
cellTO.barrier = cellDesc.barrier;
cellTO.age = cellDesc.age;
cellTO.color = cellDesc.color;
cellTO.origGenomeSize = cellDesc.origGenomeSize;
convert(dataTO, cellDesc.metadata.name, cellTO.metadata.nameSize, cellTO.metadata.nameDataIndex);
convert(dataTO, cellDesc.metadata.description, cellTO.metadata.descriptionSize, cellTO.metadata.descriptionDataIndex);
cellIndexTOByIds.insert_or_assign(cellTO.id, cellIndex);
Expand Down
2 changes: 2 additions & 0 deletions source/EngineInterface/Descriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct ConstructorDescription

//process data
int genomeReadPosition = 0;
int offspringConstructionId = 0;

ConstructorDescription();
auto operator<=>(ConstructorDescription const&) const = default;
Expand Down Expand Up @@ -293,6 +294,7 @@ struct CellDescription
CellFunctionDescription cellFunction;
ActivityDescription activity;
int activationTime = 0;
int origGenomeSize = 0;

CellMetadataDescription metadata;

Expand Down
4 changes: 2 additions & 2 deletions source/Gui/RadiationSourcesWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ void _RadiationSourcesWindow::processIntern()
AlienImGui::SliderFloatParameters()
.name("Angle")
.textWidth(RightColumnWidth)
.min(0)
.max(360.0f)
.min(-180.0f)
.max(180.0f)
.defaultEnabledValue(&origSource.useAngle)
.defaultValue(&origSource.angle)
.disabledValue(&source.angle)
Expand Down

0 comments on commit 76c2cfe

Please sign in to comment.