-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Rsnsga2 algorithm not exposed to user yet. Compute diversity metr… (
#30) * Add Rsnsga2 algorithm not exposed to user yet. Compute diversity metrix at Survival
- Loading branch information
1 parent
9418048
commit 20466d8
Showing
22 changed files
with
837 additions
and
188 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "pymoors" | ||
version = "0.1.1-rc1" | ||
version = "0.1.1" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use pyo3::prelude::*; | ||
|
||
use crate::define_multiobj_pyclass; | ||
use crate::helpers::functions::{ | ||
create_population_constraints_closure, create_population_fitness_closure, | ||
}; | ||
use crate::helpers::parser::{ | ||
unwrap_crossover_operator, unwrap_duplicates_cleaner, unwrap_mutation_operator, | ||
unwrap_sampling_operator, | ||
}; | ||
use crate::operators::selection::{DiversityComparison, RankAndCrowdingSelection}; | ||
use crate::operators::survival::RankReferencePointsSurvival; | ||
|
||
use numpy::{PyArray2, PyArrayMethods}; | ||
|
||
// Define the NSGA-II algorithm using the macro | ||
define_multiobj_pyclass!(RNsga2, PyRNsga2, "RNsga2"); | ||
|
||
// Implement PyO3 methods | ||
#[pymethods] | ||
impl PyRNsga2 { | ||
#[new] | ||
#[pyo3(signature = ( | ||
reference_points, | ||
sampler, | ||
crossover, | ||
mutation, | ||
fitness_fn, | ||
n_vars, | ||
pop_size, | ||
n_offsprings, | ||
num_iterations, | ||
epsilon = 0.001, | ||
mutation_rate=0.1, | ||
crossover_rate=0.9, | ||
keep_infeasible=false, | ||
verbose=true, | ||
duplicates_cleaner=None, | ||
constraints_fn=None, | ||
lower_bound=None, | ||
upper_bound=None | ||
))] | ||
pub fn py_new<'py>( | ||
reference_points: &Bound<'py, PyArray2<f64>>, | ||
sampler: PyObject, | ||
crossover: PyObject, | ||
mutation: PyObject, | ||
fitness_fn: PyObject, | ||
n_vars: usize, | ||
pop_size: usize, | ||
n_offsprings: usize, | ||
num_iterations: usize, | ||
epsilon: f64, | ||
mutation_rate: f64, | ||
crossover_rate: f64, | ||
keep_infeasible: bool, | ||
verbose: bool, | ||
duplicates_cleaner: Option<PyObject>, | ||
constraints_fn: Option<PyObject>, | ||
// Optional lower bound for each gene. | ||
lower_bound: Option<f64>, | ||
// Optional upper bound for each gene. | ||
upper_bound: Option<f64>, | ||
) -> PyResult<Self> { | ||
// Unwrap the genetic operators | ||
let sampler_box = unwrap_sampling_operator(sampler)?; | ||
let crossover_box = unwrap_crossover_operator(crossover)?; | ||
let mutation_box = unwrap_mutation_operator(mutation)?; | ||
let duplicates_box = if let Some(py_obj) = duplicates_cleaner { | ||
Some(unwrap_duplicates_cleaner(py_obj)?) | ||
} else { | ||
None | ||
}; | ||
|
||
// Build the MANDATORY population-level fitness closure | ||
let fitness_closure = create_population_fitness_closure(fitness_fn)?; | ||
|
||
// Build OPTIONAL population-level constraints closure | ||
let constraints_closure = if let Some(py_obj) = constraints_fn { | ||
Some(create_population_constraints_closure(py_obj)?) | ||
} else { | ||
None | ||
}; | ||
|
||
// Convert PyArray2 to Array2 | ||
let reference_points_array = reference_points.to_owned_array(); | ||
|
||
// Create an instance of the selection/survival struct | ||
let selector_box = Box::new(RankAndCrowdingSelection::new_with_comparison( | ||
DiversityComparison::Minimize, | ||
)); | ||
let survivor_box = Box::new(RankReferencePointsSurvival::new( | ||
reference_points_array, | ||
epsilon, | ||
)); | ||
|
||
// Create the Rust struct | ||
let rs_obj = RNsga2::new( | ||
sampler_box, | ||
crossover_box, | ||
mutation_box, | ||
selector_box, | ||
survivor_box, | ||
duplicates_box, | ||
fitness_closure, | ||
n_vars, | ||
pop_size, | ||
n_offsprings, | ||
num_iterations, | ||
mutation_rate, | ||
crossover_rate, | ||
keep_infeasible, | ||
verbose, | ||
constraints_closure, | ||
lower_bound, | ||
upper_bound, | ||
)?; | ||
|
||
Ok(Self { inner: rs_obj }) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod crowding; | ||
pub mod reference; | ||
|
||
pub use crowding::crowding_distance; | ||
pub use reference::{reference_points_rank_distance, weighted_distance_matrix}; |
Oops, something went wrong.