Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jssp): parameterize delay computation #454

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/jssp/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct Args {
#[arg(long = "popsize")]
pub pop_size: Option<usize>,

/// The constant that appears in formula for delay in given iteration g.
/// Delay = Gene_{n+g} * delay_const_factor * maxdur. If not specified, defaults to 1.5.
pub delay_const_factor: Option<f64>,

/// Path to config file with solver's parameters
#[arg(short = 'c', long = "config")]
pub cfg_file: Option<PathBuf>,
Expand Down
21 changes: 18 additions & 3 deletions examples/jssp/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{fs::File, error::Error, io::{BufReader, self}, path::{PathBuf, Path}, fmt::Display};
use std::{
error::Error,
fmt::Display,
fs::File,
io::{self, BufReader},
path::{Path, PathBuf},
};

use serde::Deserialize;

Expand All @@ -19,6 +25,10 @@ pub struct Config {
/// Number of individuals in population. Set this if you want to
/// override the default value computed basing on problem size
pub pop_size: Option<usize>,

/// The constant that appears in formula for delay in given iteration g.
/// Delay = Gene_{n+g} * delay_const_factor * maxdur. If not specified, defaults to 1.5.
pub delay_const_factor: Option<f64>,
}

#[derive(Deserialize, Debug, Clone)]
Expand All @@ -27,6 +37,7 @@ pub struct PartialConfig {
pub output_dir: Option<PathBuf>,
pub n_gen: Option<usize>,
pub pop_size: Option<usize>,
pub delay_const_factor: Option<f64>,
}

impl PartialConfig {
Expand All @@ -36,6 +47,7 @@ impl PartialConfig {
output_dir: None,
n_gen: None,
pop_size: None,
delay_const_factor: None,
}
}
}
Expand Down Expand Up @@ -65,6 +77,7 @@ impl TryFrom<PartialConfig> for Config {
output_dir: partial_cfg.output_dir.unwrap(),
n_gen: partial_cfg.n_gen,
pop_size: partial_cfg.pop_size,
delay_const_factor: partial_cfg.delay_const_factor,
})
}
}
Expand All @@ -76,7 +89,7 @@ impl TryFrom<Args> for Config {
let mut partial_cfg = if let Some(ref cfg_file) = args.cfg_file {
match PartialConfig::try_from(cfg_file.to_owned()) {
Ok(cfg) => cfg,
Err(err) => return Err(format!("Error while loading config from file: {}", err))
Err(err) => return Err(format!("Error while loading config from file: {}", err)),
}
} else {
PartialConfig::empty()
Expand All @@ -94,8 +107,10 @@ impl TryFrom<Args> for Config {
if let Some(pop_size) = args.pop_size {
partial_cfg.pop_size = Some(pop_size);
}
if let Some(factor) = args.delay_const_factor {
partial_cfg.delay_const_factor = Some(factor)
}

Config::try_from(partial_cfg)
}
}

2 changes: 1 addition & 1 deletion examples/jssp/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn run_with_ecrs(instance: JsspInstance, config: Config) {
.set_mutation_operator(mutation::Identity::new())
.set_population_generator(JsspPopProvider::new(instance.clone()))
.set_replacement_operator(JsspReplacement::new(JsspPopProvider::new(instance), 0.1, 0.2))
.set_fitness(JsspFitness::new())
.set_fitness(JsspFitness::new(1.5))
.set_probe(probe)
// .set_max_duration(std::time::Duration::from_secs(30))
.set_max_generation_count(n_gen)
Expand Down
9 changes: 7 additions & 2 deletions examples/jssp/problem/fitness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ pub struct JsspFitness {
// To put this in other way: all jobs that can be scheduled in time window considered in
// given iteration g.
delay_feasibles: Vec<usize>,

/// The constant used to compute delay for given iteration g. The default value used in paper
/// is 1.5.
delay_const: f64,
}

impl JsspFitness {
pub fn new() -> Self {
pub fn new(delay_constant: f64) -> Self {
Self {
delay_feasibles: Vec::new(),
delay_const: delay_constant,
}
}

Expand Down Expand Up @@ -131,7 +136,7 @@ impl JsspFitness {

#[inline(always)]
fn delay_for_g(&self, indv: &JsspIndividual, n: usize, g: usize, maxdur: usize) -> f64 {
indv.chromosome[n + g - 1] * 1.5 * (maxdur as f64)
indv.chromosome[n + g - 1] * self.delay_const * (maxdur as f64)
}

#[inline(always)]
Expand Down
9 changes: 9 additions & 0 deletions examples/jssp/problem/replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl ReplacementOperator<JsspIndividual> for JsspReplacement {
population.sort();
}

// Divergence from the papaer here. They do sample children population randomly (or just
// create just right amount of children).
//
// This implementation is biased, because children parents are selected from left-to-right
// (thus from best to worst) and the offspring is put from left-to-right to the children
// arrary & I'm copying here children from left to right ==> only children of better
// individuals make it.
//
// Selection of parents is also done differently to the paper.
for i in elite_size..(elite_size + crossover_size) {
std::mem::swap(&mut population[i], &mut children[i - elite_size]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ga/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ impl Timer {
pub(crate) fn start(&mut self) {
self.start_time = std::time::Instant::now()
}

#[inline]
pub(crate) fn elapsed(&self) -> std::time::Duration {
self.start_time.elapsed()
}
Expand Down