- Timestamp:
- 03/09/10 05:45:39 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 added
- 11 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 } -
trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs
r2933 r2975 147 147 ExceptionOccurred(this, new EventArgs<Exception>(exception)); 148 148 } 149 149 150 protected virtual void DeregisterProblemEvents() { 150 151 problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged); 151 152 problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged); 153 problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged); 152 154 } 153 155 protected virtual void RegisterProblemEvents() { 154 156 problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged); 155 157 problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged); 158 problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged); 156 159 } 157 160 158 161 protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { } 159 162 protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { } 163 protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { } 160 164 #endregion 161 165 } -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs
r2865 r2975 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; 24 25 … … 28 29 /// </summary> 29 30 public interface IProblem : IParameterizedNamedItem { 31 IParameter SolutionCreatorParameter { get; } 30 32 ISolutionCreator SolutionCreator { get; } 33 IParameter EvaluatorParameter { get; } 31 34 IEvaluator Evaluator { get; } 32 OperatorSetOperators { get; }35 IEnumerable<IOperator> Operators { get; } 33 36 34 37 event EventHandler SolutionCreatorChanged; 35 38 event EventHandler EvaluatorChanged; 39 event EventHandler OperatorsChanged; 36 40 } 37 41 } -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISingleObjectiveProblem.cs
r2865 r2975 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Data; 22 using HeuristicLab.Core; 24 23 25 24 namespace HeuristicLab.Optimization { … … 28 27 /// </summary> 29 28 public interface ISingleObjectiveProblem : IProblem { 30 BoolData Maximization{ get; }29 IParameter MaximizationParameter { get; } 31 30 new ISingleObjectiveEvaluator Evaluator { get; } 32 33 event EventHandler MaximizationChanged;34 31 } 35 32 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLab.Problems.TSP-3.3.csproj
r2906 r2975 84 84 </ItemGroup> 85 85 <ItemGroup> 86 <Compile Include="TSPCoordinatesPathEvaluator.cs" /> 87 <Compile Include="ITSPDistanceMatrixPathEvaluator.cs" /> 88 <Compile Include="ITSPCoordinatesPathEvaluator.cs" /> 89 <Compile Include="ITSPPathEvaluator.cs" /> 86 90 <Compile Include="HeuristicLabProblemsTSPPlugin.cs" /> 87 91 <Compile Include="TSPEvaluator.cs" /> … … 102 106 <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project> 103 107 <Name>HeuristicLab.Common.Resources-3.2</Name> 108 </ProjectReference> 109 <ProjectReference Include="..\..\HeuristicLab.Common\3.2\HeuristicLab.Common-3.2.csproj"> 110 <Project>{1FC004FC-59AF-4249-B1B6-FF25873A20E4}</Project> 111 <Name>HeuristicLab.Common-3.2</Name> 104 112 </ProjectReference> 105 113 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> -
trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLabProblemsTSPPlugin.cs.frame
r2906 r2975 29 29 [PluginFile("HeuristicLab.Problems.TSP-3.3.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Collections", "3.3")] 31 [PluginDependency("HeuristicLab.Common", "3.2")] 31 32 [PluginDependency("HeuristicLab.Common.Resources", "3.2")] 32 33 [PluginDependency("HeuristicLab.Core", "3.3")] -
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs
r2917 r2975 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Drawing; 24 25 using System.Linq; 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Data; … … 34 36 [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")] 35 37 [Creatable("Problems")] 36 [EmptyStorableClass]37 38 public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem { 38 39 public override Image ItemImage { … … 40 41 } 41 42 42 private IValueParameter<BoolData> MaximizationParameter { 43 get { return (IValueParameter<BoolData>)Parameters["Maximization"]; } 44 } 45 public IValueParameter<DoubleMatrixData> CoordinatesParameter { 46 get { return (IValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; } 47 } 48 public IValueParameter<DoubleData> BestKnownQualityParameter { 49 get { return (IValueParameter<DoubleData>)Parameters["BestKnownQuality"]; } 50 } 51 public IValueParameter<IPermutationCreator> SolutionCreatorParameter { 52 get { return (IValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; } 53 } 54 public IValueParameter<ITSPEvaluator> EvaluatorParameter { 55 get { return (IValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; } 56 } 57 58 public BoolData Maximization { 59 get { return MaximizationParameter.Value; } 60 } 61 public ISolutionCreator SolutionCreator { 43 #region Parameter Properties 44 public ValueParameter<BoolData> MaximizationParameter { 45 get { return (ValueParameter<BoolData>)Parameters["Maximization"]; } 46 } 47 IParameter ISingleObjectiveProblem.MaximizationParameter { 48 get { return MaximizationParameter; } 49 } 50 public ValueParameter<DoubleMatrixData> CoordinatesParameter { 51 get { return (ValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; } 52 } 53 public ValueParameter<IPermutationCreator> SolutionCreatorParameter { 54 get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; } 55 } 56 IParameter IProblem.SolutionCreatorParameter { 57 get { return SolutionCreatorParameter; } 58 } 59 public ValueParameter<ITSPEvaluator> EvaluatorParameter { 60 get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; } 61 } 62 IParameter IProblem.EvaluatorParameter { 63 get { return EvaluatorParameter; } 64 } 65 public OptionalValueParameter<DoubleData> BestKnownQualityParameter { 66 get { return (OptionalValueParameter<DoubleData>)Parameters["BestKnownQuality"]; } 67 } 68 #endregion 69 70 #region Properties 71 public DoubleMatrixData Coordinates { 72 get { return CoordinatesParameter.Value; } 73 set { CoordinatesParameter.Value = value; } 74 } 75 public IPermutationCreator SolutionCreator { 62 76 get { return SolutionCreatorParameter.Value; } 63 } 64 public ISingleObjectiveEvaluator Evaluator { 77 set { SolutionCreatorParameter.Value = value; } 78 } 79 ISolutionCreator IProblem.SolutionCreator { 80 get { return SolutionCreatorParameter.Value; } 81 } 82 public ITSPEvaluator Evaluator { 83 get { return EvaluatorParameter.Value; } 84 set { EvaluatorParameter.Value = value; } 85 } 86 ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator { 65 87 get { return EvaluatorParameter.Value; } 66 88 } … … 68 90 get { return EvaluatorParameter.Value; } 69 91 } 92 public DoubleData BestKnownQuality { 93 get { return BestKnownQualityParameter.Value; } 94 set { BestKnownQualityParameter.Value = value; } 95 } 70 96 private OperatorSet operators; 71 public OperatorSet Operators { 72 get { return operators; } 73 } 97 public IEnumerable<IOperator> Operators { 98 get { 99 if (operators == null) InitializeOperators(); 100 return operators; 101 } 102 } 103 #endregion 104 105 #region Persistence Properties 106 [Storable] 107 private object RestoreEvents { 108 get { return null; } 109 set { RegisterEvents(); } 110 } 111 #endregion 74 112 75 113 public TSP() … … 85 123 86 124 creator.PermutationParameter.ActualName = "TSPTour"; 87 creator.LengthParameter.Value = new IntData(0);88 evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;89 evaluator.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;90 125 evaluator.QualityParameter.ActualName = "TSPTourLength"; 91 92 MaximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged); 93 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 94 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 95 96 operators = new OperatorSet(); 97 if (ApplicationManager.Manager != null) { 98 var ops = ApplicationManager.Manager.GetInstances<IPermutationOperator>(); 99 foreach (IPermutationCrossover op in ops.OfType<IPermutationCrossover>()) { 100 op.ParentsParameter.ActualName = creator.PermutationParameter.ActualName; 101 op.ChildParameter.ActualName = creator.PermutationParameter.ActualName; 102 } 103 foreach (IPermutationManipulator op in ops.OfType<IPermutationManipulator>()) { 104 op.PermutationParameter.ActualName = creator.PermutationParameter.ActualName; 105 } 106 foreach (IPermutationOperator op in ops) 107 operators.Add(op); 108 } 126 ParameterizeSolutionCreator(); 127 ParameterizeEvaluator(); 128 129 RegisterEvents(); 130 } 131 132 public override IDeepCloneable Clone(Cloner cloner) { 133 TSP clone = (TSP)base.Clone(cloner); 134 clone.RegisterEvents(); 135 return clone; 109 136 } 110 137 … … 112 139 TSPLIBParser parser = new TSPLIBParser(filename); 113 140 parser.Parse(); 114 CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices); 115 int cities = CoordinatesParameter.Value.Rows; 116 SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities); 117 } 118 119 private void MaximizationParameter_ValueChanged(object sender, EventArgs e) { 120 OnMaximizationChanged(); 121 } 122 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 123 OnSolutionCreatorChanged(); 124 } 125 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 126 OnEvaluatorChanged(); 127 } 128 129 public event EventHandler MaximizationChanged; 130 private void OnMaximizationChanged() { 131 if (MaximizationChanged != null) 132 MaximizationChanged(this, EventArgs.Empty); 133 } 141 Coordinates = new DoubleMatrixData(parser.Vertices); 142 } 143 144 #region Events 134 145 public event EventHandler SolutionCreatorChanged; 135 146 private void OnSolutionCreatorChanged() { … … 142 153 EvaluatorChanged(this, EventArgs.Empty); 143 154 } 155 public event EventHandler OperatorsChanged; 156 private void OnOperatorsChanged() { 157 if (OperatorsChanged != null) 158 OperatorsChanged(this, EventArgs.Empty); 159 } 160 161 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) { 162 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 163 Coordinates.Reset += new EventHandler(Coordinates_Reset); 164 ParameterizeSolutionCreator(); 165 } 166 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { 167 } 168 private void Coordinates_Reset(object sender, EventArgs e) { 169 ParameterizeSolutionCreator(); 170 } 171 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 172 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 173 ParameterizeSolutionCreator(); 174 ParameterizeEvaluator(); 175 ParameterizeOperators(); 176 OnSolutionCreatorChanged(); 177 } 178 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) { 179 ParameterizeEvaluator(); 180 ParameterizeOperators(); 181 } 182 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 183 ParameterizeEvaluator(); 184 OnEvaluatorChanged(); 185 } 186 #endregion 187 188 #region Helpers 189 private void RegisterEvents() { 190 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 191 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 192 Coordinates.Reset += new EventHandler(Coordinates_Reset); 193 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 194 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 195 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 196 } 197 private void ParameterizeSolutionCreator() { 198 SolutionCreator.LengthParameter.Value = new IntData(Coordinates.Rows); 199 } 200 private void ParameterizeEvaluator() { 201 if (Evaluator is ITSPPathEvaluator) 202 ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 203 if (Evaluator is ITSPCoordinatesPathEvaluator) 204 ((ITSPCoordinatesPathEvaluator)Evaluator).CoordinatesParameter.ActualName = CoordinatesParameter.Name; 205 } 206 private void InitializeOperators() { 207 operators = new OperatorSet(); 208 if (ApplicationManager.Manager != null) { 209 foreach (IPermutationOperator op in ApplicationManager.Manager.GetInstances<IPermutationOperator>()) 210 operators.Add(op); 211 ParameterizeOperators(); 212 } 213 } 214 private void ParameterizeOperators() { 215 foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) { 216 op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 217 op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 218 } 219 foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) { 220 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 221 } 222 } 223 #endregion 144 224 } 145 225 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPDistanceMatrixPathEvaluator.cs
r2906 r2975 22 22 using HeuristicLab.Core; 23 23 using HeuristicLab.Data; 24 using HeuristicLab.Encodings.Permutation;25 24 using HeuristicLab.Parameters; 26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 33 32 [Creatable("Test")] 34 33 [EmptyStorableClass] 35 public sealed class TSPDistanceMatrixPathEvaluator : TSPEvaluator { 36 public LookupParameter<DoubleMatrixData> DistanceMatrixParameter { 37 get { return (LookupParameter<DoubleMatrixData>)Parameters["DistanceMatrix"]; } 38 } 39 public LookupParameter<Permutation> PermutationParameter { 40 get { return (LookupParameter<Permutation>)Parameters["Permutation"]; } 34 public sealed class TSPDistanceMatrixPathEvaluator : TSPPathEvaluator, ITSPDistanceMatrixPathEvaluator { 35 public ILookupParameter<DoubleMatrixData> DistanceMatrixParameter { 36 get { return (ILookupParameter<DoubleMatrixData>)Parameters["DistanceMatrix"]; } 41 37 } 42 38 … … 44 40 : base() { 45 41 Parameters.Add(new LookupParameter<DoubleMatrixData>("DistanceMatrix", "The distance matrix of the cities.")); 46 Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));47 42 } 48 43 49 p ublic override IOperation Apply() {44 protected override double CalculateDistance(int a, int b) { 50 45 DoubleMatrixData distanceMatrix = DistanceMatrixParameter.ActualValue; 51 Permutation permutation = PermutationParameter.ActualValue; 52 53 double length = 0; 54 for (int i = 0; i < permutation.Length - 1; i++) 55 length += distanceMatrix[permutation[i], permutation[i + 1]]; 56 length += distanceMatrix[permutation[permutation.Length - 1], permutation[0]]; 57 QualityParameter.ActualValue = new DoubleData(length); 58 59 return base.Apply(); 46 return distanceMatrix[a, b]; 60 47 } 61 48 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPPathEvaluator.cs
r2906 r2975 32 32 [Item("TSPPathEvaluator", "A base class for operators which evaluate TSP solutions given in path representation.")] 33 33 [EmptyStorableClass] 34 public abstract class TSPPathEvaluator : TSPEvaluator { 35 public LookupParameter<DoubleMatrixData> CoordinatesParameter { 36 get { return (LookupParameter<DoubleMatrixData>)Parameters["Coordinates"]; } 37 } 38 public LookupParameter<Permutation> PermutationParameter { 39 get { return (LookupParameter<Permutation>)Parameters["Permutation"]; } 34 public abstract class TSPPathEvaluator : TSPEvaluator, ITSPPathEvaluator { 35 public ILookupParameter<Permutation> PermutationParameter { 36 get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; } 40 37 } 41 38 42 39 protected TSPPathEvaluator() 43 40 : base() { 44 Parameters.Add(new LookupParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities."));45 41 Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated.")); 46 42 } 47 43 48 public override IOperation Apply() { 49 DoubleMatrixData coordinates = CoordinatesParameter.ActualValue; 44 public sealed override IOperation Apply() { 50 45 Permutation permutation = PermutationParameter.ActualValue; 51 46 52 47 double length = 0; 53 48 for (int i = 0; i < permutation.Length - 1; i++) 54 length += CalculateDistance(coordinates[permutation[i], 0], 55 coordinates[permutation[i], 1], 56 coordinates[permutation[i + 1], 0], 57 coordinates[permutation[i + 1], 1]); 58 length += CalculateDistance(coordinates[permutation[permutation.Length - 1], 0], 59 coordinates[permutation[permutation.Length - 1], 1], 60 coordinates[permutation[0], 0], 61 coordinates[permutation[0], 1]); 49 length += CalculateDistance(permutation[i], permutation[i + 1]); 50 length += CalculateDistance(permutation[permutation.Length - 1], permutation[0]); 62 51 QualityParameter.ActualValue = new DoubleData(length); 63 52 … … 66 55 67 56 /// <summary> 68 /// Calculates the distance between two points.57 /// Calculates the distance between two cities. 69 58 /// </summary> 70 /// <param name="x1">The x-coordinate of point 1.</param> 71 /// <param name="y1">The y-coordinate of point 1.</param> 72 /// <param name="x2">The x-coordinate of point 2.</param> 73 /// <param name="y2">The y-coordinate of point 2.</param> 59 /// <param name="a">The index of the first city.</param> 60 /// <param name="b">The index of the second city.</param> 74 61 /// <returns>The calculated distance.</returns> 75 protected abstract double CalculateDistance( double x1, double y1, double x2, double y2);62 protected abstract double CalculateDistance(int a, int b); 76 63 } 77 64 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPRoundedEuclideanPathEvaluator.cs
r2883 r2975 31 31 [Creatable("Test")] 32 32 [EmptyStorableClass] 33 public sealed class TSPRoundedEuclideanPathEvaluator : TSP PathEvaluator {33 public sealed class TSPRoundedEuclideanPathEvaluator : TSPCoordinatesPathEvaluator { 34 34 /// <summary> 35 35 /// Calculates the distance between two points using the rounded Euclidean distance metric.
Note: See TracChangeset
for help on using the changeset viewer.