Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17614


Ignore:
Timestamp:
06/19/20 17:53:36 (4 years ago)
Author:
abeham
Message:

#2521: work in progress (removed solution creator parameter from encoding), OrienteeringProblem and test functions are broken

Location:
branches/2521_ProblemRefactoring
Files:
31 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/GeneticAlgorithm.cs

    r17226 r17614  
    381381    }
    382382    private void UpdateCrossovers() {
    383       ICrossover oldCrossover = CrossoverParameter.Value;
    384       CrossoverParameter.ValidValues.Clear();
    385       ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
    386 
    387       foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
    388         CrossoverParameter.ValidValues.Add(crossover);
    389 
    390       if (oldCrossover != null) {
    391         ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
    392         if (crossover != null) CrossoverParameter.Value = crossover;
    393         else oldCrossover = null;
    394       }
    395       if (oldCrossover == null && defaultCrossover != null)
    396         CrossoverParameter.Value = defaultCrossover;
    397     }
     383      CrossoverParameter.Repopulate(Problem.Operators);
     384    }
     385
    398386    private void UpdateMutators() {
    399       IManipulator oldMutator = MutatorParameter.Value;
    400       MutatorParameter.ValidValues.Clear();
    401       IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
    402 
    403       foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
    404         MutatorParameter.ValidValues.Add(mutator);
    405 
    406       if (oldMutator != null) {
    407         IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
    408         if (mutator != null) MutatorParameter.Value = mutator;
    409         else oldMutator = null;
    410       }
    411 
    412       if (oldMutator == null && defaultMutator != null)
    413         MutatorParameter.Value = defaultMutator;
     387      MutatorParameter.Repopulate(Problem.Operators);
    414388    }
    415389    private void UpdateAnalyzers() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Interfaces/IConstrainedValueParameter.cs

    r17461 r17614  
    2929
    3030    void Populate(IEnumerable<IItem> items);
     31    void Repopulate(IEnumerable<IItem> items);
    3132  }
    3233}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs

    r17587 r17614  
    4646    public BinaryVectorEncoding(int length) : this("BinaryVector", length) { }
    4747    public BinaryVectorEncoding(string name, int length)
    48       : base(name, length) {     
    49       SolutionCreator = new RandomBinaryVectorCreator();
    50 
     48      : base(name, length) {
    5149      DiscoverOperators();
    5250    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorEncoding.cs

    r17587 r17614  
    7878      Parameters.Add(BoundsParameter);
    7979
    80       SolutionCreator = new UniformRandomIntegerVectorCreator();
    8180      DiscoverOperators();
    8281      RegisterEventHandlers();
     
    9998      Parameters.Add(BoundsParameter);
    10099
    101       SolutionCreator = new UniformRandomIntegerVectorCreator();
    102100      DiscoverOperators();
    103101      RegisterEventHandlers();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/LinearLinkageEncoding.cs

    r17587 r17614  
    4949    public LinearLinkageEncoding(string name, int length)
    5050      : base(name, length) {
    51       SolutionCreator = new RandomLinearLinkageCreator();
    5251      DiscoverOperators();
    5352    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationEncoding.cs

    r17587 r17614  
    7070      Parameters.Add(PermutationTypeParameter);
    7171
    72       SolutionCreator = new RandomPermutationCreator();
    7372      DiscoverOperators();
    7473      RegisterParameterEvents();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEncoding.cs

    r17587 r17614  
    7575      Parameters.Add(BoundsParameter);
    7676
    77       SolutionCreator = new UniformRandomRealVectorCreator();
    7877      RegisterParameterEvents();
    7978      DiscoverOperators();
     
    9493      Parameters.Add(BoundsParameter);
    9594
    96       SolutionCreator = new UniformRandomRealVectorCreator();
    9795      DiscoverOperators();
    9896      RegisterParameterEvents();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/JobSequenceMatrix/JSMRandomCreator.cs

    r17461 r17614  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2526using HeuristicLab.Optimization;
    2627using HeuristicLab.Parameters;
    27 using HEAL.Attic;
    2828
    2929namespace HeuristicLab.Encodings.ScheduleEncoding {
    3030  [Item("JobSequenceMatrixCreator", "Creator class used to create Job Sequence Matrix solutions for standard JobShop scheduling problems.")]
    3131  [StorableType("F8053C69-31C2-4E05-8FA0-5AED15FAF804")]
    32   public class JSMRandomCreator : ScheduleCreator<JSM>, IStochasticOperator {
     32  public class JSMRandomCreator : ScheduleCreator<JSM>, IJSMOperator, IStochasticOperator {
    3333
    3434    public ILookupParameter<IRandom> RandomParameter {
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/JobSequenceMatrix/JobSequenceMatrixEncoding.cs

    r17461 r17614  
    4343    public JobSequenceMatrixEncoding()
    4444      : base("JSM") {
    45       SolutionCreator = new JSMRandomCreator();
    4645      Decoder = new JSMDecoder();
    4746      DiscoverOperators();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/PermutationWithRepetition/PWRRandomCreator.cs

    r17461 r17614  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Optimization;
    2526using HeuristicLab.Parameters;
    26 using HEAL.Attic;
    2727
    2828namespace HeuristicLab.Encodings.ScheduleEncoding {
    2929  [Item("PermutationWithRepetitionRandomCreator", "Creates PWR-individuals at random.")]
    3030  [StorableType("6E753916-C0FD-4585-B6A6-47FD66ED098F")]
    31   public class PWRRandomCreator : ScheduleCreator<PWR>, IStochasticOperator {
     31  public class PWRRandomCreator : ScheduleCreator<PWR>, IPWROperator, IStochasticOperator {
    3232
    3333    public ILookupParameter<IRandom> RandomParameter {
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/PermutationWithRepetition/PermutationWithRepetitionEncoding.cs

    r17461 r17614  
    4242    public PermutationWithRepetitionEncoding()
    4343      : base("PWR") {
    44       SolutionCreator = new PWRRandomCreator();
    4544      Decoder = new PWRDecoder();
    4645      DiscoverOperators();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/PriorityRulesVector/PRVRandomCreator.cs

    r17461 r17614  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Optimization;
    2526using HeuristicLab.Parameters;
    26 using HEAL.Attic;
    2727
    2828namespace HeuristicLab.Encodings.ScheduleEncoding {
    2929  [Item("PriorityRulesRandomCreator", "Creator class used to create PRV encoding objects for scheduling problems.")]
    3030  [StorableType("5FF2A11E-86F9-4A8B-8E1C-713D6801506C")]
    31   public class PRVRandomCreator : ScheduleCreator<PRV>, IStochasticOperator {
     31  public class PRVRandomCreator : ScheduleCreator<PRV>, IPRVOperator, IStochasticOperator {
    3232
    3333    [Storable]
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/PriorityRulesVector/PriorityRulesVectorEncoding.cs

    r17461 r17614  
    6565      Parameters.Add(numberOfRulesParameter);
    6666
    67       SolutionCreator = new PRVRandomCreator();
    6867      Decoder = new PRVDecoder();
    6968      DiscoverOperators();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding.cs

    r17567 r17614  
    161161    }
    162162
    163 
    164163    public override void ConfigureOperators(IEnumerable<IItem> operators) {
    165164      base.ConfigureOperators(operators);
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/DirectScheduleEncoding.cs

    r17461 r17614  
    4343    public DirectScheduleEncoding()
    4444      : base("Schedule") {
    45       SolutionCreator = new DirectScheduleRandomCreator();
    4645      Decoder = new DirectScheduleDecoder();
    4746      DiscoverOperators();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs

    r17570 r17614  
    177177      Parameters.Add(functionArgumentsParameter);
    178178
    179       SolutionCreator = new ProbabilisticTreeCreator();
    180179      RegisterParameterEvents();
    181180      DiscoverOperators();
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMultiObjectiveProblem.cs

    r17315 r17614  
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Core;
     29using HeuristicLab.Data;
    2930using HeuristicLab.Optimization;
     31using HeuristicLab.Parameters;
    3032
    3133namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3234  [StorableType("f4819c68-b6fc-469f-bcb5-cb5b2a9d8aff")]
    3335  public abstract class SymbolicExpressionTreeMultiObjectiveProblem : MultiObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
     36    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
     37    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
     38    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
     39
     40    public int TreeLength {
     41      get => TreeLengthRefParameter.Value.Value;
     42      set => TreeLengthRefParameter.Value.Value = value;
     43    }
     44
     45    public int TreeDepth {
     46      get => TreeDepthRefParameter.Value.Value;
     47      set => TreeDepthRefParameter.Value.Value = value;
     48    }
     49
     50    public ISymbolicExpressionGrammar Grammar {
     51      get => GrammarRefParameter.Value;
     52      set => GrammarRefParameter.Value = value;
     53    }
    3454
    3555    // persistence
     
    3757    protected SymbolicExpressionTreeMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
    3858    [StorableHook(HookType.AfterDeserialization)]
    39     private void AfterDeserialization() { }
     59    private void AfterDeserialization() {
     60      RegisterEventHandlers();
     61    }
    4062
    4163
     
    4365    protected SymbolicExpressionTreeMultiObjectiveProblem(SymbolicExpressionTreeMultiObjectiveProblem original, Cloner cloner)
    4466      : base(original, cloner) {
     67      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
     68      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
     69      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
     70      RegisterEventHandlers();
    4571    }
    4672
     
    4874      : base(encoding) {
    4975      EncodingParameter.ReadOnly = true;
     76      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
     77      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
     78      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
     79
     80      Parameterize();
     81      RegisterEventHandlers();
    5082    }
    5183
     
    5991    }
    6092
    61     protected override void OnEncodingChanged() {
    62       base.OnEncodingChanged();
    63       Parameterize();
    64     }
    65 
    6693    private void Parameterize() {
    6794      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
     
    7097      }
    7198    }
     99
     100    private void RegisterEventHandlers() {
     101      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
     102      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
     103      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
     104    }
     105
     106    protected virtual void TreeLengthOnChanged() { }
     107    protected virtual void TreeDepthOnChanged() { }
     108    protected virtual void GrammarOnChanged() { }
    72109  }
    73110}
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs

    r17520 r17614  
    2929using HeuristicLab.Data;
    3030using HeuristicLab.Optimization;
     31using HeuristicLab.Parameters;
    3132
    3233namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3334  [StorableType("A1B9F4C8-5E29-493C-A483-2AC68453BC63")]
    3435  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
     36    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
     37    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
     38    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
     39
     40    public int TreeLength {
     41      get => TreeLengthRefParameter.Value.Value;
     42      set => TreeLengthRefParameter.Value.Value = value;
     43    }
     44
     45    public int TreeDepth {
     46      get => TreeDepthRefParameter.Value.Value;
     47      set => TreeDepthRefParameter.Value.Value = value;
     48    }
     49
     50    public ISymbolicExpressionGrammar Grammar {
     51      get => GrammarRefParameter.Value;
     52      set => GrammarRefParameter.Value = value;
     53    }
    3554
    3655    // persistence
     
    3857    protected SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
    3958    [StorableHook(HookType.AfterDeserialization)]
    40     private void AfterDeserialization() { }
    41 
     59    private void AfterDeserialization() {
     60      RegisterEventHandlers();
     61    }
    4262
    4363    // cloning
    4464    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
    4565      : base(original, cloner) {
     66      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
     67      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
     68      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
     69      RegisterEventHandlers();
    4670    }
    4771
     
    5074      : base(encoding) {
    5175      EncodingParameter.ReadOnly = true;
     76      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
     77      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
     78      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
     79
     80      Parameterize();
     81      RegisterEventHandlers();
    5282    }
    5383
     
    73103    }
    74104
    75     protected override void OnEncodingChanged() {
    76       base.OnEncodingChanged();
    77       Parameterize();
    78     }
    79 
    80105    private void Parameterize() {
    81106      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
     
    84109      }
    85110    }
     111    private void RegisterEventHandlers() {
     112      IntValueParameterChangeHandler.Create(TreeLengthRefParameter, TreeLengthOnChanged);
     113      IntValueParameterChangeHandler.Create(TreeDepthRefParameter, TreeDepthOnChanged);
     114      ParameterChangeHandler<ISymbolicExpressionGrammar>.Create(GrammarRefParameter, GrammarOnChanged);
     115    }
     116
     117    protected virtual void TreeLengthOnChanged() { }
     118    protected virtual void TreeDepthOnChanged() { }
     119    protected virtual void GrammarOnChanged() { }
    86120  }
    87121}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r17517 r17614  
    185185    protected override void DeregisterProblemEvents() {
    186186      Problem.Reset -= new EventHandler(Problem_Reset);
     187      Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
    187188    }
    188189    protected override void RegisterProblemEvents() {
    189190      Problem.Reset += new EventHandler(Problem_Reset);
     191      Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
    190192    }
    191193    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedEncoding.cs

    r17226 r17614  
    5555      : base("CombinedEncoding") {
    5656      encodings = new ItemCollection<IEncoding>();
    57       SolutionCreator = new MultiEncodingCreator() { SolutionParameter = { ActualName = Name } };
    5857      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) {
    5958        @operator.SolutionParameter.ActualName = Name;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs

    r17571 r17614  
    5252        encodingOperators.Clear();
    5353        foreach (var op in value) encodingOperators.Add(op);
    54 
    55         ISolutionCreator<TEncodedSolution> newSolutionCreator = (ISolutionCreator<TEncodedSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??
    56                                encodingOperators.OfType<ISolutionCreator<TEncodedSolution>>().First();
    57         SolutionCreator = newSolutionCreator;
    5854        OnOperatorsChanged();
    59       }
    60     }
    61 
    62     public IValueParameter SolutionCreatorParameter {
    63       get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }
    64     }
    65 
    66     ISolutionCreator IEncoding.SolutionCreator {
    67       get { return SolutionCreator; }
    68     }
    69     public ISolutionCreator<TEncodedSolution> SolutionCreator {
    70       get { return (ISolutionCreator<TEncodedSolution>)SolutionCreatorParameter.Value; }
    71       set {
    72         if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
    73         encodingOperators.Remove(SolutionCreator);
    74         encodingOperators.Add(value);
    75         SolutionCreatorParameter.Value = value;
    76         OnSolutionCreatorChanged();
    7755      }
    7856    }
     
    8260    protected Encoding(StorableConstructorFlag _) : base(_) { }
    8361    [StorableHook(HookType.AfterDeserialization)]
    84     private void AfterDeserialization() {
    85       RegisterEventHandlers();
    86     }
     62    private void AfterDeserialization() { }
    8763
    8864    protected Encoding(Encoding<TEncodedSolution> original, Cloner cloner)
    8965      : base(original, cloner) {
    9066      encodingOperators = cloner.Clone(original.encodingOperators);
    91 
    92       RegisterEventHandlers();
    9367    }
    9468
    9569    protected Encoding(string name)
    9670      : base(name) {
    97       Parameters.Add(new ValueParameter<ISolutionCreator<TEncodedSolution>>(name + ".SolutionCreator", "The operator to create a solution.") {
    98         ReadOnly = true
    99       });
    10071      Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) {
    10172        GetsCollected = false, ReadOnly = true
    10273      });
    103 
    104       RegisterEventHandlers();
    105     }
    106 
    107     private void RegisterEventHandlers() {
    108       SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();
    10974    }
    11075
     
    137102    }
    138103
    139     public event EventHandler SolutionCreatorChanged;
    140     protected virtual void OnSolutionCreatorChanged() {
    141       ConfigureOperator(SolutionCreator);
    142       var handler = SolutionCreatorChanged;
    143       if (handler != null) handler(this, EventArgs.Empty);
    144     }
    145 
    146104    public event EventHandler OperatorsChanged;
    147105    protected virtual void OnOperatorsChanged() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs

    r17226 r17614  
    2828  [StorableType("d70b2675-246c-489c-a91b-b2e19a1616a3")]
    2929  public interface IEncoding : IParameterizedNamedItem {
    30     IValueParameter SolutionCreatorParameter { get; }
    31     ISolutionCreator SolutionCreator { get; }
    32 
    3330    IEnumerable<IOperator> Operators { get; set; }
    3431
     
    3734
    3835    event EventHandler OperatorsChanged;
    39     event EventHandler SolutionCreatorChanged;
    4036  }
    4137
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblem.cs

    r17612 r17614  
    4040  //TODO Intermediate class for compatibility
    4141  //TODO move members to generic IProblem after every problem used the new architecture
     42  //TODO ABE: We can maybe use it as non-generic interface that exports IEncoding Encoding { get; }
     43  //TODO ABE: and which is explicitely implemented in some base class
    4244  public interface IEncodedProblem : IProblem {
    4345    IEnumerable<IItem> Operators { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs

    r17226 r17614  
    2020#endregion
    2121
    22 using System;
    23 using System.Linq;
    2422using HEAL.Attic;
    2523using HeuristicLab.Common;
     
    5755    }
    5856
    59     public override void AddEncoding(IEncoding encoding) {
    60       base.AddEncoding(encoding);
    61       var parameter = GetParameter(encoding);
    62       parameter.Value = encoding.SolutionCreator;
    63       encoding.SolutionCreatorChanged += Encoding_SolutionCreatorChanged;
    64     }
    65 
    66     public override bool RemoveEncoding(IEncoding encoding) {
    67       var success = base.RemoveEncoding(encoding);
    68       encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged;
    69       return success;
    70     }
    71 
    72     private void Encoding_SolutionCreatorChanged(object sender, EventArgs e) {
    73       var encoding = (IEncoding)sender;
    74       var parameter = GetParameter(encoding);
    75 
    76       var oldCreator = parameter.ValidValues.Single(creator => creator.GetType() == encoding.SolutionCreator.GetType());
    77       parameter.ValidValues.Remove(oldCreator);
    78       parameter.ValidValues.Add(encoding.SolutionCreator);
    79       parameter.Value = encoding.SolutionCreator;
    80     }
    81 
    8257
    8358    public override IOperation InstrumentedApply() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs

    r17612 r17614  
    3737    where TEvaluator : class, IEvaluator {
    3838
     39    [Storable] protected ConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; }
    3940
    4041    //TODO remove parameter for encoding?
     
    5253    }
    5354
    54 
    55     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
    56       get { return Encoding.SolutionCreator; }
    57     }
    58     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
    59       get { return Encoding.SolutionCreatorParameter; }
    60     }
     55    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get => SolutionCreatorParameter.Value; }
     56    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get => SolutionCreatorParameter; }
     57
    6158    event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged {
    6259      add {
    63         if (Encoding != null) Encoding.SolutionCreatorChanged += value;
     60        SolutionCreatorParameter.ValueChanged += value;
    6461      }
    6562      remove {
    66         if (Encoding != null) Encoding.SolutionCreatorChanged -= value;
     63        SolutionCreatorParameter.ValueChanged -= value;
    6764      }
    6865    }
     
    106103      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) { Hidden = true });
    107104      Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true });
     105      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution."));
    108106
    109107      oldEncoding = Encoding;
     
    116114      : base(original, cloner) {
    117115      oldEncoding = cloner.Clone(original.oldEncoding);
     116      SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter);
    118117      RegisterEvents();
    119118    }
     
    124123    private void AfterDeserialization() {
    125124      oldEncoding = Encoding;
     125      // TODO: remove below
     126      if (SolutionCreatorParameter == null) Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution."));
     127
    126128      RegisterEvents();
    127129    }
     
    157159
    158160      Encoding.ConfigureOperators(Operators);
     161
     162      SolutionCreatorParameter.Repopulate(GetOperators());
    159163      //var multiEncoding = Encoding as MultiEncoding;
    160164      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ConstrainedValueParameter.cs

    r17226 r17614  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HEAL.Attic;
     
    5758    }
    5859
     60    public override void Repopulate(IEnumerable<IItem> items) {
     61      var itemsOfT = items.OfType<T>().ToList();
     62      T oldItem = Value;
     63      ValidValues.Clear();
     64      T defaultItem = itemsOfT.FirstOrDefault();
     65
     66      foreach (T i in itemsOfT.OrderBy(x => x is INamedItem ? ((INamedItem)x).Name : x.ItemName))
     67        ValidValues.Add(i);
     68
     69      if (oldItem != null) {
     70        T item = ValidValues.FirstOrDefault(x => x.GetType() == oldItem.GetType());
     71        if (item != null) Value = item;
     72        else oldItem = null;
     73      }
     74      if (oldItem == null && defaultItem != null)
     75        Value = defaultItem;
     76    }
     77
    5978    protected override void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
    6079      if (Value == null) Value = ValidValues.First();
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs

    r17461 r17614  
    175175    }
    176176
     177    public virtual void Repopulate(IEnumerable<IItem> items) {
     178      var itemsOfT = items.OfType<T>().ToList();
     179      T oldItem = Value;
     180      ValidValues.Clear();
     181
     182      foreach (T i in itemsOfT.OrderBy(x => x is INamedItem ? ((INamedItem)x).Name : x.ItemName))
     183        ValidValues.Add(i);
     184
     185      if (oldItem != null) {
     186        T item = ValidValues.FirstOrDefault(x => x.GetType() == oldItem.GetType());
     187        if (item != null) Value = item;
     188      }
     189    }
     190
    177191    [StorableHook(HookType.AfterDeserialization)]
    178192    private void AfterDeserialization() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ParameterChangeHandler.cs

    r17587 r17614  
    2525
    2626namespace HeuristicLab.Parameters {
    27   public abstract class ParameterChangeHandler<TItem> where TItem : class, IItem {
     27  public class ParameterChangeHandler<TItem> where TItem : class, IItem {
    2828    protected Action handler;
    2929
     
    3737      handler();
    3838    }
     39
     40    public static ParameterChangeHandler<TItem> Create(IValueParameter<TItem> parameter, Action handler) {
     41      return new ParameterChangeHandler<TItem>(parameter, handler);
     42    }
    3943  }
    4044
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblemInstances.cs

    r17570 r17614  
    4141  [StorableType("4ea0ded8-4451-4011-b88e-4d0680721b01")]
    4242  public sealed class SingleObjectiveBinaryVectorExternalEvaluationProblem : ExternalEvaluationProblem<BinaryVectorEncoding, BinaryVector> {
    43     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    44     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     43    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    4544
    4645    public int Dimension {
     
    7271  [StorableType("46465e8c-11d8-4d02-8c45-de41a08db7fa")]
    7372  public sealed class SingleObjectiveIntegerVectorExternalEvaluationProblem : ExternalEvaluationProblem<IntegerVectorEncoding, IntegerVector> {
    74     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    75     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    76     [Storable] private ReferenceParameter<IntMatrix> BoundsRefParameter;
    77     public IValueParameter<IntMatrix> BoundsParameter => BoundsRefParameter;
     73    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
     74    [Storable] private ReferenceParameter<IntMatrix> BoundsRefParameter { get; set; }
    7875
    7976    public int Dimension {
     
    112109  [StorableType("637f091f-6601-494e-bafb-2a8ea474210c")]
    113110  public sealed class SingleObjectiveRealVectorExternalEvaluationProblem : ExternalEvaluationProblem<RealVectorEncoding, RealVector> {
    114     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    115     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    116     [Storable] private ReferenceParameter<DoubleMatrix> BoundsRefParameter;
    117     public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
     111    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
     112    [Storable] private ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; set; }
    118113
    119114    public int Dimension {
     
    152147  [StorableType("ad9d45f8-b97e-49a7-b3d2-487d9a2cbdf9")]
    153148  public sealed class SingleObjectivePermutationExternalEvaluationProblem : ExternalEvaluationProblem<PermutationEncoding, Permutation> {
    154     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    155     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     149    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    156150
    157151    public int Dimension {
     
    183177  [StorableType("9b3ee4a8-7076-4edd-ae7e-4188bc49aaa3")]
    184178  public sealed class SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem : ExternalEvaluationProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
     179    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
     180    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
     181    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
     182
     183    public int TreeLength {
     184      get => TreeLengthRefParameter.Value.Value;
     185      set => TreeLengthRefParameter.Value.Value = value;
     186    }
     187
     188    public int TreeDepth {
     189      get => TreeDepthRefParameter.Value.Value;
     190      set => TreeDepthRefParameter.Value.Value = value;
     191    }
     192
     193    public ISymbolicExpressionGrammar Grammar {
     194      get => GrammarRefParameter.Value;
     195      set => GrammarRefParameter.Value = value;
     196    }
    185197
    186198    [StorableConstructor]
    187199    private SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
    188     private SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem(SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
     200    private SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem(SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem original, Cloner cloner)
     201      : base(original, cloner) {
     202      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
     203      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
     204      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
     205    }
    189206
    190207    public SingleObjectiveSymbolicExpressionTreeExternalEvaluationProblem()
    191208      : base(new SymbolicExpressionTreeEncoding()) {
    192       // TODO: Change to ReferenceParameter
    193       var lengthParameter = new FixedValueParameter<IntValue>("TreeLength", "The total amount of nodes.", new IntValue(50));
    194       Parameters.Add(lengthParameter);
    195       Encoding.TreeLengthParameter = lengthParameter;
    196       var depthParameter = new FixedValueParameter<IntValue>("TreeDepth", "The depth of the tree.", new IntValue(10));
    197       Parameters.Add(depthParameter);
    198       Encoding.TreeDepthParameter = depthParameter;
     209      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
     210      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
     211      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
    199212      // TODO: Add and parameterize additional operators,
    200213    }
     
    210223  [StorableType("945a35d9-89a8-4423-9ea0-21829ac68887")]
    211224  public sealed class SingleObjectiveLinearLinkageExternalEvaluationProblem : ExternalEvaluationProblem<LinearLinkageEncoding, LinearLinkage> {
    212     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    213     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     225    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    214226
    215227    public int Dimension {
     
    258270  [StorableType("f14c7e88-b74d-4cad-ae55-83daf7b4c288")]
    259271  public sealed class MultiObjectiveBinaryVectorExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<BinaryVectorEncoding, BinaryVector> {
    260     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    261     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     272    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    262273
    263274    public int Dimension {
     
    289300  [StorableType("90a82c2f-6c37-4ffd-8495-bee278c583d3")]
    290301  public sealed class MultiObjectiveIntegerVectorExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<IntegerVectorEncoding, IntegerVector> {
    291     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    292     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    293     [Storable] private ReferenceParameter<IntMatrix> BoundsRefParameter;
    294     public IValueParameter<IntMatrix> BoundsParameter => BoundsRefParameter;
     302    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
     303    [Storable] private ReferenceParameter<IntMatrix> BoundsRefParameter { get; set; }
    295304
    296305    public int Dimension {
     
    329338  [StorableType("38e1d068-d569-48c5-bad6-cbdd685b7c6b")]
    330339  public sealed class MultiObjectiveRealVectorExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<RealVectorEncoding, RealVector> {
    331     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    332     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
    333     [Storable] private ReferenceParameter<DoubleMatrix> BoundsRefParameter;
    334     public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
     340    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
     341    [Storable] private ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; set; }
    335342
    336343    public int Dimension {
     
    369376  [StorableType("f1b265b0-ac7c-4c36-b346-5b3f2c37694b")]
    370377  public sealed class MultiObjectivePermutationExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<PermutationEncoding, Permutation> {
    371     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    372     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     378    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    373379
    374380    public int Dimension {
     
    400406  [StorableType("fb6834e2-2d56-4711-a3f8-5e0ab55cd040")]
    401407  public sealed class MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
     408    [Storable] private ReferenceParameter<IntValue> TreeLengthRefParameter { get; set; }
     409    [Storable] private ReferenceParameter<IntValue> TreeDepthRefParameter { get; set; }
     410    [Storable] private ReferenceParameter<ISymbolicExpressionGrammar> GrammarRefParameter { get; set; }
     411
     412    public int TreeLength {
     413      get => TreeLengthRefParameter.Value.Value;
     414      set => TreeLengthRefParameter.Value.Value = value;
     415    }
     416
     417    public int TreeDepth {
     418      get => TreeDepthRefParameter.Value.Value;
     419      set => TreeDepthRefParameter.Value.Value = value;
     420    }
     421
     422    public ISymbolicExpressionGrammar Grammar {
     423      get => GrammarRefParameter.Value;
     424      set => GrammarRefParameter.Value = value;
     425    }
    402426
    403427    [StorableConstructor]
    404428    private MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
    405     private MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem(MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
     429    private MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem(MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem original, Cloner cloner)
     430      : base(original, cloner) {
     431      TreeLengthRefParameter = cloner.Clone(original.TreeLengthRefParameter);
     432      TreeDepthRefParameter = cloner.Clone(original.TreeDepthRefParameter);
     433      GrammarRefParameter = cloner.Clone(original.GrammarRefParameter);
     434    }
    406435
    407436    public MultiObjectiveSymbolicExpressionTreeExternalEvaluationProblem()
    408437      : base(new SymbolicExpressionTreeEncoding()) {
    409       // TODO: Change to ReferenceParameter
    410       var lengthParameter = new FixedValueParameter<IntValue>("TreeLength", "The total amount of nodes.", new IntValue(50));
    411       Parameters.Add(lengthParameter);
    412       Encoding.TreeLengthParameter = lengthParameter;
    413       var depthParameter = new FixedValueParameter<IntValue>("TreeDepth", "The depth of the tree.", new IntValue(10));
    414       Parameters.Add(depthParameter);
    415       Encoding.TreeDepthParameter = depthParameter;
     438      Parameters.Add(TreeLengthRefParameter = new ReferenceParameter<IntValue>("TreeLength", "The maximum amount of nodes.", Encoding.TreeLengthParameter));
     439      Parameters.Add(TreeDepthRefParameter = new ReferenceParameter<IntValue>("TreeDepth", "The maximum depth of the tree.", Encoding.TreeDepthParameter));
     440      Parameters.Add(GrammarRefParameter = new ReferenceParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that describes a valid tree.", Encoding.GrammarParameter));
    416441      // TODO: Add and parameterize additional operators,
    417442    }
     
    427452  [StorableType("ed0c1129-651d-465f-87b0-f412f3e3b3d1")]
    428453  public sealed class MultiObjectiveLinearLinkageExternalEvaluationProblem : MultiObjectiveExternalEvaluationProblem<LinearLinkageEncoding, LinearLinkage> {
    429     [Storable] private ReferenceParameter<IntValue> DimensionRefParameter;
    430     public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
     454    [Storable] private ReferenceParameter<IntValue> DimensionRefParameter { get; set; }
    431455
    432456    public int Dimension {
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Orienteering/3.3/OrienteeringProblem.cs

    r17612 r17614  
    165165
    166166    private void InitializeOperators() {
    167       Encoding.SolutionCreator = new GreedyOrienteeringTourCreator() {
     167      ISolutionCreator creator;
     168      SolutionCreatorParameter.ValidValues.Add(creator = new GreedyOrienteeringTourCreator() {
    168169        OrienteeringProblemDataParameter = { ActualName = OrienteeringProblemDataParameter.Name }
    169       };
     170      });
     171      SolutionCreatorParameter.Value = creator;
     172
    170173      Operators.Add(new OrienteeringLocalImprovementOperator() {
    171174        OrienteeringProblemDataParameter = { ActualName = OrienteeringProblemDataParameter.Name }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r17587 r17614  
    186186        foreach (var c in calcs) {
    187187          // TODO: unified encoding parameters
    188           c.SolutionVariableName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
     188          c.SolutionVariableName = Encoding.Name;
    189189          c.QualityVariableName = Evaluator.QualityParameter.ActualName;
    190190          op.SimilarityCalculatorParameter.ValidValues.Add(c);
     
    200200      foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
    201201        // TODO: unified encoding parameters
    202         op.RealVectorParameter.ActualName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
     202        op.RealVectorParameter.ActualName = Encoding.Name;
    203203        op.RealVectorParameter.Hidden = true;
    204204        op.BoundsParameter.ActualName = BoundsRefParameter.Name;
     
    207207      foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
    208208        // TODO: unified encoding parameters
    209         op.RealVectorParameter.ActualName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
     209        op.RealVectorParameter.ActualName = Encoding.Name;
    210210        op.RealVectorParameter.Hidden = true;
    211211        op.BoundsParameter.ActualName = BoundsRefParameter.Name;
Note: See TracChangeset for help on using the changeset viewer.