Skip to main content

FloatCodec

Struct FloatCodec 

Source
pub struct FloatCodec<F: Float, T = F> { /* private fields */ }
Expand description

A Codec for a Genotype of FloatGenes. The encode function creates a Genotype with num_chromosomes chromosomes and num_genes genes per chromosome. The decode function creates a Vec<Vec<f32>> from the Genotype where the inner Vec contains the alleles of the FloatGenes in the chromosome - the f32 values.

The lower and upper bounds of the FloatGenes can be set with the with_bounds function. The default bounds are equal to min and max values.

Implementations§

Source§

impl<F: Float, T> FloatCodec<F, T>

Source

pub fn with_bounds(self, range: Range<F>) -> Self

Set the bounds of the FloatGenes in the Genotype. The default bounds are equal to the min and max values.

Source§

impl<F: Float> FloatCodec<F, Vec<Vec<Vec<F>>>>

Source

pub fn tensor(shapes: Vec<(usize, usize)>, range: Range<F>) -> Self

Source§

impl<F: Float> FloatCodec<F, Vec<Vec<F>>>

Source

pub fn matrix(rows: usize, cols: usize, range: Range<F>) -> Self

Create a new FloatCodec with the given number of chromosomes, genes, min, and max values. The f_32 values for each FloatGene will be randomly generated between the min and max values.

Source§

impl<F: Float> FloatCodec<F, Vec<F>>

Source

pub fn vector(count: usize, range: Range<F>) -> Self

Create a new FloatCodec with the given number of chromosomes, genes, min, and max values. The f_32 values for each FloatGene will be randomly generated between the min and max values.

Source§

impl FloatCodec<f32>

Source

pub fn scalar(range: Range<f32>) -> Self

Create a new FloatCodec with the given number of chromosomes, genes, min, and max values. The f_32 values for each FloatGene will be randomly generated between the min and max values.

Trait Implementations§

Source§

impl<F: Clone + Float, T: Clone> Clone for FloatCodec<F, T>

Source§

fn clone(&self) -> FloatCodec<F, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<F: Float> Codec<FloatChromosome<F>, F> for FloatCodec<F, F>

Implement the Codec trait for a FloatCodec with a f32 type. This will decode to a single f32 value. The encode function creates a Genotype with a single chromosomes and a single gene per chromosome.

§Example

use radiate_core::*;

// Create a new FloatCodec with a single gene
// per chromosome - a single f32 value.
let codec = FloatCodec::scalar(0.0_f32..1.0_f32);
let genotype: Genotype<FloatChromosome<f32>> = codec.encode();
let decoded: f32 = codec.decode(&genotype);
Source§

fn encode(&self) -> Genotype<FloatChromosome<F>>

Source§

fn decode(&self, genotype: &Genotype<FloatChromosome<F>>) -> F

Source§

impl<F: Float> Codec<FloatChromosome<F>, Vec<F>> for FloatCodec<F, Vec<F>>

Implement the Codec trait for a FloatCodec with a Vec<f32> type. This will decode to a vector of f32 values. The encode function creates a Genotype with a single chromosomes and num_genes genes per chromosome.

§Example

use radiate_core::*;

// Create a new FloatCodec with 3 genes
// per chromosome - a vector with 3 f32 values.
let codec = FloatCodec::vector(3, 0.0_f32..1.0_f32);
let genotype: Genotype<FloatChromosome<f32>> = codec.encode();
let decoded: Vec<f32> = codec.decode(&genotype);

assert_eq!(decoded.len(), 3);
Source§

fn encode(&self) -> Genotype<FloatChromosome<F>>

Source§

fn decode(&self, genotype: &Genotype<FloatChromosome<F>>) -> Vec<F>

Source§

impl<F: Float> Codec<FloatChromosome<F>, Vec<Vec<F>>> for FloatCodec<F, Vec<Vec<F>>>

Implement the Codec trait for a FloatCodec with a Vec<Vec<f32>> type. This will decode to a matrix of f32 values. The encode function creates a Genotype with num_chromosomes chromosomes and num_genes genes per chromosome.

  • Example:
use radiate_core::*;

// Create a new FloatCodec with 3 chromosomes and 4 genes
// per chromosome - a 3x4 matrix of f32 values.
let codec = FloatCodec::matrix(3, 4, 0.0_f32..1.0_f32);
let genotype: Genotype<FloatChromosome<f32>> = codec.encode();
let decoded: Vec<Vec<f32>> = codec.decode(&genotype);

assert_eq!(decoded.len(), 3);
assert_eq!(decoded[0].len(), 4);
Source§

fn encode(&self) -> Genotype<FloatChromosome<F>>

Source§

fn decode(&self, genotype: &Genotype<FloatChromosome<F>>) -> Vec<Vec<F>>

Source§

impl<F: Float> Codec<FloatChromosome<F>, Vec<Vec<Vec<F>>>> for FloatCodec<F, Vec<Vec<Vec<F>>>>

Implement the Codec for a FloatCodec with a Vec<Vec<Vec<f32>>> type. Unlike the other impls, this will decode to a 3D tensor of f32 values.

§Example

use radiate_core::*;

// Create a new FloatCodec with 2 layers:
// - First layer: 2 rows and 3 columns
// - Second layer: 3 rows and 4 columns
let codec = FloatCodec::tensor(vec![(2, 3), (3, 4)], 0.0_f32..1.0_f32);
let genotype: Genotype<FloatChromosome<f32>> = codec.encode();
let decoded: Vec<Vec<Vec<f32>>> = codec.decode(&genotype);

assert_eq!(decoded.len(), 2);
assert_eq!(decoded[0].len(), 2);
assert_eq!(decoded[0][0].len(), 3);
assert_eq!(decoded[1].len(), 3);
assert_eq!(decoded[1][0].len(), 4);
Source§

fn encode(&self) -> Genotype<FloatChromosome<F>>

Source§

fn decode(&self, genotype: &Genotype<FloatChromosome<F>>) -> Vec<Vec<Vec<F>>>

Auto Trait Implementations§

§

impl<F, T> Freeze for FloatCodec<F, T>
where F: Freeze,

§

impl<F, T> RefUnwindSafe for FloatCodec<F, T>

§

impl<F, T> Send for FloatCodec<F, T>
where F: Send, T: Send,

§

impl<F, T> Sync for FloatCodec<F, T>
where F: Sync, T: Sync,

§

impl<F, T> Unpin for FloatCodec<F, T>
where F: Unpin, T: Unpin,

§

impl<F, T> UnsafeUnpin for FloatCodec<F, T>
where F: UnsafeUnpin,

§

impl<F, T> UnwindSafe for FloatCodec<F, T>
where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.