[10142] | 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;
|
---|
[10152] | 29 | using HeuristicLab.Operators;
|
---|
[10142] | 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";
|
---|
[10177] | 44 | private const string IslandIndexParameterName = "IslandIndex";
|
---|
[10142] | 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
|
---|
[10353] | 58 | public IFixedValueParameter<PercentValue> FixedSamplesParameter {
|
---|
| 59 | get { return (IFixedValueParameter<PercentValue>)Parameters[FixedSamplesParameterName]; }
|
---|
[10142] | 60 | }
|
---|
| 61 | public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
|
---|
| 62 | get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
|
---|
| 63 | }
|
---|
[10177] | 64 | public IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator> EvaluatorParameter {
|
---|
| 65 | get { return (IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
[10142] | 66 | }
|
---|
| 67 | private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
|
---|
| 68 | get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
|
---|
| 69 | }
|
---|
| 70 | #endregion
|
---|
| 71 |
|
---|
| 72 | #region properties
|
---|
[10353] | 73 | public double FixedSamples {
|
---|
[10142] | 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 | }
|
---|
[10357] | 81 |
|
---|
| 82 | private readonly ScopeTreeAssigner<IntValue> islandIndexAssigner;
|
---|
[10142] | 83 | #endregion
|
---|
| 84 |
|
---|
| 85 | [StorableConstructor]
|
---|
| 86 | private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 87 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 88 | private void AfterDeserialization() {
|
---|
| 89 | RegisterParameterEvents();
|
---|
| 90 | }
|
---|
| 91 | private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
|
---|
| 92 | : base(original, cloner) {
|
---|
| 93 | RegisterParameterEvents();
|
---|
| 94 | }
|
---|
| 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 96 | return new SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(this, cloner);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm()
|
---|
| 100 | : base() {
|
---|
[10353] | 101 | Parameters.Add(new FixedValueParameter<PercentValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new PercentValue(0.2)));
|
---|
[10142] | 102 | Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
|
---|
[10177] | 103 | Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
|
---|
[10142] | 104 | Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
|
---|
| 105 |
|
---|
[10357] | 106 | islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
|
---|
[10177] | 107 | islandIndexAssigner.Name = "Insert island index";
|
---|
| 108 | islandIndexAssigner.LeftSideParameter.ActualName = IslandIndexParameterName;
|
---|
| 109 | var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
|
---|
| 110 | islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
|
---|
| 111 |
|
---|
[10142] | 112 | ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
|
---|
| 113 | fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
|
---|
| 114 | fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
|
---|
| 115 | fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
|
---|
| 116 |
|
---|
[10152] | 117 | SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
|
---|
[10177] | 118 | islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
|
---|
[10142] | 119 | fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
|
---|
[10177] | 120 | insertionPoint.Successor = islandIndexAssigner;
|
---|
[10142] | 121 |
|
---|
[10591] | 122 | ReevaluateImmigrants = true;
|
---|
| 123 | ReevaluteElites = true;
|
---|
| 124 |
|
---|
[10142] | 125 | RegisterParameterEvents();
|
---|
| 126 | RecalculateFixedSamplesPartitions();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | private void RegisterParameterEvents() {
|
---|
| 130 | if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
| 131 | NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
|
---|
[10357] | 132 | NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
|
---|
[10156] | 133 | FixedSamplesParameter.Value.ValueChanged += (o, e) => {
|
---|
| 134 | RecalculateFixedSamplesPartitions();
|
---|
[10230] | 135 | ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
|
---|
[10156] | 136 | };
|
---|
[10142] | 137 | Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
|
---|
[10156] | 138 | EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
|
---|
[10142] | 139 | }
|
---|
| 140 |
|
---|
| 141 | protected override void ParameterizeSolutionsCreator() {
|
---|
| 142 | base.ParameterizeSolutionsCreator();
|
---|
| 143 | SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | protected override void ParameterizeMainLoop() {
|
---|
| 147 | base.ParameterizeMainLoop();
|
---|
| 148 | MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
|
---|
| 149 | MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | protected override void ParameterizeAnalyzers() {
|
---|
| 153 | base.ParameterizeAnalyzers();
|
---|
| 154 | foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
|
---|
| 155 | IParameter evaluatorParameter;
|
---|
| 156 | if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
|
---|
| 157 | ILookupParameter param = evaluatorParameter as ILookupParameter;
|
---|
| 158 | if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[10156] | 163 | private void ParameterizeEvaluator() {
|
---|
| 164 | var evaluator = EvaluatorParameter.Value;
|
---|
[10230] | 165 | evaluator.IterationsParameter.ActualName = "Generations";
|
---|
| 166 | evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 167 | evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
[10177] | 168 |
|
---|
[10230] | 169 | ParameterizeStochasticOperator(evaluator);
|
---|
[10156] | 170 | }
|
---|
| 171 |
|
---|
[10142] | 172 | private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[10357] | 173 | NumberOfIslands.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
|
---|
| 174 | NumberOfIslandsParameterValue_Changed();
|
---|
| 175 | }
|
---|
| 176 | private void NumberOfIslandsParameterValue_Changed() {
|
---|
| 177 | var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
|
---|
| 178 | islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
|
---|
[10142] | 179 | RecalculateFixedSamplesPartitions();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[10356] | 182 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
| 183 | base.Problem_Reset(sender, e);
|
---|
| 184 | RecalculateFixedSamplesPartitions();
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[10142] | 187 | protected override void OnProblemChanged() {
|
---|
| 188 | Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
|
---|
| 189 |
|
---|
[10156] | 190 | if (Problem != null && EvaluatorParameter.Value == null) {
|
---|
[10177] | 191 | EvaluatorParameter.Value = new RandomSamplesEvaluator();
|
---|
[10156] | 192 | } else if (Problem == null)
|
---|
[10142] | 193 | EvaluatorParameter.Value = null;
|
---|
| 194 |
|
---|
| 195 | ParameterizeStochasticOperator(EvaluatorParameter.Value);
|
---|
| 196 | RecalculateFixedSamplesPartitions();
|
---|
| 197 | base.OnProblemChanged();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | private void RecalculateFixedSamplesPartitions() {
|
---|
| 201 | if (Problem == null) {
|
---|
| 202 | FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
|
---|
| 203 | return;
|
---|
| 204 | }
|
---|
| 205 | var samplesStart = Problem.FitnessCalculationPartition.Start;
|
---|
| 206 | var samplesEnd = Problem.FitnessCalculationPartition.End;
|
---|
| 207 | var totalSamples = Problem.FitnessCalculationPartition.Size;
|
---|
[10353] | 208 | var fixedSamples = (int)(FixedSamples * totalSamples);
|
---|
[10142] | 209 | var islands = NumberOfIslands.Value;
|
---|
| 210 |
|
---|
[10353] | 211 | double shift = (double)((totalSamples - fixedSamples)) / (islands - 1);
|
---|
| 212 | int offset = (int)Math.Floor(shift);
|
---|
| 213 | double remainder = shift - offset;
|
---|
| 214 |
|
---|
[10142] | 215 | List<IntRange> partitions = new List<IntRange>();
|
---|
| 216 | for (int i = 0; i < islands; i++) {
|
---|
[10353] | 217 | var partitionStart = samplesStart + offset * i + (int)(remainder * i);
|
---|
[10142] | 218 | partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | //it can be the case that the last partitions exceeds the allowed samples
|
---|
| 222 | //move the last partition forward.
|
---|
| 223 | int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
|
---|
| 224 | if (exceedsSamples > 0) {
|
---|
| 225 | partitions[partitions.Count - 1].Start -= exceedsSamples;
|
---|
| 226 | partitions[partitions.Count - 1].End -= exceedsSamples;
|
---|
| 227 | }
|
---|
| 228 | FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | }
|
---|
| 232 | }
|
---|