Skip to content

Instantly share code, notes, and snippets.

@Ameliorate
Created January 7, 2017 08:13
Show Gist options
  • Save Ameliorate/137e0afa688cd889d1938cbd21e789d6 to your computer and use it in GitHub Desktop.
Save Ameliorate/137e0afa688cd889d1938cbd21e789d6 to your computer and use it in GitHub Desktop.
//! Example of graph-based neural network thing.
use std::collections::HashMap;
use std::ops::Deref;
use std::rc::Rc;
struct NodeNeuralNetwork {
// The nodes and connections are a Vec so that you can access them randomly during the mutation phase.
nodes: Vec<Box<Node>>,
connections: Vec<Connection>,
}
#[derive(Clone)]
struct NodePointer {
id: usize,
node_network: Rc<NodeNeuralNetwork>,
}
impl Deref for NodePointer {
type Target = Box<Node>;
fn deref(&self) -> &Box<Node> {
self.node_network.nodes.get(self.id).expect("NodePointer contained invalid id value")
}
}
#[derive(Clone)]
struct ConnectionPointer {
id: usize,
node_network: Rc<NodeNeuralNetwork>,
}
impl Deref for ConnectionPointer {
type Target = Connection;
fn deref(&self) -> &Connection {
self.node_network.connections.get(self.id).expect("ConnectionPointer contained invalid id value")
}
}
trait Node {
//fn get_parent_connections(&self) -> Vec<ConnectionPointer>;
// ^ may be useful, but in what circumstances?
fn get_child_connections(&self) -> Vec<ConnectionPointer>;
/// Executes the node and all of it's childeren. side_effect should be used to queue up changes to the outside enviroment,
/// say fire a round or execute another neural network.
fn execute(&self, value: f64, side_effect: SideEffectQueue);
// Should value really be a single float? Are there any uses for larger or more values?
}
struct Connection {
parent: NodePointer,
child: NodePointer,
}
/// Globalish struct to represent side effects a node may apply to the simulation.
///
/// Clones still refer to the same side effect, and actions queued up on any SideEffectQueue will
/// apply to all of them.
#[derive(Clone)]
struct SideEffectQueue {
// Opaque, contents are mostly based on the nitty gritty of the simulation.
}
/// Represents a basic fork that executes all it's childeren with the same value.
struct ForkNode {
childeren: Vec<ConnectionPointer>,
}
impl Node for ForkNode {
fn get_child_connections(&self) -> Vec<ConnectionPointer> {
self.childeren.clone()
}
fn execute(&self, value: f64, side_effect: SideEffectQueue) {
for child in &self.childeren {
child.child.execute(value, side_effect.clone())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment