Changeset 2975 for trunk/sources/HeuristicLab.Algorithms.SGA
- Timestamp:
- 03/09/10 05:45:39 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.SGA/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.SGA/3.3/HeuristicLab.Algorithms.SGA-3.3.csproj
r2916 r2975 83 83 <ItemGroup> 84 84 <Compile Include="HeuristicLabAlgorithmsSGAPlugin.cs" /> 85 <Compile Include="SGA.cs" /> 85 <Compile Include="SGA.cs"> 86 <SubType>Code</SubType> 87 </Compile> 86 88 <Compile Include="SGAOperator.cs" /> 87 89 <Compile Include="Properties\AssemblyInfo.cs" /> -
trunk/sources/HeuristicLab.Algorithms.SGA/3.3/SGA.cs
r2924 r2975 38 38 [Creatable("Algorithms")] 39 39 public sealed class SGA : EngineAlgorithm { 40 [Storable] 41 private PopulationCreator populationCreator; 42 [Storable] 43 private SGAOperator sgaOperator; 44 45 private ValueParameter<IntData> PopulationSizeParameter { 46 get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; } 47 } 48 private ConstrainedValueParameter<ISelector> SelectorParameter { 49 get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; } 50 } 51 private ConstrainedValueParameter<ICrossover> CrossoverParameter { 52 get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; } 53 } 54 private OptionalConstrainedValueParameter<IManipulator> MutatorParameter { 55 get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; } 56 } 57 private ValueParameter<IntData> ElitesParameter { 58 get { return (ValueParameter<IntData>)Parameters["Elites"]; } 59 } 60 61 public override Type ProblemType { 62 get { return typeof(ISingleObjectiveProblem); } 63 } 64 public new ISingleObjectiveProblem Problem { 65 get { return (ISingleObjectiveProblem)base.Problem; } 66 set { base.Problem = value; } 67 } 68 69 public SGA() 70 : base() { 71 Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0))); 72 Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true))); 73 Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100))); 74 Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.")); 75 Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.")); 76 Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05))); 77 Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions.")); 78 Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1))); 79 Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000))); 80 81 PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged); 82 ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged); 83 84 RandomCreator randomCreator = new RandomCreator(); 85 populationCreator = new PopulationCreator(); 86 sgaOperator = new SGAOperator(); 87 88 randomCreator.RandomParameter.ActualName = "Random"; 89 randomCreator.SeedParameter.ActualName = "Seed"; 90 randomCreator.SeedParameter.Value = null; 91 randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly"; 92 randomCreator.SetSeedRandomlyParameter.Value = null; 93 randomCreator.Successor = populationCreator; 94 95 populationCreator.PopulationSizeParameter.ActualName = "PopulationSize"; 96 populationCreator.PopulationSizeParameter.Value = null; 97 populationCreator.Successor = sgaOperator; 98 99 sgaOperator.SelectorParameter.ActualName = "Selector"; 100 sgaOperator.CrossoverParameter.ActualName = "Crossover"; 101 sgaOperator.ElitesParameter.ActualName = "Elites"; 102 sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations"; 103 sgaOperator.MutatorParameter.ActualName = "Mutator"; 104 sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability"; 105 sgaOperator.RandomParameter.ActualName = "Random"; 106 sgaOperator.ResultsParameter.ActualName = "Results"; 107 108 OperatorGraph.InitialOperator = randomCreator; 109 110 if (ApplicationManager.Manager != null) { 111 var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name); 112 foreach (ISelector selector in selectors) { 113 selector.CopySelected = new BoolData(true); 114 selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 115 if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random"; 116 SelectorParameter.ValidValues.Add(selector); 117 } 118 } 119 } 120 121 public override IDeepCloneable Clone(Cloner cloner) { 122 SGA clone = (SGA)base.Clone(cloner); 123 clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator); 124 clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator); 125 return clone; 126 } 127 128 private void ElitesParameter_ValueChanged(object sender, EventArgs e) { 129 foreach (ISelector selector in SelectorParameter.ValidValues) 130 selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 131 } 132 private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) { 133 foreach (ISelector selector in SelectorParameter.ValidValues) 134 selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 135 } 136 137 protected override void DeregisterProblemEvents() { 138 Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged); 139 base.DeregisterProblemEvents(); 140 } 141 protected override void RegisterProblemEvents() { 142 base.RegisterProblemEvents(); 143 Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged); 144 } 145 146 protected override void OnProblemChanged() { 147 if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random"; 148 if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random"; 149 foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>()) 150 op.RandomParameter.ActualName = "Random"; 151 152 populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator; 153 populationCreator.EvaluatorParameter.Value = Problem.Evaluator; 154 sgaOperator.MaximizationParameter.Value = Problem.Maximization; 155 sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 156 sgaOperator.EvaluatorParameter.Value = Problem.Evaluator; 157 158 foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 159 op.MaximizationParameter.Value = Problem.Maximization; 160 op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 161 } 162 163 ICrossover oldCrossover = CrossoverParameter.Value; 164 CrossoverParameter.ValidValues.Clear(); 165 foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) 166 CrossoverParameter.ValidValues.Add(crossover); 167 if (oldCrossover != null) { 168 CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType()); 169 } 170 171 IManipulator oldMutator = MutatorParameter.Value; 172 MutatorParameter.ValidValues.Clear(); 173 foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) 174 MutatorParameter.ValidValues.Add(mutator); 175 if (oldMutator != null) { 176 MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType()); 177 } 178 179 base.OnProblemChanged(); 180 } 181 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 182 if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random"; 183 populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator; 184 base.Problem_SolutionCreatorChanged(sender, e); 185 } 186 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 187 if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random"; 188 189 foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 190 op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 191 } 192 193 populationCreator.EvaluatorParameter.Value = Problem.Evaluator; 194 sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 195 sgaOperator.EvaluatorParameter.Value = Problem.Evaluator; 196 base.Problem_EvaluatorChanged(sender, e); 197 } 198 private void Problem_MaximizationChanged(object sender, EventArgs e) { 199 sgaOperator.MaximizationParameter.Value = Problem.Maximization; 200 foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 201 op.MaximizationParameter.Value = Problem.Maximization; 202 } 203 } 40 //#region Private Members 41 //// store operator references in order to be able to access them easily after deserialization 42 //[Storable] 43 //private PopulationCreator populationCreator; 44 //[Storable] 45 //private SGAOperator sgaOperator; 46 //#endregion 47 48 //#region Problem Properties 49 //public override Type ProblemType { 50 // get { return typeof(ISingleObjectiveProblem); } 51 //} 52 //public new ISingleObjectiveProblem Problem { 53 // get { return (ISingleObjectiveProblem)base.Problem; } 54 // set { base.Problem = value; } 55 //} 56 //#endregion 57 58 //#region Parameter Properties 59 //private ValueParameter<IntData> SeedParameter { 60 // get { return (ValueParameter<IntData>)Parameters["Seed"]; } 61 //} 62 //private ValueParameter<BoolData> SetSeedRandomlyParameter { 63 // get { return (ValueParameter<BoolData>)Parameters["SetSeedRandomly"]; } 64 //} 65 //private ValueParameter<IntData> PopulationSizeParameter { 66 // get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; } 67 //} 68 //private ConstrainedValueParameter<ISelector> SelectorParameter { 69 // get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; } 70 //} 71 //private ConstrainedValueParameter<ICrossover> CrossoverParameter { 72 // get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; } 73 //} 74 //private ValueParameter<DoubleData> MutationProbabilityParameter { 75 // get { return (ValueParameter<DoubleData>)Parameters["MutationProbability"]; } 76 //} 77 //private OptionalConstrainedValueParameter<IManipulator> MutatorParameter { 78 // get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; } 79 //} 80 //private ValueParameter<IntData> ElitesParameter { 81 // get { return (ValueParameter<IntData>)Parameters["Elites"]; } 82 //} 83 //private ValueParameter<IntData> MaximumGenerationsParameter { 84 // get { return (ValueParameter<IntData>)Parameters["MaximumGenerations"]; } 85 //} 86 //#endregion 87 88 //#region Properties 89 //public IntData Seed { 90 // get { return SeedParameter.Value; } 91 // set { SeedParameter.Value = value; } 92 //} 93 //public BoolData SetSeedRandomly { 94 // get { return SetSeedRandomlyParameter.Value; } 95 // set { SetSeedRandomlyParameter.Value = value; } 96 //} 97 //public IntData PopulationSize { 98 // get { return PopulationSizeParameter.Value; } 99 // set { PopulationSizeParameter.Value = value; } 100 //} 101 //public ISelector Selector { 102 // get { return SelectorParameter.Value; } 103 // set { SelectorParameter.Value = value; } 104 //} 105 //public ICrossover Crossover { 106 // get { return CrossoverParameter.Value; } 107 // set { CrossoverParameter.Value = value; } 108 //} 109 //public DoubleData MutationProbability { 110 // get { return MutationProbabilityParameter.Value; } 111 // set { MutationProbabilityParameter.Value = value; } 112 //} 113 //public IManipulator Mutator { 114 // get { return MutatorParameter.Value; } 115 // set { MutatorParameter.Value = value; } 116 //} 117 //public IntData Elites { 118 // get { return ElitesParameter.Value; } 119 // set { ElitesParameter.Value = value; } 120 //} 121 //public IntData MaximumGenerations { 122 // get { return MaximumGenerationsParameter.Value; } 123 // set { MaximumGenerationsParameter.Value = value; } 124 //} 125 //#endregion 126 127 //#region Persistence Properties 128 //[Storable] 129 //private object RestoreEvents { 130 // get { return null; } 131 // set { RegisterEvents(); } 132 //} 133 //#endregion 134 135 //public SGA() 136 // : base() { 137 // Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0))); 138 // Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true))); 139 // Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100))); 140 // Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.")); 141 // Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.")); 142 // Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05))); 143 // Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions.")); 144 // Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1))); 145 // Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000))); 146 147 // RandomCreator randomCreator = new RandomCreator(); 148 // populationCreator = new PopulationCreator(); 149 // sgaOperator = new SGAOperator(); 150 151 // randomCreator.RandomParameter.ActualName = "Random"; 152 // randomCreator.SeedParameter.ActualName = "Seed"; 153 // randomCreator.SeedParameter.Value = null; 154 // randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly"; 155 // randomCreator.SetSeedRandomlyParameter.Value = null; 156 // randomCreator.Successor = populationCreator; 157 158 // populationCreator.PopulationSizeParameter.ActualName = "PopulationSize"; 159 // populationCreator.PopulationSizeParameter.Value = null; 160 // populationCreator.Successor = sgaOperator; 161 162 // sgaOperator.SelectorParameter.ActualName = "Selector"; 163 // sgaOperator.CrossoverParameter.ActualName = "Crossover"; 164 // sgaOperator.ElitesParameter.ActualName = "Elites"; 165 // sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations"; 166 // sgaOperator.MutatorParameter.ActualName = "Mutator"; 167 // sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability"; 168 // sgaOperator.RandomParameter.ActualName = "Random"; 169 // sgaOperator.ResultsParameter.ActualName = "Results"; 170 171 // OperatorGraph.InitialOperator = randomCreator; 172 173 // if (ApplicationManager.Manager != null) { 174 // var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name); 175 // foreach (ISelector selector in selectors) 176 // SelectorParameter.ValidValues.Add(selector); 177 // ParameterizeSelectors(); 178 // } 179 180 // RegisterEvents(); 181 //} 182 183 //public override IDeepCloneable Clone(Cloner cloner) { 184 // SGA clone = (SGA)base.Clone(cloner); 185 // clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator); 186 // clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator); 187 // clone.RegisterEvents(); 188 // return clone; 189 //} 190 191 //#region Events 192 //protected override void OnProblemChanged() { 193 // if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random"; 194 // if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random"; 195 // foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>()) 196 // op.RandomParameter.ActualName = "Random"; 197 198 // populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator; 199 // populationCreator.EvaluatorParameter.Value = Problem.Evaluator; 200 // sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 201 // sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 202 // sgaOperator.EvaluatorParameter.Value = Problem.Evaluator; 203 204 // foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 205 // op.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 206 // op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 207 // } 208 209 // ICrossover oldCrossover = CrossoverParameter.Value; 210 // CrossoverParameter.ValidValues.Clear(); 211 // foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) 212 // CrossoverParameter.ValidValues.Add(crossover); 213 // if (oldCrossover != null) { 214 // CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType()); 215 // } 216 217 // IManipulator oldMutator = MutatorParameter.Value; 218 // MutatorParameter.ValidValues.Clear(); 219 // foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) 220 // MutatorParameter.ValidValues.Add(mutator); 221 // if (oldMutator != null) { 222 // MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType()); 223 // } 224 225 // base.OnProblemChanged(); 226 //} 227 //protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 228 // if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random"; 229 // populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator; 230 // base.Problem_SolutionCreatorChanged(sender, e); 231 //} 232 //protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 233 // if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random"; 234 235 // foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 236 // op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 237 // } 238 239 // populationCreator.EvaluatorParameter.Value = Problem.Evaluator; 240 // sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 241 // sgaOperator.EvaluatorParameter.Value = Problem.Evaluator; 242 // base.Problem_EvaluatorChanged(sender, e); 243 //} 244 //private void ElitesParameter_ValueChanged(object sender, EventArgs e) { 245 // foreach (ISelector selector in SelectorParameter.ValidValues) 246 // selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 247 //} 248 //private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) { 249 // foreach (ISelector selector in SelectorParameter.ValidValues) 250 // selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 251 //} 252 //#endregion 253 254 //#region Helpers 255 //private void RegisterEvents() { 256 // PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged); 257 // ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged); 258 //} 259 //private void ParameterizeSelectors() { 260 // foreach (ISelector selector in SelectorParameter.ValidValues) { 261 // selector.CopySelected = new BoolData(true); 262 // selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)); 263 // if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random"; 264 // } 265 //} 266 //private void ParameterizePopulationCreator() { 267 // populationCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 268 // populationCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 269 //} 270 //private void ParameterizeSGAOperator() { 271 // sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 272 // sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 273 // sgaOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 274 //} 275 //private void ParameterizeSolutionCreator() { 276 //} 277 //#endregion 204 278 } 205 279 }
Note: See TracChangeset
for help on using the changeset viewer.