| 64 pub fitness_merging: bool, |
64 pub fitness_merging: bool, |
| 65 |
65 |
| 66 /// Iterations between merging heuristic tries |
66 /// Iterations between merging heuristic tries |
| 67 pub merge_every: usize, |
67 pub merge_every: usize, |
| 68 |
68 |
| 69 /// Additional weight optimisation steps |
69 /// Additional weight optimisation steps after merge and random insertion |
| 70 pub extra_weight_optimisation_steps: usize, |
70 pub extra_weight_optimisation_steps: usize, |
| |
71 |
| |
72 /// Random insertion after merge |
| |
73 pub random_insertions: bool, |
| 71 |
74 |
| 72 /// Maximum iteration for scaling hack, zero if no maximum. |
75 /// Maximum iteration for scaling hack, zero if no maximum. |
| 73 pub max_scaling_iter: usize, |
76 pub max_scaling_iter: usize, |
| 74 } |
77 } |
| 75 |
78 |
| 89 fitness_merging: false, |
92 fitness_merging: false, |
| 90 merge_every: 10, |
93 merge_every: 10, |
| 91 merge_tolerance_mult: 2.0, |
94 merge_tolerance_mult: 2.0, |
| 92 extra_weight_optimisation_steps: 0, |
95 extra_weight_optimisation_steps: 0, |
| 93 max_scaling_iter: 0, |
96 max_scaling_iter: 0, |
| |
97 random_insertions: false, |
| 94 } |
98 } |
| 95 } |
99 } |
| 96 } |
100 } |
| 97 |
101 |
| 98 impl<F: Float> InsertionConfig<F> { |
102 impl<F: Float> InsertionConfig<F> { |
| 99 /// Check if merging should be attempted this iteration |
103 /// Check if merging should be attempted this iteration. |
| |
104 /// Wrapped in a possible error, returns [`Some`] of the return value of `f`, if merging should |
| |
105 /// be performed. |
| 100 pub fn if_merge_now<I: AlgIterator, A>( |
106 pub fn if_merge_now<I: AlgIterator, A>( |
| 101 &self, |
107 &self, |
| 102 state: &AlgIteratorIteration<I>, |
108 state: &AlgIteratorIteration<I>, |
| 103 mut f: impl FnMut(&Self) -> A, |
109 mut f: impl FnMut(&Self) -> DynResult<A>, |
| |
110 ) -> DynResult<Option<A>> { |
| |
111 if (self.merging.enabled || self.merging.scaling.is_some()) |
| |
112 && state.iteration() % self.merge_every == 0 |
| |
113 { |
| |
114 if self.max_scaling_iter != 0 && state.iteration() > self.max_scaling_iter { |
| |
115 if !self.merging.enabled { |
| |
116 Ok(None) |
| |
117 } else { |
| |
118 Ok(Some(f(&InsertionConfig { |
| |
119 merging: SpikeMergingMethod { scaling: None, ..self.merging }, |
| |
120 ..*self |
| |
121 })?)) |
| |
122 } |
| |
123 } else { |
| |
124 Ok(Some(f(self)?)) |
| |
125 } |
| |
126 } else { |
| |
127 Ok(None) |
| |
128 } |
| |
129 } |
| |
130 |
| |
131 // Lightweight version of [`Self::if_merge_now`]. |
| |
132 pub fn if_merge_now_check<I: AlgIterator, A>( |
| |
133 &self, |
| |
134 state: &AlgIteratorIteration<I>, |
| |
135 mut f: impl FnMut() -> A, |
| 104 ) -> Option<A> { |
136 ) -> Option<A> { |
| 105 if (self.merging.enabled || self.merging.scaling.is_some()) |
137 if (self.merging.enabled || self.merging.scaling.is_some()) |
| 106 && state.iteration() % self.merge_every == 0 |
138 && state.iteration() % self.merge_every == 0 |
| 107 { |
139 { |
| 108 if self.max_scaling_iter != 0 && state.iteration() > self.max_scaling_iter { |
140 if self.max_scaling_iter != 0 && state.iteration() > self.max_scaling_iter { |
| 109 if !self.merging.enabled { |
141 if !self.merging.enabled { |
| 110 None |
142 None |
| 111 } else { |
143 } else { |
| 112 Some(f(&InsertionConfig { |
144 Some(f()) |
| 113 merging: SpikeMergingMethod { scaling: None, ..self.merging }, |
|
| 114 ..*self |
|
| 115 })) |
|
| 116 } |
145 } |
| 117 } else { |
146 } else { |
| 118 Some(f(self)) |
147 Some(f()) |
| 119 } |
148 } |
| 120 } else { |
149 } else { |
| 121 None |
150 None |
| 122 } |
151 } |
| 123 } |
152 } |