Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16811 was 16806, checked in by mkommend, 5 years ago

#2521: Added StorableType attributes to interfaces and adapted basic problems.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
54    protected CompiledSingleObjectiveProblemDefinition() : base() { }
55
56    protected CompiledSingleObjectiveProblemDefinition(TEncoding encoding)
57      : base(encoding) { }
58
59    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
60    public abstract bool Maximization { get; }
61    public abstract double Evaluate(TEncodedSolution individual, IRandom random);
62    public abstract void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random);
63    public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
64
65    public bool IsBetter(double quality, double bestQuality) {
66      return Maximization ? quality > bestQuality : quality < bestQuality;
67    }
68    #endregion
69  }
70
71  public abstract class CompiledMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> : CompiledProblemDefinition<TEncoding, TEncodedSolution>, IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution>
72    where TEncoding : class, IEncoding<TEncodedSolution>
73    where TEncodedSolution : class, IEncodedSolution {
74
75    protected CompiledMultiObjectiveProblemDefinition() : base() { }
76
77    protected CompiledMultiObjectiveProblemDefinition(TEncoding encoding)
78      : base(encoding) { }
79
80    #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
81
82    public int Objectives => Maximization.Length;
83    public abstract bool[] Maximization { get; }
84    public abstract double[] Evaluate(TEncodedSolution individual, IRandom random);
85    public abstract void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random);
86    public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random);
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.