Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file was 17745, checked in by mkommend, 4 years ago

#2971: Added first draft of results implementation and problem adaptation.

File size: 5.3 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 System.Linq;
25using System.Threading;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28
29namespace HeuristicLab.Problems.Programmable {
30  public abstract class CompiledProblemDefinition<TEncoding, TEncodedSolution> : IProblemDefinition<TEncoding, TEncodedSolution>
31    where TEncoding : class, IEncoding
32    where TEncodedSolution : class, IEncodedSolution {
33    private TEncoding encoding;
34    public TEncoding Encoding {
35      get { return encoding; }
36      internal set {
37        if (value == null) throw new ArgumentNullException("The encoding must not be null.");
38        encoding = value;
39      }
40    }
41
42    public dynamic vars { get; set; }
43    public abstract void Initialize();
44
45    protected CompiledProblemDefinition() { }
46    protected CompiledProblemDefinition(TEncoding encoding)
47      : base() {
48      Encoding = encoding;
49    }
50  }
51
52  public abstract class CompiledSingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> : CompiledProblemDefinition<TEncoding, TEncodedSolution>, ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>
53    where TEncoding : class, IEncoding
54    where TEncodedSolution : class, IEncodedSolution {
55    protected CompiledSingleObjectiveProblemDefinition() : base() { }
56
57    protected CompiledSingleObjectiveProblemDefinition(TEncoding encoding)
58      : base(encoding) { }
59
60    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
61    public abstract bool Maximization { get; }
62
63    public virtual ISingleObjectiveEvaluationResult Evaluate(TEncodedSolution solution, IRandom random) {
64      return Evaluate(solution, random, CancellationToken.None);
65    }
66    public abstract ISingleObjectiveEvaluationResult Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
67
68    public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
69      Evaluate(solutionContext, random, CancellationToken.None);
70    }
71    public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
72      var evaluationResult = Evaluate(solutionContext.EncodedSolution, random, cancellationToken);
73      solutionContext.EvaluationResult = evaluationResult;
74    }
75
76
77    public virtual void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, IRandom random) { }
78
79    public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) {
80      return Enumerable.Empty<TEncodedSolution>();
81    }
82    public virtual IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
83      return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<TEncodedSolution>(n));
84    }
85
86    public bool IsBetter(double quality, double bestQuality) {
87      return Maximization ? quality > bestQuality : quality < bestQuality;
88    }
89    #endregion
90  }
91
92  public abstract class CompiledMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> : CompiledProblemDefinition<TEncoding, TEncodedSolution>, IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
93    where TEncoding : class, IEncoding
94    where TEncodedSolution : class, IEncodedSolution {
95    protected CompiledMultiObjectiveProblemDefinition() : base() { }
96
97    protected CompiledMultiObjectiveProblemDefinition(TEncoding encoding)
98      : base(encoding) { }
99
100    #region IMultiObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
101    public int Objectives => Maximization.Length;
102    public abstract bool[] Maximization { get; }
103    public abstract IReadOnlyList<double[]> BestKnownFront { get; }
104    public abstract double[] ReferencePoint { get; }
105
106    public virtual double[] Evaluate(TEncodedSolution solution, IRandom random) {
107      return Evaluate(solution, random, CancellationToken.None);
108    }
109    public abstract double[] Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
110    public abstract void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random);
111    public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.