Add phenotype-derived cellular motion proposal
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m58s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 6s
Foundation / checks (client) (push) Canceled after 2m14s
Foundation / checks (checks) (push) Canceled after 2m17s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m58s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 6s
Foundation / checks (client) (push) Canceled after 2m14s
Foundation / checks (checks) (push) Canceled after 2m17s
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
#include "evo/biology/cellular_motion.hpp"
|
||||
|
||||
#include "evo/accounting/rates.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
constexpr std::uint64_t rate_period_microseconds = 1'000'000;
|
||||
|
||||
std::uint64_t integer_sqrt(std::uint64_t value) noexcept {
|
||||
std::uint64_t result{}, bit = std::uint64_t{1} << 62U;
|
||||
while (bit > value)
|
||||
bit >>= 2U;
|
||||
while (bit != 0) {
|
||||
if (value >= result + bit) {
|
||||
value -= result + bit;
|
||||
result = (result >> 1U) + bit;
|
||||
} else {
|
||||
result >>= 1U;
|
||||
}
|
||||
bit >>= 2U;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::uint64_t integer_sqrt_ceil(std::uint64_t value) noexcept {
|
||||
const auto floor = integer_sqrt(value);
|
||||
return floor * floor == value ? floor : floor + 1;
|
||||
}
|
||||
|
||||
bool valid(CellularIntent intent) noexcept {
|
||||
if (intent.x < -cellular_intent_component_limit || intent.x > cellular_intent_component_limit ||
|
||||
intent.y < -cellular_intent_component_limit || intent.y > cellular_intent_component_limit)
|
||||
return false;
|
||||
const auto x = static_cast<std::int64_t>(intent.x);
|
||||
const auto y = static_cast<std::int64_t>(intent.y);
|
||||
return x * x + y * y <=
|
||||
std::int64_t{cellular_intent_component_limit} * cellular_intent_component_limit;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, CellularMotionError>
|
||||
movement_cost(std::uint64_t full_cost, std::uint64_t distance,
|
||||
std::uint64_t full_distance) noexcept {
|
||||
if (distance == 0 || full_cost == 0)
|
||||
return std::uint64_t{};
|
||||
if (full_distance == 0 || distance > full_distance)
|
||||
return std::unexpected{CellularMotionError::overflow};
|
||||
const auto whole = full_cost / full_distance;
|
||||
const auto remainder = full_cost % full_distance;
|
||||
const auto base = whole * distance; // <= full_cost by construction.
|
||||
const auto product = remainder * distance; // Both are bounded by uint32 speed per tick.
|
||||
const auto fractional = product / full_distance + (product % full_distance != 0 ? 1 : 0);
|
||||
if (fractional > std::numeric_limits<std::uint64_t>::max() - base)
|
||||
return std::unexpected{CellularMotionError::overflow};
|
||||
return base + fractional;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularMotionProposal, CellularMotionError>
|
||||
propose_cellular_motion(CellularArea area, CellularPosition position, std::uint32_t extent_um,
|
||||
CellularIntent intent, const MetabolicBudget& budget) noexcept {
|
||||
if (area.width_um == 0 || area.height_um == 0 ||
|
||||
area.width_um > cellular_motion_axis_limit_um ||
|
||||
area.height_um > cellular_motion_axis_limit_um)
|
||||
return std::unexpected{CellularMotionError::invalid_area};
|
||||
if (extent_um == 0 || extent_um > area.width_um || extent_um > area.height_um)
|
||||
return std::unexpected{CellularMotionError::invalid_extent};
|
||||
if (position.x_um > area.width_um - extent_um || position.y_um > area.height_um - extent_um)
|
||||
return std::unexpected{CellularMotionError::outside_area};
|
||||
if (!valid(intent))
|
||||
return std::unexpected{CellularMotionError::invalid_intent};
|
||||
if (budget.rate_microseconds != rate_period_microseconds)
|
||||
return std::unexpected{CellularMotionError::unsupported_rate_period};
|
||||
|
||||
const auto max_step =
|
||||
accounting::per_tick(budget.phenotype.movement_speed, cellular_motion_tick_microseconds,
|
||||
accounting::Rounding::down);
|
||||
const auto full_cost = accounting::per_tick(
|
||||
budget.movement.value, cellular_motion_tick_microseconds, accounting::Rounding::up);
|
||||
if (!max_step || !full_cost)
|
||||
return std::unexpected{CellularMotionError::overflow};
|
||||
if (*max_step > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()) /
|
||||
cellular_intent_component_limit)
|
||||
return std::unexpected{CellularMotionError::overflow};
|
||||
|
||||
const auto dx = static_cast<std::int64_t>(intent.x) * static_cast<std::int64_t>(*max_step) /
|
||||
cellular_intent_component_limit;
|
||||
const auto dy = static_cast<std::int64_t>(intent.y) * static_cast<std::int64_t>(*max_step) /
|
||||
cellular_intent_component_limit;
|
||||
const auto max_x = area.width_um - extent_um;
|
||||
const auto max_y = area.height_um - extent_um;
|
||||
const auto next_x =
|
||||
static_cast<std::uint64_t>(dx < 0 ? (static_cast<std::uint64_t>(-dx) > position.x_um
|
||||
? 0
|
||||
: static_cast<std::int64_t>(position.x_um) + dx)
|
||||
: (dx > static_cast<std::int64_t>(max_x - position.x_um)
|
||||
? max_x
|
||||
: static_cast<std::int64_t>(position.x_um) + dx));
|
||||
const auto next_y =
|
||||
static_cast<std::uint64_t>(dy < 0 ? (static_cast<std::uint64_t>(-dy) > position.y_um
|
||||
? 0
|
||||
: static_cast<std::int64_t>(position.y_um) + dy)
|
||||
: (dy > static_cast<std::int64_t>(max_y - position.y_um)
|
||||
? max_y
|
||||
: static_cast<std::int64_t>(position.y_um) + dy));
|
||||
const auto moved_x = next_x >= position.x_um ? next_x - position.x_um : position.x_um - next_x;
|
||||
const auto moved_y = next_y >= position.y_um ? next_y - position.y_um : position.y_um - next_y;
|
||||
const auto distance = integer_sqrt_ceil(moved_x * moved_x + moved_y * moved_y);
|
||||
const auto cost = movement_cost(*full_cost, distance, *max_step);
|
||||
if (!cost)
|
||||
return std::unexpected{cost.error()};
|
||||
return CellularMotionProposal{{next_x, next_y}, distance, {*cost}};
|
||||
}
|
||||
|
||||
bool cellular_footprints_contact(CellularPosition a, std::uint32_t extent_a_um, CellularPosition b,
|
||||
std::uint32_t extent_b_um) noexcept {
|
||||
if (extent_a_um == 0 || extent_b_um == 0)
|
||||
return false;
|
||||
const auto axis_touches = [](std::uint64_t a_start, std::uint32_t a_extent,
|
||||
std::uint64_t b_start, std::uint32_t b_extent) {
|
||||
return a_start <= b_start ? b_start - a_start <= a_extent : a_start - b_start <= b_extent;
|
||||
};
|
||||
return axis_touches(a.x_um, extent_a_um, b.x_um, extent_b_um) &&
|
||||
axis_touches(a.y_um, extent_a_um, b.y_um, extent_b_um);
|
||||
}
|
||||
} // namespace evo::biology
|
||||
Reference in New Issue
Block a user