[13095] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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.Drawing;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Selection;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
| 33 | [Item("LayerReseeder", "")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public sealed class LayerReseeder : SingleSuccessorOperator, IOperatorGraphOperator {
|
---|
| 36 | public static new Image StaticItemImage {
|
---|
| 37 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
|
---|
| 38 | }
|
---|
| 39 | public override Image ItemImage {
|
---|
| 40 | get {
|
---|
| 41 | if (Breakpoint) return HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;
|
---|
| 42 | else return base.ItemImage;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | private ILookupParameter<BoolValue> ContinuousReseedingParameter {
|
---|
| 47 | get { return (ILookupParameter<BoolValue>)Parameters["ContinuousReseeding"]; }
|
---|
| 48 | }
|
---|
| 49 | private ILookupParameter<IntValue> GenerationsParameter {
|
---|
| 50 | get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
| 51 | }
|
---|
| 52 | private ILookupParameter<IntValue> AgeGapParameter {
|
---|
| 53 | get { return (ILookupParameter<IntValue>)Parameters["AgeGap"]; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [Storable]
|
---|
| 57 | private OperatorGraph operatorGraph;
|
---|
| 58 | public OperatorGraph OperatorGraph {
|
---|
| 59 | get { return operatorGraph; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | private LayerReseeder(bool deserializing)
|
---|
| 64 | : base(deserializing) { }
|
---|
| 65 |
|
---|
| 66 | private LayerReseeder(LayerReseeder original, Cloner cloner)
|
---|
| 67 | : base(original, cloner) {
|
---|
| 68 | operatorGraph = cloner.Clone(original.operatorGraph);
|
---|
| 69 | }
|
---|
| 70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 71 | return new LayerReseeder(this, cloner);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public LayerReseeder()
|
---|
| 75 | : base() {
|
---|
| 76 | Parameters.Add(new LookupParameter<BoolValue>("ContinuousReseeding"));
|
---|
| 77 | Parameters.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 78 | Parameters.Add(new LookupParameter<IntValue>("AgeGap"));
|
---|
| 79 |
|
---|
| 80 | operatorGraph = new OperatorGraph();
|
---|
| 81 |
|
---|
| 82 | var subScopesCounter = new SubScopesCounter();
|
---|
| 83 | //var continuousReseedingBranch = new ConditionalBranch() { Name = "ContinuousReseeding?" };
|
---|
| 84 | //var reseedingIntervalCalculator = new ExpressionCalculator() { Name = "DoReseeding = Generation % AgeGap == 0" };
|
---|
| 85 | //var doReseedingBranch = new ConditionalBranch() { Name = "DoReseeding?" };
|
---|
| 86 | var numberOfReplacedCalculator = new ExpressionCalculator() { Name = "NumberOfReplaced = if ContinuousReseeding then max(LayerPopulationSize - (PopulationSize - PopulationSize / AgeGap), 0) else PopulationSize" };
|
---|
| 87 | var numberOfNewIndividualsCalculator = new ExpressionCalculator() { Name = "NumberOfNewIndividuals = PopulationSize - (LayerPopulatioSize - NumberOfReplaced)" };
|
---|
| 88 | var oldestSelector = new BestSelector();
|
---|
| 89 | var processOldest = new SubScopesProcessor();
|
---|
| 90 | var removeIndividuals = new SubScopesRemover();
|
---|
| 91 | var createIndividuals = new SolutionsCreator();
|
---|
| 92 | var mergeIndididuals = new MergingReducer();
|
---|
| 93 | var initializeAgeProsessor = new UniformSubScopesProcessor();
|
---|
| 94 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
| 95 | var incrEvaluatedSolutionsAfterReseeding = new SubScopesCounter() { Name = "Update EvaluatedSolutions" };
|
---|
| 96 | var subScopesCounter2 = new SubScopesCounter();
|
---|
| 97 |
|
---|
| 98 | //OperatorGraph.InitialOperator = continuousReseedingBranch;
|
---|
| 99 | OperatorGraph.InitialOperator = subScopesCounter;
|
---|
| 100 |
|
---|
| 101 | subScopesCounter.ValueParameter.ActualName = "LayerPopulationSize";
|
---|
| 102 | subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
|
---|
| 103 | subScopesCounter.Successor = numberOfReplacedCalculator;
|
---|
| 104 |
|
---|
| 105 | //continuousReseedingBranch.ConditionParameter.ActualName = "ContinuousReseeding";
|
---|
| 106 | //continuousReseedingBranch.TrueBranch = numberOfChildrenCalculator;
|
---|
| 107 | //continuousReseedingBranch.FalseBranch = reseedingIntervalCalculator;
|
---|
| 108 |
|
---|
| 109 | //reseedingIntervalCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Generation"));
|
---|
| 110 | //reseedingIntervalCalculator.CollectedValues.Add(new LookupParameter<IntValue>("AgeGap"));
|
---|
| 111 | //reseedingIntervalCalculator.ExpressionResultParameter.ActualName = "DoReseeding";
|
---|
| 112 | //reseedingIntervalCalculator.ExpressionParameter.Value = new StringValue("Generation AgeGap % 0 ==");
|
---|
| 113 | //reseedingIntervalCalculator.Successor = doReseedingBranch;
|
---|
| 114 |
|
---|
| 115 | //doReseedingBranch.ConditionParameter.ActualName = "DoReseeding";
|
---|
| 116 | //doReseedingBranch.TrueBranch = numberOfChildrenCalculator;
|
---|
| 117 |
|
---|
| 118 | numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<BoolValue>("ContinuousReseeding"));
|
---|
| 119 | numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("LayerPopulationSize"));
|
---|
| 120 | numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
| 121 | numberOfReplacedCalculator.CollectedValues.Add(new LookupParameter<IntValue>("AgeGap"));
|
---|
| 122 | numberOfReplacedCalculator.ExpressionResultParameter.ActualName = "NumberOfReplaced";
|
---|
| 123 | const string numSelected = "LayerPopulationSize PopulationSize PopulationSize AgeGap / - -";
|
---|
| 124 | numberOfReplacedCalculator.ExpressionParameter.Value = new StringValue(numSelected + " 0 0 " + numSelected + " < if PopulationSize ContinuousReseeding if toint");
|
---|
| 125 | numberOfReplacedCalculator.Successor = numberOfNewIndividualsCalculator;
|
---|
| 126 |
|
---|
| 127 | numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
| 128 | numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfReplaced"));
|
---|
| 129 | numberOfNewIndividualsCalculator.CollectedValues.Add(new LookupParameter<IntValue>("LayerPopulationSize"));
|
---|
| 130 | numberOfNewIndividualsCalculator.ExpressionResultParameter.ActualName = "NumberOfNewIndividuals";
|
---|
| 131 | numberOfNewIndividualsCalculator.ExpressionParameter.Value = new StringValue("PopulationSize LayerPopulationSize NumberOfReplaced - - toint");
|
---|
| 132 | numberOfNewIndividualsCalculator.Successor = oldestSelector;
|
---|
| 133 |
|
---|
| 134 | oldestSelector.QualityParameter.ActualName = "Age";
|
---|
| 135 | oldestSelector.NumberOfSelectedSubScopesParameter.ActualName = "NumberOfReplaced";
|
---|
| 136 | oldestSelector.MaximizationParameter.Value = new BoolValue(true);
|
---|
| 137 | oldestSelector.CopySelected = new BoolValue(false);
|
---|
| 138 | oldestSelector.Successor = processOldest;
|
---|
| 139 |
|
---|
| 140 | processOldest.Operators.Add(new EmptyOperator());
|
---|
| 141 | processOldest.Operators.Add(removeIndividuals);
|
---|
| 142 | processOldest.Successor = mergeIndididuals;
|
---|
| 143 |
|
---|
| 144 | removeIndividuals.Successor = createIndividuals;
|
---|
| 145 |
|
---|
| 146 | createIndividuals.NumberOfSolutionsParameter.ActualName = "NumberOfNewIndividuals";
|
---|
| 147 | createIndividuals.Successor = initializeAgeProsessor;
|
---|
| 148 |
|
---|
| 149 | initializeAgeProsessor.Operator = initializeAge;
|
---|
| 150 | initializeAgeProsessor.Successor = incrEvaluatedSolutionsAfterReseeding;
|
---|
| 151 |
|
---|
| 152 | initializeAge.CollectedValues.Add(new ValueParameter<DoubleValue>("Age", new DoubleValue(0)));
|
---|
| 153 | initializeAge.Successor = null;
|
---|
| 154 |
|
---|
| 155 | incrEvaluatedSolutionsAfterReseeding.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 156 | incrEvaluatedSolutionsAfterReseeding.AccumulateParameter.Value = new BoolValue(true);
|
---|
| 157 | incrEvaluatedSolutionsAfterReseeding.Successor = null;
|
---|
| 158 |
|
---|
| 159 | mergeIndididuals.Successor = subScopesCounter2;
|
---|
| 160 |
|
---|
| 161 | subScopesCounter2.ValueParameter.ActualName = "LayerPopulationSize";
|
---|
| 162 | subScopesCounter2.AccumulateParameter.Value = new BoolValue(false);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | public override IOperation Apply() {
|
---|
| 166 | bool continuousReseeding = ContinuousReseedingParameter.ActualValue.Value;
|
---|
| 167 | int generations = GenerationsParameter.ActualValue.Value;
|
---|
| 168 | int ageGap = AgeGapParameter.ActualValue.Value;
|
---|
| 169 |
|
---|
| 170 | var next = new OperationCollection(base.Apply());
|
---|
| 171 | if (continuousReseeding || generations % ageGap == 0) {
|
---|
| 172 | var layerZeroScope = ExecutionContext.Scope.SubScopes[0];
|
---|
| 173 | if (operatorGraph.InitialOperator != null)
|
---|
| 174 | next.Insert(0, ExecutionContext.CreateChildOperation(operatorGraph.InitialOperator, layerZeroScope));
|
---|
| 175 | }
|
---|
| 176 | return next;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | } |
---|