Skip to content
14 changes: 7 additions & 7 deletions source/EngineImpl/DescConverterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,7 @@ GenomeDesc DescConverterService::createGenomeDesc(TOs const& to, int genomeIndex
result._id = genomeTO.id;
NumberGenerator::get().adaptMaxEntityId(genomeTO.id);
result._name = char64ToString(genomeTO.name);
result._lineageId = genomeTO.lineageId;
NumberGenerator::get().adaptMaxLineageId(genomeTO.lineageId);
result._prevLineageId = genomeTO.prevLineageId != 0 ? std::make_optional(genomeTO.prevLineageId) : std::nullopt;
result._frontAngle = genomeTO.frontAngle;
result._accumulatedMutations = genomeTO.accumulatedMutations;
result._resistanceToInjection = genomeTO.resistanceToInjection;
result._applyMetaMutations = genomeTO.applyMetaMutations;
for (int i = 0; i < 2; ++i) {
Expand Down Expand Up @@ -931,6 +927,10 @@ CreatureDesc DescConverterService::createCreatureDesc(TOs const& to, int creatur
result._generation = creatureTO.generation;
result._numCells = creatureTO.numCells;
result._mutationState = creatureTO.mutationState;
result._lineageId = creatureTO.lineageId;
NumberGenerator::get().adaptMaxLineageId(creatureTO.lineageId);
result._prevLineageId = creatureTO.prevLineageId != VALUE_NOT_SET_UINT32 ? std::make_optional(static_cast<int>(creatureTO.prevLineageId)) : std::nullopt;
result._accumulatedMutations = creatureTO.accumulatedMutations;
result._headUpdateId = creatureTO.headUpdateId;

return result;
Expand Down Expand Up @@ -967,10 +967,7 @@ void DescConverterService::convertGenomeToTO(

stringToChar64(genomeTO.name, genome._name);
genomeTO.id = genome._id;
genomeTO.lineageId = genome._lineageId;
genomeTO.prevLineageId = genome._prevLineageId.value_or(0);
genomeTO.frontAngle = genome._frontAngle;
genomeTO.accumulatedMutations = genome._accumulatedMutations;
genomeTO.resistanceToInjection = genome._resistanceToInjection;
genomeTO.applyMetaMutations = genome._applyMetaMutations;
for (int i = 0; i < 2; ++i) {
Expand Down Expand Up @@ -1246,6 +1243,9 @@ void DescConverterService::convertCreatureToTO(
creatureTO.headUpdateId = creatureDesc._headUpdateId;
creatureTO.numCells = creatureDesc._numCells;
creatureTO.mutationState = creatureDesc._mutationState;
creatureTO.lineageId = creatureDesc._lineageId;
creatureTO.prevLineageId = creatureDesc._prevLineageId.value_or(VALUE_NOT_SET_UINT32);
creatureTO.accumulatedMutations = creatureDesc._accumulatedMutations;
creatureTO.genomeArrayIndex = genomeTOIndexById.at(creatureDesc._genomeId);
}

Expand Down
3 changes: 3 additions & 0 deletions source/EngineInterface/Desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ struct CreatureDesc
MEMBER(CreatureDesc, int, numCells, 0);
MEMBER(CreatureDesc, uint64_t, genomeId, 0);
MEMBER(CreatureDesc, MutationState, mutationState, MutationState_Mutated);
MEMBER(CreatureDesc, int, lineageId, 0);
MEMBER(CreatureDesc, std::optional<int>, prevLineageId, std::nullopt);
MEMBER(CreatureDesc, float, accumulatedMutations, 0.0f);

// Process data
MEMBER(CreatureDesc, int, headUpdateId, 0);
Expand Down
6 changes: 3 additions & 3 deletions source/EngineInterface/DescEditService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ void DescEditService::randomizeCountdowns(Desc& description, int minValue, int m

void DescEditService::randomizeLineageIds(Desc& description) const
{
for (auto& genome : description._genomes) {
genome._lineageId = NumberGenerator::get().getRandomInt();
genome._prevLineageId = 0;
for (auto& creature : description._creatures) {
creature._lineageId = NumberGenerator::get().getRandomInt();
creature._prevLineageId = std::nullopt;
}
}

Expand Down
17 changes: 11 additions & 6 deletions source/EngineInterface/DescValidationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ void DescValidationService::validateAndCorrect(GenomeDesc& genome)
genome._frontAngle = Math::getNormalizedAngle(genome._frontAngle, -180.0f);

// Validate mutation rate fields
genome._lineageId = std::max(genome._lineageId, 0);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method void validateAndCorrect(ObjectDesc& object); should be void validateAndCorrect(ExtendedObjectDesc& object);. Then the removed validations here can be move to this place.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8023cc3: validateAndCorrect now takes ExtendedObjectDesc& and the creature lineage validation lives there.

if (genome._prevLineageId.has_value()) {
genome._prevLineageId = std::max(genome._prevLineageId.value(), 0);
}
genome._accumulatedMutations = std::max(genome._accumulatedMutations, 0.0f);
auto validateCellTypePropertiesMutation = [](CellTypePropertiesMutationDesc& mutation) {
mutation._nodeProbability = std::clamp(mutation._nodeProbability, 0.0f, 1.0f);
mutation._valueChangeSigma = std::clamp(mutation._valueChangeSigma, 0.0f, 1.0f);
Expand Down Expand Up @@ -303,11 +298,21 @@ void DescValidationService::validateAndCorrect(GenomeDesc& genome)
}
}

void DescValidationService::validateAndCorrect(ObjectDesc& object)
void DescValidationService::validateAndCorrect(ExtendedObjectDesc& extendedObject)
{
auto& object = extendedObject.object;
object._stiffness = std::clamp(object._stiffness, 0.0f, 1.0f);
object._color = std::clamp(object._color, 0, MAX_COLORS - 1);

if (extendedObject.creature.has_value()) {
auto& creature = extendedObject.creature.value();
creature._lineageId = std::max(creature._lineageId, 0);
if (creature._prevLineageId.has_value()) {
creature._prevLineageId = std::max(creature._prevLineageId.value(), 0);
}
creature._accumulatedMutations = std::max(creature._accumulatedMutations, 0.0f);
}

if (object.getObjectType() == ObjectType_Cell) {
auto& cell = object.getCellRef();
cell._usableEnergy = std::max(0.0f, cell._usableEnergy);
Expand Down
2 changes: 1 addition & 1 deletion source/EngineInterface/DescValidationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class DescValidationService

public:
void validateAndCorrect(GenomeDesc& genome);
void validateAndCorrect(ObjectDesc& object);
void validateAndCorrect(ExtendedObjectDesc& extendedObject);
};
1 change: 1 addition & 0 deletions source/EngineInterface/EngineConstants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

auto constexpr VALUE_NOT_SET_UINT64 = 0x7fffffffffffffff;
auto constexpr VALUE_NOT_SET_UINT32 = 0x7fffffff;
auto constexpr VALUE_NOT_SET_FLOAT = 1e16f;

auto constexpr MAX_OBJECT_CONNECTIONS = 6;
Expand Down
3 changes: 0 additions & 3 deletions source/EngineInterface/GenomeDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,7 @@ struct GenomeDesc
GenomeDesc id(uint64_t id);
MEMBER(GenomeDesc, std::string, name, "");
MEMBER(GenomeDesc, std::vector<GeneDesc>, genes, {})
MEMBER(GenomeDesc, int, lineageId, 0);
MEMBER(GenomeDesc, std::optional<int>, prevLineageId, std::nullopt);
MEMBER(GenomeDesc, float, frontAngle, 0.0f);
MEMBER(GenomeDesc, float, accumulatedMutations, 0.0f);
MEMBER(GenomeDesc, bool, resistanceToInjection, false);
MEMBER(GenomeDesc, bool, applyMetaMutations, true);

Expand Down
2 changes: 0 additions & 2 deletions source/EngineInterface/GenomeDescEditService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ namespace

void adaptGenomeAttributesForPreview(GenomeDesc& genome, bool detailSimulation)
{
genome._lineageId = 0;
genome._accumulatedMutations = 0;
genome._mutationRates._neuronMutations[0] = NeuronMutationDesc();
genome._mutationRates._neuronMutations[1] = NeuronMutationDesc();
genome._mutationRates._connectionMutations[0] = ConnectionMutationDesc();
Expand Down
2 changes: 0 additions & 2 deletions source/EngineInterface/GenomeDescHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,8 @@ struct std::hash<GenomeDesc>
hash_combine(seed, std::hash<GeneDesc>{}(gene));
}
hash_combine(seed, desc._frontAngle);
hash_combine(seed, desc._accumulatedMutations);
hash_combine(seed, desc._resistanceToInjection);
hash_combine(seed, desc._applyMetaMutations);
hash_combine(seed, desc._prevLineageId);
for (int i = 0; i < 2; ++i) {
hash_combine(seed, desc._mutationRates._neuronMutations[i]._nodeProbability);
hash_combine(seed, desc._mutationRates._neuronMutations[i]._weightChangeSigma);
Expand Down
2 changes: 1 addition & 1 deletion source/EngineKernels/AttackerProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ __device__ __inline__ void AttackerProcessor::processCell(SimulationData& data,
ParameterCalculator::calcParameter(cudaSimulationParameters.attackerFoodChainColorMatrix, data, object->pos, color, otherColor);

// Evaluate lineage
if (cell->creature->genome->isRelatedLineage(otherCell->creature->genome)) {
if (cell->creature->isRelatedLineage(otherCell->creature)) {
energyToTransfer *= (1.0f - cudaSimulationParameters.attackerRelatedLineageProtection.value[object->color]);
}

Expand Down
4 changes: 2 additions & 2 deletions source/EngineKernels/CommunicatorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ __device__ __inline__ void CommunicatorProcessor::processSender(SimulationData&
// Check lineage restriction
if (receiver.restrictToLineage != LineageRestriction_No) {
if (receiver.restrictToLineage == LineageRestriction_RelatedLineage) {
if (!object->typeData.cell.creature->genome->isRelatedLineage(otherObject->typeData.cell.creature->genome)) {
if (!object->typeData.cell.creature->isRelatedLineage(otherObject->typeData.cell.creature)) {
return false;
}
} else if (receiver.restrictToLineage == LineageRestriction_UnrelatedLineage) {
if (object->typeData.cell.creature->genome->isRelatedLineage(otherObject->typeData.cell.creature->genome)) {
if (object->typeData.cell.creature->isRelatedLineage(otherObject->typeData.cell.creature)) {
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions source/EngineKernels/DataAccessKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ namespace
}
auto& genomeTO = to.genomes[genomeTOIndex];
genomeTO.id = genome->id;
genomeTO.lineageId = genome->lineageId;
genomeTO.prevLineageId = genome->prevLineageId;
genomeTO.frontAngle = genome->frontAngle;
genomeTO.accumulatedMutations = genome->accumulatedMutations;
genomeTO.resistanceToInjection = genome->resistanceToInjection;
genomeTO.applyMetaMutations = genome->applyMetaMutations;
for (int i = 0; i < 2; ++i) {
Expand Down Expand Up @@ -293,6 +290,9 @@ namespace
creatureTO.generation = creature->generation;
creatureTO.numCells = creature->numCells;
creatureTO.mutationState = creature->mutationState;
creatureTO.lineageId = creature->lineageId;
creatureTO.prevLineageId = creature->prevLineageId;
creatureTO.accumulatedMutations = creature->accumulatedMutations;
creatureTO.headUpdateId = creature->headUpdateId;
creatureTO.genomeArrayIndex = creature->genome->genomeIndex;

Expand Down Expand Up @@ -1042,6 +1042,7 @@ __global__ void cudaAdaptNumberGenerator(CudaNumberGenerator numberGen, TOs to)
for (int index = partition.startIndex; index <= partition.endIndex; index += partition.step) {
auto const& creature = to.creatures[index];
maxIds.entityId = max(maxIds.entityId, creature.id);
maxIds.lineageId = max(maxIds.lineageId, creature.lineageId);
}
}
{
Expand All @@ -1050,7 +1051,6 @@ __global__ void cudaAdaptNumberGenerator(CudaNumberGenerator numberGen, TOs to)
for (int index = partition.startIndex; index <= partition.endIndex; index += partition.step) {
auto const& genome = to.genomes[index];
maxIds.entityId = max(maxIds.entityId, genome.id);
maxIds.lineageId = max(maxIds.lineageId, genome.lineageId);
}
}
numberGen.adaptMaxIds(maxIds);
Expand Down
4 changes: 4 additions & 0 deletions source/EngineKernels/EditKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ __global__ void cudaChangeObject(SimulationData data, TOs changeTO)
EntityFactory entityFactory;
entityFactory.init(&data);
entityFactory.changeObjectFromTO(changeTO, objectTO, object);
if (objectTO.type == ObjectType_Cell) {
auto const& creatureTO = changeTO.creatures[objectTO.typeData.cell.creatureIndex];

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a new method changeCreateFromTO (to be created in entityFactory)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8023cc3: added EntityFactory::changeCreatureFromTO and call it from cudaChangeObject.

entityFactory.changeCreatureFromTO(creatureTO, object->typeData.cell.creature);
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions source/EngineKernels/Entities.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,25 @@ struct Creature
Genome* genome;
MutationState mutationState;

uint32_t lineageId;
uint32_t prevLineageId;
float accumulatedMutations;

// Process data
uint32_t headUpdateId; // Will be updated regularly to trigger head updates

// Temporary data
uint64_t creatureIndex; // May be invalid

__device__ __inline__ bool isRelatedLineage(Creature* other)
{
if (prevLineageId != VALUE_NOT_SET_UINT32 && other->prevLineageId != VALUE_NOT_SET_UINT32) {
return lineageId == other->lineageId || lineageId == other->prevLineageId || prevLineageId == other->lineageId
|| prevLineageId == other->prevLineageId;
} else {
return lineageId == other->lineageId;
}
}
};

struct Solid
Expand Down
22 changes: 14 additions & 8 deletions source/EngineKernels/EntityFactory.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public:
__inline__ __device__ Genome* createGenomeFromTO(TOs const& to, int genomeIndex);
__inline__ __device__ Object* createObjectFromTO(TOs const& to, int objectIndex, Object* objectArray);
__inline__ __device__ void changeObjectFromTO(TOs const& to, ObjectTO const& objectTO, Object* object);
__inline__ __device__ void changeCreatureFromTO(CreatureTO const& creatureTO, Creature* creature);
__inline__ __device__ void changeEnergyFromTO(EnergyTO const& particleTO, Energy* particle);

__inline__ __device__ Energy* createEnergy(float energy, float2 const& pos, float2 const& vel, int color);
Expand Down Expand Up @@ -86,10 +87,7 @@ __inline__ __device__ Genome* EntityFactory::createGenomeFromTO(TOs const& to, i
auto genome = _data->entities.heap.getTypedSubArray<Genome>(1);
genomeTO.genomeIndexOnGpu = static_cast<uint64_t>(reinterpret_cast<uint8_t*>(genome) - _data->entities.heap.getArray());
genome->id = genomeTO.id;
genome->lineageId = genomeTO.lineageId;
genome->prevLineageId = genomeTO.prevLineageId;
genome->frontAngle = genomeTO.frontAngle;
genome->accumulatedMutations = genomeTO.accumulatedMutations;
genome->resistanceToInjection = genomeTO.resistanceToInjection;
genome->applyMetaMutations = genomeTO.applyMetaMutations;
for (int i = 0; i < 2; ++i) {
Expand Down Expand Up @@ -327,11 +325,7 @@ __inline__ __device__ Creature* EntityFactory::createCreatureFromTO(TOs const& t
creatureTO.creatureIndexOnGpu = static_cast<uint64_t>(reinterpret_cast<uint8_t*>(creature) - _data->entities.heap.getArray());

creature->id = creatureTO.id;
creature->ancestorId = creatureTO.ancestorId;
creature->generation = creatureTO.generation;
creature->numCells = creatureTO.numCells;
creature->mutationState = creatureTO.mutationState;
creature->headUpdateId = creatureTO.headUpdateId;
changeCreatureFromTO(creatureTO, creature);

auto const& genomeTO = to.genomes[creatureTO.genomeArrayIndex];
creature->genome = &_data->entities.heap.atType<Genome>(genomeTO.genomeIndexOnGpu);
Expand Down Expand Up @@ -609,6 +603,18 @@ __inline__ __device__ void EntityFactory::changeObjectFromTO(TOs const& to, Obje
}
}

__inline__ __device__ void EntityFactory::changeCreatureFromTO(CreatureTO const& creatureTO, Creature* creature)
{
creature->ancestorId = creatureTO.ancestorId;
creature->generation = creatureTO.generation;
creature->numCells = creatureTO.numCells;
creature->mutationState = creatureTO.mutationState;
creature->lineageId = creatureTO.lineageId;
creature->prevLineageId = creatureTO.prevLineageId;
creature->accumulatedMutations = creatureTO.accumulatedMutations;
creature->headUpdateId = creatureTO.headUpdateId;
}

__inline__ __device__ void EntityFactory::changeEnergyFromTO(EnergyTO const& particleTO, Energy* particle)
{
particle->energy = particleTO.energy;
Expand Down
13 changes: 0 additions & 13 deletions source/EngineKernels/Genome.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -426,24 +426,11 @@ struct Genome
int numGenes;
Gene* genes;

uint32_t lineageId;
uint32_t prevLineageId;
float frontAngle;
float accumulatedMutations;
bool resistanceToInjection;
bool applyMetaMutations;
MutationRates mutationRates;

// Temporary data
uint64_t genomeIndex; // May be invalid

__device__ __inline__ bool isRelatedLineage(Genome* other)
{
if (prevLineageId != 0 && other->prevLineageId != 0) {
return lineageId == other->lineageId || lineageId == other->prevLineageId || prevLineageId == other->lineageId
|| prevLineageId == other->prevLineageId;
} else {
return lineageId == other->lineageId;
}
}
};
3 changes: 0 additions & 3 deletions source/EngineKernels/GenomeTO.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,7 @@ struct GenomeTO
int numGenes;
uint64_t geneArrayIndex;

uint32_t lineageId;
uint32_t prevLineageId;
float frontAngle;
float accumulatedMutations;
bool resistanceToInjection;
bool applyMetaMutations;
MutationRatesTO mutationRates;
Expand Down
4 changes: 2 additions & 2 deletions source/EngineKernels/GeometryKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace
float b = fminf(1.0f, fmaxf(0.0f, color.b));
float h, s, v;
rgbToHsv(r, g, b, h, s, v);
auto lineageId = object->typeData.cell.creature->genome->lineageId;
auto lineageId = object->typeData.cell.creature->lineageId;
uint32_t hash = lineageId * 2654435761u;
float hueOffset = (static_cast<float>(hash & 0xFFFFu) / 65535.0f) * 0.2f - 0.1f;
h += hueOffset;
Expand All @@ -149,7 +149,7 @@ namespace
if (object->type != ObjectType_Cell) {
return getCustomizationColor(object->color);
}
auto lineageId = object->typeData.cell.creature->genome->lineageId;
auto lineageId = object->typeData.cell.creature->lineageId;
uint32_t hash1 = lineageId * 2654435761u;
uint32_t hash2 = lineageId * 2246822519u;
uint32_t hash3 = lineageId * 3266489917u;
Expand Down
Loading
Loading