Skip to content

Commit 7f95382

Browse files
committed
fix: readme typo, poor rng handling, small tweaks
Signed-off-by: hashcatHitman <155700084+hashcatHitman@users.noreply.github.com>
1 parent 364c0ce commit 7f95382

4 files changed

Lines changed: 31 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SPDX-License-Identifier: Apache-2.0 OR MIT
1616

1717
#### Botania
1818

19-
_Botania_ is a tech mod for _Minecraft_ created by Vazkii,with a focus on
19+
_Botania_ is a tech mod for _Minecraft_ created by Vazkii, with a focus on
2020
"natural magic". The primary resource in the mod is "mana", which is primarily
2121
obtained by means of specially crafted "generating flora". Some are trivial,
2222
such as "Hydroangeas", which consume nearby water and slowly produce mana.

src/bees.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! 2 flower_patch[i] = Site_abandonment(flower_patch[i])
1818
//! 3 flower_patch[i] = Neighbourhood_shrinking(flower_patch[i])
1919
//! iii for i = nb, ..., ns
20-
//! 1 flower_patch[i] = Global_search(flower_patch[i])}
20+
//! 1 flower_patch[i] = Global_search(flower_patch[i])
2121
//! ```
2222
2323
use core::array;
@@ -95,10 +95,11 @@ where
9595

9696
/// Explore random nearby [`Colony::Flower`]s based on the radius size. The
9797
/// current best is also provided for consideration.
98-
fn explore(
98+
fn explore<R: Rng>(
9999
origin: &Self::Flower,
100100
current_best: &Scout<N_SCOUTS, Self>,
101101
radius: usize,
102+
rng: &mut R,
102103
) -> Self::Flower;
103104

104105
/// Return `true` when it is time to stop searching. Use the implementor of
@@ -174,7 +175,7 @@ where
174175
reason = "index is correct if invariants are upheld"
175176
)]
176177
for i in 0..Self::BEST_SITES {
177-
flower_patches[i].local_search(&current_best);
178+
flower_patches[i].local_search(&current_best, rng);
178179
flower_patches[i].abandonment(&mut current_best, rng);
179180
flower_patches[i].shrinking();
180181
}
@@ -287,13 +288,18 @@ where
287288
}
288289

289290
/// Have the foragers explore nearby [`Colony::Flower`]s.
290-
pub(crate) fn local_search(&mut self, current_best: &Scout<NS, Hive>) {
291+
pub(crate) fn local_search<R: Rng>(
292+
&mut self,
293+
current_best: &Scout<NS, Hive>,
294+
rng: &mut R,
295+
) {
291296
self.stagnation = true;
292297
for _ in 0..self.foragers {
293298
let new_solution = Hive::explore(
294299
&self.scout.solution,
295300
current_best,
296301
self.neighbourhood,
302+
rng,
297303
);
298304
let new_scout: Scout<NS, Hive> = Scout::with_solution(new_solution);
299305
if new_scout.fitness < self.scout.fitness {
@@ -327,6 +333,11 @@ where
327333
} else {
328334
if self.scout.fitness < current_best.fitness {
329335
*current_best = self.scout;
336+
// Just in case...
337+
println!(
338+
"New best during abandonment:\n\tFitness:{}\n\tSolution:\n{}",
339+
current_best.fitness, current_best.solution
340+
);
330341
}
331342
*self = Self::new(rng);
332343
}

src/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use core::cmp::Ordering;
99
use core::fmt::{Display, Formatter, Result};
1010

11-
use rand::rngs::SmallRng;
11+
use rand::Rng;
1212
use rand::seq::SliceRandom as _;
13-
use rand::{Rng as _, SeedableRng as _};
1413

1514
use crate::bees::{Colony, Scout};
1615
use crate::simulation::PetriDish;
@@ -105,28 +104,24 @@ impl Colony<60> for Hive {
105104
}
106105
}
107106

108-
fn explore(
107+
fn explore<R: Rng>(
109108
origin: &Self::Flower,
110109
current_best: &Scout<60, Self>,
111110
radius: usize,
111+
rng: &mut R,
112112
) -> Self::Flower {
113-
let mut rng = SmallRng::seed_from_u64(42);
114113
if radius == (25 * 25) {
115114
let new: Self::Flower = rng.random();
116115
return new;
117116
}
118-
let mut base: Self::Flower = *origin;
119-
120-
if radius <= Self::MINIMUM_RADIUS {
121-
if rng.random_bool(0.5) {
122-
base = Self::Flower::OPTIMAL_100_ROUND;
123-
} else {
124-
base = current_best.solution();
125-
}
126-
}
117+
let mut base: Self::Flower = if radius <= Self::MINIMUM_RADIUS {
118+
current_best.solution()
119+
} else {
120+
*origin
121+
};
127122
let mut coords: [(u8, u8); 624] = Self::Flower::NONCENTER_COORDS;
128123

129-
coords.shuffle(&mut rng);
124+
coords.shuffle(rng);
130125

131126
for (x, y) in coords.into_iter().take(radius) {
132127
let value: u8 = rng.random_range(0..3);
@@ -136,13 +131,7 @@ impl Colony<60> for Hive {
136131
}
137132

138133
fn stopping_condition(&mut self) -> bool {
139-
if self.iters < 5000_usize {
140-
self.iters = self.iters.saturating_add(1);
141-
false
142-
} else {
143-
true
144-
}
145-
// self.iters += 1;
146-
// false
134+
// Just keep going.
135+
false
147136
}
148137
}

src/simulation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl PetriDish {
4848
/// algorithm in 2016.
4949
///
5050
/// [the setup found by Cobra1117]: https://www.reddit.com/r/botania/comments/5by0jl/optimal_100round_dandelifeon_setup/
51+
#[expect(
52+
dead_code,
53+
reason = "Didn't want to use this when generating solutions to present."
54+
)]
5155
pub(crate) const OPTIMAL_100_ROUND: Self = {
5256
let mut current_record: Self = Self::new();
5357

0 commit comments

Comments
 (0)