Free cookie consent management tool by TermsFeed Policy Generator

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

#2521: Refactored encodings and problems.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.