Add shared feeding community with atomic resource transfers
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
#include "evo/processes/feeding.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::processes {
|
||||
namespace {
|
||||
bool fits(const accounting::Inventory& amount, const accounting::Inventory& available) noexcept {
|
||||
if (amount.energy > available.energy) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t index = 0; index < 4; ++index) {
|
||||
if (amount.matter[index] > available.matter[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::uint64_t portion(std::uint64_t quantity, std::uint16_t efficiency) noexcept {
|
||||
return (quantity / 1000) * efficiency + (quantity % 1000) * efficiency / 1000;
|
||||
}
|
||||
accounting::ReservoirId reservoir(std::uint32_t actor) noexcept {
|
||||
return {actor == 1 ? 3U : actor + 3U};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
FeedingCommunity::FeedingCommunity(ProducerProcess producer, std::vector<ConsumerConfig> consumers)
|
||||
: producer_{std::move(producer)}, consumers_{std::move(consumers)},
|
||||
active_(consumers_.size() + 1, 1) {}
|
||||
|
||||
std::expected<FeedingCommunity, FeedingError>
|
||||
FeedingCommunity::create(ProducerConfig producer, accounting::Picojoules source,
|
||||
std::array<accounting::Femtograms, 4> nutrients,
|
||||
std::span<const ConsumerConfig> consumers) {
|
||||
if (consumers.empty() || consumers.size() > max_consumers ||
|
||||
std::ranges::any_of(consumers,
|
||||
[](const auto& c) { return c.assimilation_per_thousand > 1000; })) {
|
||||
return std::unexpected{FeedingError::invalid_config};
|
||||
}
|
||||
auto isolated = ProducerProcess::create(producer, source, nutrients);
|
||||
if (!isolated) {
|
||||
return std::unexpected{FeedingError::invalid_config};
|
||||
}
|
||||
std::vector<accounting::Inventory> opening{isolated->balances().begin(),
|
||||
isolated->balances().end()};
|
||||
for (const auto& consumer : consumers) {
|
||||
opening.push_back(consumer.opening);
|
||||
}
|
||||
opening.emplace_back(); // Waste is owned matter, not destroyed inventory.
|
||||
auto ledger = accounting::Ledger::create(opening);
|
||||
if (!ledger) {
|
||||
return std::unexpected{FeedingError::accounting_failure};
|
||||
}
|
||||
isolated->ledger_ = std::move(*ledger);
|
||||
return FeedingCommunity{std::move(*isolated), {consumers.begin(), consumers.end()}};
|
||||
}
|
||||
|
||||
std::expected<bool, FeedingError> FeedingCommunity::active(std::uint32_t id) const noexcept {
|
||||
if (id == 0 || id > active_.size()) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
}
|
||||
return active_[id - 1] != 0;
|
||||
}
|
||||
|
||||
std::expected<FeedReceipt, FeedingError> FeedingCommunity::feed(const FeedIntent& input) {
|
||||
if (input.consumer < 2 || input.consumer > active_.size() || input.target == 0 ||
|
||||
input.target > active_.size() || input.target == input.consumer) {
|
||||
return std::unexpected{FeedingError::invalid_actor};
|
||||
}
|
||||
if (active_[input.consumer - 1] == 0 || active_[input.target - 1] == 0) {
|
||||
return std::unexpected{FeedingError::inactive_actor};
|
||||
}
|
||||
const auto& config = consumers_[input.consumer - 2];
|
||||
if (input.amount == accounting::Inventory{} || !fits(input.amount, config.intake_limit)) {
|
||||
return std::unexpected{FeedingError::intake_limit};
|
||||
}
|
||||
const auto from = reservoir(input.target);
|
||||
if (!fits(input.amount, balances()[from.value - 1])) {
|
||||
return std::unexpected{FeedingError::insufficient_inventory};
|
||||
}
|
||||
accounting::Inventory assimilated{}, heat{}, waste{};
|
||||
assimilated.energy.value = portion(input.amount.energy.value, config.assimilation_per_thousand);
|
||||
heat.energy.value = input.amount.energy.value - assimilated.energy.value;
|
||||
for (std::size_t index = 0; index < 4; ++index) {
|
||||
assimilated.matter[index].value =
|
||||
portion(input.amount.matter[index].value, config.assimilation_per_thousand);
|
||||
waste.matter[index].value =
|
||||
input.amount.matter[index].value - assimilated.matter[index].value;
|
||||
}
|
||||
const std::array legs{
|
||||
accounting::Transfer{from, reservoir(input.consumer), assimilated},
|
||||
accounting::Transfer{from, {4}, heat},
|
||||
accounting::Transfer{from, {static_cast<std::uint32_t>(balances().size())}, waste}};
|
||||
auto& ledger = producer_.ledger_;
|
||||
if (ledger.last_action() == std::numeric_limits<std::uint64_t>::max() ||
|
||||
!ledger.transfer(ledger.last_action() + 1, legs)) {
|
||||
return std::unexpected{FeedingError::accounting_failure};
|
||||
}
|
||||
const bool depleted = balances()[from.value - 1] == accounting::Inventory{};
|
||||
if (depleted) {
|
||||
active_[input.target - 1] = 0;
|
||||
}
|
||||
return FeedReceipt{input.sequence, assimilated, depleted};
|
||||
}
|
||||
|
||||
std::expected<std::vector<FeedReceipt>, FeedingError>
|
||||
FeedingCommunity::advance(Tick target, std::span<const FeedIntent> inputs) {
|
||||
if (progress().tick.value == std::numeric_limits<std::uint64_t>::max() ||
|
||||
target.value != progress().tick.value + 1) {
|
||||
return std::unexpected{FeedingError::wrong_tick};
|
||||
}
|
||||
if (inputs.size() > max_consumers || active_.empty()) {
|
||||
return std::unexpected{FeedingError::invalid_batch};
|
||||
}
|
||||
std::vector<FeedIntent> ordered{inputs.begin(), inputs.end()};
|
||||
std::ranges::sort(ordered, {}, &FeedIntent::sequence);
|
||||
std::array<bool, max_consumers + 2> seen{};
|
||||
std::uint64_t previous{};
|
||||
for (const auto& input : ordered) {
|
||||
if (input.sequence == 0 || input.sequence == previous || input.consumer < 2 ||
|
||||
input.consumer > consumers_.size() + 1 || seen[input.consumer]) {
|
||||
return std::unexpected{FeedingError::invalid_batch};
|
||||
}
|
||||
seen[input.consumer] = true;
|
||||
previous = input.sequence;
|
||||
}
|
||||
auto candidate = *this;
|
||||
if (candidate.active_[0] != 0) {
|
||||
if (!candidate.producer_.advance(target)) {
|
||||
return std::unexpected{FeedingError::accounting_failure};
|
||||
}
|
||||
} else {
|
||||
candidate.producer_.progress_.tick = target;
|
||||
}
|
||||
std::vector<FeedReceipt> receipts;
|
||||
receipts.reserve(ordered.size());
|
||||
for (const auto& input : ordered) {
|
||||
const auto result = candidate.feed(input);
|
||||
if (!result) {
|
||||
return std::unexpected{result.error()};
|
||||
}
|
||||
receipts.push_back(*result);
|
||||
}
|
||||
static_assert(std::is_nothrow_move_assignable_v<FeedingCommunity>);
|
||||
*this = std::move(candidate);
|
||||
return receipts;
|
||||
}
|
||||
} // namespace evo::processes
|
||||
Reference in New Issue
Block a user