Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Problems.Scheduling/3.3/Evaluators/SchedulingEvaluator.cs @ 17578

Last change on this file since 17578 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ScheduleEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Scheduling {
33  [Item("SchedulingEvaluator", "First applies the decoder operator to obtain a schedule from an encoding and then applies the evaluator to obtain a quality.")]
34  [StorableClass]
35  public class SchedulingEvaluator : InstrumentedOperator, ISchedulingEvaluator, IStochasticOperator {
36
37    public IValueLookupParameter<IScheduleDecoder> ScheduleDecoderParameter {
38      get { return (IValueLookupParameter<IScheduleDecoder>)Parameters["ScheduleDecoder"]; }
39    }
40    ILookupParameter<IScheduleDecoder> ISchedulingEvaluator.ScheduleDecoderParameter {
41      get { return ScheduleDecoderParameter; }
42    }
43    public IValueLookupParameter<IScheduleEvaluator> ScheduleEvaluatorParameter {
44      get { return (IValueLookupParameter<IScheduleEvaluator>)Parameters["ScheduleEvaluator"]; }
45    }
46    ILookupParameter<IScheduleEvaluator> ISchedulingEvaluator.ScheduleEvaluatorParameter {
47      get { return ScheduleEvaluatorParameter; }
48    }
49    public ILookupParameter<DoubleValue> QualityParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    // ABE: This parameter exists purely, because some IScheduleDecoders are stochastic...
53    // ... which could be solved by letting the algorithm parameterize them ...
54    // ... but they have to use the same RNG as the evaluator (due to parallel execution)...
55    // ... in particular relevant for Island-GA and ALPS (Local- vs GlobalRandom).
56    public ILookupParameter<IRandom> RandomParameter {
57      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
58    }
59
60    [StorableConstructor]
61    protected SchedulingEvaluator(bool deserializing) : base(deserializing) { }
62    protected SchedulingEvaluator(SchedulingEvaluator original, Cloner cloner) : base(original, cloner) { }
63    public SchedulingEvaluator()
64      : base() {
65      Parameters.Add(new ValueLookupParameter<IScheduleDecoder>("ScheduleDecoder", "The decoding operator that is used to calculate a schedule from the used representation."));
66      Parameters.Add(new ValueLookupParameter<IScheduleEvaluator>("ScheduleEvaluator", "The actual schedule evaluation operator."));
67      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value aka fitness value of the solution."));
68      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
69      QualityParameter.Hidden = true;
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new SchedulingEvaluator(this, cloner);
74    }
75   
76    [StorableHook(HookType.AfterDeserialization)]
77    private void AfterDeserialization() {
78      // BackwardsCompatibility3.3
79      #region Backwards compatible code, remove with 3.4
80      if (!Parameters.ContainsKey("Random")) {
81        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
82      }
83      #endregion
84    }
85
86    public override IOperation InstrumentedApply() {
87      var decoder = ScheduleDecoderParameter.ActualValue;
88      var evaluator = ScheduleEvaluatorParameter.ActualValue;
89      if (evaluator == null) throw new InvalidOperationException("A ScheduleEvaluator could not be found.");
90
91      var operations = new OperationCollection(base.InstrumentedApply());
92      operations.Insert(0, ExecutionContext.CreateChildOperation(evaluator));
93      if (decoder != null) // decode before evaluating
94        operations.Insert(0, ExecutionContext.CreateChildOperation(decoder));
95      return operations;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.