Changeset 1661 for trunk/sources/HeuristicLab.FixedOperators
- Timestamp:
- 04/24/09 16:09:22 (16 years ago)
- 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 88 88 while (s.SubScopes.Count > 0) 89 89 s.RemoveSubScope(s.SubScopes[0]); 90 90 91 91 counter++; 92 92 } // foreach -
trunk/sources/HeuristicLab.FixedOperators/3.2/CreateReplacement.cs
r1646 r1661 42 42 Sorter sorter; 43 43 44 IRandom random;45 DoubleData probability;46 47 44 public override string Description { 48 45 get { return @"Implements the control structures of CreateReplacement hard wired. Operators are delegated."; } … … 63 60 ls.GetVariableInfo("Selected").ActualName = "Elites"; 64 61 rs.GetVariableInfo("Selected").ActualName = "Elites"; 65 66 // variables infos67 //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));69 62 } 70 63 … … 73 66 ls.Execute(scope.SubScopes[0]); 74 67 rr.Execute(scope.SubScopes[0]); 75 68 76 69 rs.Execute(scope.SubScopes[1]); 77 70 lr.Execute(scope.SubScopes[1]); -
trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs
r1646 r1661 32 32 using HeuristicLab.Logging; 33 33 using System.Diagnostics; 34 using HeuristicLab.Selection; 34 35 35 36 namespace HeuristicLab.FixedOperators { 36 37 class FixedSGAMain : CombinedOperator { 37 38 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; 41 49 ChildrenInitializer ci; 42 50 OperatorBase crossover; … … 44 52 OperatorBase evaluator; 45 53 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 50 62 51 63 long[] timesExecuteCreateChildren; … … 57 69 AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out)); 58 70 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 65 96 // variables infos 66 97 AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In)); … … 69 100 AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In)); 70 101 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 children78 ci = new ChildrenInitializer();79 80 // variables infos81 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));86 102 87 103 sr = new SubScopesRemover(); … … 90 106 counter = new Counter(); 91 107 counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions"; 92 93 sorter = new Sorter();94 sorter.GetVariableInfo("Descending").ActualName = "Maximization";95 sorter.GetVariableInfo("Value").ActualName = "Quality";96 108 } 97 109 … … 107 119 108 120 OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true); 109 OperatorBase createChildren = new CreateChildren();110 OperatorBase createReplacement = new CreateReplacement();111 121 QualityLogger ql = new QualityLogger(); 112 122 … … 122 132 lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3; 123 133 124 Counter c = new Counter();125 c.GetVariableInfo("Value").ActualName = "Generations";126 127 134 LessThanComparator ltc = new LessThanComparator(); 128 135 ltc.GetVariableInfo("LeftSide").ActualName = "Generations"; … … 134 141 135 142 InitializeExecuteCreateChildren(scope); 136 IntData evaluatedSolutions = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);137 143 Stopwatch watch = new Stopwatch(); 138 144 long[] times = new long[10]; 139 145 timesExecuteCreateChildren = new long[10]; 140 for (int i = 0; i < maxGenerations.Data; i++) {141 146 for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) { 147 watch.Start(); 142 148 selector.Execute(scope); 143 149 watch.Stop(); … … 145 151 watch.Reset(); 146 152 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]); 151 154 watch.Stop(); 152 155 times[1] += watch.ElapsedTicks; 153 156 watch.Reset(); 154 157 watch.Start(); 155 createReplacement.Execute(scope);158 ExecuteCreateReplacementWithFixedConstrolStructures(scope); 156 159 watch.Stop(); 157 160 times[2] += watch.ElapsedTicks; … … 183 186 swApply.Stop(); 184 187 Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed); 188 189 if (Canceled) 190 return new AtomicOperation(this, scope); 191 185 192 return null; 186 193 } // Apply 194 195 187 196 188 197 /// <summary> … … 201 210 } 202 211 203 pr ivate void ExecuteCreateChildrenHWCS(IScope scope) {212 protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) { 204 213 // ChildrenInitializer 205 214 ci.Apply(scope); … … 218 227 } // ExecuteCreateChildrenHWCS 219 228 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 297 240 } // class FixedSGAMain 298 241 } // namespace HeuristicLab.FixedOperators -
trunk/sources/HeuristicLab.FixedOperators/3.2/HeuristicLab.FixedOperators-3.2.csproj
r1646 r1661 68 68 </ItemGroup> 69 69 <ItemGroup> 70 <Compile Include="FixedAllSGAMain.cs" /> 70 71 <Compile Include="CreateReplacement.cs" /> 71 72 <Compile Include="CreateChildrenHardWired.cs" />
Note: See TracChangeset
for help on using the changeset viewer.