Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1610


Ignore:
Timestamp:
04/20/09 11:39:15 (16 years ago)
Author:
dtraxing
Message:

added implementation of CreateChildren operator to SGAMainWithHWControllStructures. (ticket #580)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SGA.Hardwired/3.2/SGAMainWithHWControllStructures.cs

    r1576 r1610  
    3131using HeuristicLab.Routing.TSP;
    3232using HeuristicLab.Logging;
     33using System.Diagnostics;
    3334
    3435namespace HeuristicLab.SGA.Hardwired {
     
    3839    }
    3940
     41    ChildrenInitializer ci;
     42    OperatorBase crossover;
     43    OperatorBase mutator;
     44    OperatorBase evaluator;
     45    SubScopesRemover sr;
     46    Counter counter;
     47    Sorter sorter;
     48    IRandom random;
     49    DoubleData probability;
     50
    4051    public SGAMainWithHWControllStructures()
    4152      : base() {
    4253      AddVariableInfo(new VariableInfo("Selector", "Selection strategy for SGA", typeof(OperatorBase), VariableKind.In));
    4354      AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In));
    44 
     55      AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));
    4556      Name = "SGAMainWithHWControllStructures";
     57
     58      //InitCreateChildrenHWCS();
     59      InitCreateChildrenHW();
     60
     61    }
     62
     63    private void InitCreateChildrenHW() {
     64      // variables infos
     65      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
     66      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
     67      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
     68      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
     69      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
     70      AddVariableInfo(new VariableInfo("EvaluatedSolutions", "Number of evaluated solutions", typeof(IntData), VariableKind.In | VariableKind.Out));
     71      AddVariableInfo(new VariableInfo("Maximization", "Sort in descending order", typeof(BoolData), VariableKind.In));
     72      AddVariableInfo(new VariableInfo("Quality", "Sorting value", typeof(DoubleData), VariableKind.In));
     73    }
     74
     75    private void InitCreateChildrenHWCS() {
     76      // variables for create children
     77      ci = new ChildrenInitializer();
     78
     79      // variables infos
     80      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
     81      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
     82      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
     83      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
     84      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
     85
     86      sr = new SubScopesRemover();
     87      sr.GetVariableInfo("SubScopeIndex").Local = true;
     88
     89      counter = new Counter();
     90      counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
     91
     92      sorter = new Sorter();
     93      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
     94      sorter.GetVariableInfo("Value").ActualName = "Quality";
    4695    }
    4796
     
    80129
    81130      IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
    82       //IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
    83 
     131      IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
     132
     133      InitializeExecuteCreateChildren(scope);
     134      IntData evaluatedSolutions = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);
     135      Stopwatch watch = new Stopwatch();
    84136      for (int i = 0; i < maxGenerations.Data; i++) {
    85137        selector.Execute(scope);
    86         createChildren.Execute(scope.SubScopes[1]);
     138        //createChildren.Execute(scope.SubScopes[1]);
     139        //ExecuteCreateChildrenHWCS(scope.SubScopes[1]);
     140        ExecuteCreateChildrenHW(scope.SubScopes[1], evaluatedSolutions);
    87141        createReplacement.Execute(scope);
    88142        ql.Execute(scope);
     
    90144        dc.Execute(scope);
    91145        lci.Execute(scope);
    92         //c.Execute(scope);
    93         //ltc.Execute(scope);
    94         //nrOfGenerations.Data = i;
     146        nrOfGenerations.Data++;
    95147      }
    96148
     
    98150      return null;
    99151    } // Apply
     152
     153    private void InitializeExecuteCreateChildren(IScope scope) {
     154      crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
     155      mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
     156      evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
     157
     158      random = GetVariableValue<IRandom>("Random", scope, true);
     159      probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
     160
     161      ci = new ChildrenInitializer();
     162    }
     163
     164    private void ExecuteCreateChildrenHWCS(IScope scope) {
     165      // ChildrenInitializer
     166      ci.Apply(scope);
     167      // UniformSequentialSubScopesProcessor
     168      foreach (IScope s in scope.SubScopes) {
     169        crossover.Execute(s);
     170        // Stochastic Branch
     171        if (random.NextDouble() < probability.Data)
     172          mutator.Execute(s);
     173        evaluator.Execute(s);
     174        sr.Execute(s);
     175        counter.Execute(s);
     176      } // foreach
     177
     178      sorter.Execute(scope);
     179    } // ExecuteCreateChildrenHWCS
     180
     181    private void ExecuteCreateChildrenHW(IScope scope, IntData evaluatedSolutions){
     182      // ChildrenInitializer
     183      ci.Apply(scope);
     184      // UniformSequentialSubScopesProcessor
     185      foreach (IScope s in scope.SubScopes) {
     186        if (crossover.Execute(s) != null)
     187          throw new InvalidOperationException("ERROR: no support for combined operators!");
     188
     189        // Stochastic Branch
     190        if (random.NextDouble() < probability.Data) {
     191          if (mutator.Execute(s) != null)
     192            throw new InvalidOperationException("ERROR: no support for combined operators!");
     193        }
     194
     195        if (evaluator.Execute(s) != null)
     196          throw new InvalidOperationException("ERROR: no support for combined operators!");
     197
     198        // subscopes remover
     199        while (s.SubScopes.Count > 0)
     200          s.RemoveSubScope(s.SubScopes[0]);
     201
     202        evaluatedSolutions.Data++;
     203      } // foreach
     204
     205      // sort scopes
     206      //bool descending = GetVariableValue<BoolData>("Maximization", scope, true).Data;
     207      //double[] keys = new double[scope.SubScopes.Count];
     208      //int[] sequence = new int[keys.Length];
     209
     210      //for (int i = 0; i < keys.Length; i++) {
     211      //  keys[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
     212      //  sequence[i] = i;
     213      //}
     214
     215      //Array.Sort<double, int>(keys, sequence);
     216
     217      //if (descending) {
     218      //  int temp;
     219      //  for (int i = 0; i < sequence.Length / 2; i++) {
     220      //    temp = sequence[i];
     221      //    sequence[i] = sequence[sequence.Length - 1 - i];
     222      //    sequence[sequence.Length - 1 - i] = temp;
     223      //  }
     224      //}
     225      //scope.ReorderSubScopes(sequence);
     226
     227      return;
     228    }
     229
    100230  } // class SGAMainWithHWControllStructures
    101231} // namespace HeuristicLab.SGA.Hardwired
Note: See TracChangeset for help on using the changeset viewer.