- Timestamp:
- 07/29/14 02:17:14 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r11032 r11227 21 21 22 22 using System.Linq; 23 using HeuristicLab.Analysis; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 34 35 public class GenealogyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer 35 36 where T : class,IItem { 37 #region parameter names 36 38 private const string GenerationsParameterName = "Generations"; 37 39 private const string ResultsParameterName = "Results"; … … 53 55 private const string EnableManipulatorTrackingParameterName = "EnableManipulatorTracking"; 54 56 private const string EnableSolutionCreatorTrackingParameterName = "EnableSolutionCreatorTracking"; // should always be enabled. maybe superfluous 57 #endregion 55 58 56 59 #region parameter properties … … 140 143 141 144 public GenealogyAnalyzer() { 145 #region add parameters 142 146 // the instrumented operators 143 147 Parameters.Add(new LookupParameter<ICrossover>(CrossoverParameterName, "The crossover operator.")); … … 157 161 Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(BeforeManipulatorOperatorParameterName)); 158 162 Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(AfterManipulatorOperatorParameterName)); 163 #endregion 159 164 } 160 165 public override IDeepCloneable Clone(Cloner cloner) { … … 164 169 : base(original, cloner) { 165 170 } 171 172 [StorableConstructor] 173 protected GenealogyAnalyzer(bool deserializing) : base(deserializing) { } 174 166 175 [StorableHook(HookType.AfterDeserialization)] 167 176 private void AfterDeserialization() { … … 232 241 233 242 public override IOperation Apply() { 234 var population = PopulationParameter.ActualValue .ToList();243 var population = PopulationParameter.ActualValue; 235 244 var qualities = QualityParameter.ActualValue.ToList(); 236 245 237 int currentGeneration = GenerationsParameter.ActualValue.Value;238 if ( currentGeneration == 0) {246 int generation = GenerationsParameter.ActualValue.Value; 247 if (generation == 0) { 239 248 ConfigureTrackingOperators(); 240 249 241 for (int i = 0; i < population. Count; ++i) {250 for (int i = 0; i < population.Length; ++i) { 242 251 var individual = population[i]; 243 var vertex = new GenealogyGraphNode<T>(individual) { Rank = currentGeneration };252 var vertex = new GenealogyGraphNode<T>(individual) { Rank = generation }; 244 253 GenealogyGraph.AddVertex(vertex); 245 254 // save the vertex id in the individual scope (so that we can identify graph indices) … … 249 258 int index = 0; 250 259 T elite = null; 251 for (int i = 0; i < population. Count; ++i) {260 for (int i = 0; i < population.Length; ++i) { 252 261 if (GenealogyGraph.Contains(population[i])) { 253 262 elite = population[i]; 254 263 index = i; 255 } 256 break; 257 } 258 264 break; 265 } 266 } 267 268 #region add elite in the graph and connect it with the previous elite 259 269 if (elite != null) { 260 270 var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph.GetVertex(elite); … … 264 274 265 275 var vertex = new GenealogyGraphNode<T>(prevVertex.Content) { 266 Rank = currentGeneration,276 Rank = generation, 267 277 Quality = prevVertex.Quality, 268 278 IsElite = false … … 272 282 // inject the graph node unique id to the scope 273 283 ExecutionContext.Scope.SubScopes[index].Variables["Id"].Value = new StringValue(vertex.Id); 274 275 284 GenealogyGraph.AddVertex(vertex); 276 277 285 GenealogyGraph.AddArc(prevVertex, vertex); // connect current elite with previous elite 278 286 … … 281 289 } 282 290 } 291 #endregion 292 293 ComputeSuccessRatios(); 283 294 } 284 295 // update qualities 285 for (int i = 0; i < population. Count; ++i) {296 for (int i = 0; i < population.Length; ++i) { 286 297 var vertex = (IGenealogyGraphNode)GenealogyGraph.GetVertex(population[i]); 287 298 vertex.Quality = qualities[i].Value; … … 289 300 290 301 // remove extra graph nodes (added by the instrumented operators in the case of offspring selection) 291 var discardedOffspring = GenealogyGraph.Ranks[currentGeneration].Select(x => (T)x.Content).Except(population).ToList(); 292 foreach (var individual in discardedOffspring) { 293 var vertex = GenealogyGraph.GetVertex(individual); 302 var discardedOffspring = GenealogyGraph.Ranks[generation].Select(x => (T)x.Content).Except(population).ToList(); 303 foreach (var vertex in discardedOffspring.Select(individual => GenealogyGraph.GetVertex(individual))) { 294 304 GenealogyGraph.RemoveVertex(vertex); 295 305 } 296 306 297 307 return base.Apply(); 308 } 309 310 private void ComputeSuccessRatios() { 311 var population = PopulationParameter.ActualValue; 312 var generation = GenerationsParameter.ActualValue.Value; 313 // compute the weight of each genealogy graph node as the ratio (produced offspring) / (surviving offspring) 314 foreach (var ind in population) { 315 var v = (IGenealogyGraphNode)GenealogyGraph.GetVertex(ind); 316 foreach (var p in v.Parents) 317 p.Weight++; 318 } 319 foreach (var v in GenealogyGraph.Ranks[generation - 1]) { 320 if (v.OutDegree > 0) 321 v.Weight /= v.OutDegree; 322 } 323 324 var results = ResultsParameter.ActualValue; 325 DataTable table; 326 if (!results.ContainsKey("Successful offspring ratio")) { 327 table = new DataTable("Successful offspring ratio"); 328 results.Add(new Result("Successful offspring ratio", table)); 329 table.Rows.Add(new DataRow("Successful offspring ratio") { VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Columns, StartIndexZero = true } }); 330 } else { 331 table = (DataTable)results["Successful offspring ratio"].Value; 332 } 333 var row = table.Rows["Successful offspring ratio"]; 334 row.Values.Replace(GenealogyGraph.Ranks[generation - 1].OrderByDescending(x => x.Quality).Select(x => x.Weight)); 298 335 } 299 336 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/HeuristicLab.EvolutionTracking-3.4.csproj
r10458 r11227 56 56 </PropertyGroup> 57 57 <ItemGroup> 58 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 59 <SpecificVersion>False</SpecificVersion> 60 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath> 61 <Private>False</Private> 62 </Reference> 58 63 <Reference Include="HeuristicLab.Collections-3.3"> 59 64 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/AfterCrossoverOperator.cs
r10830 r11227 45 45 return new AfterCrossoverOperator<T>(this, cloner); 46 46 } 47 [StorableConstructor] 48 protected AfterCrossoverOperator(bool deserializing) : base(deserializing) { } 47 49 48 50 public AfterCrossoverOperator() { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/AfterManipulatorOperator.cs
r10830 r11227 41 41 return new AfterManipulatorOperator<T>(this, cloner); 42 42 } 43 [StorableConstructor] 44 protected AfterManipulatorOperator(bool deserializing) : base(deserializing) { } 43 45 44 46 public AfterManipulatorOperator() { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs
r10897 r11227 54 54 } 55 55 56 [StorableConstructor] 57 protected BeforeCrossoverOperator(bool deserializing) : base(deserializing) { } 58 56 59 public BeforeCrossoverOperator() { 57 60 Parameters.Add(new ScopeTreeLookupParameter<T>(ParentsParameterName)); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r11032 r11227 30 30 [Item("AfterCrossoverOperator", "Performs an action after the crossover operator is applied.")] 31 31 public class BeforeManipulatorOperator<T> : EvolutionTrackingOperator<T>, IManipulatorOperator<T> where T : class,IItem { 32 33 32 private const string ChildParameterName = "Child"; 34 33 … … 43 42 return new BeforeManipulatorOperator<T>(this, cloner); 44 43 } 44 [StorableConstructor] 45 protected BeforeManipulatorOperator(bool deserializing) : base(deserializing) { } 45 46 46 47 public BeforeManipulatorOperator() { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/EvolutionTrackingOperator.cs
r10650 r11227 26 26 using HeuristicLab.Optimization; 27 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 29 30 namespace HeuristicLab.EvolutionTracking { 31 [Item("EvolutionTrackingOperator", "A base operator which facilitates access to the genealogy graph.")] 32 [StorableClass] 30 33 public class EvolutionTrackingOperator<T> : SingleSuccessorOperator where T : class,IItem { 31 34 // evolution tracking-related parameters … … 69 72 return new EvolutionTrackingOperator<T>(this, cloner); 70 73 } 74 [StorableConstructor] 75 protected EvolutionTrackingOperator(bool deserializing) : base(deserializing) { } 71 76 } 72 77 }
Note: See TracChangeset
for help on using the changeset viewer.