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.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
|
---|
34 | [Item("Symbolic DataAnalysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
|
---|
35 | [Creatable("Algorithms")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
|
---|
38 | private const string FixedSamplesParameterName = "NumberOfFixedSamples";
|
---|
39 | private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
|
---|
40 | private const string RandomSamplesParameterName = "NumberOfRandomSamples";
|
---|
41 |
|
---|
42 | #region Problem Properties
|
---|
43 | public override Type ProblemType {
|
---|
44 | get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
|
---|
45 | }
|
---|
46 | public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
|
---|
47 | get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
|
---|
48 | set { base.Problem = value; }
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region parameters
|
---|
53 | public IFixedValueParameter<IntValue> FixedSamplesParameter {
|
---|
54 | get { return (IFixedValueParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
|
---|
55 | }
|
---|
56 | public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
|
---|
57 | get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
|
---|
58 | }
|
---|
59 | public IFixedValueParameter<IntValue> RandomSamplesParameter {
|
---|
60 | get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region properties
|
---|
65 | public int FixedSamples {
|
---|
66 | get { return FixedSamplesParameter.Value.Value; }
|
---|
67 | set { FixedSamplesParameter.Value.Value = value; }
|
---|
68 | }
|
---|
69 | public ItemArray<IntRange> FixedSamplesPartitions {
|
---|
70 | get { return FixedSamplesPartitionsParameter.Value; }
|
---|
71 | set { FixedSamplesPartitionsParameter.Value = value; }
|
---|
72 | }
|
---|
73 | public int RandomSamples {
|
---|
74 | get { return RandomSamplesParameter.Value.Value; }
|
---|
75 | set { RandomSamplesParameter.Value.Value = value; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
82 | private void AfterDeserialization() {
|
---|
83 | RegisterParameterEvents();
|
---|
84 | }
|
---|
85 | private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
|
---|
86 | : base(original, cloner) {
|
---|
87 | RegisterParameterEvents();
|
---|
88 | }
|
---|
89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
90 | return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public SymbolicDataAnalysisIslandGeneticAlgorithm()
|
---|
94 | : base() {
|
---|
95 | Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
|
---|
96 | Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
|
---|
97 | Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island..", new IntValue(0)));
|
---|
98 |
|
---|
99 | RegisterParameterEvents();
|
---|
100 | RecalculateFixedSamplesPartitions();
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void RegisterParameterEvents() {
|
---|
104 | NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
|
---|
105 | NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
106 | FixedSamplesParameter.Value.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
110 | NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
|
---|
111 | RecalculateFixedSamplesPartitions();
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected override void Problem_Reset(object sender, EventArgs e) {
|
---|
115 | RecalculateFixedSamplesPartitions();
|
---|
116 | base.Problem_Reset(sender, e);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void OnProblemChanged() {
|
---|
120 | base.OnProblemChanged();
|
---|
121 | Problem.FitnessCalculationPartition.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
|
---|
122 | RecalculateFixedSamplesPartitions();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void RecalculateFixedSamplesPartitions() {
|
---|
126 | if (Problem == null) {
|
---|
127 | FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | var samplesStart = Problem.FitnessCalculationPartition.Start;
|
---|
131 | var samplesEnd = Problem.FitnessCalculationPartition.End;
|
---|
132 | var totalSamples = Problem.FitnessCalculationPartition.Size;
|
---|
133 | var fixedSamples = FixedSamples;
|
---|
134 | var islands = NumberOfIslands.Value;
|
---|
135 |
|
---|
136 | int offset = 0;
|
---|
137 | //fixed samples partition do not overlap
|
---|
138 | if (((double)totalSamples) / fixedSamples <= islands) {
|
---|
139 | offset = totalSamples / islands;
|
---|
140 | } else {
|
---|
141 | offset = (totalSamples - fixedSamples) / (islands - 1);
|
---|
142 | }
|
---|
143 |
|
---|
144 | List<IntRange> partitions = new List<IntRange>();
|
---|
145 | for (int i = 0; i < islands; i++) {
|
---|
146 | var partitionStart = samplesStart + offset * i;
|
---|
147 | partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
|
---|
148 | }
|
---|
149 |
|
---|
150 | //it can be the case that the last partitions exceeds the allowed samples
|
---|
151 | //move the last partition forward.
|
---|
152 | int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
|
---|
153 | if (exceedsSamples > 0) {
|
---|
154 | partitions[partitions.Count - 1].Start -= exceedsSamples;
|
---|
155 | partitions[partitions.Count - 1].End -= exceedsSamples;
|
---|
156 | }
|
---|
157 | FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 | }
|
---|