Skip to content

Commit

Permalink
Remove dumping population
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Oct 18, 2024
1 parent 02a7aac commit ae98950
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 125 deletions.
3 changes: 1 addition & 2 deletions examples/data/config/config.full.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@
"progress": {
"enabled": true,
"logBest": 100,
"logPopulation": 1000,
"dumpPopulation": false
"logPopulation": 1000
},
"metrics": {
"enabled": false,
Expand Down
3 changes: 1 addition & 2 deletions examples/data/config/config.telemetry.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"progress": {
"enabled": true,
"logBest": 100,
"logPopulation": 1000,
"dumpPopulation": false
"logPopulation": 1000
},
"metrics": {
"enabled": true,
Expand Down
12 changes: 0 additions & 12 deletions experiments/heuristic-research/src/solver/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use rosomaxa::prelude::*;
use std::any::TypeId;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::MutexGuard;
use vrp_scientific::core::construction::heuristics::InsertionContext;

Expand Down Expand Up @@ -169,17 +168,6 @@ where
}
}

impl<P, O, S> Display for ProxyPopulation<P, O, S>
where
P: HeuristicPopulation<Objective = O, Individual = S> + 'static,
O: HeuristicObjective<Solution = S> + Shuffled + 'static,
S: HeuristicSolution + RosomaxaWeighted + 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}

/// Creates info logger proxy to catch dynamic heuristic state.
pub fn create_info_logger_proxy(inner: InfoLogger) -> InfoLogger {
Arc::new(move |msg| {
Expand Down
3 changes: 1 addition & 2 deletions experiments/heuristic-research/src/solver/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ pub fn solve_function(
move |objective, environment| {
let population =
get_population(&population_type, objective.clone(), environment.clone(), selection_size);
let telemetry_mode =
TelemetryMode::OnlyLogging { logger, log_best: 100, log_population: 500, dump_population: false };
let telemetry_mode = TelemetryMode::OnlyLogging { logger, log_best: 100, log_population: 500 };
VectorContext::new(objective, population, telemetry_mode, environment)
}
}))
Expand Down
7 changes: 1 addition & 6 deletions experiments/heuristic-research/src/solver/vrp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ pub fn solve_vrp(
..Environment::new_with_time_quota(Some(300))
});
let population = get_population(population_type, problem.goal.clone(), environment.clone(), selection_size);
let telemetry_mode = TelemetryMode::OnlyLogging {
logger: logger.clone(),
log_best: 100,
log_population: 1000,
dump_population: false,
};
let telemetry_mode = TelemetryMode::OnlyLogging { logger: logger.clone(), log_best: 100, log_population: 1000 };

let config = VrpConfigBuilder::new(problem.clone())
.set_environment(environment.clone())
Expand Down
28 changes: 1 addition & 27 deletions rosomaxa/src/algorithms/gsom/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod state_test;

use super::*;
use crate::algorithms::gsom::Coordinate;
use std::fmt::{Display, Formatter, Result, Write};
use std::fmt::Write;
use std::ops::Range;

/// Represents state of the network.
Expand Down Expand Up @@ -83,29 +83,3 @@ where
},
)
}

impl Display for NetworkState {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
// NOTE serialize state in simple representation which can be embedded
// to json as string and then easily parsed.
let nodes = self.nodes.iter().fold(String::new(), |mut res, n| {
let (x, y) = n.coordinate;
let weights = n.weights.iter().map(|w| format!("{w:.7}")).collect::<Vec<_>>().join(",");

write!(
&mut res,
"({},{},{:.7},{},{},[{}],{}),",
x, y, n.unified_distance, n.total_hits, n.last_hits, weights, n.dump
)
.unwrap();

res
});

write!(
f,
"({},{},{},{},{},[{}])",
self.shape.0.start, self.shape.0.end, self.shape.1.start, self.shape.1.end, self.shape.2, nodes
)
}
}
27 changes: 7 additions & 20 deletions rosomaxa/src/evolution/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::algorithms::math::relative_distance;
use crate::prelude::*;
use crate::utils::Timer;
use crate::{DynHeuristicPopulation, RemedianUsize};
use std::fmt::Write;
use std::marker::PhantomData;

