Skip to content

Commit

Permalink
Calculate the sets once
Browse files Browse the repository at this point in the history
  • Loading branch information
gwilymk committed Aug 26, 2024
1 parent e16ba11 commit 306fb55
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ fn main() {
let optimiser = Optimiser::new(&config, initial_set, &items).unwrap();

let final_state = optimiser.optimise().unwrap();
println!("Set Energy: {}", -final_state.energy(&config, &items));
let sets = final_state.sets(&items);
println!("Set Energy: {}", -final_state.energy(&config, &sets));
}
}

Expand Down
25 changes: 18 additions & 7 deletions dofus_set/src/dofus_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ impl State {

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

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

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

for item in self.items(items) {
if item.level > config.max_level {
Expand Down Expand Up @@ -165,8 +167,12 @@ impl State {
true
}

pub fn energy(&self, config: &config::Config, items: &Items) -> f64 {
let stats = self.stats(config, items);
pub fn energy(
&self,
config: &config::Config,
sets: &heapless::Vec<SetBonus<'_>, MAX_SETS>,
) -> f64 {
let stats = self.stats(config, sets);
// need to take the negative due to being a minimiser
let energy_non_element = stats
.iter()
Expand Down Expand Up @@ -232,10 +238,14 @@ impl State {
self.cached_totals += &item.stats;
}

pub fn stats(&self, config: &config::Config, items: &Items) -> Characteristic {
pub fn stats(
&self,
config: &config::Config,
sets: &heapless::Vec<SetBonus<'_>, MAX_SETS>,
) -> Characteristic {
let mut stat = self.cached_totals.clone();

for set_bonus in self.sets(items) {
for set_bonus in sets {
stat += set_bonus.bonus;
}

Expand Down Expand Up @@ -397,7 +407,8 @@ impl<'a> anneal::Anneal<State> for Optimiser<'a> {
}

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

fn temperature(&self, iteration: f64, _energy: f64) -> f64 {
Expand Down
6 changes: 4 additions & 2 deletions dofus_set/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ fn main() {

let final_state = optimiser.optimise().unwrap();
print_state(&final_state, &config, &items);
println!("Set Energy: {}", -final_state.energy(&config, &items));
let sets = final_state.sets(&items);
println!("Set Energy: {}", -final_state.energy(&config, &sets));
}

pub fn print_state(state: &State, config: &config::Config, items: &Items) {
Expand All @@ -53,7 +54,8 @@ pub fn print_state(state: &State, config: &config::Config, items: &Items) {
}
println!("Stats");
println!("-----------------------------");
print_stats(&state.stats(config, items));
let sets = state.sets(items);
print_stats(&state.stats(config, &sets));
println!("\nSet bonuses");
println!("-----------------------------");
for set_bonus in state.sets(items) {
Expand Down
9 changes: 5 additions & 4 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ fn create_optimised_set(

let final_state = optimiser.optimise()?;

let set_bonuses = final_state
.sets(items)
let sets = final_state.sets(items);

let set_bonuses = sets
.iter()
.map(|set| OptimiseResponseSetBonus {
name: set.name.to_owned(),
Expand All @@ -122,8 +123,8 @@ fn create_optimised_set(
.collect();

Ok(OptimiseResponse {
energy: -final_state.energy(&dofus_set_config, items),
overall_characteristics: final_state.stats(&dofus_set_config, items).clone(),
energy: -final_state.energy(&dofus_set_config, &sets),
overall_characteristics: final_state.stats(&dofus_set_config, &sets).clone(),
items: final_state
.set()
.map(|idx| {
Expand Down

0 comments on commit 306fb55

Please sign in to comment.