Skip to content

Commit

Permalink
+ combined coloring (mutation and cell function) restored
Browse files Browse the repository at this point in the history
+ prevent attacking offspring with different mutationId
+ inactive max age does not apply for cells with zero mutation id
+ destroy offspring if construction failed
+ exception during shutdown fixed
  • Loading branch information
chrxh committed May 21, 2024
1 parent e65d486 commit d429cc9
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 25 deletions.
20 changes: 19 additions & 1 deletion source/EngineGpuKernels/AttackerProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,33 @@ __device__ __inline__ void AttackerProcessor::processCell(SimulationData& data,
if (cell->creatureId == 0 && CellConnectionProcessor::isConnectedConnected(cell, otherCell)) {
return;
}
if (otherCell->barrier /*&& otherCell->livingState != LivingState_UnderConstruction*/) {
if (otherCell->barrier) {
return;
}
//if (otherCell->livingState == LivingState_UnderConstruction) {
// return;
//}

auto energyToTransfer = (atomicAdd(&otherCell->energy, 0) - baseValue) * cudaSimulationParameters.cellFunctionAttackerStrength[cell->color];
if (energyToTransfer < 0) {
return;
}

if (otherCell->livingState == LivingState_UnderConstruction) {
for (int i = 0; i < otherCell->numConnections; ++i) {
auto connectedCell = otherCell->connections[i].cell;
if (connectedCell->creatureId != 0 && connectedCell->creatureId == cell->creatureId) {
return;
}
for (int j = 0; j < connectedCell->numConnections; ++j) {
auto otherConnectedCell = connectedCell->connections[j].cell;
if (otherConnectedCell->creatureId != 0 && otherConnectedCell->creatureId == cell->creatureId) {
return;
}
}
}
}

auto color = calcMod(cell->color, MAX_COLORS);
auto otherColor = calcMod(otherCell->color, MAX_COLORS);

Expand Down
8 changes: 7 additions & 1 deletion source/EngineGpuKernels/CellProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ __inline__ __device__ void CellProcessor::checkConnections(SimulationData& data)
}
if (scheduleForDestruction) {
CellConnectionProcessor::scheduleDeleteAllConnections(data, cell);
atomicCAS(&cell->livingState, LivingState_UnderConstruction, LivingState_Dying);
}
}
}
Expand Down Expand Up @@ -733,6 +734,10 @@ __inline__ __device__ void CellProcessor::decay(SimulationData& data)
if (cell->livingState == LivingState_Dying) {
if (data.numberGen1.random() < cudaSimulationParameters.clusterDecayProb[cell->color]) {
CellConnectionProcessor::scheduleDeleteCell(data, index);

//for (int i = 0; i < cell->numConnections; ++i) {
// atomicCAS(&cell->connections[i].cell->livingState, LivingState_UnderConstruction, LivingState_Dying);
//}
}
}

Expand All @@ -741,10 +746,11 @@ __inline__ __device__ void CellProcessor::decay(SimulationData& data)
cellDestruction = true;
} else if (cell->energy > cellMaxBindingEnergy) {
CellConnectionProcessor::scheduleDeleteAllConnections(data, cell);
atomicCAS(&cell->livingState, LivingState_UnderConstruction, LivingState_Dying);
}

