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.OffspringSelectionGeneticAlgorithm;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
|
---|
36 | [Item("Symbolic Data Analysis Island Offspring Selection Genetic Algorithm", "A symbolic data analysis island offspring selection genetic algorithm.")]
|
---|
37 | [Creatable("Data Analysis")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm : IslandOffspringSelectionGeneticAlgorithm {
|
---|
40 | private const string FixedSamplesParameterName = "NumberOfFixedSamples";
|
---|
41 | private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
|
---|
42 | private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
|
---|
43 | private const string EvaluatorParameterName = "IslandEvaluator";
|
---|
44 | private const string IslandIndexParameterName = "IslandIndex";
|
---|
45 | private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
|
---|
46 |
|
---|
47 | #region Problem Properties
|
---|
48 | public override Type ProblemType {
|
---|
49 | get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
|
---|
50 | }
|
---|
51 | public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
|
---|
52 | get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
|
---|
53 | set { base.Problem = value; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region parameters
|
---|
58 | public IFixedValueParameter<IntValue> FixedSamplesParameter {
|
---|
59 | get { return (IFixedValueParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
|
---|
60 | }
|
---|
61 | public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
|
---|
62 | get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
|
---|
63 | }
|
---|
64 | public IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator> EvaluatorParameter {
|
---|
65 | get { return (IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
66 | }
|
---|
67 | private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
|
---|
68 | get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | #region properties
|
---|
73 | public int FixedSamples {
|
---|
74 | get { return FixedSamplesParameter.Value.Value; }
|
---|
75 | set { FixedSamplesParameter.Value.Value = value; }
|
---|
76 | }
|
---|
77 | public ItemArray<IntRange> FixedSamplesPartitions {
|
---|
78 | get { return FixedSamplesPartitionsParameter.Value; }
|
---|
79 | set { FixedSamplesPartitionsParameter.Value = value; }
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | [StorableConstructor]
|
---|
84 | private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
86 | private void AfterDeserialization() {
|
---|
87 | RegisterParameterEvents();
|
---|
88 | }
|
---|
89 | private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | RegisterParameterEvents();
|
---|
92 | }
|
---|
93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
94 | return new SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(this, cloner);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm()
|
---|
98 | : base() {
|
---|
99 | Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
|
---|
100 | Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
|
---|
101 | Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
|
---|
102 | Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
|
---|
103 |
|
---|
104 | ScopeTreeAssigner<IntValue> islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
|
---|
105 | islandIndexAssigner.Name = "Insert island index";
|
---|
106 | islandIndexAssigner.LeftSideParameter.ActualName = IslandIndexParameterName;
|
---|
107 | var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
|
---|
108 | islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
|
---|
109 |
|
---|
110 | ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
|
---|
111 | fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
|
---|
112 | fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
|
---|
113 | fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
|
---|
114 |
|
---|
115 | SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
|
---|
116 | islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
|
---|
117 | fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
|
---|
118 | insertionPoint.Successor = islandIndexAssigner;
|
---|
119 |
|
---|
120 | RegisterParameterEvents();
|
---|
121 | RecalculateFixedSamplesPartitions();
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void RegisterParameterEvents() {
|
---|
125 | if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
126 | NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
|
---|
127 | NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
128 | FixedSamplesParameter.Value.ValueChanged += (o, e) => {
|
---|
129 | RecalculateFixedSamplesPartitions();
|
---|
130 | ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
|
---|
131 | };
|
---|
132 | Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
|
---|
133 | EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected override void ParameterizeSolutionsCreator() {
|
---|
137 | base.ParameterizeSolutionsCreator();
|
---|
138 | SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected override void ParameterizeMainLoop() {
|
---|
142 | base.ParameterizeMainLoop();
|
---|
143 | MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
144 | MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected override void ParameterizeAnalyzers() {
|
---|
148 | base.ParameterizeAnalyzers();
|
---|
149 | foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
|
---|
150 | IParameter evaluatorParameter;
|
---|
151 | if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
|
---|
152 | ILookupParameter param = evaluatorParameter as ILookupParameter;
|
---|
153 | if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void ParameterizeEvaluator() {
|
---|
159 | var evaluator = EvaluatorParameter.Value;
|
---|
160 | evaluator.IterationsParameter.ActualName = "Generations";
|
---|
161 | evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
162 | evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
163 |
|
---|
164 | ParameterizeStochasticOperator(evaluator);
|
---|
165 | }
|
---|
166 |
|
---|
167 | private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
168 | NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
169 | RecalculateFixedSamplesPartitions();
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
173 | FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
174 | base.Problem_Reset(sender, e);
|
---|
175 | }
|
---|
176 |
|
---|
177 | protected override void OnProblemChanged() {
|
---|
178 | Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
179 | FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
|
---|
180 |
|
---|
181 | if (Problem != null && EvaluatorParameter.Value == null) {
|
---|
182 | EvaluatorParameter.Value = new RandomSamplesEvaluator();
|
---|
183 | } else if (Problem == null)
|
---|
184 | EvaluatorParameter.Value = null;
|
---|
185 |
|
---|
186 | ParameterizeStochasticOperator(EvaluatorParameter.Value);
|
---|
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 | }
|
---|