-
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 #158 from mahf-opt/state-refactors
- Loading branch information
Showing
42 changed files
with
593 additions
and
509 deletions.
There are no files selected for viewing
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
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,32 +1,29 @@ | ||
use mahf::framework::Random; | ||
use mahf::prelude::*; | ||
use problems::bmf::BenchmarkFunction; | ||
|
||
type P = problems::bmf::BenchmarkFunction; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let problem = P::sphere(10); | ||
let config = pso::real_pso( | ||
fn main() { | ||
// Specify the problem: Sphere function with 10 dimensions. | ||
let problem: BenchmarkFunction = BenchmarkFunction::sphere(/*dim: */ 10); | ||
// Specify the metaheuristic: Particle Swarm Optimization (pre-implemented in MAHF). | ||
let config: Configuration<BenchmarkFunction> = pso::real_pso( | ||
/*params: */ | ||
pso::RealProblemParameters { | ||
num_particles: 100, | ||
num_particles: 20, | ||
weight: 1.0, | ||
c_one: 1.0, | ||
c_two: 1.0, | ||
v_max: 1.0, | ||
}, | ||
termination::FixedIterations::new(500), | ||
/*termination: */ | ||
termination::FixedIterations::new(/*max_iterations: */ 500) | ||
& termination::DistanceToOpt::new(0.01), | ||
); | ||
|
||
let state = config.optimize_with(&problem, |state| state.insert(Random::seeded(0))); | ||
// Execute the metaheuristic on the problem with a random seed. | ||
let state: State<BenchmarkFunction> = config.optimize(&problem); | ||
|
||
println!( | ||
"Found Fitness: {:?}", | ||
state.best_objective_value::<P>().unwrap() | ||
); | ||
println!( | ||
"Found Individual: {:?}", | ||
state.best_individual::<P>().unwrap(), | ||
); | ||
// Print the results. | ||
println!("Found Individual: {:?}", state.best_individual().unwrap()); | ||
println!("This took {} iterations.", state.iterations()); | ||
println!("Global Optimum: {}", problem.known_optimum()); | ||
|
||
Ok(()) | ||
} |
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,26 +1,43 @@ | ||
use aco::ant_ops; | ||
use mahf::prelude::*; | ||
use problems::tsp::{self, SymmetricTsp}; | ||
use tracking::{files, functions, trigger}; | ||
|
||
type P = problems::tsp::SymmetricTsp; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let problem = problems::tsp::Instances::BERLIN52.load(); | ||
|
||
let config = ils::permutation_iterated_local_search( | ||
ils::PermutationProblemParameters { | ||
local_search_params: ls::PermutationProblemParameters { | ||
n_neighbors: 100, | ||
n_swap: 10, | ||
fn main() { | ||
// Specify the problem: TSPLIB instance Berlin52. | ||
let problem: SymmetricTsp = tsp::Instances::BERLIN52.load(); | ||
// Specify the metaheuristic: Ant System. | ||
let config: Configuration<SymmetricTsp> = Configuration::builder() | ||
.do_(initialization::Empty::new()) | ||
.while_( | ||
termination::FixedEvaluations::new(/*max_evaluations: */ 10_000), | ||
|builder| { | ||
builder | ||
.do_(ant_ops::AcoGeneration::new( | ||
/*num_ants: */ 20, /*alpha: */ 2.0, /*beta: */ 1.0, | ||
/*initial_pheromones: */ 0.0, | ||
)) | ||
.evaluate() | ||
.update_best_individual() | ||
.do_(ant_ops::AsPheromoneUpdate::new( | ||
/*evaporation: */ 0.2, /*decay_coefficient: */ 1.0, | ||
)) | ||
.do_(tracking::Logger::new()) | ||
}, | ||
local_search_termination: termination::FixedIterations::new(100), | ||
}, | ||
termination::FixedIterations::new(10), | ||
) | ||
.into_builder() | ||
.assert(|state| state.population_stack::<P>().current().len() == 1) | ||
.single_objective_summary() | ||
.build(); | ||
) | ||
.build(); | ||
|
||
config.optimize(&problem); | ||
// Execute the metaheuristic on the problem. | ||
let state: State<SymmetricTsp> = config.optimize_with(&problem, |state| { | ||
// Set the seed to 42. | ||
state.insert(Random::seeded(42)); | ||
// Log the best individual every 50 iterations. | ||
state.insert( | ||
tracking::LogSet::<SymmetricTsp>::new() | ||
.with(trigger::Iteration::new(50), functions::best_individual), | ||
); | ||
}); | ||
|
||
Ok(()) | ||
// Save the log to file "aco_berlin52.log". | ||
files::write_log_file("aco_berlin52.log", state.log()).unwrap(); | ||
} |
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
Oops, something went wrong.