[4012] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 HeuristicLab.Core;
|
---|
[4068] | 23 | using HeuristicLab.Data;
|
---|
[4012] | 24 | using HeuristicLab.Operators;
|
---|
[4068] | 25 | using HeuristicLab.Optimization.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[4012] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4017] | 28 | using HeuristicLab.Selection;
|
---|
[4902] | 29 | using HeuristicLab.Common;
|
---|
[4012] | 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.NSGA2 {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// An operator that represents the mainloop of the NSGA-II
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("NSGA2MainLoop", "An operator which represents the main loop of the NSGA-II algorithm.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public class NSGA2MainLoop : AlgorithmOperator {
|
---|
[4017] | 38 | #region Parameter properties
|
---|
| 39 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 40 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 41 | }
|
---|
[4045] | 42 | public ValueLookupParameter<BoolArray> MaximizationParameter {
|
---|
| 43 | get { return (ValueLookupParameter<BoolArray>)Parameters["Maximization"]; }
|
---|
[4017] | 44 | }
|
---|
[4045] | 45 | public ScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
|
---|
| 46 | get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
|
---|
[4017] | 47 | }
|
---|
[4045] | 48 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 49 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 50 | }
|
---|
[4017] | 51 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 52 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 53 | }
|
---|
| 54 | public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
|
---|
| 55 | get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
|
---|
| 56 | }
|
---|
| 57 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 58 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 59 | }
|
---|
| 60 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 61 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 62 | }
|
---|
| 63 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 64 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 65 | }
|
---|
| 66 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 67 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 68 | }
|
---|
| 69 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 70 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 71 | }
|
---|
| 72 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 73 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 74 | }
|
---|
| 75 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 76 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
[4012] | 80 | [StorableConstructor]
|
---|
[4902] | 81 | protected NSGA2MainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 82 | protected NSGA2MainLoop(NSGA2MainLoop original, Cloner cloner) : base(original, cloner) { }
|
---|
[4012] | 83 | public NSGA2MainLoop()
|
---|
| 84 | : base() {
|
---|
| 85 | Initialize();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void Initialize() {
|
---|
[4017] | 89 | #region Create parameters
|
---|
| 90 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
[4045] | 91 | Parameters.Add(new ValueLookupParameter<BoolArray>("Maximization", "True if an objective should be maximized, or false if it should be minimized."));
|
---|
| 92 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The vector of quality values."));
|
---|
| 93 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The population size."));
|
---|
[4017] | 94 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
|
---|
| 96 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 97 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 99 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
| 100 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 101 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 102 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
[4012] | 103 | #endregion
|
---|
| 104 |
|
---|
[4017] | 105 | #region Create operators
|
---|
| 106 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 107 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 108 | Placeholder analyzer1 = new Placeholder();
|
---|
| 109 | Placeholder selector = new Placeholder();
|
---|
| 110 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
| 111 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 112 | UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
|
---|
[4045] | 113 | StochasticBranch crossoverStochasticBranch = new StochasticBranch();
|
---|
[4017] | 114 | Placeholder crossover = new Placeholder();
|
---|
[5143] | 115 | ParentCopyCrossover noCrossover = new ParentCopyCrossover();
|
---|
[4045] | 116 | StochasticBranch mutationStochasticBranch = new StochasticBranch();
|
---|
[4017] | 117 | Placeholder mutator = new Placeholder();
|
---|
| 118 | Placeholder evaluator = new Placeholder();
|
---|
| 119 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
[4045] | 120 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 121 | RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter();
|
---|
| 122 | LeftSelector leftSelector = new LeftSelector();
|
---|
[4017] | 123 | RightReducer rightReducer = new RightReducer();
|
---|
| 124 | IntCounter intCounter = new IntCounter();
|
---|
| 125 | Comparator comparator = new Comparator();
|
---|
| 126 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
| 127 | Placeholder analyzer2 = new Placeholder();
|
---|
| 128 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 129 |
|
---|
| 130 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
| 131 |
|
---|
| 132 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[4045] | 133 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[4017] | 134 |
|
---|
| 135 | analyzer1.Name = "Analyzer";
|
---|
[4045] | 136 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[4017] | 137 |
|
---|
| 138 | selector.Name = "Selector";
|
---|
[4045] | 139 | selector.OperatorParameter.ActualName = SelectorParameter.Name;
|
---|
[4017] | 140 |
|
---|
| 141 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
| 142 |
|
---|
[4045] | 143 | crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
|
---|
| 144 | crossoverStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 145 |
|
---|
[4017] | 146 | crossover.Name = "Crossover";
|
---|
[4045] | 147 | crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
|
---|
[4017] | 148 |
|
---|
[4045] | 149 | noCrossover.Name = "Clone parent";
|
---|
| 150 | noCrossover.RandomParameter.ActualName = RandomParameter.Name;
|
---|
[4017] | 151 |
|
---|
[4045] | 152 | mutationStochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 153 | mutationStochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 154 |
|
---|
[4017] | 155 | mutator.Name = "Mutator";
|
---|
[4045] | 156 | mutator.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
[4017] | 157 |
|
---|
| 158 | evaluator.Name = "Evaluator";
|
---|
[4045] | 159 | evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
[4017] | 160 |
|
---|
| 161 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 162 |
|
---|
[4045] | 163 | rankAndCrowdingSorter.CrowdingDistanceParameter.ActualName = "CrowdingDistance";
|
---|
| 164 | rankAndCrowdingSorter.RankParameter.ActualName = "Rank";
|
---|
[4017] | 165 |
|
---|
[4045] | 166 | leftSelector.CopySelected = new BoolValue(false);
|
---|
| 167 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 168 |
|
---|
[4017] | 169 | intCounter.Increment = new IntValue(1);
|
---|
| 170 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 171 |
|
---|
| 172 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 173 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 174 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
[4045] | 175 | comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[4017] | 176 |
|
---|
| 177 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[4045] | 178 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[4017] | 179 |
|
---|
| 180 | analyzer2.Name = "Analyzer";
|
---|
| 181 | analyzer2.OperatorParameter.ActualName = "Analyzer";
|
---|
| 182 |
|
---|
| 183 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
[4012] | 184 | #endregion
|
---|
| 185 |
|
---|
[4017] | 186 | #region Create operator graph
|
---|
| 187 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 188 | variableCreator.Successor = resultsCollector1;
|
---|
| 189 | resultsCollector1.Successor = analyzer1;
|
---|
| 190 | analyzer1.Successor = selector;
|
---|
| 191 | selector.Successor = subScopesProcessor1;
|
---|
| 192 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 193 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
[4045] | 194 | subScopesProcessor1.Successor = mergingReducer;
|
---|
[4017] | 195 | childrenCreator.Successor = uniformSubScopesProcessor;
|
---|
[4045] | 196 | uniformSubScopesProcessor.Operator = crossoverStochasticBranch;
|
---|
[4017] | 197 | uniformSubScopesProcessor.Successor = null;
|
---|
[4045] | 198 | crossoverStochasticBranch.FirstBranch = crossover;
|
---|
| 199 | crossoverStochasticBranch.SecondBranch = noCrossover;
|
---|
| 200 | crossoverStochasticBranch.Successor = mutationStochasticBranch;
|
---|
| 201 | crossover.Successor = null;
|
---|
| 202 | noCrossover.Successor = null;
|
---|
| 203 | mutationStochasticBranch.FirstBranch = mutator;
|
---|
| 204 | mutationStochasticBranch.SecondBranch = null;
|
---|
| 205 | mutationStochasticBranch.Successor = evaluator;
|
---|
[4017] | 206 | mutator.Successor = null;
|
---|
| 207 | evaluator.Successor = subScopesRemover;
|
---|
| 208 | subScopesRemover.Successor = null;
|
---|
[4045] | 209 | mergingReducer.Successor = rankAndCrowdingSorter;
|
---|
| 210 | rankAndCrowdingSorter.Successor = leftSelector;
|
---|
| 211 | leftSelector.Successor = rightReducer;
|
---|
| 212 | rightReducer.Successor = intCounter;
|
---|
[4017] | 213 | intCounter.Successor = comparator;
|
---|
| 214 | comparator.Successor = resultsCollector2;
|
---|
| 215 | resultsCollector2.Successor = analyzer2;
|
---|
| 216 | analyzer2.Successor = conditionalBranch;
|
---|
| 217 | conditionalBranch.FalseBranch = selector;
|
---|
| 218 | conditionalBranch.TrueBranch = null;
|
---|
| 219 | conditionalBranch.Successor = null;
|
---|
[4012] | 220 | #endregion
|
---|
| 221 | }
|
---|
[4902] | 222 |
|
---|
| 223 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 224 | return new NSGA2MainLoop(this, cloner);
|
---|
| 225 | }
|
---|
[4012] | 226 | }
|
---|
| 227 | }
|
---|