Changeset 13269


Ignore:
Timestamp:
11/19/15 09:57:06 (4 years ago)
Author:
mkommend
Message:

#2472: Moved cloning and persistence code near the default ctor for all new GP problems.

Location:
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Interpreter.cs

    r12911 r13269  
    3232    public int FoodEaten { get; private set; }
    3333    public BoolMatrix World { get; private set; }
    34 
    3534    public ISymbolicExpressionTree Expression { get; private set; }
    3635
     36    public int ElapsedTime { get; set; }
    3737
    38     public int ElapsedTime { get; set; }
    3938    private int currentDirection;
    4039    private int currentAntLocationRow;
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Problem.cs

    r13055 r13269  
    2222using System;
    2323using System.Diagnostics.Contracts;
    24 using System.Drawing.Text;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    103102    }
    104103
     104    #region item cloning and persistence
     105    // persistence
     106    [StorableConstructor]
     107    private Problem(bool deserializing) : base(deserializing) { }
     108    [StorableHook(HookType.AfterDeserialization)]
     109    private void AfterDeserialization() { }
     110
     111    // cloning
     112    private Problem(Problem original, Cloner cloner) : base(original, cloner) { }
     113    public override IDeepCloneable Clone(Cloner cloner) {
     114      return new Problem(this, cloner);
     115    }
     116    #endregion
     117
    105118    public Problem()
    106119      : base() {
     
    136149    }
    137150
    138     // persistence
    139     [StorableConstructor]
    140     private Problem(bool deserializing) : base(deserializing) { }
    141     [StorableHook(HookType.AfterDeserialization)]
    142     private void AfterDeserialization() {
    143     }
    144 
    145     // cloning
    146     private Problem(Problem original, Cloner cloner)
    147       : base(original, cloner) {
    148     }
    149     public override IDeepCloneable Clone(Cloner cloner) {
    150       return new Problem(this, cloner);
    151     }
    152 
    153151    #region helpers
    154152    private bool[,] ToBoolMatrix(char[][] ch) {
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Trail.cs

    r12911 r13269  
    2020#endregion
    2121
    22 using System;
    2322using System.Drawing;
    2423using HeuristicLab.Common;
     
    5453    }
    5554
     55    #region item cloning and persistence
    5656    [StorableConstructor]
    5757    private Solution(bool deserializing) : base(deserializing) { }
     
    7070      return new Solution(this, cloner);
    7171    }
     72    #endregion
    7273  }
    7374}
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs

    r13267 r13269  
    3939
    4040    #region parameter names
    41 
    4241    private const string ProblemDataParameterName = "ProblemData";
    43 
    4442    #endregion
    45 
    46     public event EventHandler ProblemDataChanged;
    4743
    4844    #region Parameter Properties
     
    5248      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
    5349    }
    54 
    5550    #endregion
    5651
    5752    #region Properties
    58 
    5953    public IRegressionProblemData ProblemData {
    6054      get { return ProblemDataParameter.Value; }
     
    6256    }
    6357    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { get { return ProblemData; } }
     58    #endregion
    6459
    65 
    66     #endregion
     60    public event EventHandler ProblemDataChanged;
    6761
    6862    public override bool Maximization {
    6963      get { return true; }
    7064    }
     65
     66    #region item cloning and persistence
     67    // persistence
     68    [StorableConstructor]
     69    private Problem(bool deserializing) : base(deserializing) { }
     70    [StorableHook(HookType.AfterDeserialization)]
     71    private void AfterDeserialization() {
     72      RegisterEventHandlers();
     73    }
     74
     75    // cloning
     76    private Problem(Problem original, Cloner cloner)
     77      : base(original, cloner) {
     78      RegisterEventHandlers();
     79    }
     80    public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); }
     81    #endregion
    7182
    7283    public Problem()
     
    102113      return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows);
    103114    }
    104                                                                                                        
     115
    105116    private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) {
    106117      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval =
    107         (left, right, f) => InterpretRec(left, dataset,rows).Zip(InterpretRec(right, dataset, rows), f);
     118        (left, right, f) => InterpretRec(left, dataset, rows).Zip(InterpretRec(right, dataset, rows), f);
    108119
    109120      switch (node.Symbol.Name) {
     
    124135    }
    125136
    126     #region item cloning and persistence
    127     // persistence
    128     [StorableConstructor]
    129     private Problem(bool deserializing) : base(deserializing) { }
    130 
    131     [StorableHook(HookType.AfterDeserialization)]
    132     private void AfterDeserialization() {
    133       RegisterEventHandlers();
    134     }
    135 
    136     // cloning
    137     private Problem(Problem original, Cloner cloner)
    138       : base(original, cloner) {
    139       RegisterEventHandlers();
    140     }
    141     public override IDeepCloneable Clone(Cloner cloner) {
    142       return new Problem(this, cloner);
    143     }
    144     #endregion
    145137
    146138    #region events
    147 
    148139    private void RegisterEventHandlers() {
    149140      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
     
    188179      Encoding.Grammar = g;
    189180    }
    190 
    191181    #endregion
    192182
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/EvenParityProblem.cs

    r13267 r13269  
    5858    }
    5959
     60    #region item cloning and persistence
     61    // persistence
     62    [StorableConstructor]
     63    private EvenParityProblem(bool deserializing) : base(deserializing) { }
     64    [StorableHook(HookType.AfterDeserialization)]
     65    private void AfterDeserialization() {
     66      RegisterEventHandlers();
     67    }
     68
     69    // cloning
     70    private EvenParityProblem(EvenParityProblem original, Cloner cloner)
     71      : base(original, cloner) {
     72      RegisterEventHandlers();
     73    }
     74    public override IDeepCloneable Clone(Cloner cloner) {
     75      return new EvenParityProblem(this, cloner);
     76    }
     77    #endregion
     78
    6079    public EvenParityProblem()
    6180      : base() {
     
    109128
    110129      switch (node.Symbol.Name) {
    111         case "AND":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
    112         case "OR":   return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
     130        case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
     131        case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
    113132        case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));
    114         case "NOR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));
     133        case "NOR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));
    115134        default: {
    116135            byte bitPos;
     
    126145    }
    127146
    128     #region item cloning and persistence
    129     // persistence
    130     [StorableConstructor]
    131     private EvenParityProblem(bool deserializing) : base(deserializing) { }
    132 
    133     [StorableHook(HookType.AfterDeserialization)]
    134     private void AfterDeserialization() {
    135       RegisterEventHandlers();
    136     }
    137 
    138     // cloning
    139     private EvenParityProblem(EvenParityProblem original, Cloner cloner)
    140       : base(original, cloner) {
    141       RegisterEventHandlers();
    142     }
    143     public override IDeepCloneable Clone(Cloner cloner) {
    144       return new EvenParityProblem(this, cloner);
    145     }
    146     #endregion
    147 
    148147    #region events
    149148    private void RegisterEventHandlers() {
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs

    r13267 r13269  
    5959      get { return true; }
    6060    }
     61
     62    #region item cloning and persistence
     63    // persistence
     64    [StorableConstructor]
     65    private MultiplexerProblem(bool deserializing) : base(deserializing) { }
     66    [StorableHook(HookType.AfterDeserialization)]
     67    private void AfterDeserialization() {
     68      RegisterEventHandlers();
     69    }
     70
     71    // cloning
     72    private MultiplexerProblem(MultiplexerProblem original, Cloner cloner)
     73      : base(original, cloner) {
     74      RegisterEventHandlers();
     75    }
     76    public override IDeepCloneable Clone(Cloner cloner) {
     77      return new MultiplexerProblem(this, cloner);
     78    }
     79    #endregion
     80
    6181
    6282    public MultiplexerProblem()
     
    134154      switch (node.Symbol.Name) {
    135155        case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
    136         case "OR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
     156        case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
    137157        case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x);
    138         case "IF":  return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);
     158        case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);
    139159        default: {
    140160            if (node.Symbol.Name[0] == 'a') {
     
    171191    }
    172192
    173     #region item cloning and persistence
    174     // persistence
    175     [StorableConstructor]
    176     private MultiplexerProblem(bool deserializing) : base(deserializing) { }
    177 
    178     [StorableHook(HookType.AfterDeserialization)]
    179     private void AfterDeserialization() {
    180       RegisterEventHandlers();
    181     }
    182 
    183     // cloning
    184     private MultiplexerProblem(MultiplexerProblem original, Cloner cloner)
    185       : base(original, cloner) {
    186       RegisterEventHandlers();
    187     }
    188     public override IDeepCloneable Clone(Cloner cloner) {
    189       return new MultiplexerProblem(this, cloner);
    190     }
    191     #endregion
    192 
    193193    #region events
    194194    private void RegisterEventHandlers() {
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs

    r13267 r13269  
    5050    }
    5151
     52    #region item cloning and persistence
    5253    [StorableConstructor]
    5354    protected Problem(bool deserializing) : base(deserializing) { }
     
    5960      return new Problem(this, cloner);
    6061    }
     62    #endregion
    6163
    6264    public Problem()
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Solution.cs

    r13267 r13269  
    3737    public double Quality { get; private set; }
    3838
     39    #region item cloning and persistence
    3940    [StorableConstructor]
    4041    private Solution(bool deserializing) : base(deserializing) { }
     
    5253      return new Solution(this, cloner);
    5354    }
     55    #endregion
    5456
    5557    public Solution(ISymbolicExpressionTree tree, int length, int width, double quality)
Note: See TracChangeset for help on using the changeset viewer.