Skip to content

Commit

Permalink
feat(jssp): parameterize delay computation (#454)
Browse files Browse the repository at this point in the history
- Add some important comments
- Parameterize delay_g calculation

<!-- If applicable - remember to add the PR to the EA Rust project (ONLY
IF THERE IS NO LINKED ISSUE) -->

## Description

<!-- Please describe the motivation & changes introduced by this PR -->

## Linked issues

<!-- Please use "Resolves #<issue_no> syntax in case this PR should be
linked to an issue -->

## Important implementation details

<!-- if any, optional section -->
  • Loading branch information
kkafar authored Oct 31, 2023
1 parent 8c648e1 commit 0c06ae8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
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

0 comments on commit 0c06ae8

Please sign in to comment.