network_compiler (macula_tweann v0.18.1)

View Source

Network compiler for NIF-accelerated evaluation.

This module compiles TWEANN genotypes from Mnesia records into a flat representation suitable for the Rust NIF evaluator.

Compilation Process

1. Load genotype records from Mnesia (cortex, neurons, sensors, actuators) 2. Build a node graph with connections 3. Topologically sort nodes (inputs -> hidden -> outputs) 4. Convert to flat indexed representation 5. Call tweann_nif:compile_network/3

Usage

Compile a genotype for fast evaluation: {ok, Network} = network_compiler:compile(AgentId) Outputs = tweann_nif:evaluate(Network, Inputs)

Or compile from in-memory records: {ok, Network} = network_compiler:compile_from_records(Cortex, Neurons, Sensors, Actuators)

Summary

Functions

Compile a genotype from Mnesia for NIF evaluation.

Compile a simple feedforward network.

Functions

compile(AgentId)

-spec compile(AgentId :: term()) ->
                 {ok, reference()} |
                 {error, {mnesia_error, term()}} |
                 {error, {compilation_failed, term(), [{atom(), atom(), non_neg_integer(), term()}]}}.

Compile a genotype from Mnesia for NIF evaluation.

Loads the agent's neural network from Mnesia and compiles it to a format suitable for the Rust NIF.

compile_from_records(Cortex, Neurons, Sensors, Actuators)

-spec compile_from_records(Cortex ::
                               #cortex{id :: term(),
                                       agent_id :: term(),
                                       neuron_ids :: term(),
                                       sensor_ids :: term(),
                                       actuator_ids :: term()},
                           Neurons ::
                               [#neuron{id :: term(),
                                        generation :: term(),
                                        cx_id :: term(),
                                        pre_processor :: term(),
                                        signal_integrator :: term(),
                                        af :: term(),
                                        post_processor :: term(),
                                        pf :: term(),
                                        aggr_f :: term(),
                                        input_idps :: term(),
                                        input_idps_modulation :: term(),
                                        output_ids :: term(),
                                        ro_ids :: term(),
                                        neuron_type :: term(),
                                        time_constant :: term(),
                                        state_bound :: term(),
                                        ltc_backbone_weights :: term(),
                                        ltc_head_weights :: term(),
                                        internal_state :: term(),
                                        innovation :: term()}],
                           Sensors ::
                               [#sensor{id :: term(),
                                        name :: term(),
                                        type :: term(),
                                        cx_id :: term(),
                                        scape :: term(),
                                        vl :: term(),
                                        fanout_ids :: term(),
                                        generation :: term(),
                                        format :: term(),
                                        parameters :: term(),
                                        gt_parameters :: term(),
                                        phys_rep :: term(),
                                        vis_rep :: term(),
                                        pre_f :: term(),
                                        post_f :: term(),
                                        innovation :: term()}],
                           Actuators ::
                               [#actuator{id :: term(),
                                          name :: term(),
                                          type :: term(),
                                          cx_id :: term(),
                                          scape :: term(),
                                          vl :: term(),
                                          fanin_ids :: term(),
                                          generation :: term(),
                                          format :: term(),
                                          parameters :: term(),
                                          gt_parameters :: term(),
                                          phys_rep :: term(),
                                          vis_rep :: term(),
                                          pre_f :: term(),
                                          post_f :: term(),
                                          innovation :: term()}]) ->
                              {ok, reference()} | {error, term()}.

Compile from in-memory records.

Use this when you already have the network records loaded.

compile_simple(InputCount, HiddenLayers, OutputCount)

-spec compile_simple(InputCount :: pos_integer(),
                     HiddenLayers :: [pos_integer()],
                     OutputCount :: pos_integer()) ->
                        {ok, reference(), [{non_neg_integer(), non_neg_integer(), float()}]}.

Compile a simple feedforward network.

Convenience function for creating simple networks without Mnesia. Useful for testing and examples.