Skip to content

Commit

Permalink
reduce calls of sets
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinkuiper committed Sep 2, 2024
1 parent 8cf5abf commit 8fb92ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
12 changes: 5 additions & 7 deletions dofus_set/src/anneal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ pub trait Anneal<T> {
fn random(&self) -> f64;

fn temperature(&self, iteration: f64, energy: f64) -> f64;
fn energy(&self, state: &T) -> f64;
fn neighbour(&self, state: &T, temperature: f64) -> Result<T, Self::Error>;
fn neighbour(&self, state: &T, temperature: f64) -> Result<(T, f64), Self::Error>;

fn optimise(&self, initial_state: T, num_iterations: i64) -> Result<T, Self::Error> {
fn optimise(&self, initial_state: (T, f64), num_iterations: i64) -> Result<T, Self::Error> {
let number_of_iterations = num_iterations as f64;
let mut current_state = initial_state;
let mut current_state_energy = self.energy(&current_state);
let mut current_state = initial_state.0;
let mut current_state_energy = initial_state.1;
for iteration in 0..num_iterations {
let iteration = iteration as f64;
let temperature = self.temperature(
(iteration + 1.0) / number_of_iterations,
current_state_energy,
);
let neighbour = self.neighbour(&current_state, temperature)?;
let neighbour_energy = self.energy(&neighbour);
let (neighbour, neighbour_energy) = self.neighbour(&current_state, temperature)?;
let acceptance_rate =
Self::accept_probability(current_state_energy, neighbour_energy, temperature);
if acceptance_rate >= self.random() {
Expand Down
43 changes: 24 additions & 19 deletions dofus_set/src/dofus_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct SetBonus<'a> {
pub bonus: &'a Characteristic,
pub number_of_items: i32,
}
type SetBonusList<'a> = heapless::Vec<SetBonus<'a>, MAX_SETS>;

const MAX_SETS: usize = 12;

Expand All @@ -82,7 +83,7 @@ impl State {
self.set.iter().copied()
}

pub fn sets<'a>(&self, items: &'a Items) -> heapless::Vec<SetBonus<'a>, MAX_SETS> {
pub fn sets<'a>(&self, items: &'a Items) -> SetBonusList<'a> {
let mut sets_linear_map: heapless::Vec<(SetIndex, i32), MAX_SETS> = heapless::Vec::new();

for item in self.items(items) {
Expand Down Expand Up @@ -114,11 +115,16 @@ impl State {
.collect()
}

fn valid(&self, config: &config::Config, items: &Items, leniency: i32) -> bool {
fn valid(
&self,
config: &config::Config,
items: &Items,
leniency: i32,
sets: &SetBonusList,
) -> bool {
let mut total_set_bonuses = 0;
let sets = self.sets(items);

for set_bonus in &sets {
for set_bonus in sets {
total_set_bonuses += set_bonus.number_of_items - 1;
}

Expand Down Expand Up @@ -163,11 +169,7 @@ impl State {
true
}

pub fn energy(
&self,
config: &config::Config,
sets: &heapless::Vec<SetBonus<'_>, MAX_SETS>,
) -> f64 {
pub fn energy(&self, config: &config::Config, sets: &SetBonusList) -> f64 {
let stats = self.stats(config, sets);
// need to take the negative due to being a minimiser
let energy_non_element = stats
Expand Down Expand Up @@ -301,7 +303,8 @@ impl<'a> Optimiser<'a> {
items: &'a Items,
) -> Result<Optimiser<'a>, OptimiseError> {
let initial_state: State = State::new_from_initial_equipment(initial_set, items)?;
if !initial_state.valid(config, items, 1000) {
let sets = initial_state.sets(items);
if !initial_state.valid(config, items, 1000, &sets) {
return Err(OptimiseError::InvalidState);
}

Expand Down Expand Up @@ -340,7 +343,11 @@ impl<'a> Optimiser<'a> {
{
return Ok(self.initial_state);
}
anneal::Anneal::optimise(&self, self.initial_state.clone(), 1_000_000)

let sets = self.initial_state.sets(self.items);
let energy = self.initial_state.energy(&self.config, &sets);

anneal::Anneal::optimise(&self, (self.initial_state.clone(), energy), 1_000_000)
}
}

Expand All @@ -364,7 +371,7 @@ impl<'a> anneal::Anneal<State> for Optimiser<'a> {
rand::thread_rng().gen_range(0.0..1.0)
}

fn neighbour(&self, state: &State, temperature: f64) -> Result<State, OptimiseError> {
fn neighbour(&self, state: &State, temperature: f64) -> Result<(State, f64), OptimiseError> {
let mut attempts = 0;
let mut rng = rand::thread_rng();
loop {
Expand All @@ -385,8 +392,11 @@ impl<'a> anneal::Anneal<State> for Optimiser<'a> {
new_state.add_item(&self.items[item]);

new_state.set[item_slot] = Some(item);
if new_state.valid(self.config, self.items, temperature as i32) {
return Ok(new_state);
let sets = new_state.sets(self.items);

if new_state.valid(self.config, self.items, temperature as i32, &sets) {
let energy = new_state.energy(self.config, &sets);
return Ok((new_state, energy));
}
attempts += 1;
if attempts > 1000 {
Expand All @@ -395,11 +405,6 @@ impl<'a> anneal::Anneal<State> for Optimiser<'a> {
}
}

fn energy(&self, state: &State) -> f64 {
let sets = state.sets(self.items);
state.energy(self.config, &sets)
}

fn temperature(&self, iteration: f64, _energy: f64) -> f64 {
self.temperature_initial
* std::f64::consts::E
Expand Down

0 comments on commit 8fb92ab

Please sign in to comment.