Changeset 1661


Ignore:
Timestamp:
04/24/09 16:09:22 (12 years ago)
Author:
dtraxing
Message:

FixedSGAMain, added support for engine abortion. (ticket #580)

Location:
trunk/sources/HeuristicLab.FixedOperators/3.2
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.FixedOperators/3.2/CreateChildrenHardWired.cs

    r1646 r1661  
    8888        while (s.SubScopes.Count > 0)
    8989          s.RemoveSubScope(s.SubScopes[0]);
    90        
     90
    9191        counter++;
    9292      } // foreach
  • trunk/sources/HeuristicLab.FixedOperators/3.2/CreateReplacement.cs

    r1646 r1661  
    4242    Sorter sorter;
    4343
    44     IRandom random;
    45     DoubleData probability;
    46 
    4744    public override string Description {
    4845      get { return @"Implements the control structures of CreateReplacement hard wired. Operators are delegated."; }
     
    6360      ls.GetVariableInfo("Selected").ActualName = "Elites";
    6461      rs.GetVariableInfo("Selected").ActualName = "Elites";
    65 
    66       // variables infos
    67       //AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
    68       //AddVariableInfo(new VariableInfo("Elites", "Number of selected sub-scopes", typeof(IntData), VariableKind.In));
    6962    }
    7063
     
    7366      ls.Execute(scope.SubScopes[0]);
    7467      rr.Execute(scope.SubScopes[0]);
    75      
     68
    7669      rs.Execute(scope.SubScopes[1]);
    7770      lr.Execute(scope.SubScopes[1]);
  • trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs

    r1646 r1661  
    3232using HeuristicLab.Logging;
    3333using System.Diagnostics;
     34using HeuristicLab.Selection;
    3435
    3536namespace HeuristicLab.FixedOperators {
    3637  class FixedSGAMain : CombinedOperator {
    3738    public override string Description {
    38       get { return @"Implements the SGAMain with hardwired control structures. Operators are delegated."; }
    39     }
    40 
     39      get { return @"Implements the functionality of SGAMain with fixed control structures. Operators like selection, crossover, mutation and evaluation are delegated."; }
     40    }
     41
     42    // Shared
     43    Sorter sorter;
     44
     45    // CreateChildren
     46    Counter counter;
     47    IRandom random;
     48    DoubleData probability;
    4149    ChildrenInitializer ci;
    4250    OperatorBase crossover;
     
    4452    OperatorBase evaluator;
    4553    SubScopesRemover sr;
    46     Counter counter;
    47     Sorter sorter;
    48     IRandom random;
    49     DoubleData probability;
     54
     55    // CreateReplacement
     56    LeftSelector ls;
     57    RightReducer rr;
     58    RightSelector rs;
     59    LeftReducer lr;
     60    MergingReducer mr;
     61
    5062
    5163    long[] timesExecuteCreateChildren;
     
    5769      AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));
    5870      Name = "FixedSGAMain";
    59       //InitCreateChildrenHWCS();
    60       InitCreateChildrenHW();
    61 
    62     }
    63 
    64     private void InitCreateChildrenHW() {
     71
     72      sorter = new Sorter();
     73      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
     74      sorter.GetVariableInfo("Value").ActualName = "Quality";
     75
     76      InitVariablesForCreateChildren();
     77      InitVariablesForCreateReplacement();
     78    }
     79
     80    private void InitVariablesForCreateReplacement() {
     81      ls = new LeftSelector();
     82      rr = new RightReducer();
     83      rs = new RightSelector();
     84      lr = new LeftReducer();
     85      mr = new MergingReducer();
     86
     87      ls.GetVariableInfo("Selected").ActualName = "Elites";
     88      rs.GetVariableInfo("Selected").ActualName = "Elites";
     89
     90    }
     91
     92    protected void InitVariablesForCreateChildren() {
     93      // variables for create children
     94      ci = new ChildrenInitializer();
     95
    6596      // variables infos
    6697      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
     
    69100      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
    70101      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
    71       AddVariableInfo(new VariableInfo("EvaluatedSolutions", "Number of evaluated solutions", typeof(IntData), VariableKind.In | VariableKind.Out));
    72       AddVariableInfo(new VariableInfo("Maximization", "Sort in descending order", typeof(BoolData), VariableKind.In));
    73       AddVariableInfo(new VariableInfo("Quality", "Sorting value", typeof(DoubleData), VariableKind.In));
    74     }
    75 
    76     private void InitCreateChildrenHWCS() {
    77       // variables for create children
    78       ci = new ChildrenInitializer();
    79 
    80       // variables infos
    81       AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
    82       AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
    83       AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
    84       AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
    85       AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
    86102
    87103      sr = new SubScopesRemover();
     
    90106      counter = new Counter();
    91107      counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
    92 
    93       sorter = new Sorter();
    94       sorter.GetVariableInfo("Descending").ActualName = "Maximization";
    95       sorter.GetVariableInfo("Value").ActualName = "Quality";
    96108    }
    97109
     
    107119
    108120      OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
    109       OperatorBase createChildren = new CreateChildren();
    110       OperatorBase createReplacement = new CreateReplacement();
    111121      QualityLogger ql = new QualityLogger();
    112122
     
    122132      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
    123133
    124       Counter c = new Counter();
    125       c.GetVariableInfo("Value").ActualName = "Generations";
    126 
    127134      LessThanComparator ltc = new LessThanComparator();
    128135      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
     
    134141
    135142      InitializeExecuteCreateChildren(scope);
    136       IntData evaluatedSolutions = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);
    137143      Stopwatch watch = new Stopwatch();
    138144      long[] times = new long[10];
    139145      timesExecuteCreateChildren = new long[10];
    140       for (int i = 0; i < maxGenerations.Data; i++) {
    141           watch.Start();
     146      for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
     147        watch.Start();
    142148        selector.Execute(scope);
    143149        watch.Stop();
     
    145151        watch.Reset();
    146152        watch.Start();
    147         // if uncomment, adapt constructor
    148         //createChildren.Execute(scope.SubScopes[1]);
    149         //ExecuteCreateChildrenHWCS(scope.SubScopes[1]);
    150         ExecuteCreateChildrenHW(scope.SubScopes[1], evaluatedSolutions);
     153        ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
    151154        watch.Stop();
    152155        times[1] += watch.ElapsedTicks;
    153156        watch.Reset();
    154157        watch.Start();
    155         createReplacement.Execute(scope);
     158        ExecuteCreateReplacementWithFixedConstrolStructures(scope);
    156159        watch.Stop();
    157160        times[2] += watch.ElapsedTicks;
     
    183186      swApply.Stop();
    184187      Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
     188
     189      if (Canceled)
     190        return new AtomicOperation(this, scope);
     191
    185192      return null;
    186193    } // Apply
     194
     195 
    187196
    188197    /// <summary>
     
    201210    }
    202211
    203     private void ExecuteCreateChildrenHWCS(IScope scope) {
     212    protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
    204213      // ChildrenInitializer
    205214      ci.Apply(scope);
     
    218227    } // ExecuteCreateChildrenHWCS
    219228
    220     private void ExecuteCreateChildrenHW(IScope scope, IntData evaluatedSolutions){
    221       //Stopwatch watch = new Stopwatch();
    222       //long[] times = new long[10];
    223       // ChildrenInitializer
    224       //watch.Start();
    225       ci.Apply(scope);
    226       //watch.Stop();
    227       //timesExecuteCreateChildren[0] += watch.ElapsedTicks;
    228       //watch.Reset();
    229       // UniformSequentialSubScopesProcessor
    230       foreach (IScope s in scope.SubScopes) {
    231         //watch.Start();
    232         if (crossover.Execute(s) != null)
    233           throw new InvalidOperationException("ERROR: no support for combined operators!");
    234         //watch.Stop();
    235         //timesExecuteCreateChildren[1] += watch.ElapsedTicks;
    236         //watch.Reset();
    237 
    238 
    239         // Stochastic Branch
    240         if (random.NextDouble() < probability.Data) {
    241           //watch.Start();
    242           if (mutator.Execute(s) != null)
    243             throw new InvalidOperationException("ERROR: no support for combined operators!");
    244           //watch.Stop();
    245           //timesExecuteCreateChildren[2] += watch.ElapsedTicks;
    246           //watch.Reset();
    247 
    248         }
    249 
    250         //watch.Start();
    251         if (evaluator.Execute(s) != null)
    252           throw new InvalidOperationException("ERROR: no support for combined operators!");
    253         //watch.Stop();
    254         //timesExecuteCreateChildren[3] += watch.ElapsedTicks;
    255         //watch.Reset();
    256 
    257         // subscopes remover
    258         //watch.Start();
    259         while (s.SubScopes.Count > 0)
    260           s.RemoveSubScope(s.SubScopes[0]);
    261         //watch.Stop();
    262         //timesExecuteCreateChildren[4] += watch.ElapsedTicks;
    263         //watch.Reset();
    264 
    265         evaluatedSolutions.Data++;
    266       } // foreach
    267 
    268       // sort scopes
    269       bool descending = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    270 
    271       double[] keys = new double[scope.SubScopes.Count];
    272       int[] sequence = new int[keys.Length];
    273 
    274       for (int i = 0; i < keys.Length; i++) {
    275         keys[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
    276         sequence[i] = i;
    277       }
    278 
    279       Array.Sort<double, int>(keys, sequence);
    280 
    281       if (descending) {
    282         int temp;
    283         for (int i = 0; i < sequence.Length / 2; i++) {
    284           temp = sequence[i];
    285           sequence[i] = sequence[sequence.Length - 1 - i];
    286           sequence[sequence.Length - 1 - i] = temp;
    287         }
    288       }
    289       scope.ReorderSubScopes(sequence);
    290       //watch.Stop();
    291       //timesExecuteCreateChildren[5] += watch.ElapsedTicks;
    292       //watch.Reset();
    293 
    294       return;
    295     } // ExecuteCreateChildrenHW
    296 
     229    private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
     230      // SequentialSubScopesProcessor
     231      ls.Execute(scope.SubScopes[0]);
     232      rr.Execute(scope.SubScopes[0]);
     233
     234      rs.Execute(scope.SubScopes[1]);
     235      lr.Execute(scope.SubScopes[1]);
     236
     237      mr.Execute(scope);
     238      sorter.Execute(scope);
     239    } // ExecuteCreateReplacementWithFixedConstrolStructures
    297240  } // class FixedSGAMain
    298241} // namespace HeuristicLab.FixedOperators
  • trunk/sources/HeuristicLab.FixedOperators/3.2/HeuristicLab.FixedOperators-3.2.csproj

    r1646 r1661  
    6868  </ItemGroup>
    6969  <ItemGroup>
     70    <Compile Include="FixedAllSGAMain.cs" />
    7071    <Compile Include="CreateReplacement.cs" />
    7172    <Compile Include="CreateChildrenHardWired.cs" />
Note: See TracChangeset for help on using the changeset viewer.