Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2975


Ignore:
Timestamp:
03/09/10 05:45:39 (15 years ago)
Author:
swagner
Message:

Worked on linkage between algorithms and problems (#898)

  • finished TSP and started to work on SGA
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  
    8383  <ItemGroup>
    8484    <Compile Include="HeuristicLabAlgorithmsSGAPlugin.cs" />
    85     <Compile Include="SGA.cs" />
     85    <Compile Include="SGA.cs">
     86      <SubType>Code</SubType>
     87    </Compile>
    8688    <Compile Include="SGAOperator.cs" />
    8789    <Compile Include="Properties\AssemblyInfo.cs" />
  • trunk/sources/HeuristicLab.Algorithms.SGA/3.3/SGA.cs

    r2924 r2975  
    3838  [Creatable("Algorithms")]
    3939  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
    204278  }
    205279}
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs

    r2933 r2975  
    147147        ExceptionOccurred(this, new EventArgs<Exception>(exception));
    148148    }
     149
    149150    protected virtual void DeregisterProblemEvents() {
    150151      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
    151152      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
     153      problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
    152154    }
    153155    protected virtual void RegisterProblemEvents() {
    154156      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
    155157      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
     158      problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
    156159    }
    157160
    158161    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
    159162    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
     163    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
    160164    #endregion
    161165  }
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs

    r2865 r2975  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Core;
    2425
     
    2829  /// </summary>
    2930  public interface IProblem : IParameterizedNamedItem {
     31    IParameter SolutionCreatorParameter { get; }
    3032    ISolutionCreator SolutionCreator { get; }
     33    IParameter EvaluatorParameter { get; }
    3134    IEvaluator Evaluator { get; }
    32     OperatorSet Operators { get; }
     35    IEnumerable<IOperator> Operators { get; }
    3336
    3437    event EventHandler SolutionCreatorChanged;
    3538    event EventHandler EvaluatorChanged;
     39    event EventHandler OperatorsChanged;
    3640  }
    3741}
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISingleObjectiveProblem.cs

    r2865 r2975  
    2020#endregion
    2121
    22 using System;
    23 using HeuristicLab.Data;
     22using HeuristicLab.Core;
    2423
    2524namespace HeuristicLab.Optimization {
     
    2827  /// </summary>
    2928  public interface ISingleObjectiveProblem : IProblem {
    30     BoolData Maximization { get; }
     29    IParameter MaximizationParameter { get; }
    3130    new ISingleObjectiveEvaluator Evaluator { get; }
    32 
    33     event EventHandler MaximizationChanged;
    3431  }
    3532}
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLab.Problems.TSP-3.3.csproj

    r2906 r2975  
    8484  </ItemGroup>
    8585  <ItemGroup>
     86    <Compile Include="TSPCoordinatesPathEvaluator.cs" />
     87    <Compile Include="ITSPDistanceMatrixPathEvaluator.cs" />
     88    <Compile Include="ITSPCoordinatesPathEvaluator.cs" />
     89    <Compile Include="ITSPPathEvaluator.cs" />
    8690    <Compile Include="HeuristicLabProblemsTSPPlugin.cs" />
    8791    <Compile Include="TSPEvaluator.cs" />
     
    102106      <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project>
    103107      <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>
    104112    </ProjectReference>
    105113    <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLabProblemsTSPPlugin.cs.frame

    r2906 r2975  
    2929  [PluginFile("HeuristicLab.Problems.TSP-3.3.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Collections", "3.3")]
     31  [PluginDependency("HeuristicLab.Common", "3.2")]
    3132  [PluginDependency("HeuristicLab.Common.Resources", "3.2")]
    3233  [PluginDependency("HeuristicLab.Core", "3.3")]
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs

    r2917 r2975  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Drawing;
    2425using System.Linq;
     26using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Data;
     
    3436  [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
    3537  [Creatable("Problems")]
    36   [EmptyStorableClass]
    3738  public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem {
    3839    public override Image ItemImage {
     
    4041    }
    4142
    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 {
    6276      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 {
    6587      get { return EvaluatorParameter.Value; }
    6688    }
     
    6890      get { return EvaluatorParameter.Value; }
    6991    }
     92    public DoubleData BestKnownQuality {
     93      get { return BestKnownQualityParameter.Value; }
     94      set { BestKnownQualityParameter.Value = value; }
     95    }
    7096    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
    74112
    75113    public TSP()
     
    85123
    86124      creator.PermutationParameter.ActualName = "TSPTour";
    87       creator.LengthParameter.Value = new IntData(0);
    88       evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    89       evaluator.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
    90125      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;
    109136    }
    110137
     
    112139      TSPLIBParser parser = new TSPLIBParser(filename);
    113140      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
    134145    public event EventHandler SolutionCreatorChanged;
    135146    private void OnSolutionCreatorChanged() {
     
    142153        EvaluatorChanged(this, EventArgs.Empty);
    143154    }
     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
    144224  }
    145225}
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPDistanceMatrixPathEvaluator.cs

    r2906 r2975  
    2222using HeuristicLab.Core;
    2323using HeuristicLab.Data;
    24 using HeuristicLab.Encodings.Permutation;
    2524using HeuristicLab.Parameters;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3332  [Creatable("Test")]
    3433  [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"]; }
    4137    }
    4238
     
    4440      : base() {
    4541      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."));
    4742    }
    4843
    49     public override IOperation Apply() {
     44    protected override double CalculateDistance(int a, int b) {
    5045      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];
    6047    }
    6148  }
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPPathEvaluator.cs

    r2906 r2975  
    3232  [Item("TSPPathEvaluator", "A base class for operators which evaluate TSP solutions given in path representation.")]
    3333  [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"]; }
    4037    }
    4138
    4239    protected TSPPathEvaluator()
    4340      : base() {
    44       Parameters.Add(new LookupParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities."));
    4541      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
    4642    }
    4743
    48     public override IOperation Apply() {
    49       DoubleMatrixData coordinates = CoordinatesParameter.ActualValue;
     44    public sealed override IOperation Apply() {
    5045      Permutation permutation = PermutationParameter.ActualValue;
    5146
    5247      double length = 0;
    5348      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]);
    6251      QualityParameter.ActualValue = new DoubleData(length);
    6352
     
    6655
    6756    /// <summary>
    68     /// Calculates the distance between two points.
     57    /// Calculates the distance between two cities.
    6958    /// </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>
    7461    /// <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);
    7663  }
    7764}
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSPRoundedEuclideanPathEvaluator.cs

    r2883 r2975  
    3131  [Creatable("Test")]
    3232  [EmptyStorableClass]
    33   public sealed class TSPRoundedEuclideanPathEvaluator : TSPPathEvaluator {
     33  public sealed class TSPRoundedEuclideanPathEvaluator : TSPCoordinatesPathEvaluator {
    3434    /// <summary>
    3535    /// Calculates the distance between two points using the rounded Euclidean distance metric.
Note: See TracChangeset for help on using the changeset viewer.