/// Encapsulates different measurements regarding algorithm evaluation.
Expand Down Expand Up @@ -66,8 +65,6 @@ pub enum TelemetryMode {
log_best: usize,
/// Specifies how often population is logged.
log_population: usize,
/// Specifies whether population should be dumped.
dump_population: bool,
},
/// Only metrics collection.
OnlyMetrics {
Expand All @@ -84,8 +81,6 @@ pub enum TelemetryMode {
log_population: usize,
/// Specifies how often population is tracked.
track_population: usize,
/// Specifies whether population should be dumped.
dump_population: bool,
},
}

Expand Down Expand Up @@ -166,14 +161,12 @@ where
termination_estimate,
};

let (log_best, log_population, track_population, should_dump_population) = match &self.mode {
let (log_best, log_population, track_population) = match &self.mode {
TelemetryMode::None => return,
TelemetryMode::OnlyLogging { log_best, log_population, dump_population, .. } => {
(Some(log_best), Some(log_population), None, *dump_population)
}
TelemetryMode::OnlyMetrics { track_population, .. } => (None, None, Some(track_population), false),
TelemetryMode::All { log_best, log_population, track_population, dump_population, .. } => {
(Some(log_best), Some(log_population), Some(track_population), *dump_population)
TelemetryMode::OnlyLogging { log_best, log_population, .. } => (Some(log_best), Some(log_population), None),
TelemetryMode::OnlyMetrics { track_population, .. } => (None, None, Some(track_population)),
TelemetryMode::All { log_best, log_population, track_population, .. } => {
(Some(log_best), Some(log_population), Some(track_population))
}
};

Expand All @@ -189,7 +182,7 @@ where
)
}

self.on_population(population, should_log_population, should_track_population, should_dump_population);
self.on_population(population, should_log_population, should_track_population);
} else {
self.log("no progress yet");
}
Expand All @@ -201,7 +194,6 @@ where
population: &DynHeuristicPopulation<O, S>,
should_log_population: bool,
should_track_population: bool,
should_dump_population: bool,
) {
if !should_log_population && !should_track_population {
return;
Expand Down Expand Up @@ -233,11 +225,6 @@ where

if should_log_population {
individuals.iter().for_each(|metrics| self.log_individual(metrics, None));
if should_dump_population {
let mut state = String::new();
write!(state, "{population}").unwrap();
self.log(&format!("\t{state}"));
}
}

if should_track_population {
Expand All @@ -263,7 +250,7 @@ where
_ => return,
};

self.on_population(population, should_log_population, should_track_population, false);
self.on_population(population, should_log_population, should_track_population);

let elapsed = self.time.elapsed_secs() as usize;
let speed = generations as Float / self.time.elapsed_secs_as_float();
Expand Down
1 change: 0 additions & 1 deletion rosomaxa/src/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ impl Solver {
logger: environment.logger.clone(),
log_best: 100,
log_population: 500,
dump_population: false,
},
environment.clone(),
)
Expand Down
2 changes: 1 addition & 1 deletion rosomaxa/src/population/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum SelectionPhase {
}

/// A trait which models a population with individuals.
pub trait HeuristicPopulation: Display + Send + Sync {
pub trait HeuristicPopulation: Send + Sync {
/// A heuristic objective type.
type Objective: HeuristicObjective;
/// A solution type.
Expand Down
16 changes: 0 additions & 16 deletions rosomaxa/src/population/rosomaxa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,6 @@ where
}
}

impl<O, S> Display for Rosomaxa<O, S>
where
O: HeuristicObjective<Solution = S> + Shuffled,
S: HeuristicSolution + RosomaxaWeighted,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.phase {
RosomaxaPhases::Exploration { network, .. } => {
let state = get_network_state(network);
write!(f, "{state}")
}
_ => write!(f, "{}", self.elite),
}
}
}

impl<'a, O, S> TryFrom<&'a Rosomaxa<O, S>> for NetworkState
where
O: HeuristicObjective<Solution = S> + Shuffled,
Expand Down
10 changes: 0 additions & 10 deletions rosomaxa/tests/unit/algorithms/gsom/state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,3 @@ fn can_get_state() {
assert_eq!(state.nodes.len(), 4);
assert_eq!(state.shape, (0..1, 0..1, 3));
}

