1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.BinaryVectorEncoding.SolutionModel {
|
---|
33 | public enum ModelBiasOptions {
|
---|
34 | Unbiased,
|
---|
35 | RankBiased,
|
---|
36 | FitnessBiased
|
---|
37 | };
|
---|
38 |
|
---|
39 | [Item("Univariate Model Training Operator", "Adds a univariate solution sampling model to the scope.")]
|
---|
40 | [StorableClass]
|
---|
41 | public class UnivariateModelTrainingOperator : InstrumentedOperator, IStochasticOperator, ISingleObjectiveOperator {
|
---|
42 |
|
---|
43 | public ILookupParameter<IRandom> RandomParameter {
|
---|
44 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
48 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
|
---|
52 | get { return (IScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
56 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ILookupParameter<UnivariateModel> ModelParameter {
|
---|
60 | get { return (ILookupParameter<UnivariateModel>)Parameters["Model"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IValueParameter<EnumValue<ModelBiasOptions>> ModelBiasParameter {
|
---|
64 | get { return (IValueParameter<EnumValue<ModelBiasOptions>>)Parameters["ModelBias"]; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public ModelBiasOptions ModelBias {
|
---|
68 | get { return ModelBiasParameter.Value.Value; }
|
---|
69 | set { ModelBiasParameter.Value.Value = value; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | [StorableConstructor]
|
---|
73 | protected UnivariateModelTrainingOperator(bool deserializing) : base() { }
|
---|
74 | protected UnivariateModelTrainingOperator(UnivariateModelTrainingOperator original, Cloner cloner)
|
---|
75 | : base(original, cloner) { }
|
---|
76 | public UnivariateModelTrainingOperator() {
|
---|
77 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use for sampling from the model."));
|
---|
78 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether or not solution qualities are to be maximized."));
|
---|
79 | Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The population of solutions to create the model."));
|
---|
80 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The fitness values of the solutions."));
|
---|
81 | Parameters.Add(new LookupParameter<UnivariateModel>("Model", "The model that is being trained by this operator."));
|
---|
82 | Parameters.Add(new ValueParameter<EnumValue<ModelBiasOptions>>("ModelBias", "The type of bias that is used to created the model.", new EnumValue<ModelBiasOptions>(ModelBiasOptions.Unbiased)));
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
86 | return new UnivariateModelTrainingOperator(this, cloner);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IOperation InstrumentedApply() {
|
---|
90 | var random = RandomParameter.ActualValue;
|
---|
91 | var population = BinaryVectorParameter.ActualValue;
|
---|
92 | var biasType = ModelBiasParameter.Value.Value;
|
---|
93 | UnivariateModel model = null;
|
---|
94 | switch (biasType) {
|
---|
95 | case ModelBiasOptions.Unbiased:
|
---|
96 | model = UnivariateModelTrainer.TrainUnbiased(random, population);
|
---|
97 | break;
|
---|
98 | case ModelBiasOptions.RankBiased: {
|
---|
99 | var qualities = QualityParameter.ActualValue;
|
---|
100 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
101 | model = UnivariateModelTrainer.TrainWithRankBias(random, maximization, population,
|
---|
102 | qualities.Select(x => x.Value));
|
---|
103 | }
|
---|
104 | break;
|
---|
105 | case ModelBiasOptions.FitnessBiased: {
|
---|
106 | var qualities = QualityParameter.ActualValue;
|
---|
107 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
108 | model = UnivariateModelTrainer.TrainWithFitnessBias(random, maximization, population,
|
---|
109 | qualities.Select(x => x.Value));
|
---|
110 | }
|
---|
111 | break;
|
---|
112 | default: throw new InvalidOperationException(string.Format("Unknown bias type {0}", biasType));
|
---|
113 | }
|
---|
114 | ModelParameter.ActualValue = model;
|
---|
115 | return base.InstrumentedApply();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|