Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/11/20 13:36:02 (4 years ago)
Author:
bburlacu
Message:

#1772: Merge trunk changes and fix all errors and compilation warnings.

Location:
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/EvolutionTrackingAnalyzer.cs

    r13482 r17434  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3031namespace HeuristicLab.EvolutionTracking {
    3132  [Item("EvolutionTrackingAnalyzer", "Base class for analyzers that use the genealogy graph")]
    32   [StorableClass]
     33  [StorableType("272F8098-CFD0-473C-AFC4-A5F3A51BC154")]
    3334  public abstract class EvolutionTrackingAnalyzer : SingleSuccessorOperator, IAnalyzer {
    3435    #region parameter names
     
    9293
    9394    [StorableConstructor]
    94     protected EvolutionTrackingAnalyzer(bool deserializing) : base(deserializing) { }
     95    protected EvolutionTrackingAnalyzer(StorableConstructorFlag _) : base(_) { }
    9596  }
    9697
    9798  [Item("EvolutionTrackingAnalyzer", "Base class for analyzers that use the genealogy graph")]
    98   [StorableClass]
     99  [StorableType("B48D6E89-3D0F-4F38-BC23-FE648E43421F")]
    99100  public class EvolutionTrackingAnalyzer<T> : EvolutionTrackingAnalyzer where T : class, IItem {
    100101    public EvolutionTrackingAnalyzer() { }
     
    107108
    108109    [StorableConstructor]
    109     protected EvolutionTrackingAnalyzer(bool deserializing) : base(deserializing) { }
     110    protected EvolutionTrackingAnalyzer(StorableConstructorFlag _) : base(_) { }
    110111
    111112    public new IGenealogyGraph<T> PopulationGraph {
  • branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r13495 r17434  
    2222using System.Collections.Generic;
    2323using System.Linq;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.EvolutionTracking;
    2729using HeuristicLab.Operators;
    2830using HeuristicLab.Optimization;
    2931using HeuristicLab.Parameters;
    30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 
    32 namespace HeuristicLab.EvolutionTracking {
    33   [StorableClass]
    34   [Item("GenealogyAnalyzer", "An analyzer which performs the necessary instrumentation to record the evolution of a genetic algorithm.")]
    35   public abstract class GenealogyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer
    36   where T : class, IItem {
    37     #region parameter names
    38     private const string GenerationsParameterName = "Generations";
    39     private const string ResultsParameterName = "Results";
    40     private const string PopulationGraphParameterName = "PopulationGraph";
    41     public const string QualityParameterName = "Quality";
    42     public const string PopulationParameterName = "SymbolicExpressionTree";
    43 
    44     private const string CrossoverParameterName = "Crossover";
    45     private const string ManipulatorParameterName = "Mutator";
    46     private const string SolutionCreatorParameterName = "SolutionCreator";
    47 
    48     private const string BeforeCrossoverOperatorParameterName = "BeforeCrossoverOperator";
    49     private const string AfterCrossoverOperatorParameterName = "AfterCrossoverOperator";
    50 
    51     private const string BeforeManipulatorOperatorParameterName = "BeforeManipulatorOperator";
    52     private const string AfterManipulatorOperatorParameterName = "AfterManipulatorOperator";
    53 
    54     private const string EnableCrossoverTrackingParameterName = "EnableCrossoverTracking";
    55     private const string EnableManipulatorTrackingParameterName = "EnableManipulatorTracking";
    56     private const string EnableSolutionCreatorTrackingParameterName = "EnableSolutionCreatorTracking"; // should always be enabled. maybe superfluous
    57     private const string TrimOlderGenerationsParameterName = "TrimOlderGenerations";
    58     #endregion parameter names
    59 
    60     #region parameter properties
    61 
    62     public IFixedValueParameter<BoolValue> TrimOlderGenerationsParameter {
    63       get { return (IFixedValueParameter<BoolValue>)Parameters[TrimOlderGenerationsParameterName]; }
    64     }
    65 
    66     public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
    67       get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
    68     }
    69 
    70     public IScopeTreeLookupParameter<T> PopulationParameter {
    71       get { return (IScopeTreeLookupParameter<T>)Parameters[PopulationParameterName]; }
    72     }
    73 
    74     public IValueParameter<ICrossoverOperator<T>> BeforeCrossoverOperatorParameter {
    75       get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[BeforeCrossoverOperatorParameterName]; }
    76     }
    77 
    78     public IValueParameter<ICrossoverOperator<T>> AfterCrossoverOperatorParameter {
    79       get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[AfterCrossoverOperatorParameterName]; }
    80     }
    81 
    82     public IValueParameter<IManipulatorOperator<T>> BeforeManipulatorOperatorParameter {
    83       get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[BeforeManipulatorOperatorParameterName]; }
    84     }
    85 
    86     public IValueParameter<IManipulatorOperator<T>> AfterManipulatorOperatorParameter {
    87       get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[AfterManipulatorOperatorParameterName]; }
    88     }
    89 
    90     public ILookupParameter<ResultCollection> ResultsParameter {
    91       get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
    92     }
    93 
    94     public ILookupParameter<IntValue> GenerationsParameter {
    95       get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
    96     }
    97 
    98     public IValueParameter<BoolValue> EnableCrossoverTrackingParameter {
    99       get { return (IValueParameter<BoolValue>)Parameters[EnableCrossoverTrackingParameterName]; }
    100     }
    101 
    102     public IValueParameter<BoolValue> EnableManipulatorTrackingParameter {
    103       get { return (IValueParameter<BoolValue>)Parameters[EnableManipulatorTrackingParameterName]; }
    104     }
    105 
    106     public IValueParameter<BoolValue> EnableSolutionCreatorTrackingParameter {
    107       get { return (IValueParameter<BoolValue>)Parameters[EnableSolutionCreatorTrackingParameterName]; }
    108     }
    109 
    110     public ILookupParameter<ICrossover> CrossoverParameter {
    111       get { return (ILookupParameter<ICrossover>)Parameters[CrossoverParameterName]; }
    112     }
    113 
    114     public ILookupParameter<IManipulator> ManipulatorParameter {
    115       get { return (ILookupParameter<IManipulator>)Parameters[ManipulatorParameterName]; }
    116     }
    117 
    118     public ILookupParameter<ISolutionCreator> SolutionCreatorParameter {
    119       get { return (ILookupParameter<ISolutionCreator>)Parameters[SolutionCreatorParameterName]; }
    120     }
    121     #endregion parameter properties
    122 
    123     #region properties
    124     public ICrossoverOperator<T> BeforeCrossoverOperator {
    125       get { return BeforeCrossoverOperatorParameter.Value; }
    126     }
    127 
    128     public ICrossoverOperator<T> AfterCrossoverOperator {
    129       get { return AfterCrossoverOperatorParameter.Value; }
    130     }
    131 
    132     public IManipulatorOperator<T> BeforeManipulatorOperator {
    133       get { return BeforeManipulatorOperatorParameter.Value; }
    134     }
    135 
    136     public IManipulatorOperator<T> AfterManipulatorOperator {
    137       get { return AfterManipulatorOperatorParameter.Value; }
    138     }
    139 
    140     public BoolValue EnableCrossoverTracking {
    141       get { return EnableCrossoverTrackingParameter.Value; }
    142     }
    143 
    144     public BoolValue EnableManipulatorTracking {
    145       get { return EnableManipulatorTrackingParameter.Value; }
    146     }
    147 
    148     public BoolValue EnableSolutionCreatorTracking {
    149       get { return EnableSolutionCreatorTrackingParameter.Value; }
    150     }
    151 
    152     public bool TrimOlderGenerations {
    153       get { return TrimOlderGenerationsParameter.Value.Value; }
    154     }
    155     #endregion properties
    156 
    157     protected GenealogyAnalyzer() {
    158       #region add parameters
    159       // the instrumented operators
     32
     33[Item("GenealogyAnalyzer", "An analyzer which performs the necessary instrumentation to record the evolution of a genetic algorithm.")]
     34[StorableType("04001C52-025C-4555-8812-AC3FA26A2B2F")]
     35public abstract class GenealogyAnalyzer<T> : SingleSuccessorOperator, IAnalyzer
     36where T : class, IItem
     37{
     38  #region parameter names
     39  private const string GenerationsParameterName = "Generations";
     40  private const string ResultsParameterName = "Results";
     41  private const string PopulationGraphParameterName = "PopulationGraph";
     42  public const string QualityParameterName = "Quality";
     43  public const string PopulationParameterName = "SymbolicExpressionTree";
     44
     45  private const string CrossoverParameterName = "Crossover";
     46  private const string ManipulatorParameterName = "Mutator";
     47  private const string SolutionCreatorParameterName = "SolutionCreator";
     48
     49  private const string BeforeCrossoverOperatorParameterName = "BeforeCrossoverOperator";
     50  private const string AfterCrossoverOperatorParameterName = "AfterCrossoverOperator";
     51
     52  private const string BeforeManipulatorOperatorParameterName = "BeforeManipulatorOperator";
     53  private const string AfterManipulatorOperatorParameterName = "AfterManipulatorOperator";
     54
     55  private const string EnableCrossoverTrackingParameterName = "EnableCrossoverTracking";
     56  private const string EnableManipulatorTrackingParameterName = "EnableManipulatorTracking";
     57  private const string EnableSolutionCreatorTrackingParameterName = "EnableSolutionCreatorTracking"; // should always be enabled. maybe superfluous
     58  private const string TrimOlderGenerationsParameterName = "TrimOlderGenerations";
     59  #endregion parameter names
     60
     61  #region parameter properties
     62
     63  public IFixedValueParameter<BoolValue> TrimOlderGenerationsParameter {
     64    get { return (IFixedValueParameter<BoolValue>)Parameters[TrimOlderGenerationsParameterName]; }
     65  }
     66
     67  public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     68    get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
     69  }
     70
     71  public IScopeTreeLookupParameter<T> PopulationParameter {
     72    get { return (IScopeTreeLookupParameter<T>)Parameters[PopulationParameterName]; }
     73  }
     74
     75  public IValueParameter<ICrossoverOperator<T>> BeforeCrossoverOperatorParameter {
     76    get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[BeforeCrossoverOperatorParameterName]; }
     77  }
     78
     79  public IValueParameter<ICrossoverOperator<T>> AfterCrossoverOperatorParameter {
     80    get { return (IValueParameter<ICrossoverOperator<T>>)Parameters[AfterCrossoverOperatorParameterName]; }
     81  }
     82
     83  public IValueParameter<IManipulatorOperator<T>> BeforeManipulatorOperatorParameter {
     84    get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[BeforeManipulatorOperatorParameterName]; }
     85  }
     86
     87  public IValueParameter<IManipulatorOperator<T>> AfterManipulatorOperatorParameter {
     88    get { return (IValueParameter<IManipulatorOperator<T>>)Parameters[AfterManipulatorOperatorParameterName]; }
     89  }
     90
     91  public ILookupParameter<ResultCollection> ResultsParameter {
     92    get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
     93  }
     94
     95  public ILookupParameter<IntValue> GenerationsParameter {
     96    get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
     97  }
     98
     99  public IValueParameter<BoolValue> EnableCrossoverTrackingParameter {
     100    get { return (IValueParameter<BoolValue>)Parameters[EnableCrossoverTrackingParameterName]; }
     101  }
     102
     103  public IValueParameter<BoolValue> EnableManipulatorTrackingParameter {
     104    get { return (IValueParameter<BoolValue>)Parameters[EnableManipulatorTrackingParameterName]; }
     105  }
     106
     107  public IValueParameter<BoolValue> EnableSolutionCreatorTrackingParameter {
     108    get { return (IValueParameter<BoolValue>)Parameters[EnableSolutionCreatorTrackingParameterName]; }
     109  }
     110
     111  public ILookupParameter<ICrossover> CrossoverParameter {
     112    get { return (ILookupParameter<ICrossover>)Parameters[CrossoverParameterName]; }
     113  }
     114
     115  public ILookupParameter<IManipulator> ManipulatorParameter {
     116    get { return (ILookupParameter<IManipulator>)Parameters[ManipulatorParameterName]; }
     117  }
     118
     119  public ILookupParameter<ISolutionCreator> SolutionCreatorParameter {
     120    get { return (ILookupParameter<ISolutionCreator>)Parameters[SolutionCreatorParameterName]; }
     121  }
     122  #endregion parameter properties
     123
     124  #region properties
     125  public ICrossoverOperator<T> BeforeCrossoverOperator {
     126    get { return BeforeCrossoverOperatorParameter.Value; }
     127  }
     128
     129  public ICrossoverOperator<T> AfterCrossoverOperator {
     130    get { return AfterCrossoverOperatorParameter.Value; }
     131  }
     132
     133  public IManipulatorOperator<T> BeforeManipulatorOperator {
     134    get { return BeforeManipulatorOperatorParameter.Value; }
     135  }
     136
     137  public IManipulatorOperator<T> AfterManipulatorOperator {
     138    get { return AfterManipulatorOperatorParameter.Value; }
     139  }
     140
     141  public BoolValue EnableCrossoverTracking {
     142    get { return EnableCrossoverTrackingParameter.Value; }
     143  }
     144
     145  public BoolValue EnableManipulatorTracking {
     146    get { return EnableManipulatorTrackingParameter.Value; }
     147  }
     148
     149  public BoolValue EnableSolutionCreatorTracking {
     150    get { return EnableSolutionCreatorTrackingParameter.Value; }
     151  }
     152
     153  public bool TrimOlderGenerations {
     154    get { return TrimOlderGenerationsParameter.Value.Value; }
     155  }
     156  #endregion properties
     157
     158  protected GenealogyAnalyzer() {
     159    #region add parameters
     160    // the instrumented operators
     161    Parameters.Add(new LookupParameter<ICrossover>(CrossoverParameterName, "The crossover operator."));
     162    Parameters.Add(new LookupParameter<IManipulator>(ManipulatorParameterName, "The manipulator operator."));
     163    Parameters.Add(new LookupParameter<ISolutionCreator>(SolutionCreatorParameterName, "The solution creator operator."));
     164    // the analyzer parameters
     165    Parameters.Add(new ValueParameter<BoolValue>(EnableCrossoverTrackingParameterName, new BoolValue(true)));
     166    Parameters.Add(new ValueParameter<BoolValue>(EnableManipulatorTrackingParameterName, new BoolValue(true)));
     167    Parameters.Add(new ValueParameter<BoolValue>(EnableSolutionCreatorTrackingParameterName, new BoolValue(true)));
     168    // parameters required by the analyzer to do its work
     169    Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
     170    Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName));
     171    Parameters.Add(new ScopeTreeLookupParameter<T>(PopulationParameterName, "The population of individuals."));
     172    Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
     173    Parameters.Add(new ValueParameter<ICrossoverOperator<T>>(BeforeCrossoverOperatorParameterName));
     174    Parameters.Add(new ValueParameter<ICrossoverOperator<T>>(AfterCrossoverOperatorParameterName));
     175    Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(BeforeManipulatorOperatorParameterName));
     176    Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(AfterManipulatorOperatorParameterName));
     177    Parameters.Add(new FixedValueParameter<BoolValue>(TrimOlderGenerationsParameterName, "Remove all the generations older than the last generation from the genealoy graph to save memory."));
     178    #endregion add parameters
     179  }
     180
     181  protected GenealogyAnalyzer(GenealogyAnalyzer<T> original, Cloner cloner)
     182    : base(original, cloner) {
     183  }
     184
     185  [StorableConstructor]
     186  protected GenealogyAnalyzer(StorableConstructorFlag _) : base(_) { }
     187
     188  [StorableHook(HookType.AfterDeserialization)]
     189  private void AfterDeserialization() {
     190    // the instrumented operators
     191    if (!Parameters.ContainsKey(CrossoverParameterName))
    160192      Parameters.Add(new LookupParameter<ICrossover>(CrossoverParameterName, "The crossover operator."));
     193    if (!Parameters.ContainsKey(ManipulatorParameterName))
    161194      Parameters.Add(new LookupParameter<IManipulator>(ManipulatorParameterName, "The manipulator operator."));
     195    if (!Parameters.ContainsKey(SolutionCreatorParameterName))
    162196      Parameters.Add(new LookupParameter<ISolutionCreator>(SolutionCreatorParameterName, "The solution creator operator."));
    163       // the analyzer parameters
     197    // the analyzer parameters
     198    if (!Parameters.ContainsKey(EnableCrossoverTrackingParameterName))
    164199      Parameters.Add(new ValueParameter<BoolValue>(EnableCrossoverTrackingParameterName, new BoolValue(true)));
     200    if (!Parameters.ContainsKey(EnableManipulatorTrackingParameterName))
    165201      Parameters.Add(new ValueParameter<BoolValue>(EnableManipulatorTrackingParameterName, new BoolValue(true)));
     202    if (!Parameters.ContainsKey(EnableSolutionCreatorTrackingParameterName))
    166203      Parameters.Add(new ValueParameter<BoolValue>(EnableSolutionCreatorTrackingParameterName, new BoolValue(true)));
    167       // parameters required by the analyzer to do its work
     204    // parameters required by the analyzer to do its work
     205    if (!Parameters.ContainsKey(GenerationsParameterName))
    168206      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
     207    if (!Parameters.ContainsKey(ResultsParameterName))
    169208      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName));
     209    if (!Parameters.ContainsKey(PopulationParameterName)) {
    170210      Parameters.Add(new ScopeTreeLookupParameter<T>(PopulationParameterName, "The population of individuals."));
     211    }
     212    if (!Parameters.ContainsKey(QualityParameterName)) {
    171213      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
    172       Parameters.Add(new ValueParameter<ICrossoverOperator<T>>(BeforeCrossoverOperatorParameterName));
    173       Parameters.Add(new ValueParameter<ICrossoverOperator<T>>(AfterCrossoverOperatorParameterName));
    174       Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(BeforeManipulatorOperatorParameterName));
    175       Parameters.Add(new ValueParameter<IManipulatorOperator<T>>(AfterManipulatorOperatorParameterName));
     214    }
     215    if (!Parameters.ContainsKey(TrimOlderGenerationsParameterName))
    176216      Parameters.Add(new FixedValueParameter<BoolValue>(TrimOlderGenerationsParameterName, "Remove all the generations older than the last generation from the genealoy graph to save memory."));
    177       #endregion add parameters
    178     }
    179 
    180     protected GenealogyAnalyzer(GenealogyAnalyzer<T> original, Cloner cloner)
    181       : base(original, cloner) {
    182     }
    183 
    184     [StorableConstructor]
    185     protected GenealogyAnalyzer(bool deserializing) : base(deserializing) { }
    186 
    187     [StorableHook(HookType.AfterDeserialization)]
    188     private void AfterDeserialization() {
    189       // the instrumented operators
    190       if (!Parameters.ContainsKey(CrossoverParameterName))
    191         Parameters.Add(new LookupParameter<ICrossover>(CrossoverParameterName, "The crossover operator."));
    192       if (!Parameters.ContainsKey(ManipulatorParameterName))
    193         Parameters.Add(new LookupParameter<IManipulator>(ManipulatorParameterName, "The manipulator operator."));
    194       if (!Parameters.ContainsKey(SolutionCreatorParameterName))
    195         Parameters.Add(new LookupParameter<ISolutionCreator>(SolutionCreatorParameterName, "The solution creator operator."));
    196       // the analyzer parameters
    197       if (!Parameters.ContainsKey(EnableCrossoverTrackingParameterName))
    198         Parameters.Add(new ValueParameter<BoolValue>(EnableCrossoverTrackingParameterName, new BoolValue(true)));
    199       if (!Parameters.ContainsKey(EnableManipulatorTrackingParameterName))
    200         Parameters.Add(new ValueParameter<BoolValue>(EnableManipulatorTrackingParameterName, new BoolValue(true)));
    201       if (!Parameters.ContainsKey(EnableSolutionCreatorTrackingParameterName))
    202         Parameters.Add(new ValueParameter<BoolValue>(EnableSolutionCreatorTrackingParameterName, new BoolValue(true)));
    203       // parameters required by the analyzer to do its work
    204       if (!Parameters.ContainsKey(GenerationsParameterName))
    205         Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
    206       if (!Parameters.ContainsKey(ResultsParameterName))
    207         Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName));
    208       if (!Parameters.ContainsKey(PopulationParameterName)) {
    209         Parameters.Add(new ScopeTreeLookupParameter<T>(PopulationParameterName, "The population of individuals."));
    210       }
    211       if (!Parameters.ContainsKey(QualityParameterName)) {
    212         Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
    213       }
    214       if (!Parameters.ContainsKey(TrimOlderGenerationsParameterName))
    215         Parameters.Add(new FixedValueParameter<BoolValue>(TrimOlderGenerationsParameterName, "Remove all the generations older than the last generation from the genealoy graph to save memory."));
    216     }
    217 
    218     public bool EnabledByDefault {
    219       get { return false; }
    220     }
    221 
    222     private void ConfigureTrackingOperators() {
    223       // at the beginning we add the before/after operators to the instrumented operators
    224       var crossover = CrossoverParameter.ActualValue;
    225       if (crossover != null) {
    226         var instrumentedCrossover = (InstrumentedOperator)crossover;
    227         instrumentedCrossover.AfterExecutionOperators.Clear();
    228         instrumentedCrossover.BeforeExecutionOperators.Clear();
    229 
    230         if (EnableCrossoverTracking.Value) {
    231           if (BeforeCrossoverOperator != null) {
    232             instrumentedCrossover.BeforeExecutionOperators.Add(BeforeCrossoverOperator);
    233           }
    234           if (AfterCrossoverOperator != null) {
    235             instrumentedCrossover.AfterExecutionOperators.Add(AfterCrossoverOperator);
    236           }
    237         }
    238       }
    239       var manipulator = ManipulatorParameter.ActualValue;
    240       if (manipulator != null) {
    241         var instrumentedManipulator = (InstrumentedOperator)manipulator;
    242         instrumentedManipulator.AfterExecutionOperators.Clear();
    243         instrumentedManipulator.BeforeExecutionOperators.Clear();
    244 
    245         if (EnableManipulatorTracking.Value) {
    246           if (BeforeManipulatorOperator != null) {
    247             instrumentedManipulator.BeforeExecutionOperators.Add(BeforeManipulatorOperator);
    248           }
    249           if (AfterManipulatorOperator != null) {
    250             instrumentedManipulator.AfterExecutionOperators.Add(AfterManipulatorOperator);
    251           }
    252         }
    253       }
    254     }
    255 
    256     protected abstract void EvaluateIntermediateChildren();
    257 
    258     public override IOperation Apply() {
    259       IGenealogyGraph<T> genealogyGraph;
    260       var results = ResultsParameter.ActualValue;
    261       if (!results.ContainsKey(PopulationGraphParameterName)) {
    262         genealogyGraph = new GenealogyGraph<T>();
    263         results.Add(new Result(PopulationGraphParameterName, genealogyGraph));
    264       } else {
    265         genealogyGraph = (IGenealogyGraph<T>)results[PopulationGraphParameterName].Value;
    266       }
    267 
    268       var population = PopulationParameter.ActualValue;
    269       var qualities = QualityParameter.ActualValue;
    270 
    271       int generation = GenerationsParameter.ActualValue.Value;
    272       if (generation == 0) {
    273         ConfigureTrackingOperators();
    274 
    275         for (int i = 0; i < population.Length; ++i) {
    276           var individual = population[i];
    277           var vertex = new GenealogyGraphNode<T>(individual) { Rank = generation };
    278           genealogyGraph.AddVertex(vertex);
    279           // save the vertex id in the individual scope (so that we can identify graph indices)
    280           ExecutionContext.Scope.SubScopes[i].Variables.Add(new Variable("Id", new StringValue(vertex.Id)));
    281         }
    282       } else {
    283         int index = 0;
    284         T elite = null;
    285         // identify previous elite individual
    286         for (int i = 0; i < population.Length; ++i) {
    287           if (genealogyGraph.GetByContent(population[i]).Rank.Equals(generation - 1)) {
    288             elite = population[i];
    289             index = i;
    290             break;
    291           }
    292         }
    293         // add current elite and connect with previous
    294         #region add elite in the graph and connect it with the previous elite
    295         if (elite != null) {
    296           var prevVertex = genealogyGraph.GetByContent(elite);
    297           prevVertex.IsElite = true; // mark elites in the graph retroactively
    298           var v = (IGenealogyGraphNode<T>)prevVertex.Clone();
    299           v.Rank = generation;
    300           v.IsElite = false;
    301           population[index] = v.Data;
    302           genealogyGraph.AddVertex(v);
    303           genealogyGraph.AddArc(prevVertex, v);
    304           // inject the graph node unique id to the scope
    305           ExecutionContext.Scope.SubScopes[index].Variables[BeforeManipulatorOperator.ChildParameter.ActualName].Value = v.Data;
    306           ExecutionContext.Scope.SubScopes[index].Variables["Id"].Value = new StringValue(v.Id);
    307         }
    308         #endregion add elite in the graph and connect it with the previous elite
    309       }
    310       // update qualities
     217  }
     218
     219  public bool EnabledByDefault {
     220    get { return false; }
     221  }
     222
     223  private void ConfigureTrackingOperators() {
     224    // at the beginning we add the before/after operators to the instrumented operators
     225    var crossover = CrossoverParameter.ActualValue;
     226    if (crossover != null) {
     227      var instrumentedCrossover = (InstrumentedOperator)crossover;
     228      instrumentedCrossover.AfterExecutionOperators.Clear();
     229      instrumentedCrossover.BeforeExecutionOperators.Clear();
     230
     231      if (EnableCrossoverTracking.Value) {
     232        if (BeforeCrossoverOperator != null) {
     233          instrumentedCrossover.BeforeExecutionOperators.Add(BeforeCrossoverOperator);
     234        }
     235        if (AfterCrossoverOperator != null) {
     236          instrumentedCrossover.AfterExecutionOperators.Add(AfterCrossoverOperator);
     237        }
     238      }
     239    }
     240    var manipulator = ManipulatorParameter.ActualValue;
     241    if (manipulator != null) {
     242      var instrumentedManipulator = (InstrumentedOperator)manipulator;
     243      instrumentedManipulator.AfterExecutionOperators.Clear();
     244      instrumentedManipulator.BeforeExecutionOperators.Clear();
     245
     246      if (EnableManipulatorTracking.Value) {
     247        if (BeforeManipulatorOperator != null) {
     248          instrumentedManipulator.BeforeExecutionOperators.Add(BeforeManipulatorOperator);
     249        }
     250        if (AfterManipulatorOperator != null) {
     251          instrumentedManipulator.AfterExecutionOperators.Add(AfterManipulatorOperator);
     252        }
     253      }
     254    }
     255  }
     256
     257  protected abstract void EvaluateIntermediateChildren();
     258
     259  public override IOperation Apply() {
     260    IGenealogyGraph<T> genealogyGraph;
     261    var results = ResultsParameter.ActualValue;
     262    if (!results.ContainsKey(PopulationGraphParameterName)) {
     263      genealogyGraph = new GenealogyGraph<T>();
     264      results.Add(new Result(PopulationGraphParameterName, genealogyGraph));
     265    } else {
     266      genealogyGraph = (IGenealogyGraph<T>)results[PopulationGraphParameterName].Value;
     267    }
     268
     269    var population = PopulationParameter.ActualValue;
     270    var qualities = QualityParameter.ActualValue;
     271
     272    int generation = GenerationsParameter.ActualValue.Value;
     273    if (generation == 0) {
     274      ConfigureTrackingOperators();
     275
    311276      for (int i = 0; i < population.Length; ++i) {
    312         var vertex = genealogyGraph.GetByContent(population[i]);
    313         vertex.Quality = qualities[i].Value;
    314       }
    315       // update qualities for intermediate vertices
    316       EvaluateIntermediateChildren();
    317 
    318       // remove extra graph nodes (added by the instrumented operators in the case of offspring selection)
    319       var pop = new HashSet<T>(population);
    320       var discarded = genealogyGraph.GetByRank(generation).Where(x => !pop.Contains(x.Data)).ToList();
    321       for (int i = 0; i < discarded.Count; ++i) {
    322         var v = discarded[i];
    323         if (v.InDegree == 1) {
    324           var p = v.Parents.First();
    325           if (p.Rank.Equals(generation - 0.5))
    326             // the individual was a product of mutation. since it wasn't selected, we must remove both it and its parent
    327             // ("intermediate" vertex result of crossover)
    328             discarded.Add(v.Parents.First());
    329         }
    330       }
    331       genealogyGraph.RemoveVertices(discarded);
    332 
    333       //trim
    334       if (TrimOlderGenerations) {
    335         var vertices = genealogyGraph.Vertices.Where(x => x.Rank < generation - 1).ToList(); // select all ranks older than the previous generation
    336         genealogyGraph.RemoveVertices(vertices);
    337       }
    338 
    339       return base.Apply();
    340     }
     277        var individual = population[i];
     278        var vertex = new GenealogyGraphNode<T>(individual) { Rank = generation };
     279        genealogyGraph.AddVertex(vertex);
     280        // save the vertex id in the individual scope (so that we can identify graph indices)
     281        ExecutionContext.Scope.SubScopes[i].Variables.Add(new Variable("Id", new StringValue(vertex.Id)));
     282      }
     283    } else {
     284      int index = 0;
     285      T elite = null;
     286      // identify previous elite individual
     287      for (int i = 0; i < population.Length; ++i) {
     288        if (genealogyGraph.GetByContent(population[i]).Rank.Equals(generation - 1)) {
     289          elite = population[i];
     290          index = i;
     291          break;
     292        }
     293      }
     294      // add current elite and connect with previous
     295      #region add elite in the graph and connect it with the previous elite
     296      if (elite != null) {
     297        var prevVertex = genealogyGraph.GetByContent(elite);
     298        prevVertex.IsElite = true; // mark elites in the graph retroactively
     299        var v = (IGenealogyGraphNode<T>)prevVertex.Clone();
     300        v.Rank = generation;
     301        v.IsElite = false;
     302        population[index] = v.Data;
     303        genealogyGraph.AddVertex(v);
     304        genealogyGraph.AddArc(prevVertex, v);
     305        // inject the graph node unique id to the scope
     306        ExecutionContext.Scope.SubScopes[index].Variables[BeforeManipulatorOperator.ChildParameter.ActualName].Value = v.Data;
     307        ExecutionContext.Scope.SubScopes[index].Variables["Id"].Value = new StringValue(v.Id);
     308      }
     309      #endregion add elite in the graph and connect it with the previous elite
     310    }
     311    // update qualities
     312    for (int i = 0; i < population.Length; ++i) {
     313      var vertex = genealogyGraph.GetByContent(population[i]);
     314      vertex.Quality = qualities[i].Value;
     315    }
     316    // update qualities for intermediate vertices
     317    EvaluateIntermediateChildren();
     318
     319    // remove extra graph nodes (added by the instrumented operators in the case of offspring selection)
     320    var pop = new HashSet<T>(population);
     321    var discarded = genealogyGraph.GetByRank(generation).Where(x => !pop.Contains(x.Data)).ToList();
     322    for (int i = 0; i < discarded.Count; ++i) {
     323      var v = discarded[i];
     324      if (v.InDegree == 1) {
     325        var p = v.Parents.First();
     326        if (p.Rank.Equals(generation - 0.5))
     327          // the individual was a product of mutation. since it wasn't selected, we must remove both it and its parent
     328          // ("intermediate" vertex result of crossover)
     329          discarded.Add(v.Parents.First());
     330      }
     331    }
     332    genealogyGraph.RemoveVertices(discarded);
     333
     334    //trim
     335    if (TrimOlderGenerations) {
     336      var vertices = genealogyGraph.Vertices.Where(x => x.Rank < generation - 1).ToList(); // select all ranks older than the previous generation
     337      genealogyGraph.RemoveVertices(vertices);
     338    }
     339
     340    return base.Apply();
    341341  }
    342342}
  • branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/SelectionSchemeAnalyzer.cs

    r12894 r17434  
    2121
    2222using System.Linq;
     23using HEAL.Attic;
    2324using HeuristicLab.Analysis;
    2425using HeuristicLab.Common;
     
    2930using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3031
    31 namespace HeuristicLab.EvolutionTracking {
     32namespace HeuristicLab.EvolutionTracking
     33{
    3234  [Item("SelectionSchemeAnalyzer", "An analyzer which gives information about the relative amount of individuals that get selected each generation")]
    33   [StorableClass]
    34   public class SelectionSchemeAnalyzer : EvolutionTrackingAnalyzer {
     35  [StorableType("96FBBBE5-059C-4EA7-A633-0D2CBD7F58BE")]
     36  public class SelectionSchemeAnalyzer : EvolutionTrackingAnalyzer
     37  {
    3538    private const string StoreHistoryParameterName = "StoreHistory";
    3639
     
    4750
    4851    [StorableConstructor]
    49     protected SelectionSchemeAnalyzer(bool deserializing) : base(deserializing) { }
     52    protected SelectionSchemeAnalyzer(StorableConstructorFlag _) : base(_) { }
    5053
    5154    protected SelectionSchemeAnalyzer(SelectionSchemeAnalyzer original, Cloner cloner) : base(original, cloner) { }
Note: See TracChangeset for help on using the changeset viewer.