Struct cge::network::Network [] [src]

pub struct Network {
    pub size: usize,
    pub genome: Vec<Gene>,
    pub function: TransferFunction,
}

Fields

size: usize genome: Vec<Gene> function: TransferFunction

Methods

impl Network
[src]

fn evaluate(&mut self, inputs: &[f64]) -> Vec<f64>

Evaluates the neural network with the given inputs, returning a vector of outputs. The encoding can encode recurrent connections and bias inputs, so an internal state is used. It is important to run the clear_state method before calling evaluate again, unless it is desired to allow data carry over from the previous evaluation, for example if the network is being used as a real time controller.

If too little inputs are given, the empty inputs will have a value of zero. If too many inputs are given, the extras are discarded.

Examples

use cge::Network;

// Load a neural network
let mut network = Network::load_from_file("neural_network.ann").unwrap();

// Get the output of the neural network the the specified inputs
let result = network.evaluate(&vec![1.0, 1.0]);

// Get the output of the neural network with no inputs
let result = network.evaluate(&[]);

// Get the output of the neural network with too many inputs (extras aren't used)
let result = network.evaluate(&[1.0, 1.0, 1.0]);
 
// Let's say adder.ann is a file with a neural network with recurrent connections, used for
// adding numbers together.
let mut adder = Network::load_from_file("adder.ann").unwrap();

// result_one will be 1.0
let result_one = adder.evaluate(&[1.0]);

// result_two will be 3.0
let result_two = adder.evaluate(&[2.0]);

// result_three will be 5.0
let result_three = adder.evaluate(&[2.0]);

// If this behavior is not desired, call the clear_state method between evaluations:
let result_one = adder.evaluate(&[1.0]);

adder.clear_state();

// The 1.0 from the previous call is gone, so result_two will be 2.0
let result_two = adder.evaluate(&[2.0]);

fn debug_eval(&mut self, inputs: &[f64]) -> Vec<f64>

Evaluates the neural network the same as evaluate, but prints debug info.

fn clear_state(&mut self)

Clears the internal state of the neural network.

fn from_str(string: &str) -> Option<Network>

Loads a neural network from a string. Returns None if the format is incorrect.

Examples

use cge::Network;

// Store the neural network in a string
let string = "0: n 1 0 2,n 1 1 2,n 1 3 2,
              i 1 0,i 1 1,i 1 1,n 1 2 4,
              f 1 3,i 1 0,i 1 1,r 1 0";

// Load a neural network from the string
let network = Network::from_str(string).unwrap();

Format

The format for the string is simple enough to build by hand:

First, the number 0, 1, 2 or 3 is entered to represent the linear, threshold, sign, or sigmoid function, followed by a colon. The rest is the genome, encoded with comma separated genes:

Neuron: n [weight] [id] [input count]
Input: i [weight] [id]
Connection: f [weight] [id]
Recurrent: r [weight] [id]
Bias: b [weight]

For more information about what this means, see here.

fn to_str(&self) -> String

Saves the neural network to a string. Allows embedding a neural network in source code.

Examples

use cge::*;

// Create a neural network
let network = Network {
    size: 0,
    genome: Vec::new(),
    function: TransferFunction::Sign
};

// Save the neural network to the string
let string = network.to_str();

fn save_to_file(&self, path: &str) -> Result<()>

Saves the neural network to a file. Returns an empty tuple on success, or an io error.

Examples

use cge::*;

// Create a neural network
let network = Network {
    size: 0,
    genome: Vec::new(),
    function: TransferFunction::Sign
};

// Save the neural network to neural_network.ann
network.save_to_file("neural_network.ann").unwrap();

fn load_from_file(path: &str) -> Result<Network>

Loads a neural network from a file. No guarantees are made about the validity of the genome. Returns the network, or an io error. If the file is in a bad format, std::io::ErrorKind::InvalidData is returned.

Examples

use cge::Network;

// Loads a neural network from the file neural_network.ann
let mut network = Network::load_from_file("neural_network.ann").unwrap();

fn get_subnetwork_index(&self, id: usize) -> Option<Range<usize>>

Returns the start and end index of the subnetwork starting at the neuron with the given id, or None if it does not exist.

fn get_neuron_index(&self, id: usize) -> Option<usize>

Returns the index of the neuron with the given id, or None if it does not exist.

Trait Implementations

impl PartialEq for Network
[src]

fn eq(&self, __arg_0: &Network) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, __arg_0: &Network) -> bool

This method tests for !=.

impl Debug for Network
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Clone for Network
[src]

fn clone(&self) -> Network

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more