Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/15 15:16:24 (9 years ago)
Author:
mkommend
Message:

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

Location:
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs

    r13396 r13469  
    5353        foreach (var op in value) encodingOperators.Add(op);
    5454
    55         ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
     55        ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??
    5656                               encodingOperators.OfType<ISolutionCreator<TSolution>>().First();
    5757        SolutionCreator = newSolutionCreator;
     
    6060    }
    6161
    62     ISolutionCreator IEncoding.SolutionCreator {
    63       get { return solutionCreator; }
     62    public IValueParameter SolutionCreatorParameter {
     63      get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }
    6464    }
    6565
    66     [Storable]
    67     private ISolutionCreator<TSolution> solutionCreator;
     66    ISolutionCreator IEncoding.SolutionCreator {
     67      get { return SolutionCreator; }
     68    }
    6869    public ISolutionCreator<TSolution> SolutionCreator {
    69       get {
    70         return solutionCreator;
    71       }
     70      get { return (ISolutionCreator<TSolution>)SolutionCreatorParameter.Value; }
    7271      set {
    7372        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
    74         if (solutionCreator == value) return;
    75         encodingOperators.Remove(solutionCreator);
     73        encodingOperators.Remove(SolutionCreator);
    7674        encodingOperators.Add(value);
    77         solutionCreator = value;
     75        SolutionCreatorParameter.Value = value;
    7876        OnSolutionCreatorChanged();
    7977      }
    8078    }
    8179
     80
    8281    [StorableConstructor]
    8382    protected Encoding(bool deserializing) : base(deserializing) { }
     83    [StorableHook(HookType.AfterDeserialization)]
     84    private void AfterDeserialization() {
     85      RegisterEventHandlers();
     86    }
     87
    8488    protected Encoding(Encoding<TSolution> original, Cloner cloner)
    8589      : base(original, cloner) {
    8690      encodingOperators = cloner.Clone(original.encodingOperators);
    87       solutionCreator = cloner.Clone(original.solutionCreator);
     91
     92      RegisterEventHandlers();
    8893    }
     94
    8995    protected Encoding(string name)
    9096      : base(name) {
     97      Parameters.Add(new ValueParameter<ISolutionCreator<TSolution>>(name + ".SolutionCreator", "The operator to create a solution."));
    9198      Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()));
     99
     100      RegisterEventHandlers();
     101    }
     102
     103    private void RegisterEventHandlers() {
     104      SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();
    92105    }
    93106
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs

    r13396 r13469  
    2626namespace HeuristicLab.Optimization {
    2727  public interface IEncoding : IParameterizedNamedItem {
     28    IValueParameter SolutionCreatorParameter { get; }
    2829    ISolutionCreator SolutionCreator { get; }
     30
    2931    IEnumerable<IOperator> Operators { get; set; }
    3032
     
    3234    void ConfigureOperators(IEnumerable<IItem> operators);
    3335
     36    event EventHandler OperatorsChanged;
    3437    event EventHandler SolutionCreatorChanged;
    35     event EventHandler OperatorsChanged;
    3638  }
    3739
    3840  public interface IEncoding<TSolution> : IEncoding
    3941    where TSolution : class, ISolution {
    40     new ISolutionCreator<TSolution> SolutionCreator { get; set; }
     42    //new ISolutionCreator<TSolution> SolutionCreator { get; }
    4143  }
    4244}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs

    r13396 r13469  
    3030namespace HeuristicLab.Optimization {
    3131  [StorableClass]
    32   public abstract class Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent
     32  public abstract class Problem<TEncoding, TSolution, TEvaluator> : Problem,
     33    IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TSolution>, IStorableContent
    3334    where TEncoding : class, IEncoding<TSolution>
    3435    where TSolution : class, ISolution
    3536    where TEvaluator : class, IEvaluator {
    3637
    37     public string Filename { get; set; } // TODO: Really okay here?
     38    public string Filename { get; set; } // TODO: Really okay here? should be in Problem (non-generic)
    3839
     40    //TODO remove parametr for encoding?
    3941    protected IValueParameter<TEncoding> EncodingParameter {
    4042      get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
    4143    }
    42 
    4344    //mkommend necessary for reuse of operators if the encoding changes
    4445    private TEncoding oldEncoding;
    45 
    4646    public TEncoding Encoding {
    4747      get { return EncodingParameter.Value; }
     
    5151      }
    5252    }
     53
     54    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
     55      get { return Encoding.SolutionCreator; }
     56    }
     57    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
     58      get { return Encoding.SolutionCreatorParameter; }
     59    }
     60    event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged {
     61      add { Encoding.SolutionCreatorChanged += value; }
     62      remove { Encoding.SolutionCreatorChanged -= value; }
     63    }
     64
     65    //TODO is a parameter for the evaluator really necessary, only single-objective or multi-objective evulators calling the func are possible
     66    public ValueParameter<TEvaluator> EvaluatorParameter {
     67      get { return (ValueParameter<TEvaluator>)Parameters["Evaluator"]; }
     68    }
     69    public TEvaluator Evaluator {
     70      get { return EvaluatorParameter.Value; }
     71      protected set { EvaluatorParameter.Value = value; }
     72    }
     73    IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } }
     74    IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
     75
     76    public event EventHandler EvaluatorChanged;
     77    protected virtual void OnEvaluatorChanged() {
     78      EventHandler handler = EvaluatorChanged;
     79      if (handler != null)
     80        handler(this, EventArgs.Empty);
     81    }
     82
    5383
    5484    protected override IEnumerable<IItem> GetOperators() {
     
    6696      : base() {
    6797      Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
     98      Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution."));
     99
    68100      if (Encoding != null) {
    69101        oldEncoding = Encoding;
    70         SolutionCreator = Encoding.SolutionCreator;
    71102        Parameterize();
    72103      }
     
    76107      if (encoding == null) throw new ArgumentNullException("encoding");
    77108      Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.", encoding));
     109      Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution."));
     110
    78111      oldEncoding = Encoding;
    79       SolutionCreator = Encoding.SolutionCreator;
    80112      Parameterize();
    81113
     
    99131    private void RegisterEvents() {
    100132      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
     133      EvaluatorParameter.ValueChanged += (o, e) => OnEvaluatorChanged();
    101134      //var multiEncoding = Encoding as MultiEncoding;
    102135      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
     
    122155        op.EncodingParameter.ActualName = EncodingParameter.Name;
    123156
    124       SolutionCreator = Encoding.SolutionCreator;
    125 
    126157      //var multiEncoding = Encoding as MultiEncoding;
    127158      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
    128159    }
    129160
    130     protected override void OnSolutionCreatorChanged() {
    131       base.OnSolutionCreatorChanged();
    132       Encoding.SolutionCreator = SolutionCreator;
    133     }
     161    //protected override void OnSolutionCreatorChanged() {
     162    //  base.OnSolutionCreatorChanged();
     163    //  Encoding.SolutionCreator = SolutionCreator;
     164    //}
    134165
    135166    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
    136167      if (oldEncoding.GetType() != newEncoding.GetType()) return;
    137168
    138       if (oldEncoding.GetType() == typeof(CombinedEncoding)) {
     169      if (oldEncoding is CombinedEncoding) {
    139170        var oldMultiEncoding = (CombinedEncoding)oldEncoding;
    140171        var newMultiEncoding = (CombinedEncoding)newEncoding;
Note: See TracChangeset for help on using the changeset viewer.