1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Operators {
|
---|
29 | /// <summary>
|
---|
30 | /// Calculates the quality of a solution candidate.
|
---|
31 | /// </summary>
|
---|
32 | public abstract class SingleObjectiveEvaluatorBase : OperatorBase {
|
---|
33 | /// <summary>
|
---|
34 | /// Initializes a new instance of <see cref="SingleObjectiveEvaluatorBase"/> with one variable info
|
---|
35 | /// (<c>Quality</c>).
|
---|
36 | /// </summary>
|
---|
37 | public SingleObjectiveEvaluatorBase()
|
---|
38 | : base() {
|
---|
39 | AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.New));
|
---|
40 | }
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Evaluates the quality of a solution candidate.
|
---|
44 | /// </summary>
|
---|
45 | /// <remarks>Creates a new variable with the calculated quality if it not already exists, and
|
---|
46 | /// injects it in the given <paramref name="scope"/> if it is not a local one.</remarks>
|
---|
47 | /// <param name="scope">The scope where to evaluate the quality.</param>
|
---|
48 | /// <returns><c>null</c>.</returns>
|
---|
49 | public override IOperation Apply(IScope scope) {
|
---|
50 | double qualityValue = Evaluate(scope);
|
---|
51 | DoubleData quality = GetVariableValue<DoubleData>("Quality", scope, false, false);
|
---|
52 | if (quality == null) {
|
---|
53 | quality = new DoubleData(qualityValue);
|
---|
54 | IVariableInfo qualityInfo = GetVariableInfo("Quality");
|
---|
55 | if (qualityInfo.Local)
|
---|
56 | AddVariable(new Variable(qualityInfo.ActualName, quality));
|
---|
57 | else
|
---|
58 | scope.AddVariable(new Variable(scope.TranslateName(qualityInfo.FormalName), quality));
|
---|
59 | } else {
|
---|
60 | quality.Data = qualityValue;
|
---|
61 | }
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Calculates the quality of a solution candidate in the given <paramref name="scope"/>.
|
---|
67 | /// </summary>
|
---|
68 | /// <param name="scope">The scope where to calculate the quality.</param>
|
---|
69 | /// <returns>A double value that indicates the quality.</returns>
|
---|
70 | protected abstract double Evaluate(IScope scope);
|
---|
71 | }
|
---|
72 | }
|
---|