Skip to main content

radiate_gp/collections/graphs/
codec.rs

1use super::{Graph, GraphChromosome, GraphNode};
2use crate::{Factory, NodeStore};
3use radiate_core::{Codec, Genotype};
4
5#[derive(Clone)]
6pub struct GraphCodec<T> {
7    store: NodeStore<T>,
8    template: GraphChromosome<T>,
9}
10
11impl<T: Clone + Default> GraphCodec<T> {
12    pub fn new(
13        template: impl IntoIterator<Item = GraphNode<T>>,
14        store: impl Into<NodeStore<T>>,
15    ) -> Self {
16        GraphCodec {
17            store: store.into(),
18            template: template.into_iter().collect(),
19        }
20    }
21
22    pub fn directed(input_size: usize, output_size: usize, store: impl Into<NodeStore<T>>) -> Self {
23        let new_store = store.into();
24
25        GraphCodec {
26            store: new_store.clone(),
27            template: Graph::directed(input_size, output_size, &new_store)
28                .into_iter()
29                .collect(),
30        }
31    }
32
33    pub fn recurrent(
34        input_size: usize,
35        output_size: usize,
36        store: impl Into<NodeStore<T>>,
37    ) -> Self {
38        let new_store = store.into();
39
40        GraphCodec {
41            store: new_store.clone(),
42            template: Graph::recurrent(input_size, output_size, &new_store)
43                .into_iter()
44                .collect(),
45        }
46    }
47
48    pub fn weighted_directed(
49        input_size: usize,
50        output_size: usize,
51        store: impl Into<NodeStore<T>>,
52    ) -> Self {
53        let new_store = store.into();
54
55        GraphCodec {
56            store: new_store.clone(),
57            template: Graph::weighted_directed(input_size, output_size, &new_store)
58                .into_iter()
59                .collect(),
60        }
61    }
62
63    pub fn weighted_recurrent(
64        input_size: usize,
65        output_size: usize,
66        store: impl Into<NodeStore<T>>,
67    ) -> Self {
68        let new_store = store.into();
69
70        GraphCodec {
71            store: new_store.clone(),
72            template: Graph::weighted_recurrent(input_size, output_size, &new_store)
73                .into_iter()
74                .collect(),
75        }
76    }
77
78    pub fn lstm(input_size: usize, output_size: usize, store: impl Into<NodeStore<T>>) -> Self {
79        let new_store = store.into();
80
81        GraphCodec {
82            store: new_store.clone(),
83            template: Graph::lstm(input_size, output_size, &new_store)
84                .into_iter()
85                .collect(),
86        }
87    }
88
89    pub fn gru(input_size: usize, output_size: usize, store: impl Into<NodeStore<T>>) -> Self {
90        let new_store = store.into();
91
92        GraphCodec {
93            store: new_store.clone(),
94            template: Graph::gru(input_size, output_size, &new_store)
95                .into_iter()
96                .collect(),
97        }
98    }
99
100    pub fn mesh(
101        input_size: usize,
102        output_size: usize,
103        rows: usize,
104        cols: usize,
105        store: impl Into<NodeStore<T>>,
106    ) -> Self {
107        let new_store = store.into();
108
109        GraphCodec {
110            store: new_store.clone(),
111            template: Graph::mesh(input_size, output_size, rows, cols, &new_store)
112                .into_iter()
113                .collect(),
114        }
115    }
116
117    pub fn with_max_nodes(mut self, max_nodes: usize) -> Self {
118        self.template = self.template.with_max_nodes(max_nodes);
119        self
120    }
121}
122
123impl<T> Codec<GraphChromosome<T>, Graph<T>> for GraphCodec<T>
124where
125    T: Clone + PartialEq + Default,
126{
127    fn encode(&self) -> Genotype<GraphChromosome<T>> {
128        self.template.new_instance(Some(self.store.clone())).into()
129    }
130
131    #[inline]
132    fn decode(&self, genotype: &Genotype<GraphChromosome<T>>) -> Graph<T> {
133        Graph::new(genotype[0].as_ref().to_vec())
134    }
135}