#[test]
fn can_format_state() {
let network = create_test_network(false);
let state = get_network_state(&network);

let result = format!("{state}");

assert!(result.starts_with("(0,1,0,1,3,[("));
}
10 changes: 0 additions & 10 deletions rosomaxa/tests/unit/population/rosomaxa_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ fn can_optimize_network() {
assert!(get_network(&rosomaxa).get_nodes().next().is_some());
}

#[test]
fn can_format_network() {
let (objective, mut rosomaxa) = create_rosomaxa(4);
rosomaxa.add_all(vec![VectorSolution::new_with_objective(vec![0.5, 0.5], objective.as_ref())]);

let str = format!("{rosomaxa}");

assert_eq!(str, "[[6.5000000],]");
}

#[test]
fn can_handle_empty_population() {
let (_, mut rosomaxa) = create_rosomaxa(10);
Expand Down
22 changes: 8 additions & 14 deletions vrp-cli/src/extensions/solve/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,6 @@ pub struct ProgressConfig {
log_best: Option<usize>,
/// Specifies how often population is logged. Default is 1000 (generations).
log_population: Option<usize>,
/// Specifies whether population should be dumped.
dump_population: Option<bool>,
}

#[derive(Clone, Deserialize, Debug)]
Expand Down Expand Up @@ -723,32 +721,28 @@ fn get_telemetry_mode(environment: Arc<Environment>, telemetry_config: &Option<T
track_population: track_population.unwrap_or(TRACK_POPULATION),
};

let create_progress = |log_best: &Option<usize>, log_population: &Option<usize>, dump_population: &Option<bool>| {
TelemetryMode::OnlyLogging {
logger: environment.logger.clone(),
log_best: log_best.unwrap_or(LOG_BEST),
log_population: log_population.unwrap_or(LOG_POPULATION),
dump_population: dump_population.unwrap_or(false),
}
let create_progress = |log_best: &Option<usize>, log_population: &Option<usize>| TelemetryMode::OnlyLogging {
logger: environment.logger.clone(),
log_best: log_best.unwrap_or(LOG_BEST),
log_population: log_population.unwrap_or(LOG_POPULATION),
};

match telemetry_config.as_ref().map(|t| (&t.progress, &t.metrics)) {
Some((None, Some(MetricsConfig { enabled, track_population }))) if *enabled => create_metrics(track_population),
Some((Some(ProgressConfig { enabled, log_best, log_population, dump_population }), None)) if *enabled => {
create_progress(log_best, log_population, dump_population)
Some((Some(ProgressConfig { enabled, log_best, log_population }), None)) if *enabled => {
create_progress(log_best, log_population)
}
Some((
Some(ProgressConfig { enabled: progress_enabled, log_best, log_population, dump_population }),
Some(ProgressConfig { enabled: progress_enabled, log_best, log_population }),
Some(MetricsConfig { enabled: metrics_enabled, track_population }),
)) => match (progress_enabled, metrics_enabled) {
(true, true) => TelemetryMode::All {
logger: environment.logger.clone(),
log_best: log_best.unwrap_or(LOG_BEST),
log_population: log_population.unwrap_or(LOG_POPULATION),
track_population: track_population.unwrap_or(TRACK_POPULATION),
dump_population: dump_population.unwrap_or(false),
},
(true, false) => create_progress(log_best, log_population, dump_population),
(true, false) => create_progress(log_best, log_population),
(false, true) => create_metrics(track_population),
_ => TelemetryMode::None,
},
Expand Down
1 change: 0 additions & 1 deletion vrp-cli/tests/unit/extensions/solve/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fn can_read_full_config() {
assert!(logging.enabled);
assert_eq!(logging.log_best, Some(100));
assert_eq!(logging.log_population, Some(1000));
assert_eq!(logging.dump_population, Some(false));
let metrics = telemetry.metrics.unwrap();
assert!(!metrics.enabled);
assert_eq!(metrics.track_population, Some(1000));
Expand Down
2 changes: 1 addition & 1 deletion vrp-core/src/solver/heuristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl VrpConfigBuilder {

/// Creates default telemetry mode.B
pub fn get_default_telemetry_mode(logger: InfoLogger) -> TelemetryMode {
TelemetryMode::OnlyLogging { logger, log_best: 100, log_population: 1000, dump_population: false }
TelemetryMode::OnlyLogging { logger, log_best: 100, log_population: 1000 }
}

/// Gets default heuristic.
Expand Down

0 comments on commit ae98950

Please sign in to comment.