1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
25 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
|
---|
37 | [Item("Symbolic Data Analysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
|
---|
38 | [Creatable("Data Analysis")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
|
---|
41 | private const string FixedSamplesParameterName = "NumberOfFixedSamples";
|
---|
42 | private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
|
---|
43 | private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
|
---|
44 | private const string RandomSamplesParameterName = "NumberOfRandomSamples";
|
---|
45 | private const string EvaluatorParameterName = "IslandEvaluator";
|
---|
46 | private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
|
---|
47 |
|
---|
48 | #region Problem Properties
|
---|
49 | public override Type ProblemType {
|
---|
50 | get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
|
---|
51 | }
|
---|
52 | public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
|
---|
53 | get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
|
---|
54 | set { base.Problem = value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region parameters
|
---|
59 | public IFixedValueParameter<IntValue> FixedSamplesParameter {
|
---|
60 | get { return (IFixedValueParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
|
---|
61 | }
|
---|
62 | public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
|
---|
63 | get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
|
---|
64 | }
|
---|
65 | public IFixedValueParameter<IntValue> RandomSamplesParameter {
|
---|
66 | get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
|
---|
67 | }
|
---|
68 | public IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator> EvaluatorParameter {
|
---|
69 | get { return (IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
70 | }
|
---|
71 | private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
|
---|
72 | get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region properties
|
---|
77 | public int FixedSamples {
|
---|
78 | get { return FixedSamplesParameter.Value.Value; }
|
---|
79 | set { FixedSamplesParameter.Value.Value = value; }
|
---|
80 | }
|
---|
81 | public ItemArray<IntRange> FixedSamplesPartitions {
|
---|
82 | get { return FixedSamplesPartitionsParameter.Value; }
|
---|
83 | set { FixedSamplesPartitionsParameter.Value = value; }
|
---|
84 | }
|
---|
85 | public int RandomSamples {
|
---|
86 | get { return RandomSamplesParameter.Value.Value; }
|
---|
87 | set { RandomSamplesParameter.Value.Value = value; }
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | [StorableConstructor]
|
---|
92 | private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void AfterDeserialization() {
|
---|
95 | RegisterParameterEvents();
|
---|
96 | }
|
---|
97 | private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
|
---|
98 | : base(original, cloner) {
|
---|
99 | RegisterParameterEvents();
|
---|
100 | }
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public SymbolicDataAnalysisIslandGeneticAlgorithm()
|
---|
106 | : base() {
|
---|
107 | Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
|
---|
108 | Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
|
---|
109 | Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue(0)));
|
---|
110 | Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
|
---|
111 | Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
|
---|
112 |
|
---|
113 | ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
|
---|
114 | fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
|
---|
115 | fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
|
---|
116 | fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
|
---|
117 |
|
---|
118 | RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
|
---|
119 | fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
|
---|
120 | insertionPoint.Successor = fixedSamplesPartitionCreator;
|
---|
121 |
|
---|
122 | RegisterParameterEvents();
|
---|
123 | RecalculateFixedSamplesPartitions();
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void RegisterParameterEvents() {
|
---|
127 | if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
128 | NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
|
---|
129 | NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
130 | FixedSamplesParameter.Value.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
|
---|
131 | RandomSamplesParameter.Value.ValueChanged += (o, e) => { ReevaluteElites = RandomSamples != 0; };
|
---|
132 | Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected override void ParameterizeSolutionsCreator() {
|
---|
136 | base.ParameterizeSolutionsCreator();
|
---|
137 | SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected override void ParameterizeMainLoop() {
|
---|
141 | base.ParameterizeMainLoop();
|
---|
142 | MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
143 | MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected override void ParameterizeAnalyzers() {
|
---|
147 | base.ParameterizeAnalyzers();
|
---|
148 | foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
|
---|
149 | IParameter evaluatorParameter;
|
---|
150 | if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
|
---|
151 | ILookupParameter param = evaluatorParameter as ILookupParameter;
|
---|
152 | if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
158 | NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
159 | RecalculateFixedSamplesPartitions();
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
163 | FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
164 | RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
165 | RecalculateFixedSamplesPartitions();
|
---|
166 | base.Problem_Reset(sender, e);
|
---|
167 | }
|
---|
168 |
|
---|
169 | protected override void OnProblemChanged() {
|
---|
170 | Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
171 | FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
172 | RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
173 |
|
---|
174 | if (Problem is IRegressionProblem) {
|
---|
175 | var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IRegressionProblemData>();
|
---|
176 | evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
|
---|
177 | EvaluatorParameter.Value = evaluator;
|
---|
178 | } else if (Problem is IClassificationProblem) {
|
---|
179 | var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IClassificationProblemData>();
|
---|
180 | evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
|
---|
181 | EvaluatorParameter.Value = evaluator;
|
---|
182 | } else
|
---|
183 | EvaluatorParameter.Value = null;
|
---|
184 |
|
---|
185 | ParameterizeStochasticOperatorForIsland(EvaluatorParameter.Value);
|
---|
186 |
|
---|
187 | RecalculateFixedSamplesPartitions();
|
---|
188 | base.OnProblemChanged();
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void RecalculateFixedSamplesPartitions() {
|
---|
192 | if (Problem == null) {
|
---|
193 | FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | var samplesStart = Problem.FitnessCalculationPartition.Start;
|
---|
197 | var samplesEnd = Problem.FitnessCalculationPartition.End;
|
---|
198 | var totalSamples = Problem.FitnessCalculationPartition.Size;
|
---|
199 | var fixedSamples = FixedSamples;
|
---|
200 | var islands = NumberOfIslands.Value;
|
---|
201 |
|
---|
202 | int offset = (int)Math.Ceiling(((double)(totalSamples - fixedSamples)) / (islands - 1));
|
---|
203 | List<IntRange> partitions = new List<IntRange>();
|
---|
204 | for (int i = 0; i < islands; i++) {
|
---|
205 | var partitionStart = samplesStart + offset * i;
|
---|
206 | partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
|
---|
207 | }
|
---|
208 |
|
---|
209 | //it can be the case that the last partitions exceeds the allowed samples
|
---|
210 | //move the last partition forward.
|
---|
211 | int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
|
---|
212 | if (exceedsSamples > 0) {
|
---|
213 | partitions[partitions.Count - 1].Start -= exceedsSamples;
|
---|
214 | partitions[partitions.Count - 1].End -= exceedsSamples;
|
---|
215 | }
|
---|
216 | FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
|
---|
217 | }
|
---|
218 |
|
---|
219 | }
|
---|
220 | }
|
---|