style: rustfmt the Phase 1 test additions
The per-file Phase 1 test commits were written without running rustfmt as I went; this pass formats the new test code (long assert_eq! lines wrapped, etc.). Formatting-only — no behavioural change.
This commit is contained in:
+32
-24
@@ -39,24 +39,21 @@ const N_ITEMS: usize = 30;
|
||||
|
||||
/// Profit vector A (one of two objectives), U(10, 100) style.
|
||||
const PROFITS_A: [f64; N_ITEMS] = [
|
||||
61.0, 17.0, 92.0, 49.0, 73.0, 28.0, 84.0, 36.0, 55.0, 78.0,
|
||||
23.0, 91.0, 12.0, 67.0, 45.0, 58.0, 33.0, 71.0, 14.0, 26.0,
|
||||
87.0, 42.0, 19.0, 65.0, 30.0, 51.0, 79.0, 22.0, 47.0, 88.0,
|
||||
61.0, 17.0, 92.0, 49.0, 73.0, 28.0, 84.0, 36.0, 55.0, 78.0, 23.0, 91.0, 12.0, 67.0, 45.0, 58.0,
|
||||
33.0, 71.0, 14.0, 26.0, 87.0, 42.0, 19.0, 65.0, 30.0, 51.0, 79.0, 22.0, 47.0, 88.0,
|
||||
];
|
||||
|
||||
/// Profit vector B (the other objective). Intentionally anti-correlated with
|
||||
/// A on many items so the Pareto front spans a wide trade-off.
|
||||
const PROFITS_B: [f64; N_ITEMS] = [
|
||||
24.0, 81.0, 16.0, 67.0, 29.0, 73.0, 41.0, 60.0, 52.0, 19.0,
|
||||
77.0, 34.0, 95.0, 22.0, 71.0, 88.0, 56.0, 27.0, 64.0, 90.0,
|
||||
18.0, 43.0, 79.0, 31.0, 85.0, 25.0, 38.0, 92.0, 70.0, 13.0,
|
||||
24.0, 81.0, 16.0, 67.0, 29.0, 73.0, 41.0, 60.0, 52.0, 19.0, 77.0, 34.0, 95.0, 22.0, 71.0, 88.0,
|
||||
56.0, 27.0, 64.0, 90.0, 18.0, 43.0, 79.0, 31.0, 85.0, 25.0, 38.0, 92.0, 70.0, 13.0,
|
||||
];
|
||||
|
||||
/// Item weights.
|
||||
const WEIGHTS: [f64; N_ITEMS] = [
|
||||
35.0, 58.0, 22.0, 71.0, 14.0, 86.0, 31.0, 53.0, 78.0, 19.0,
|
||||
44.0, 16.0, 67.0, 88.0, 25.0, 51.0, 33.0, 74.0, 12.0, 47.0,
|
||||
63.0, 28.0, 91.0, 36.0, 55.0, 17.0, 82.0, 41.0, 24.0, 68.0,
|
||||
35.0, 58.0, 22.0, 71.0, 14.0, 86.0, 31.0, 53.0, 78.0, 19.0, 44.0, 16.0, 67.0, 88.0, 25.0, 51.0,
|
||||
33.0, 74.0, 12.0, 47.0, 63.0, 28.0, 91.0, 36.0, 55.0, 17.0, 82.0, 41.0, 24.0, 68.0,
|
||||
];
|
||||
|
||||
/// Capacity = roughly half the total weight (standard Zitzler-Thiele convention).
|
||||
@@ -79,16 +76,16 @@ impl Problem for BiKnapsack {
|
||||
}
|
||||
|
||||
fn evaluate(&self, take: &Vec<bool>) -> Evaluation {
|
||||
let (pa, pb, w) = take.iter().enumerate().fold(
|
||||
(0.0_f64, 0.0_f64, 0.0_f64),
|
||||
|(pa, pb, w), (i, &t)| {
|
||||
if t {
|
||||
(pa + PROFITS_A[i], pb + PROFITS_B[i], w + WEIGHTS[i])
|
||||
} else {
|
||||
(pa, pb, w)
|
||||
}
|
||||
},
|
||||
);
|
||||
let (pa, pb, w) =
|
||||
take.iter()
|
||||
.enumerate()
|
||||
.fold((0.0_f64, 0.0_f64, 0.0_f64), |(pa, pb, w), (i, &t)| {
|
||||
if t {
|
||||
(pa + PROFITS_A[i], pb + PROFITS_B[i], w + WEIGHTS[i])
|
||||
} else {
|
||||
(pa, pb, w)
|
||||
}
|
||||
});
|
||||
// Penalty: large coefficient on weight overrun, applied to both objectives.
|
||||
let overrun = (w - self.cap).max(0.0);
|
||||
let penalty = 1000.0 * overrun;
|
||||
@@ -122,7 +119,10 @@ struct OnePointCrossoverBool;
|
||||
|
||||
impl Variation<Vec<bool>> for OnePointCrossoverBool {
|
||||
fn vary(&mut self, parents: &[Vec<bool>], rng: &mut Rng) -> Vec<Vec<bool>> {
|
||||
assert!(parents.len() >= 2, "OnePointCrossoverBool requires 2 parents");
|
||||
assert!(
|
||||
parents.len() >= 2,
|
||||
"OnePointCrossoverBool requires 2 parents"
|
||||
);
|
||||
let p1 = &parents[0];
|
||||
let p2 = &parents[1];
|
||||
assert_eq!(p1.len(), p2.len(), "parent lengths differ");
|
||||
@@ -154,14 +154,19 @@ fn main() {
|
||||
RandomBinary { n: N_ITEMS },
|
||||
CompositeVariation {
|
||||
crossover: OnePointCrossoverBool,
|
||||
mutation: BitFlipMutation { probability: 1.0 / N_ITEMS as f64 },
|
||||
mutation: BitFlipMutation {
|
||||
probability: 1.0 / N_ITEMS as f64,
|
||||
},
|
||||
},
|
||||
);
|
||||
let result = optimizer.run(&problem);
|
||||
|
||||
println!("Bi-objective 0/1 knapsack — Zitzler–Thiele style, 30 items");
|
||||
println!("Capacity = {:.0} (≈ half of total weight {:.0})",
|
||||
cap, WEIGHTS.iter().sum::<f64>());
|
||||
println!(
|
||||
"Capacity = {:.0} (≈ half of total weight {:.0})",
|
||||
cap,
|
||||
WEIGHTS.iter().sum::<f64>()
|
||||
);
|
||||
println!();
|
||||
println!("Total evaluations: {}", result.evaluations);
|
||||
println!("Pareto-front size: {}", result.pareto_front.len());
|
||||
@@ -202,5 +207,8 @@ fn main() {
|
||||
let owned: Vec<Candidate<Vec<bool>>> = result.pareto_front.to_vec();
|
||||
let hv = hypervolume_2d(&owned, &problem.objectives(), ref_point);
|
||||
println!();
|
||||
println!("Hypervolume vs. reference (profit_A=0, profit_B=0): {:.0}", hv);
|
||||
println!(
|
||||
"Hypervolume vs. reference (profit_A=0, profit_B=0): {:.0}",
|
||||
hv
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user