-
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 #27 from s-simoncelli/add-adaptive-nsga3
Add adaptive nsga3
- Loading branch information
Showing
32 changed files
with
5,332 additions
and
61 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::env; | ||
use std::error::Error; | ||
use std::path::PathBuf; | ||
|
||
use log::LevelFilter; | ||
|
||
use optirustic::algorithms::{ | ||
AdaptiveNSGA3, Algorithm, MaxGeneration, NSGA3Arg, Nsga3NumberOfIndividuals, | ||
StoppingConditionType, | ||
}; | ||
use optirustic::core::builtin_problems::DTLZ1Problem; | ||
use optirustic::operators::SimulatedBinaryCrossoverArgs; | ||
use optirustic::utils::NumberOfPartitions; | ||
|
||
/// Solve the inverted DTLZ1 problem from Deb et al. (2013) with 3 objectives. This is a problem where the | ||
/// optimal solutions or objectives lie on the hyper-plane passing through the intercept point | ||
/// at 0.5 on each objective axis. This code replicates the first testing problem in Deb et al. | ||
/// (2013). | ||
/// | ||
/// Make sure to compile this in release mode to speed up the calculation: | ||
/// | ||
/// `cargo run --example nsga3_inverted_dtlz1 -p optirustic --release` | ||
fn main() -> Result<(), Box<dyn Error>> { | ||
// Add log | ||
env_logger::builder().filter_level(LevelFilter::Info).init(); | ||
|
||
let number_objectives: usize = 3; | ||
let k: usize = 5; | ||
// Set the number of variables to use in the DTLZ1 problem | ||
let number_variables: usize = number_objectives + k - 1; | ||
// Get the built-in problem | ||
let problem = DTLZ1Problem::create(number_variables, number_objectives, true)?; | ||
|
||
// Set the number of partitions to create the reference points for the NSGA3 algorithm. This | ||
// uses one layer of 12 uniform gaps | ||
let number_of_partitions = NumberOfPartitions::OneLayer(12); | ||
|
||
// Customise the SBX and PM operators like in the paper | ||
let crossover_operator_options = SimulatedBinaryCrossoverArgs { | ||
distribution_index: 30.0, | ||
crossover_probability: 1.0, | ||
..SimulatedBinaryCrossoverArgs::default() | ||
}; | ||
|
||
// Set up the adaptive NSGA3 algorithm | ||
let args = NSGA3Arg { | ||
// number of individuals from the paper (possibly equal to number of reference points) | ||
number_of_individuals: Nsga3NumberOfIndividuals::Custom(92), | ||
number_of_partitions, | ||
crossover_operator_options: Some(crossover_operator_options), | ||
mutation_operator_options: None, | ||
// stop at generation 400 | ||
stopping_condition: StoppingConditionType::MaxGeneration(MaxGeneration(400)), | ||
parallel: None, | ||
export_history: None, | ||
// to reproduce results | ||
seed: Some(1), | ||
}; | ||
|
||
// Initialise the algorithm | ||
let mut algo = AdaptiveNSGA3::new(problem, args).unwrap(); | ||
|
||
// Run the algorithm | ||
algo.run()?; | ||
|
||
// Export the last results to a JSON file | ||
let destination = PathBuf::from(&env::current_dir().unwrap()) | ||
.join("examples") | ||
.join("results"); | ||
|
||
algo.save_to_json(&destination, Some("DTLZ1_3obj_Adaptive"))?; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from optirustic import NSGA3 | ||
|
||
# Generate a 3D Pareto front charts and objective vs. reference point charts | ||
file = Path(__file__).parent / "results" / "DTLZ1_3obj_Adaptive_NSGA3_gen400.json" | ||
data = NSGA3(file.as_posix()) | ||
|
||
# Generate Pareto front chart | ||
data.plot() | ||
plt.gca().view_init( | ||
azim=60, | ||
elev=20, | ||
) | ||
plt.savefig(file.parent / f"{file.stem}_Pareto_front.png") | ||
|
||
# Generate a chart with the normalised objectives against the reference points | ||
# The plane is limited in the [0, 1] range. | ||
normalised_objectives = [ind.data["normalised_objectives"] for ind in data.individuals] | ||
normalised_objectives = np.array(normalised_objectives) | ||
obj_names = data.problem.objective_names | ||
|
||
ref_points = data.additional_data["reference_points"] | ||
ref_points = np.array(ref_points) | ||
|
||
fig = plt.figure() | ||
ax = plt.axes(projection="3d") | ||
|
||
ax.scatter( | ||
ref_points[:91, 0], | ||
ref_points[:91, 1], | ||
ref_points[:91, 2], | ||
color="r", | ||
marker="x", | ||
s=20, | ||
label="Original reference points", | ||
) | ||
ax.scatter( | ||
ref_points[91:, 0], | ||
ref_points[91:, 1], | ||
ref_points[91:, 2], | ||
color="b", | ||
marker="x", | ||
s=20, | ||
label="Adaptive reference points", | ||
) | ||
ax.scatter( | ||
normalised_objectives[:, 0], | ||
normalised_objectives[:, 1], | ||
normalised_objectives[:, 2], | ||
color="k", | ||
marker=".", | ||
label="Normalised objectives", | ||
) | ||
|
||
ax.set_xlabel(obj_names[0]) | ||
ax.set_ylabel(obj_names[1]) | ||
ax.set_zlabel(obj_names[2]) | ||
ax.view_init(azim=10) | ||
|
||
plt.legend() | ||
plt.title( | ||
f"Normalised objectives vs. reference points \n" | ||
f"for {data.algorithm} @ generation={data.generation}" | ||
) | ||
plt.savefig(file.parent / f"{file.stem}_obj_vs_ref_points.png") |
Oops, something went wrong.