auto cellMaxAge = cudaSimulationParameters.cellMaxAge[cell->color];
if (cudaSimulationParameters.features.cellAgeLimiter && cudaSimulationParameters.cellInactiveMaxAgeActivated
if (cudaSimulationParameters.features.cellAgeLimiter && cudaSimulationParameters.cellInactiveMaxAgeActivated && cell->mutationId != 0
&& cell->cellFunctionUsed == CellFunctionUsed_No && cell->livingState == LivingState_Ready && cell->activationTime == 0) {
bool adjacentCellsUsed = false;
for (int i = 0; i < cell->numConnections; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion source/EngineGpuKernels/ConstructorProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
bool adaptReferenceAngle = false;
if (!constructionData.isLastNodeOfLastRepetition || !constructionData.genomeHeader.separateConstruction) {

//move connection
//move connection between lastConstructionCell and hostCell to a connection between newCell and hostCell
auto distance = constructionData.isLastNodeOfLastRepetition && !constructionData.genomeHeader.separateConstruction
? 1.0f
: cudaSimulationParameters.cellFunctionConstructorOffspringDistance[hostCell->color];
Expand Down Expand Up @@ -515,6 +515,7 @@ __inline__ __device__ bool ConstructorProcessor::continueConstruction(
adaptReferenceAngle = false;
CellConnectionProcessor::scheduleDeleteCell(data, cellPointerIndex);
hostCell->livingState = LivingState_Dying;
constructionData.lastConstructionCell->livingState = LivingState_Dying;
}

Math::normalize(posDelta);
Expand Down
19 changes: 10 additions & 9 deletions source/EngineGpuKernels/RenderingKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace
return {toInt((r_ + m) * 255), toInt((g_ + m) * 255), toInt((b_ + m) * 255)};
}

__device__ __inline__ float3 calcColor(Cell* cell, int selected, CellColoring cellColoring)
__device__ __inline__ float3 calcColor(Cell* cell, int selected, CellColoring cellColoring, bool primary)
{
float factor = max(20.0f, min(300.0f, cell->energy)) / 320.0f;
if (1 == selected) {
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace
}
}
}
if (cellColoring == CellColoring_MutationId) {
if (cellColoring == CellColoring_MutationId || (cellColoring == CellColoring_MutationId_AllCellFunctions && primary)) {
auto h = abs(toInt((cell->mutationId * 12107) % 360));
auto s = 0.6f + toFloat(abs(toInt(cell->mutationId * 12107)) % 400) / 1000;
auto rgb = convertHSVtoRGB(toFloat(h), s, 1.0f);
Expand All @@ -139,16 +139,16 @@ namespace
if (cellColoring == CellColoring_LivingState) {
switch (cell->livingState) {
case LivingState_Ready:
cellColor = 0x0000ff;
cellColor = 0x1010ff;
break;
case LivingState_UnderConstruction:
cellColor = 0x00ff00;
cellColor = 0x10ff10;
break;
case LivingState_Activating:
cellColor = 0xffffff;
break;
case LivingState_Dying:
cellColor = 0xff0000;
cellColor = 0xff1010;
break;
default:
cellColor = 0x000000;
Expand All @@ -172,7 +172,7 @@ namespace
}
}

if (cellColoring == CellColoring_AllCellFunction) {
if (cellColoring == CellColoring_AllCellFunctions || (cellColoring == CellColoring_MutationId_AllCellFunctions && !primary)) {
auto h = (toFloat(cell->cellFunction) / toFloat(CellFunction_Count - 1)) * 360.0f;
auto rgb = convertHSVtoRGB(toFloat(h), 0.7f, 1.0f);
cellColor = (rgb.x << 16) | (rgb.y << 8) | rgb.z;
Expand Down Expand Up @@ -421,12 +421,13 @@ __global__ void cudaDrawCells(uint64_t timestep, int2 worldSize, float2 rectUppe
if (isContainedInRect({0, 0}, toFloat2(imageSize), cellImagePos)) {

//draw primary color for cell
auto primaryColor = calcColor(cell, cell->selected, coloring) * 0.85f;
auto primaryColor = calcColor(cell, cell->selected, coloring, true) * 0.85f;
drawCircle(imageData, imageSize, cellImagePos, primaryColor * 0.45f, zoom / 2.5f, false, false);

//draw secondary color for cell
auto radius = zoom / 4;
drawCircle(imageData, imageSize, cellImagePos, primaryColor * 0.6f, radius, shadedCells, true);
auto secondaryColor = calcColor(cell, cell->selected, coloring, false) * 0.5f;
drawCircle(imageData, imageSize, cellImagePos, secondaryColor, radius, shadedCells, true);

//draw activity
if (cell->isActive() && zoom >= cudaSimulationParameters.zoomLevelNeuronalActivity) {
Expand Down Expand Up @@ -558,7 +559,7 @@ __global__ void cudaDrawCellGlow(int2 worldSize, float2 rectUpperLeft, Array<Cel

//draw background for cell
if (threadIdx.x == 0) {
color = calcColor(cell, cell->selected, coloring);
color = calcColor(cell, cell->selected, coloring, true);
}
__syncthreads();

Expand Down
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/SimulationKernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ __global__ void cudaNextTimestep_cellFunction_detonator(SimulationData data, Sim
DetonatorProcessor::process(data, statistics);
}

__global__ void cudaNextTimestep_physics_substep7_innerFriction(SimulationData data)
__global__ void cudaNextTimestep_physics_applyInnerFriction(SimulationData data)
{
CellProcessor::applyInnerFriction(data);
}

__global__ void cudaNextTimestep_physics_substep8(SimulationData data)
__global__ void cudaNextTimestep_physics_applyFriction(SimulationData data)
{
CellFunctionProcessor::resetFetchedActivities(data);
CellProcessor::applyFriction(data);
Expand Down
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/SimulationKernels.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ __global__ void cudaNextTimestep_cellFunction_muscle(SimulationData data, Simula
__global__ void cudaNextTimestep_cellFunction_sensor(SimulationData data, SimulationStatistics statistics);
__global__ void cudaNextTimestep_cellFunction_reconnector(SimulationData data, SimulationStatistics statistics);
__global__ void cudaNextTimestep_cellFunction_detonator(SimulationData data, SimulationStatistics statistics);
__global__ void cudaNextTimestep_physics_substep7_innerFriction(SimulationData data);
__global__ void cudaNextTimestep_physics_substep8(SimulationData data);
__global__ void cudaNextTimestep_physics_applyInnerFriction(SimulationData data);
__global__ void cudaNextTimestep_physics_applyFriction(SimulationData data);
__global__ void cudaNextTimestep_structuralOperations_substep1(SimulationData data);
__global__ void cudaNextTimestep_structuralOperations_substep2(SimulationData data);
__global__ void cudaNextTimestep_structuralOperations_substep3(SimulationData data);
Expand Down
6 changes: 3 additions & 3 deletions source/EngineGpuKernels/SimulationKernelsLauncher.cu
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void _SimulationKernelsLauncher::calcTimestep(Settings const& settings, Simulati
bool considerRigidityUpdate = (data.timestep % 3 == 0);

KERNEL_CALL(cudaNextTimestep_physics_init, data);
KERNEL_CALL(cudaNextTimestep_physics_fillMaps, data);
cudaNextTimestep_physics_fillMaps<<<gpuSettings.numBlocks, 32>>>(data);
if (settings.simulationParameters.motionType == MotionType_Fluid) {
auto threads = calcOptimalThreadsForFluidKernel(settings.simulationParameters);
cudaNextTimestep_physics_calcFluidForces<<<gpuSettings.numBlocks, threads>>>(data);
Expand Down Expand Up @@ -69,9 +69,9 @@ void _SimulationKernelsLauncher::calcTimestep(Settings const& settings, Simulati
KERNEL_CALL(cudaNextTimestep_cellFunction_detonator, data, statistics);

if (considerInnerFriction) {
KERNEL_CALL(cudaNextTimestep_physics_substep7_innerFriction, data);
KERNEL_CALL(cudaNextTimestep_physics_applyInnerFriction, data);
}
KERNEL_CALL(cudaNextTimestep_physics_substep8, data);
KERNEL_CALL(cudaNextTimestep_physics_applyFriction, data);

if (considerRigidityUpdate && isRigidityUpdateEnabled(settings)) {
KERNEL_CALL(cudaInitClusterData, data);
Expand Down
3 changes: 2 additions & 1 deletion source/EngineInterface/SimulationParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ enum CellColoring_
CellColoring_None,
CellColoring_CellColor,
CellColoring_MutationId,
CellColoring_MutationId_AllCellFunctions,
CellColoring_LivingState,
CellColoring_GenomeSize,
CellColoring_CellFunction,
CellColoring_AllCellFunction
CellColoring_AllCellFunctions
};

struct SimulationParameters
Expand Down
3 changes: 2 additions & 1 deletion source/EngineInterface/StatisticsHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
StatisticsHistoryData StatisticsHistory::getCopiedData() const
{
std::lock_guard lock(_mutex);
return _data;
auto copy = _data;
return copy;
}

std::mutex& StatisticsHistory::getMutex() const
Expand Down
18 changes: 14 additions & 4 deletions source/Gui/SimulationParametersWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@ void _SimulationParametersWindow::processBase(
{"None",
"Standard cell colors",
"Mutants",
"Mutants and cell functions",
"Cell states",
"Genome complexities",
"Single cell function",
"All cell functions"})
.tooltip("Here, one can set how the cells are to be colored during rendering. \n\n"
ICON_FA_CHEVRON_RIGHT " Standard cell colors: Each cell is assigned one of 7 default colors, which is displayed with this option. \n\n"
ICON_FA_CHEVRON_RIGHT " Mutants: Different mutants are represented by different colors (only larger structural mutations such as translations or duplications are taken into account).\n\n"
ICON_FA_CHEVRON_RIGHT " Mutants and cell functions: Combination of mutants and cell function coloring.\n\n"
ICON_FA_CHEVRON_RIGHT " Cell states: green = under construction, blue = ready, red = dying\n\n"
ICON_FA_CHEVRON_RIGHT " Genome complexities: This property can be utilized by attacker cells when the parameter 'Complex genome protection' is "
"activated (see tooltip there). The coloring is as follows: blue = creature with low bonus (usually small or simple genome structure), red = large bonus\n\n"
Expand Down Expand Up @@ -1479,7 +1481,15 @@ void _SimulationParametersWindow::processBase(
.name("Coloring")
.textWidth(RightColumnWidth)
.defaultValue(origParameters.cellGlowColoring)
.values({"None", "Standard cell colors", "Mutants", "Cell states", "Genome complexities", "Single cell function", "All cell functions"})
.values(
{"None",
"Standard cell colors",
"Mutants",
"Mutants and cell functions",
"Cell states",
"Genome complexities",
"Single cell function",
"All cell functions"})
.tooltip(""),
parameters.cellGlowColoring);
AlienImGui::SliderFloat(
Expand Down Expand Up @@ -2187,6 +2197,9 @@ void _SimulationParametersWindow::processAddonList(
.defaultValue(origParameters.features.cellColorTransitionRules)
.tooltip("This can be used to define color transitions for cells depending on their age."),
parameters.features.cellColorTransitionRules);
AlienImGui::Checkbox(
AlienImGui::CheckboxParameters().name("Cell glow").textWidth(0).defaultValue(origParameters.features.cellGlow).tooltip(""),
parameters.features.cellGlow);
AlienImGui::Checkbox(
AlienImGui::CheckboxParameters()
.name("External energy control")
Expand All @@ -2205,9 +2218,6 @@ void _SimulationParametersWindow::processAddonList(
"and 'Advanced attacker control' to favor more complex genomes in natural selection. If it is deactivated, default values are "
"used that simply take the genome size into account."),
parameters.features.genomeComplexityMeasurement);
AlienImGui::Checkbox(
AlienImGui::CheckboxParameters().name("Secondary cell rendering").textWidth(0).defaultValue(origParameters.features.cellGlow).tooltip(""),
parameters.features.cellGlow);
}
ImGui::EndChild();
AlienImGui::EndTreeNode();
Expand Down

0 comments on commit d429cc9

Please sign in to comment.