1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
extern crate rand;
use crate::construction::heuristics::InsertionContext;
use crate::models::Problem;
use crate::construction::Quota;
use crate::refinement::objectives::ObjectiveCostType;
use hashbrown::HashMap;
use std::any::Any;
use std::sync::Arc;
pub struct RefinementContext {
pub problem: Arc<Problem>,
pub population: Box<dyn Population + Sync + Send>,
pub state: HashMap<String, Box<dyn Any>>,
pub generation: usize,
}
pub type Individuum = (InsertionContext, ObjectiveCostType, usize);
pub trait Population {
fn add(&mut self, individuum: Individuum);
fn all<'a>(&'a self) -> Box<dyn Iterator<Item = &Individuum> + 'a>;
fn best(&self) -> Option<&Individuum>;
fn size(&self) -> usize;
}
struct SinglePopulation {
individuums: Vec<Individuum>,
}
impl Default for SinglePopulation {
fn default() -> Self {
Self { individuums: vec![] }
}
}
impl Population for SinglePopulation {
fn add(&mut self, individuum: Individuum) {
self.individuums.clear();
self.individuums.push(individuum);
}
fn all<'a>(&'a self) -> Box<dyn Iterator<Item = &Individuum> + 'a> {
Box::new(self.individuums.iter())
}
fn best(&self) -> Option<&Individuum> {
self.individuums.first()
}
fn size(&self) -> usize {
self.individuums.len()
}
}
impl RefinementContext {
pub fn new(problem: Arc<Problem>) -> Self {
Self::new_with_population(problem, Box::new(SinglePopulation::default()))
}
pub fn new_with_population(problem: Arc<Problem>, population: Box<dyn Population + Sync + Send>) -> Self {
Self { problem, population, state: Default::default(), generation: 1 }
}
pub fn get_quota(&self) -> Option<&Box<dyn Quota + Send + Sync>> {
self.state.get("quota").and_then(|q| q.downcast_ref::<Box<dyn Quota + Send + Sync>>())
}
pub fn set_quota(&mut self, quota: Box<dyn Quota + Send + Sync>) {
self.state.insert("quota".to_string(), Box::new(quota));
}
}
pub mod acceptance;
pub mod mutation;
pub mod objectives;
pub mod selection;
pub mod termination;