-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from unhindered-ec/3-implement-cliff-scorer
implement cliff scorer
- Loading branch information
Showing
6 changed files
with
174 additions
and
18 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
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,74 @@ | ||
use ec_core::individual::scorer::Scorer; | ||
use ec_linear::genome::bitstring::Bitstring; | ||
|
||
use crate::knapsack::Knapsack; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum CliffScore { | ||
Score(u64), | ||
Overloaded, | ||
} | ||
|
||
// We'll need to impl `PartialOrd` and `Ord` on `CliffScore`. | ||
|
||
struct CliffScorer { | ||
knapsack: Knapsack, | ||
} | ||
|
||
impl CliffScorer { | ||
fn new(knapsack: Knapsack) -> Self { | ||
Self { knapsack } | ||
} | ||
} | ||
|
||
impl Scorer<Bitstring> for CliffScorer { | ||
type Score = CliffScore; | ||
|
||
fn score(&self, genome: &Bitstring) -> Self::Score { | ||
let value = self.knapsack.value(genome); | ||
let weight = self.knapsack.weight(genome); | ||
if weight > self.knapsack.capacity() { | ||
CliffScore::Overloaded | ||
} else { | ||
CliffScore::Score(value) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use test_case::test_case; | ||
|
||
use ec_core::individual::scorer::Scorer; | ||
use ec_linear::genome::bitstring::Bitstring; | ||
|
||
use crate::{cliff_scorer::CliffScore, item::Item, knapsack::Knapsack}; | ||
|
||
use super::CliffScorer; | ||
|
||
#[test_case([false, false, false], 0; "choose no items")] | ||
#[test_case([false, true, false], 9; "choose one item")] | ||
#[test_case([true, false, true], 7; "choose two items")] | ||
fn test_choices(choices: [bool; 3], expected_value: u64) { | ||
let knapsack = Knapsack::new( | ||
vec![Item::new(1, 5, 8), Item::new(2, 9, 6), Item::new(3, 2, 7)], | ||
100, | ||
); | ||
let scorer = CliffScorer::new(knapsack); | ||
|
||
let choices = Bitstring::from_iter(choices); | ||
assert_eq!(scorer.score(&choices), CliffScore::Score(expected_value)); | ||
} | ||
|
||
#[test] | ||
fn test_overloading() { | ||
let knapsack = Knapsack::new( | ||
vec![Item::new(1, 5, 8), Item::new(2, 9, 6), Item::new(3, 2, 7)], | ||
10, | ||
); | ||
let scorer = CliffScorer::new(knapsack); | ||
|
||
let choices = Bitstring::from_iter([true, true, false]); | ||
assert_eq!(scorer.score(&choices), CliffScore::Overloaded); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod cliff_scorer; | ||
mod item; | ||
mod knapsack; | ||
mod run_error; | ||
|