Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs @ 17309

Last change on this file since 17309 was 17309, checked in by abeham, 4 years ago

#2521: Refactored maximization property for multi-objective problems

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26
27namespace HeuristicLab.Problems.Programmable {
28  public abstract class CompiledProblemDefinition<TEncoding, TEncodedSolution> : IProblemDefinition<TEncoding, TEncodedSolution>
29    where TEncoding : class, IEncoding<TEncodedSolution>
30    where TEncodedSolution : class, IEncodedSolution {
31    private TEncoding encoding;
32    public TEncoding Encoding {
33      get { return encoding; }
34      internal set {
35        if (value == null) throw new ArgumentNullException("The encoding must not be null.");
36        encoding = value;
37      }
38    }
39
40    public dynamic vars { get; set; }
41    public abstract void Initialize();
42
43    protected CompiledProblemDefinition() { }
44    protected CompiledProblemDefinition(TEncoding encoding)
45      : base() {
46      Encoding = encoding;
47    }
48  }
49
50  public abstract class CompiledSingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> : CompiledProblemDefinition<TEncoding, TEncodedSolution>, ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>
51    where TEncoding : class, IEncoding<TEncodedSolution>
52    where TEncodedSolution : class, IEncodedSolution {
53    protected CompiledSingleObjectiveProblemDefinition() : base() { }
54
55    protected CompiledSingleObjectiveProblemDefinition(TEncoding encoding)
56      : base(encoding) { }
57
58    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
59    public abstract bool Maximization { get; }
60    public abstract double Evaluate(TEncodedSolution individual, IRandom random);
61    public abstract void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random);
62    public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
63
64    public bool IsBetter(double quality, double bestQuality) {
65      return Maximization ? quality > bestQuality : quality < bestQuality;
66    }
67    #endregion
68  }
69
70  public abstract class CompiledMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> : CompiledProblemDefinition<TEncoding, TEncodedSolution>, IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
71    where TEncoding : class, IEncoding<TEncodedSolution>
72    where TEncodedSolution : class, IEncodedSolution {
73    protected CompiledMultiObjectiveProblemDefinition() : base() { }
74
75    protected CompiledMultiObjectiveProblemDefinition(TEncoding encoding)
76      : base(encoding) { }
77
78    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
79    public int Objectives => Maximization.Count;
80    public abstract IReadOnlyList<bool> Maximization { get; }
81    public abstract IReadOnlyList<double[]> BestKnownFront { get; }
82    public abstract double[] ReferencePoint { get; }
83    public abstract double[] Evaluate(TEncodedSolution individual, IRandom random);
84    public abstract void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random);
85    public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.