36 template <std::derived_from<Node> NodeType>
37 Source(std::shared_ptr<NodeType> node)
49 template <std::derived_from<NodeNetwork> NetworkType>
55 template <
typename Callable>
56 requires(!std::same_as<std::remove_cvref_t<Callable>,
Source>
57 && std::is_invocable_r_v<double, Callable&>)
72 if (
const auto* constant = std::get_if<double>(&
m_value)) {
75 if (
const auto* node = std::get_if<std::shared_ptr<Node>>(&
m_value)) {
76 return *node ? (*node)->get_last_output() : 0.0;
78 if (
const auto*
index = std::get_if<NetworkIndex>(&
m_value)) {
79 return index->network ?
index->network->get_node_output(
index->index).value_or(0.0) : 0.0;
81 const auto& function = std::get<Function>(
m_value);
82 return function ? function() : 0.0;
129 void set_slots(std::vector<RelationSlot>& slots)
noexcept { m_slots = &slots; }
136 for (
const auto& binding : m_bindings) {
137 set_parameter(binding.parameter, binding.source.evaluate());
151 auto it = std::ranges::find(m_bindings, parameter, &Binding::parameter);
152 if (it != m_bindings.end()) {
153 it->source = std::move(source);
155 m_bindings.push_back({ .parameter = std::move(parameter), .source = std::move(source) });
163 void unmap(std::string_view parameter)
165 std::erase_if(m_bindings, [&](
const Binding&
b) {
return b.parameter == parameter; });
170 [[nodiscard]]
virtual std::span<const double>
get_output_block(
size_t )
const {
return {}; }
173 [[nodiscard]] std::span<RelationSlot>
slots() const noexcept
175 return m_slots ? std::span<RelationSlot>(*m_slots) : std::span<RelationSlot> {};
185 std::vector<RelationSlot>* m_slots {
nullptr };
207 using Body = std::function<void(
const std::shared_ptr<StepOperator>& self, std::span<RelationSlot> slots, uint32_t frames)>;
210 : m_body(
std::move(body))
214 void process(
float frames)
override;
215 void set_parameter(std::string_view param,
double value)
override;
216 [[nodiscard]] std::optional<double> query_state(std::string_view query)
const override;
217 [[nodiscard]] std::string_view
get_type_name()
const override {
return "Step"; }
223 [[nodiscard]]
double parameter(std::string_view
name)
const;
253 void process(
float frames)
override;
257 [[nodiscard]] std::optional<double>
query_state(std::string_view )
const override {
return std::nullopt; }
258 [[nodiscard]] std::string_view
get_type_name()
const override {
return "Advance"; }
262 size_t m_scratch_pos {};
294 using Function = std::function<double(std::span<const double>)>;
295 using CoefFunction = std::function<double(std::span<const double>, std::span<double>)>;
320 template <
typename Gate>
321 requires(std::invocable<Gate&, const std::vector<double>&, std::span<double>>
322 && std::same_as<std::invoke_result_t<Gate&, const std::vector<double>&, std::span<double>>,
bool>)
323 DeriveOperator(
size_t target, std::vector<size_t> inputs, Gate function, std::vector<double> coefficients)
325 , m_inputs(std::move(inputs))
326 , m_values(m_inputs.size(), 0.0)
327 , m_coefficients(std::move(coefficients))
328 , m_kernel([gate = std::move(function)](
const std::vector<double>&
values, std::span<double> coefs) {
329 return gate(
values, coefs) ? 1.0 : 0.0;
335 void process(
float frames)
override;
342 void set_parameter(std::string_view param,
double value)
override;
347 [[nodiscard]] std::optional<double> query_state(std::string_view query)
const override;
349 [[nodiscard]] std::string_view
get_type_name()
const override {
return "Derive"; }
356 void set_coefficients(std::vector<double> coefficients) { m_coefficients = std::move(coefficients); }
358 [[nodiscard]]
const std::vector<double>&
get_coefficients() const noexcept {
return m_coefficients; }
361 using Kernel = std::function<double(
const std::vector<double>&, std::span<double>)>;
363 [[nodiscard]]
static std::optional<size_t> coefficient_index(std::string_view param);
399 using Function = std::function<double(std::span<const double>)>;
407 m_function = std::move(function);
413 m_function =
nullptr;
416 [[nodiscard]]
bool has_combine() const noexcept {
return static_cast<bool>(m_function); }
417 [[nodiscard]] std::span<const double>
block() const noexcept {
return m_block; }
438 explicit CombineOperator(std::shared_ptr<Kinesis::Stochastic::Weights> table);
440 void process(
float frames)
override;
444 [[nodiscard]] std::optional<double>
query_state(std::string_view )
const override {
return std::nullopt; }
445 [[nodiscard]] std::string_view
get_type_name()
const override {
return "Combine"; }
449 [[nodiscard]] std::span<const double> get_output_block(
size_t index)
const override;
454 [[nodiscard]]
const std::shared_ptr<Kinesis::Stochastic::Weights>&
weights() const noexcept {
return m_weights; }
471 std::shared_ptr<Kinesis::Stochastic::Weights>
m_weights;
Core::GlobalNetworkConfig network
std::vector< std::byte > values
Proxy for one cell with atomic load and store.
void set_parameter(std::string_view, double) override
std::vector< double > m_scratch
std::optional< double > query_state(std::string_view) const override
Query operator internal state.
std::string_view get_type_name() const override
Type name for introspection.
Produces every slot's signal for the block.
std::function< double(std::span< const double >)> Function
void combine(Function function)
Replace the weighted sum with a function of all slot values.
std::span< const double > block() const noexcept
std::vector< double > m_block
bool has_combine() const noexcept
void clear_combine()
Return to the weighted sum.
Output & output(size_t index)
std::optional< double > query_state(std::string_view) const override
Query operator internal state.
std::shared_ptr< Kinesis::Stochastic::Weights > m_weights
bool publishes_output() const override
const std::shared_ptr< Kinesis::Stochastic::Weights > & weights() const noexcept
Slot-to-output weights, slot_count rows by output_count columns.
void set_parameter(std::string_view, double) override
std::vector< double > m_values
size_t get_output_count() const override
std::string_view get_type_name() const override
Type name for introspection.
std::vector< Output > m_outputs
const Output & output(size_t index) const
Turns slot blocks into published outputs.
std::vector< double > m_coefficients
std::vector< double > m_values
void set_coefficients(std::vector< double > coefficients)
Replace the coefficient array.
std::function< double(std::span< const double >, std::span< double >)> CoefFunction
std::function< double(std::span< const double >)> Function
std::string_view get_type_name() const override
Type name for introspection.
DeriveOperator(size_t target, std::vector< size_t > inputs, Gate function, std::vector< double > coefficients)
std::vector< size_t > m_inputs
const std::vector< double > & get_coefficients() const noexcept
std::function< double(const std::vector< double > &, std::span< double >)> Kernel
Defines one slot's block as a function of other slots' blocks.
Domain-agnostic interpretive lens for network processing.
Abstract base class for structured collections of nodes with defined relationships.
void begin_block()
Apply mapped parameters at the start of a block.
virtual size_t get_output_count() const
virtual bool publishes_output() const
void unmap(std::string_view parameter)
Remove the mapping of a parameter.
virtual std::span< const double > get_output_block(size_t) const
std::span< RelationSlot > slots() const noexcept
void set_slots(std::vector< RelationSlot > &slots) noexcept
Supply the current slot storage.
void map(std::string parameter, Source source)
Drive an operator parameter from a source.
std::vector< Binding > m_bindings
Operator that acts on a network's slots over one block at a time.
std::function< double()> Function
Source(Callable &&callable)
Source(std::shared_ptr< NetworkType > network, size_t index)
One indexed output of a NodeNetwork.
std::variant< double, std::shared_ptr< Node >, NetworkIndex, Function > m_value
double evaluate() const
Current value.
Source(std::shared_ptr< NodeType > node)
Anything that can supply a number when evaluated.
std::string_view get_type_name() const override
Type name for introspection.
std::function< void(const std::shared_ptr< StepOperator > &self, std::span< RelationSlot > slots, uint32_t frames)> Body
std::map< std::string, double, std::less<> > m_parameters
General operator whose body is a callable, run once per block.
Base interface for all computational processing nodes.
std::shared_ptr< NodeNetwork > network