Changeset 4017 for trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2.cs
- Timestamp:
- 07/08/10 12:28:06 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2.cs
r4012 r4017 21 21 22 22 using System; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Analysis; 31 using HeuristicLab.Random; 32 using HeuristicLab.Optimization.Operators; 33 using HeuristicLab.PluginInfrastructure; 29 34 30 35 namespace HeuristicLab.Algorithms.NSGA2 { … … 32 37 /// The Nondominated Sorting Genetic Algorithm II was introduced in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197. 33 38 /// </summary> 34 [Item("NSGA 2", "The Nondominated Sorting Genetic Algorithm II was introduced in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")]39 [Item("NSGA-II", "The Nondominated Sorting Genetic Algorithm II was introduced in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")] 35 40 [Creatable("Algorithms")] 36 41 [StorableClass] … … 47 52 48 53 #region Parameter Properties 49 private ValueLookupParameter<IntValue> PopulationSizeParameter { 50 get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; } 54 private ValueParameter<IntValue> SeedParameter { 55 get { return (ValueParameter<IntValue>)Parameters["Seed"]; } 56 } 57 private ValueParameter<BoolValue> SetSeedRandomlyParameter { 58 get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; } 59 } 60 private ValueParameter<IntValue> PopulationSizeParameter { 61 get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; } 62 } 63 private ConstrainedValueParameter<ISelector> SelectorParameter { 64 get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; } 65 } 66 private ValueParameter<PercentValue> CrossoverProbabilityParameter { 67 get { return (ValueParameter<PercentValue>)Parameters["CrossoverProbability"]; } 68 } 69 private ConstrainedValueParameter<ICrossover> CrossoverParameter { 70 get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; } 71 } 72 private ValueParameter<PercentValue> MutationProbabilityParameter { 73 get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; } 74 } 75 private OptionalConstrainedValueParameter<IManipulator> MutatorParameter { 76 get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; } 77 } 78 private ValueParameter<MultiAnalyzer> AnalyzerParameter { 79 get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; } 80 } 81 private ValueParameter<IntValue> MaximumGenerationsParameter { 82 get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; } 51 83 } 52 84 #endregion 53 85 54 86 #region Properties 87 public IntValue Seed { 88 get { return SeedParameter.Value; } 89 set { SeedParameter.Value = value; } 90 } 91 public BoolValue SetSeedRandomly { 92 get { return SetSeedRandomlyParameter.Value; } 93 set { SetSeedRandomlyParameter.Value = value; } 94 } 95 public IntValue PopulationSize { 96 get { return PopulationSizeParameter.Value; } 97 set { PopulationSizeParameter.Value = value; } 98 } 99 public ISelector Selector { 100 get { return SelectorParameter.Value; } 101 set { SelectorParameter.Value = value; } 102 } 103 public PercentValue CrossoverProbability { 104 get { return CrossoverProbabilityParameter.Value; } 105 set { CrossoverProbabilityParameter.Value = value; } 106 } 107 public ICrossover Crossover { 108 get { return CrossoverParameter.Value; } 109 set { CrossoverParameter.Value = value; } 110 } 111 public PercentValue MutationProbability { 112 get { return MutationProbabilityParameter.Value; } 113 set { MutationProbabilityParameter.Value = value; } 114 } 115 public IManipulator Mutator { 116 get { return MutatorParameter.Value; } 117 set { MutatorParameter.Value = value; } 118 } 119 public MultiAnalyzer Analyzer { 120 get { return AnalyzerParameter.Value; } 121 set { AnalyzerParameter.Value = value; } 122 } 123 public IntValue MaximumGenerations { 124 get { return MaximumGenerationsParameter.Value; } 125 set { MaximumGenerationsParameter.Value = value; } 126 } 127 private RandomCreator RandomCreator { 128 get { return (RandomCreator)OperatorGraph.InitialOperator; } 129 } 130 private SolutionsCreator SolutionsCreator { 131 get { return (SolutionsCreator)RandomCreator.Successor; } 132 } 133 private NSGA2MainLoop MainLoop { 134 get { return (NSGA2MainLoop)SolutionsCreator.Successor; } 135 } 55 136 #endregion 56 137 … … 58 139 public NSGA2(bool deserializing) : base(deserializing) { } 59 140 public NSGA2() { 60 // TODO: Add your parameters here 61 Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The population size of the algorithm.", new IntValue(100))); 141 Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 142 Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 143 Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100))); 144 Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.")); 145 Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on two parents.", new PercentValue(0.05))); 146 Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.")); 147 Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05))); 148 Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions.")); 149 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer())); 150 Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000))); 151 152 RandomCreator randomCreator = new RandomCreator(); 153 SolutionsCreator solutionsCreator = new SolutionsCreator(); 154 NSGA2MainLoop mainLoop = new NSGA2MainLoop(); 155 OperatorGraph.InitialOperator = randomCreator; 156 157 randomCreator.RandomParameter.ActualName = "Random"; 158 randomCreator.SeedParameter.ActualName = SeedParameter.Name; 159 randomCreator.SeedParameter.Value = null; 160 randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name; 161 randomCreator.SetSeedRandomlyParameter.Value = null; 162 randomCreator.Successor = solutionsCreator; 163 164 solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name; 165 solutionsCreator.Successor = mainLoop; 166 167 mainLoop.SelectorParameter.ActualName = SelectorParameter.Name; 168 mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name; 169 mainLoop.CrossoverProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name; 170 mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name; 171 mainLoop.MutatorParameter.ActualName = MutatorParameter.Name; 172 mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name; 173 mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName; 174 mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name; 175 mainLoop.ResultsParameter.ActualName = "Results"; 176 177 foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name)) 178 SelectorParameter.ValidValues.Add(selector); 179 ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector")); 180 if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector; 181 182 // TODO: parameterize selectors 183 184 AttachEventHandlers(); 62 185 } 63 186 … … 67 190 return clone; 68 191 } 192 193 #region Events 194 protected override void OnProblemChanged() { 195 // parameterize pretty much everything 196 base.OnProblemChanged(); 197 } 198 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 199 // parameterize SolutionCreator 200 base.Problem_SolutionCreatorChanged(sender, e); 201 } 202 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 203 // parameterize StochasticOperators 204 // parameterize SolutionsCreator 205 // parameterize MainLoop 206 // parameterize Selectors 207 // parameterize Analyzers 208 Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged); 209 base.Problem_EvaluatorChanged(sender, e); 210 } 211 protected override void Problem_OperatorsChanged(object sender, EventArgs e) { 212 base.Problem_OperatorsChanged(sender, e); 213 } 214 protected override void Problem_Reset(object sender, EventArgs e) { 215 base.Problem_Reset(sender, e); 216 } 217 218 private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) { 219 PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged); 220 // parameterize Selectors 221 } 222 private void PopulationSize_ValueChanged(object sender, EventArgs e) { 223 // parameterize Selectors 224 } 225 226 private void Evaluator_QualitiesParameter_ActualNameChanged(object sender, EventArgs e) { 227 // parameterize Analyzers 228 // parameterize MainLoop 229 // parameterize Selectors 230 } 231 #endregion 232 233 #region Helpers 234 [StorableHook(HookType.AfterDeserialization)] 235 private void AttachEventHandlers() { 236 PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged); 237 PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged); 238 if (Problem != null) { 239 Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged); 240 } 241 } 242 #endregion 69 243 } 70 244 }
Note: See TracChangeset
for help on using the changeset viewer.