Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change fitness from function to struct #221

Merged
merged 8 commits into from
Dec 19, 2022

Conversation

co012
Copy link
Collaborator

@co012 co012 commented Dec 16, 2022

Linked issues

Closes #217

@co012 co012 self-assigned this Dec 16, 2022
@co012 co012 linked an issue Dec 16, 2022 that may be closed by this pull request
@co012 co012 requested a review from kkafar December 16, 2022 17:36
Copy link
Collaborator

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, after merging this PR we should update text of our thesis (these diagrams with physical architecture)

Comment on lines +1 to +21
use crate::ga::individual::Chromosome;

pub trait Fitness<T: Chromosome> {
fn apply(&self, chromosome: &T) -> f64;
}

pub struct FnBasedFitness<T: Chromosome> {
fn_ptr: fn(&T) -> f64,
}

impl<T: Chromosome> FnBasedFitness<T> {
pub fn new(fn_ptr: fn(&T) -> f64) -> Self {
FnBasedFitness { fn_ptr }
}
}

impl<T: Chromosome> Fitness<T> for FnBasedFitness<T> {
fn apply(&self, chromosome: &T) -> f64 {
(self.fn_ptr)(chromosome)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to avoid usage of pointers here (I know that it was implemented with function pointers before), as we spent a lot of time on making the whole to use static-dispatch only. Calling the most hot function in our system via pointer kinda defeats this purpose. But I guess, we should maybe create separate issue for that.

# Conflicts:
#	src/ga/builder/bistring.rs
#	src/ga/builder/realvalued.rs
@co012 co012 merged commit b40a10e into main Dec 19, 2022
@co012 co012 deleted the @co012/ga-change-fitness-function-to-trait branch December 19, 2022 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[GA] Change fitness function to trait
2 participants