Struct PSO

Source
pub struct PSO { /* private fields */ }
Expand description

Particle Swarm Optimizer

Base struct to run optimization jobs using Particle Swarms and an objective function

§Examples

use pso::PSO;
use pso::job_config::JobConfig;

// create a PSO with 6 swarms using the default swarm configuration
// the verbose flag tells the pso to provide updates about configuration information
let pso = PSO::default(6, true);

// create a Job Configuration to run an optimization job on the PSO over a 3 dimensional search space
let mut jc = JobConfig::new(3);

// set some configuration parameters
jc.exit_cost(10e-10);
jc.variable_bound([-10.0, 10.0]);
jc.update_console(10);

// define some data to be used in the optimization (this will be moved in to the closure below)
let mins = [1.0, -3.0, 3.0];

// run an optimization job with a closure as the cost function
let mins_opt = pso.run_job_fn(jc, move |position: &[f64]| -> f64 {
    position.iter().enumerate().map(|(i, x)| {
        (x - mins[i]).powi(2)
    }).sum::<f64>()
});

for (min, min_opt) in mins.iter().zip(mins_opt.1.iter()) {
    assert!((min - min_opt).abs() < 0.0001);
}

Implementations§

Source§

impl PSO

Source

pub fn from_swarm_configs( verbose: bool, swarm_configs: Vec<SwarmConfig>, ) -> Self

Generate a PSO from a list of Swarm Configurations

Source

pub fn from_swarm_config( num_swarms: usize, verbose: bool, swarm_config: &SwarmConfig, ) -> Self

Generate a PSO with num_swarms swarms using the given Swarm Configuration

Source

pub fn sampled_swarm_config( num_swarms: usize, verbose: bool, swarm_config_sampler: &SwarmConfigSampler, ) -> Self

Generate a PSO with num_swarms swarms by sampling SwarmConfig’s from a SwarmConfigSampler

Source

pub fn default(num_swarms: usize, verbose: bool) -> Self

Generate a PSO with num_swarms swarms using the default Swarm Configuration

Source

pub fn run_job_fn<T>( &self, job_config: JobConfig, objective_function: T, ) -> (f64, Vec<f64>)
where T: Fn(&[f64]) -> f64 + Sync + Send + 'static,

Run an optimization with the given Job Configuration and Objective Function

Returns a tuple with the minimum cost and corresponding location in the search space.

This function puts the objective function in an Arc in order to share it across threads, so the function must be Sync + Send (it must not mutate itself).

This method of objective function sharing prioritizes memory efficiency, but may be less efficient at runtime than run_job_fn_mut() due to Atomic instruction overhead (especially when using many swarms/ threads).

Source

pub fn run_job_fn_mut<T>( &self, job_config: JobConfig, objective_function: T, ) -> (f64, Vec<f64>)
where T: FnMut(&[f64]) -> f64 + Clone + 'static,

Run an optimization with the given Job Configuration and Objective Function

Returns a tuple with the minimum cost and corresponding location in the search space.

This function clones the objective function S.T. each swarm/thread has its own copy of the function. This allows the function to mutate itself and use internal memory sharing that is !Sync + !Send.

This method of objective function sharing prioritizes performance but is not as memory efficient as run_job_fn() (especially when using many swarms/ threads).

Trait Implementations§

Source§

impl Clone for PSO

Source§

fn clone(&self) -> PSO

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 Debug for PSO

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for PSO

§

impl RefUnwindSafe for PSO

§

impl Send for PSO

§

impl Sync for PSO

§

impl Unpin for PSO

§

impl UnwindSafe for PSO

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V