Changeset 13336


Ignore:
Timestamp:
11/23/15 15:15:27 (4 years ago)
Author:
mkommend
Message:

#2521: Refactored encodings and problems.

Location:
branches/ProblemRefactoring
Files:
34 edited
3 moved

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVector.cs

    r12012 r13336  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
     25using HeuristicLab.Optimization;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
     
    2829  [StorableClass]
    2930  [Item("BinaryVector", "Represents a vector of binary values.")]
    30   public class BinaryVector : BoolArray {
     31  public class BinaryVector : BoolArray, ISolution {
    3132    [StorableConstructor]
    3233    protected BinaryVector(bool deserializing) : base(deserializing) { }
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs

    r12012 r13336  
    3434  [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
    3535  [StorableClass]
    36   public sealed class BinaryVectorEncoding : Encoding<IBinaryVectorCreator> {
     36  public sealed class BinaryVectorEncoding : Encoding<BinaryVector> {
    3737    #region Encoding Parameters
    3838    [Storable]
     
    166166    #endregion
    167167  }
    168 
    169   public static class IndividualExtensionMethods {
    170     public static BinaryVector BinaryVector(this Individual individual) {
    171       var encoding = individual.GetEncoding<BinaryVectorEncoding>();
    172       return individual.BinaryVector(encoding.Name);
    173     }
    174 
    175     public static BinaryVector BinaryVector(this Individual individual, string name) {
    176       return (BinaryVector)individual[name];
    177     }
    178   }
    179168}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorCreator.cs

    r12012 r13336  
    2828  /// An interface which represents an operator for creating vectors of bool-valued data.
    2929  /// </summary>
    30   public interface IBinaryVectorCreator : IBinaryVectorOperator, ISolutionCreator {
     30  public interface IBinaryVectorCreator : IBinaryVectorOperator, ISolutionCreator<BinaryVector> {
    3131    IValueLookupParameter<IntValue> LengthParameter { get; }
    3232    ILookupParameter<BinaryVector> BinaryVectorParameter { get; }
  • branches/ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs

    r12921 r13336  
    3232namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3333  [StorableClass]
    34   public abstract class SymbolicExpressionTreeProblem : SingleObjectiveBasicProblem<SymbolicExpressionTreeEncoding> {
     34  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<SymbolicExpressionTreeEncoding> {
    3535
    3636    // persistence
  • branches/ProblemRefactoring/HeuristicLab.Optimization.Views/3.3/HeuristicLab.Optimization.Views-3.3.csproj

    r12975 r13336  
    142142      <DependentUpon>BatchRunView.cs</DependentUpon>
    143143    </Compile>
    144     <Compile Include="CreateNewSingleEncodingDialog.cs">
    145       <SubType>Form</SubType>
    146     </Compile>
    147     <Compile Include="CreateNewSingleEncodingDialog.Designer.cs">
    148       <DependentUpon>CreateNewSingleEncodingDialog.cs</DependentUpon>
    149     </Compile>
    150144    <Compile Include="ISolutionSimilarityCalculatorView.cs">
    151145      <SubType>UserControl</SubType>
     
    159153    <Compile Include="ExperimentListView.Designer.cs">
    160154      <DependentUpon>ExperimentListView.cs</DependentUpon>
    161     </Compile>
    162     <Compile Include="MultiEncodingView.cs">
    163       <SubType>UserControl</SubType>
    164     </Compile>
    165     <Compile Include="MultiEncodingView.Designer.cs">
    166       <DependentUpon>MultiEncodingView.cs</DependentUpon>
    167155    </Compile>
    168156    <Compile Include="Plugin.cs" />
     
    427415    </ProjectReference>
    428416  </ItemGroup>
    429   <ItemGroup>
    430     <EmbeddedResource Include="CreateNewSingleEncodingDialog.resx">
    431       <DependentUpon>CreateNewSingleEncodingDialog.cs</DependentUpon>
    432     </EmbeddedResource>
    433   </ItemGroup>
    434417  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    435418  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs

    r12981 r13336  
    3131  [Item("Encoding", "Base class for describing different encodings.")]
    3232  [StorableClass]
    33   public abstract class Encoding<T> : ParameterizedNamedItem, IEncoding
    34     where T : class,ISolutionCreator {
     33  public abstract class Encoding<TSolution> : ParameterizedNamedItem, IEncoding<TSolution>
     34    where TSolution : class,ISolution {
    3535    public override sealed bool CanChangeName {
    3636      get { return false; }
     
    4848      get { return encodingOperators; }
    4949      set {
    50         if (!value.OfType<T>().Any())
     50        if (!value.OfType<ISolutionCreator<TSolution>>().Any())
    5151          throw new ArgumentException("The provided operators contain no suitable solution creator");
    5252        encodingOperators.Clear();
    5353        foreach (var op in value) encodingOperators.Add(op);
    5454
    55         T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
    56                                encodingOperators.OfType<T>().First();
     55        ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
     56                               encodingOperators.OfType<ISolutionCreator<TSolution>>().First();
    5757        SolutionCreator = newSolutionCreator;
    5858        OnOperatorsChanged();
     
    6060    }
    6161
    62     ISolutionCreator IEncoding.SolutionCreator {
    63       get { return SolutionCreator; }
    64       set {
    65         if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
    66         SolutionCreator = (T)value;
    67       }
    68     }
    6962    [Storable]
    70     private T solutionCreator;
    71     public T SolutionCreator {
     63    private ISolutionCreator<TSolution> solutionCreator;
     64    public ISolutionCreator<TSolution> SolutionCreator {
    7265      get {
    7366        return solutionCreator;
     
    8578    [StorableConstructor]
    8679    protected Encoding(bool deserializing) : base(deserializing) { }
    87     protected Encoding(Encoding<T> original, Cloner cloner)
     80    protected Encoding(Encoding<TSolution> original, Cloner cloner)
    8881      : base(original, cloner) {
    8982      encodingOperators = cloner.Clone(original.encodingOperators);
     
    9386      : base(name) {
    9487      Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()));
    95     }
    96 
    97     public virtual Individual GetIndividual(IScope scope) {
    98       return new SingleEncodingIndividual(this, scope);
    9988    }
    10089
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs

    r12012 r13336  
    2525
    2626namespace HeuristicLab.Optimization {
    27   public interface IEncoding : IParameterizedNamedItem {
    28     ISolutionCreator SolutionCreator { get; set; }
     27  public interface IEncoding<TSolution> : IParameterizedNamedItem
     28    where TSolution : class, ISolution {
     29    ISolutionCreator<TSolution> SolutionCreator { get; set; }
    2930    IEnumerable<IOperator> Operators { get; set; }
    3031
    31     Individual GetIndividual(IScope scope);
    3232    void ConfigureOperator(IOperator @operator);
    3333    void ConfigureOperators(IEnumerable<IOperator> operators);
     
    3636    event EventHandler OperatorsChanged;
    3737  }
     38
     39
    3840}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodingOperator.cs

    r12012 r13336  
    2323
    2424namespace HeuristicLab.Optimization {
    25   public interface IEncodingOperator : IOperator {
    26     ILookupParameter<IEncoding> EncodingParameter { get; }
     25  public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution{
     26    ILookupParameter<IEncoding<TSolution>> EncodingParameter { get; }
    2727  }
    2828}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs

    r12012 r13336  
    2323
    2424namespace HeuristicLab.Optimization {
    25   public interface IMultiEncodingOperator : IOperator {
    26     void AddEncoding(IEncoding encoding);
    27     bool RemoveEncoding(IEncoding encoding);
     25  public interface IMultiEncodingOperator<TSolution> : IOperator where TSolution : class,ISolution {
     26    void AddEncoding(IEncoding<TSolution> encoding);
     27    bool RemoveEncoding(IEncoding<TSolution> encoding);
    2828  }
    2929}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs

    r12012 r13336  
    2323
    2424namespace HeuristicLab.Optimization {
    25   public interface IMultiObjectiveProblemDefinition : IProblemDefinition {
     25  public interface IMultiObjectiveProblemDefinition<TSolution> : IProblemDefinition
     26  where TSolution : class, ISolution {
    2627    bool[] Maximization { get; }
    27     double[] Evaluate(Individual individual, IRandom random);
    28     void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random);
     28    double[] Evaluate(TSolution individual, IRandom random);
     29    void Analyze(TSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random);
    2930  }
    3031}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblemDefinition.cs

    r12012 r13336  
    2222namespace HeuristicLab.Optimization {
    2323  public interface IProblemDefinition {
    24     IEncoding Encoding { get; }
     24    //IEncoding Encoding { get; }
    2525  }
    2626}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs

    r12012 r13336  
    2424
    2525namespace HeuristicLab.Optimization {
    26   public interface ISingleObjectiveProblemDefinition : IProblemDefinition {
     26  public interface ISingleObjectiveProblemDefinition<TSolution> : IProblemDefinition
     27  where TSolution : class, ISolution {
    2728    bool Maximization { get; }
    28     double Evaluate(Individual individual, IRandom random);
    29     void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random);
    30     IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random);
     29    double Evaluate(TSolution individual, IRandom random);
     30    void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random);
     31    IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random);
    3132  }
    3233}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveAnalysisOperator.cs

    r12012 r13336  
    2424
    2525namespace HeuristicLab.Optimization {
    26   internal interface IMultiObjectiveAnalysisOperator : IEncodingOperator, IAnalyzer, IMultiObjectiveOperator {
    27     Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
     26  internal interface IMultiObjectiveAnalysisOperator<TSolution> : IEncodingOperator<TSolution>, IAnalyzer, IMultiObjectiveOperator
     27  where TSolution : class, ISolution {
     28    Action<TSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
    2829  }
    2930}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveEvaluationOperator.cs

    r12012 r13336  
    2424
    2525namespace HeuristicLab.Optimization {
    26   internal interface IMultiObjectiveEvaluationOperator : IMultiObjectiveEvaluator, IEncodingOperator {
    27     Func<Individual, IRandom, double[]> EvaluateFunc { get; set; }
     26  internal interface IMultiObjectiveEvaluationOperator<TSolution> : IMultiObjectiveEvaluator, IEncodingOperator<TSolution>
     27  where TSolution : class, ISolution {
     28    Func<TSolution, IRandom, double[]> EvaluateFunc { get; set; }
    2829  }
    2930}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/INeighborBasedOperator.cs

    r12012 r13336  
    2525
    2626namespace HeuristicLab.Optimization {
    27   internal interface INeighborBasedOperator : IEncodingOperator {
    28     Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
     27  internal interface INeighborBasedOperator<TSolution> : IEncodingOperator<TSolution>
     28  where TSolution : class, ISolution {
     29    Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }
    2930  }
    3031}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs

    r12012 r13336  
    2424
    2525namespace HeuristicLab.Optimization {
    26   internal interface ISingleObjectiveAnalysisOperator : IEncodingOperator, ISingleObjectiveOperator {
    27     Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
     26  internal interface ISingleObjectiveAnalysisOperator<TSolution> : IEncodingOperator<TSolution>, ISingleObjectiveOperator
     27  where TSolution : class, ISolution {
     28    Action<TSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
    2829  }
    2930}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs

    r12012 r13336  
    2424
    2525namespace HeuristicLab.Optimization {
    26   internal interface ISingleObjectiveEvaluationOperator : ISingleObjectiveEvaluator, IEncodingOperator {
    27     Func<Individual, IRandom, double> EvaluateFunc { get; set; }
     26  internal interface ISingleObjectiveEvaluationOperator<TSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TSolution>
     27  where TSolution : class, ISolution {
     28    Func<TSolution, IRandom, double> EvaluateFunc { get; set; }
    2829  }
    2930}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs

    r13335 r13336  
    2929namespace HeuristicLab.Optimization {
    3030  [StorableClass]
    31   public abstract class MultiObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition
    32   where TEncoding : class, IEncoding {
     31  public abstract class MultiObjectiveProblem<TSolution> : Problem<TSolution, MultiObjectiveEvaluator<TSolution>>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition<TSolution>
     32  where TSolution : class, ISolution {
    3333    [StorableConstructor]
    34     protected MultiObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
     34    protected MultiObjectiveProblem(bool deserializing) : base(deserializing) { }
    3535
    36     protected MultiObjectiveBasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)
     36    protected MultiObjectiveProblem(MultiObjectiveProblem<TSolution> original, Cloner cloner)
    3737      : base(original, cloner) {
    3838      ParameterizeOperators();
    3939    }
    4040
    41     protected MultiObjectiveBasicProblem()
     41    protected MultiObjectiveProblem()
    4242      : base() {
    4343      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()));
    4444
    4545      Operators.Add(Evaluator);
    46       Operators.Add(new MultiObjectiveAnalyzer());
     46      Operators.Add(new MultiObjectiveAnalyzer<TSolution>());
    4747
    4848      ParameterizeOperators();
     
    5555
    5656    public abstract bool[] Maximization { get; }
    57     public abstract double[] Evaluate(Individual individual, IRandom random);
    58     public virtual void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
     57    public abstract double[] Evaluate(TSolution individual, IRandom random);
     58    public virtual void Analyze(TSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }
    5959
    60     protected override void OnOperatorsChanged() {
    61       base.OnOperatorsChanged();
    62       if (Encoding != null) {
    63         PruneSingleObjectiveOperators(Encoding);
    64         var multiEncoding = Encoding as MultiEncoding;
    65         if (multiEncoding != null) {
    66           foreach (var encoding in multiEncoding.Encodings.ToList()) {
    67             PruneSingleObjectiveOperators(encoding);
    68           }
    69         }
    70       }
    71     }
     60    //TODO
     61    //protected override void OnOperatorsChanged() {
     62    //  base.OnOperatorsChanged();
     63    //  if (Encoding != null) {
     64    //    PruneSingleObjectiveOperators(Encoding);
     65    //    var multiEncoding = Encoding as MultiEncoding;
     66    //    if (multiEncoding != null) {
     67    //      foreach (var encoding in multiEncoding.Encodings.ToList()) {
     68    //        PruneSingleObjectiveOperators(encoding);
     69    //      }
     70    //    }
     71    //  }
     72    //}
    7273
    73     private void PruneSingleObjectiveOperators(IEncoding encoding) {
    74       if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
    75         encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
    76     }
     74    //private void PruneSingleObjectiveOperators(IEncoding encoding) {
     75    //  if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))
     76    //    encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();
     77    //}
    7778
    7879    protected override void OnEvaluatorChanged() {
     
    8283
    8384    private void ParameterizeOperators() {
    84       foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator>())
     85      foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TSolution>>())
    8586        op.EvaluateFunc = Evaluate;
    86       foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator>())
     87      foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TSolution>>())
    8788        op.AnalyzeAction = Analyze;
    8889    }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveAnalyzer.cs

    r11961 r13336  
    3333  [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")]
    3434  [StorableClass]
    35   public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator, IStochasticOperator {
     35  public class MultiObjectiveAnalyzer<TSolution> : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator<TSolution>, IStochasticOperator
     36  where TSolution : class, ISolution {
    3637    public bool EnabledByDefault { get { return true; } }
    3738
    38     public ILookupParameter<IEncoding> EncodingParameter {
    39       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     39    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     40      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4041    }
    4142
     
    5253    }
    5354
    54     public Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
     55    public Action<TSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }
    5556
    5657    [StorableConstructor]
    5758    protected MultiObjectiveAnalyzer(bool deserializing) : base(deserializing) { }
    58     protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }
     59    protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer<TSolution> original, Cloner cloner) : base(original, cloner) { }
    5960    public MultiObjectiveAnalyzer() {
    6061      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    61       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     62      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    6263      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
    6364      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
     
    6566
    6667    public override IDeepCloneable Clone(Cloner cloner) {
    67       return new MultiObjectiveAnalyzer(this, cloner);
     68      return new MultiObjectiveAnalyzer<TSolution>(this, cloner);
    6869    }
    6970
     
    7778        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    7879
    79       var individuals = scopes.Select(encoding.GetIndividual).ToArray();
     80      var individuals = scopes.Select(s => ScopeUtil.GetSolution(s, encoding)).ToArray();
    8081      AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results, random);
    8182      return base.Apply();
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveEvaluator.cs

    r12617 r13336  
    3131  [Item("Multi-objective Evaluator", "Calls the Evaluate method of the problem definition and writes the return value into the scope.")]
    3232  [StorableClass]
    33   public class MultiObjectiveEvaluator : InstrumentedOperator, IMultiObjectiveEvaluationOperator, IStochasticOperator {
     33  public class MultiObjectiveEvaluator<TSolution> : InstrumentedOperator, IMultiObjectiveEvaluationOperator<TSolution>, IStochasticOperator
     34  where TSolution : class, ISolution {
    3435
    3536    public ILookupParameter<IRandom> RandomParameter {
     
    3738    }
    3839
    39     public ILookupParameter<IEncoding> EncodingParameter {
    40       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     40    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     41      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4142    }
    4243
     
    4546    }
    4647
    47     public Func<Individual, IRandom, double[]> EvaluateFunc { get; set; }
     48    public Func<TSolution, IRandom, double[]> EvaluateFunc { get; set; }
    4849
    4950    [StorableConstructor]
    5051    protected MultiObjectiveEvaluator(bool deserializing) : base(deserializing) { }
    51     protected MultiObjectiveEvaluator(MultiObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
     52    protected MultiObjectiveEvaluator(MultiObjectiveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { }
    5253    public MultiObjectiveEvaluator() {
    5354      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    54       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     55      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    5556      Parameters.Add(new LookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
    5657    }
    5758
    5859    public override IDeepCloneable Clone(Cloner cloner) {
    59       return new MultiObjectiveEvaluator(this, cloner);
     60      return new MultiObjectiveEvaluator<TSolution>(this, cloner);
    6061    }
    6162
     
    6364      var random = RandomParameter.ActualValue;
    6465      var encoding = EncodingParameter.ActualValue;
    65       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    66       QualitiesParameter.ActualValue = new DoubleArray(EvaluateFunc(individual, random));
     66      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     67      QualitiesParameter.ActualValue = new DoubleArray(EvaluateFunc(solution, random));
    6768      return base.InstrumentedApply();
    6869    }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs

    r11961 r13336  
    3333  [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
    3434  [StorableClass]
    35   public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer, IStochasticOperator {
     35  public sealed class SingleObjectiveAnalyzer<TSolution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TSolution>, IAnalyzer, IStochasticOperator
     36  where TSolution : class, ISolution {
    3637    public bool EnabledByDefault { get { return true; } }
    3738
    38     public ILookupParameter<IEncoding> EncodingParameter {
    39       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     39    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     40      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4041    }
    4142
     
    5253    }
    5354
    54     public Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
     55    public Action<TSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
    5556
    5657    [StorableConstructor]
    5758    private SingleObjectiveAnalyzer(bool deserializing) : base(deserializing) { }
    58     private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }
     59    private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<TSolution> original, Cloner cloner) : base(original, cloner) { }
    5960    public SingleObjectiveAnalyzer() {
    60       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     61      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    6162      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    6263      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
     
    6566
    6667    public override IDeepCloneable Clone(Cloner cloner) {
    67       return new SingleObjectiveAnalyzer(this, cloner);
     68      return new SingleObjectiveAnalyzer<TSolution>(this, cloner);
    6869    }
    6970
     
    7778        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    7879
    79       var individuals = scopes.Select(encoding.GetIndividual).ToArray();
     80      var individuals = scopes.Select(s => ScopeUtil.GetSolution(s, encoding)).ToArray();
    8081      AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random);
    8182      return base.Apply();
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs

    r12617 r13336  
    3131  [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")]
    3232  [StorableClass]
    33   public sealed class SingleObjectiveEvaluator : InstrumentedOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
     33  public sealed class SingleObjectiveEvaluator<TSolution> : InstrumentedOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator
     34  where TSolution : class, ISolution {
    3435
    3536    public ILookupParameter<IRandom> RandomParameter {
     
    3738    }
    3839
    39     public ILookupParameter<IEncoding> EncodingParameter {
    40       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     40    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     41      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4142    }
    4243
     
    4546    }
    4647
    47     public Func<Individual, IRandom, double> EvaluateFunc { get; set; }
     48    public Func<TSolution, IRandom, double> EvaluateFunc { get; set; }
    4849
    4950    [StorableConstructor]
    5051    private SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
    51     private SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
     52    private SingleObjectiveEvaluator(SingleObjectiveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { }
    5253    public SingleObjectiveEvaluator() {
    5354      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    54       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     55      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    5556      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    5657    }
    5758
    58     public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator(this, cloner); }
     59    public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator<TSolution>(this, cloner); }
    5960
    6061    public override IOperation InstrumentedApply() {
    6162      var random = RandomParameter.ActualValue;
    6263      var encoding = EncodingParameter.ActualValue;
    63       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    64       QualityParameter.ActualValue = new DoubleValue(EvaluateFunc(individual, random));
     64      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     65      QualityParameter.ActualValue = new DoubleValue(EvaluateFunc(solution, random));
    6566      return base.InstrumentedApply();
    6667    }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs

    r12012 r13336  
    3333  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
    3434  [StorableClass]
    35   public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
     35  public sealed class SingleObjectiveImprover<TSolution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator
     36    where TSolution : class, ISolution {
    3637    public ILookupParameter<IRandom> RandomParameter {
    3738      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    3839    }
    3940
    40     public ILookupParameter<IEncoding> EncodingParameter {
    41       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     41    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     42      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4243    }
    4344
     
    6263    }
    6364
    64     public Func<Individual, IRandom, double> EvaluateFunc { get; set; }
    65     public Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
     65    public Func<TSolution, IRandom, double> EvaluateFunc { get; set; }
     66    public Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }
    6667
    6768    [StorableConstructor]
    6869    private SingleObjectiveImprover(bool deserializing) : base(deserializing) { }
    69     private SingleObjectiveImprover(SingleObjectiveImprover original, Cloner cloner) : base(original, cloner) { }
     70    private SingleObjectiveImprover(SingleObjectiveImprover<TSolution> original, Cloner cloner) : base(original, cloner) { }
    7071    public SingleObjectiveImprover() {
    7172      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    72       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     73      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    7374      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    7475      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
     
    7980
    8081    public override IDeepCloneable Clone(Cloner cloner) {
    81       return new SingleObjectiveImprover(this, cloner);
     82      return new SingleObjectiveImprover<TSolution>(this, cloner);
    8283    }
    8384
     
    8889      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
    8990      var sampleSize = SampleSizeParameter.ActualValue.Value;
    90       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    91       var quality = QualityParameter.ActualValue == null ? EvaluateFunc(individual, random) : QualityParameter.ActualValue.Value;
     91      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     92      var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value;
    9293
    9394      var count = 0;
    9495      for (var i = 0; i < maxAttempts; i++) {
    95         Individual best = null;
     96        TSolution best = default(TSolution);
    9697        var bestQuality = quality;
    97         foreach (var neighbor in GetNeighborsFunc(individual, random).Take(sampleSize)) {
     98        foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) {
    9899          var q = EvaluateFunc(neighbor, random);
    99100          count++;
     
    103104        }
    104105        if (best == null) break;
    105         individual = best;
     106        solution = best;
    106107        quality = bestQuality;
    107108      }
     
    109110      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
    110111      QualityParameter.ActualValue = new DoubleValue(quality);
    111       individual.CopyToScope(ExecutionContext.Scope);
     112
     113      ScopeUtil.CopySolutionToScope(ExecutionContext.Scope, encoding, solution);
    112114      return base.Apply();
    113115    }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs

    r12012 r13336  
    3131  [Item("Single-objective MoveEvaluator", "Evaluates a parameter vector that results from a move.")]
    3232  [StorableClass]
    33   public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator {
     33  public class SingleObjectiveMoveEvaluator<TSolution> : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator<TSolution>, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator
     34  where TSolution : class, ISolution {
    3435
    3536    public ILookupParameter<IRandom> RandomParameter {
     
    3738    }
    3839
    39     public ILookupParameter<IEncoding> EncodingParameter {
    40       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     40    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     41      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4142    }
    4243
     
    4950    }
    5051
    51     public Func<Individual, IRandom, double> EvaluateFunc { get; set; }
     52    public Func<TSolution, IRandom, double> EvaluateFunc { get; set; }
    5253
    5354    [StorableConstructor]
    5455    protected SingleObjectiveMoveEvaluator(bool deserializing) : base(deserializing) { }
    55     protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
     56    protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { }
    5657    public SingleObjectiveMoveEvaluator() {
    5758      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    58       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     59      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    5960      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    6061      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
     
    6263
    6364    public override IDeepCloneable Clone(Cloner cloner) {
    64       return new SingleObjectiveMoveEvaluator(this, cloner);
     65      return new SingleObjectiveMoveEvaluator<TSolution>(this, cloner);
    6566    }
    6667
     
    6869      var random = RandomParameter.ActualValue;
    6970      var encoding = EncodingParameter.ActualValue;
    70       var individual = encoding.GetIndividual(ExecutionContext.Scope);
     71      var individual = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
    7172      MoveQualityParameter.ActualValue = new DoubleValue(EvaluateFunc(individual, random));
    7273      return base.Apply();
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs

    r12012 r13336  
    3434  [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
    3535  [StorableClass]
    36   public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator {
     36  public class SingleObjectiveMoveGenerator<TSolution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator
     37  where TSolution : class, ISolution {
    3738    public ILookupParameter<IRandom> RandomParameter {
    3839      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     
    4344    }
    4445
    45     public ILookupParameter<IEncoding> EncodingParameter {
    46       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     46    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     47      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4748    }
    4849
    49     public Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
     50    public Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }
    5051
    5152    [StorableConstructor]
    5253    protected SingleObjectiveMoveGenerator(bool deserializing) : base(deserializing) { }
    53     protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator original, Cloner cloner)
     54    protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<TSolution> original, Cloner cloner)
    5455      : base(original, cloner) { }
    5556    public SingleObjectiveMoveGenerator() {
    5657      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    5758      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample."));
    58       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     59      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    5960    }
    6061
    6162    public override IDeepCloneable Clone(Cloner cloner) {
    62       return new SingleObjectiveMoveGenerator(this, cloner);
     63      return new SingleObjectiveMoveGenerator<TSolution>(this, cloner);
    6364    }
    6465
     
    6768      var sampleSize = SampleSizeParameter.ActualValue.Value;
    6869      var encoding = EncodingParameter.ActualValue;
    69       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    70       var nbhood = GetNeighborsFunc(individual, random).Take(sampleSize).ToList();
     70      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     71      var nbhood = GetNeighborsFunc(solution, random).Take(sampleSize).ToList();
    7172
    7273      var moveScopes = new Scope[nbhood.Count];
    7374      for (int i = 0; i < moveScopes.Length; i++) {
    7475        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
    75         nbhood[i].CopyToScope(moveScopes[i]);
     76        ScopeUtil.CopySolutionToScope(moveScopes[i], encoding, nbhood[i]);
    7677      }
    7778      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveMaker.cs

    r12012 r13336  
    3131  [Item("Single-objective MoveMaker", "Applies a move.")]
    3232  [StorableClass]
    33   public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator {
    34     public ILookupParameter<IEncoding> EncodingParameter {
    35       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     33  public class SingleObjectiveMoveMaker<TSolution> : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator
     34  where TSolution : class, ISolution {
     35    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     36      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    3637    }
    3738
     
    4647    [StorableConstructor]
    4748    protected SingleObjectiveMoveMaker(bool deserializing) : base(deserializing) { }
    48     protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker original, Cloner cloner) : base(original, cloner) { }
     49    protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker<TSolution> original, Cloner cloner) : base(original, cloner) { }
    4950    public SingleObjectiveMoveMaker() {
    50       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     51      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    5152      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    5253      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
     
    5455
    5556    public override IDeepCloneable Clone(Cloner cloner) {
    56       return new SingleObjectiveMoveMaker(this, cloner);
     57      return new SingleObjectiveMoveMaker<TSolution>(this, cloner);
    5758    }
    5859
     
    6162
    6263      var encoding = EncodingParameter.ActualValue;
    63       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    64       individual.CopyToScope(ExecutionContext.Scope.Parent.Parent);
     64      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     65      ScopeUtil.CopySolutionToScope(ExecutionContext.Scope.Parent.Parent, encoding, solution);
    6566
    6667      if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs

    r13335 r13336  
    3030namespace HeuristicLab.Optimization {
    3131  [StorableClass]
    32   public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
    33     where TEncoding : class, IEncoding
     32  public abstract class Problem<TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition, IStorableContent
     33    where TSolution : class, ISolution
    3434    where TEvaluator : class, IEvaluator {
    3535
    3636    public string Filename { get; set; }
    3737
    38     protected IValueParameter<TEncoding> EncodingParameter {
    39       get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
     38    protected IValueParameter<IEncoding<TSolution>> EncodingParameter {
     39      get { return (IValueParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4040    }
    4141
    42     //mkommend necessary for reuse of operators if the encoding changes
    43     private TEncoding oldEncoding;
    44 
    45     IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
    46     public TEncoding Encoding {
     42    public IEncoding<TSolution> Encoding {
    4743      get { return EncodingParameter.Value; }
    4844      protected set {
     
    6359    }
    6460
    65     protected BasicProblem()
     61    protected Problem()
    6662      : base() {
    67       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."));
    68       oldEncoding = Encoding;
    69       if(Encoding != null) Parameterize();
     63      Parameters.Add(new ValueParameter<IEncoding<TSolution>>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
     64      if (Encoding != null) Parameterize();
    7065      RegisterEvents();
    7166    }
    7267
    73     protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
     68    protected Problem(Problem<TSolution, TEvaluator> original, Cloner cloner)
    7469      : base(original, cloner) {
    75       oldEncoding = cloner.Clone(original.oldEncoding);
    7670      RegisterEvents();
    7771    }
    7872
    7973    [StorableConstructor]
    80     protected BasicProblem(bool deserializing) : base(deserializing) { }
     74    protected Problem(bool deserializing) : base(deserializing) { }
    8175    [StorableHook(HookType.AfterDeserialization)]
    8276    private void AfterDeserialization() {
    83       oldEncoding = Encoding;
    8477      RegisterEvents();
    8578    }
     
    8780    private void RegisterEvents() {
    8881      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
    89       var multiEncoding = Encoding as MultiEncoding;
    90       if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
     82      //var multiEncoding = Encoding as MultiEncoding;
     83      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
    9184    }
    9285
     
    9992
    10093    private void Parameterize() {
    101       if (oldEncoding != null) {
    102         AdaptEncodingOperators(oldEncoding, Encoding);
    103         var oldMultiEncoding = oldEncoding as MultiEncoding;
    104         if (oldMultiEncoding != null)
    105           oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
    106       }
    107       oldEncoding = Encoding;
    108 
    109       foreach (var op in Operators.OfType<IEncodingOperator>())
     94      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
    11095        op.EncodingParameter.ActualName = EncodingParameter.Name;
    11196
    112       var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
    113       var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
    114       var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
    115         Encoding.SolutionCreator);
    116       Parameters.Remove(SolutionCreatorParameter);
    117       Parameters.Add(solutionCreatorParam);
    118       ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
    119 
    120       var multiEncoding = Encoding as MultiEncoding;
    121       if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
     97      //var multiEncoding = Encoding as MultiEncoding;
     98      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
    12299    }
    123100
     
    127104    }
    128105
    129     private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
    130       if (oldEncoding.GetType() != newEncoding.GetType()) return;
    131 
    132       if (oldEncoding.GetType() == typeof(MultiEncoding)) {
    133         var oldMultiEncoding = (MultiEncoding)oldEncoding;
    134         var newMultiEncoding = (MultiEncoding)newEncoding;
    135         if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
    136 
    137         var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
    138         foreach (var multi in nestedEncodings)
    139           AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
    140       }
    141 
    142       var comparer = new TypeEqualityComparer<IOperator>();
    143       var cloner = new Cloner();
    144       var oldOperators = oldEncoding.Operators;
    145       var newOperators = newEncoding.Operators;
    146 
    147       cloner.RegisterClonedObject(oldEncoding, newEncoding);
    148       var operators = oldOperators.Intersect(newOperators, comparer)
    149                                   .Select(cloner.Clone)
    150                                   .Union(newOperators, comparer).ToList();
    151 
    152       newEncoding.ConfigureOperators(operators);
    153       newEncoding.Operators = operators;
    154     }
    155 
    156106    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
    157107      OnOperatorsChanged();
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r13335 r13336  
    3030namespace HeuristicLab.Optimization {
    3131  [StorableClass]
    32   public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
    33     ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
    34   where TEncoding : class, IEncoding {
     32  public abstract class SingleObjectiveProblem<TSolution> : Problem<TSolution, SingleObjectiveEvaluator<TSolution>>,
     33    ISingleObjectiveProblemDefinition<TSolution>, ISingleObjectiveHeuristicOptimizationProblem
     34  where TSolution : class, ISolution {
    3535
    3636    protected IValueParameter<DoubleValue> BestKnownQualityParameter {
     
    5050
    5151    [StorableConstructor]
    52     protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
     52    protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
    5353
    54     protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
     54    protected SingleObjectiveProblem(SingleObjectiveProblem<TSolution> original, Cloner cloner)
    5555      : base(original, cloner) {
    5656      ParameterizeOperators();
    5757    }
    5858
    59     protected SingleObjectiveBasicProblem()
     59    protected SingleObjectiveProblem()
    6060      : base() {
    6161      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
     
    6363
    6464      Operators.Add(Evaluator);
    65       Operators.Add(new SingleObjectiveAnalyzer());
    66       Operators.Add(new SingleObjectiveImprover());
    67       Operators.Add(new SingleObjectiveMoveEvaluator());
    68       Operators.Add(new SingleObjectiveMoveGenerator());
    69       Operators.Add(new SingleObjectiveMoveMaker());
     65      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
     66      Operators.Add(new SingleObjectiveImprover<TSolution>());
     67      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
     68      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
     69      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
    7070
    7171      ParameterizeOperators();
     
    7878
    7979    public abstract bool Maximization { get; }
    80     public abstract double Evaluate(Individual individual, IRandom random);
    81     public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
    82     public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
    83       return Enumerable.Empty<Individual>();
     80    public abstract double Evaluate(TSolution individual, IRandom random);
     81    public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
     82    public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
     83      return Enumerable.Empty<TSolution>();
    8484    }
    8585
    86     protected override void OnOperatorsChanged() {
    87       base.OnOperatorsChanged();
    88       if (Encoding != null) {
    89         PruneMultiObjectiveOperators(Encoding);
    90         var multiEncoding = Encoding as MultiEncoding;
    91         if (multiEncoding != null) {
    92           foreach (var encoding in multiEncoding.Encodings.ToList()) {
    93             PruneMultiObjectiveOperators(encoding);
    94           }
    95         }
    96       }
     86    public virtual bool IsBetter(double quality, double bestQuality) {
     87      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
    9788    }
    9889
    99     private void PruneMultiObjectiveOperators(IEncoding encoding) {
    100       if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
    101         encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
    102     }
     90    //TODO
     91    //protected override void OnOperatorsChanged() {
     92    //  base.OnOperatorsChanged();
     93    //  if (Encoding != null) {
     94    //    PruneMultiObjectiveOperators(Encoding);
     95    //    var multiEncoding = Encoding as MultiEncoding;
     96    //    if (multiEncoding != null) {
     97    //      foreach (var encoding in multiEncoding.Encodings.ToList()) {
     98    //        PruneMultiObjectiveOperators(encoding);
     99    //      }
     100    //    }
     101    //  }
     102    //}
     103
     104    //private void PruneMultiObjectiveOperators(IEncoding<TSolution> encoding) {
     105    //  if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
     106    //    encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
     107    //}
    103108
    104109    protected override void OnEvaluatorChanged() {
     
    108113
    109114    private void ParameterizeOperators() {
    110       foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
     115      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
    111116        op.EvaluateFunc = Evaluate;
    112       foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
     117      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
    113118        op.AnalyzeAction = Analyze;
    114       foreach (var op in Operators.OfType<INeighborBasedOperator>())
     119      foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
    115120        op.GetNeighborsFunc = GetNeighbors;
    116121    }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r12975 r13336  
    121121  <ItemGroup>
    122122    <Compile Include="Algorithms\BasicAlgorithm.cs" />
    123     <Compile Include="BasicProblems\BasicProblem.cs" />
     123    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" />
     124    <Compile Include="BasicProblems\Problem.cs" />
    124125    <Compile Include="BasicProblems\Encoding.cs" />
    125     <Compile Include="BasicProblems\Individuals\Individual.cs" />
    126     <Compile Include="BasicProblems\Individuals\MultiEncodingIndividual.cs" />
    127     <Compile Include="BasicProblems\Individuals\SingleEncodingIndividual.cs" />
    128126    <Compile Include="BasicProblems\Interfaces\IEncoding.cs" />
    129127    <Compile Include="BasicProblems\Interfaces\IEncodingOperator.cs" />
     
    138136    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveMoveOperator.cs" />
    139137    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblemDefinition.cs" />
    140     <Compile Include="BasicProblems\MultiEncoding.cs" />
    141     <Compile Include="BasicProblems\MultiObjectiveBasicProblem.cs" />
    142     <Compile Include="BasicProblems\Operators\MultiEncodingCreator.cs" />
    143     <Compile Include="BasicProblems\Operators\MultiEncodingCrossover.cs" />
    144     <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" />
    145     <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" />
     138    <Compile Include="BasicProblems\Interfaces\ISolution.cs" />
     139    <Compile Include="BasicProblems\MultiObjectiveProblem.cs" />
    146140    <Compile Include="BasicProblems\Operators\MultiObjectiveAnalyzer.cs" />
    147141    <Compile Include="BasicProblems\Operators\MultiObjectiveEvaluator.cs" />
     142    <Compile Include="BasicProblems\Operators\ScopeUtil.cs" />
    148143    <Compile Include="BasicProblems\Operators\SingleObjectiveAnalyzer.cs" />
    149144    <Compile Include="BasicProblems\Operators\SingleObjectiveEvaluator.cs" />
     
    152147    <Compile Include="BasicProblems\Operators\SingleObjectiveMoveGenerator.cs" />
    153148    <Compile Include="BasicProblems\Operators\SingleObjectiveMoveMaker.cs" />
    154     <Compile Include="BasicProblems\SingleObjectiveBasicProblem.cs" />
     149    <Compile Include="BasicProblems\SingleObjectiveProblem.cs" />
    155150    <Compile Include="Interfaces\ILocalImprovementAlgorithmOperator.cs" />
    156151    <Compile Include="Interfaces\IMultiObjectiveOperator.cs" />
     
    328323    </BootstrapperPackage>
    329324  </ItemGroup>
    330   <ItemGroup />
     325  <ItemGroup>
     326    <Folder Include="BasicProblems\Individuals\" />
     327  </ItemGroup>
    331328  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    332329  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs

    r12012 r13336  
    3131    IEnumerable<IItem> Operators { get; }
    3232
    33 
    3433    IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
    3534    event EventHandler OperatorsChanged;
    3635    event EventHandler Reset;
    3736  }
     37
     38  public interface IProblem<TSolution> : IHeuristicOptimizationProblem where TSolution : class, ISolution {
     39    IEncoding<TSolution> Encoding { get; }
     40  }
    3841}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/ISolutionCreator.cs

    r12012 r13336  
    2727  /// </summary>
    2828  public interface ISolutionCreator : IOperator { }
     29
     30  public interface ISolutionCreator<TSolution> : ISolutionCreator where TSolution : class, ISolution { }
    2931}
  • branches/ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs

    r12000 r13336  
    3434namespace HeuristicLab.Problems.Binary {
    3535  [StorableClass]
    36   public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
     36  public abstract class BinaryProblem : SingleObjectiveProblem<BinaryVector> {
    3737    public virtual int Length {
    3838      get { return Encoding.Length; }
    3939      set { Encoding.Length = value; }
     40    }
     41
     42    public new BinaryVectorEncoding Encoding {
     43      get { return (BinaryVectorEncoding)base.Encoding; }
     44      set { base.Encoding = value; }
    4045    }
    4146
     
    6065      var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
    6166      Parameters.Add(lengthParameter);
     67      Encoding = new BinaryVectorEncoding();
    6268      Encoding.LengthParameter = lengthParameter;
    6369      RegisterEventHandlers();
    6470    }
    6571
    66     public virtual bool IsBetter(double quality, double bestQuality) {
    67       return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
    68     }
    69 
    70     public abstract double Evaluate(BinaryVector vector, IRandom random);
    71     public sealed override double Evaluate(Individual individual, IRandom random) {
    72       return Evaluate(individual.BinaryVector(), random);
    73     }
    74 
    75     public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     72    public override void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    7673      base.Analyze(individuals, qualities, results, random);
    7774      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
     
    8178        results.Add(new Result("Best Solution", typeof(BinaryVector)));
    8279      }
    83       results["Best Solution"].Value = (IItem)best.BinaryVector().Clone();
     80      results["Best Solution"].Value = (IItem)best.Clone();
    8481    }
    8582
  • branches/ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs

    r13257 r13336  
    4040  // BackwardsCompatibility3.3
    4141  // Rename class to SingleObjectiveExternalEvaluationProblem
    42   public class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding>, IExternalEvaluationProblem {
     42  public class ExternalEvaluationProblem : SingleObjectiveProblem<IEncoding>, IExternalEvaluationProblem {
    4343
    4444    public static new Image StaticItemImage {
  • branches/ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs

    r13257 r13336  
    3737  [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 200)]
    3838  [StorableClass]
    39   public class MultiObjectiveExternalEvaluationProblem : MultiObjectiveBasicProblem<IEncoding>, IExternalEvaluationProblem {
     39  public class MultiObjectiveExternalEvaluationProblem : MultiObjectiveProblem<IEncoding>, IExternalEvaluationProblem {
    4040
    4141    public static new Image StaticItemImage {
  • branches/ProblemRefactoring/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs

    r13243 r13336  
    3838  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
    3939  [StorableClass]
    40   public sealed class GEArtificialAntProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IStorableContent {
     40  public sealed class GEArtificialAntProblem : SingleObjectiveProblem<IntegerVectorEncoding>, IStorableContent {
    4141    public string Filename { get; set; }
    4242
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r12616 r13336  
    3333  [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)]
    3434  [StorableClass]
    35   public sealed class MultiObjectiveProgrammableProblem : MultiObjectiveBasicProblem<IEncoding>, IProgrammableItem {
     35  public sealed class MultiObjectiveProgrammableProblem : MultiObjectiveProblem<IEncoding>, IProgrammableItem {
    3636    public static new Image StaticItemImage {
    3737      get { return VSImageLibrary.Script; }
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r12616 r13336  
    3535  [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
    3636  [StorableClass]
    37   public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveBasicProblem<IEncoding>, IProgrammableItem {
     37  public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveProblem<IEncoding>, IProgrammableItem {
    3838    public static new Image StaticItemImage {
    3939      get { return VSImageLibrary.Script; }
Note: See TracChangeset for help on using the changeset viewer.