- Timestamp:
- 06/03/19 14:15:11 (5 years ago)
- Location:
- branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM
- Property svn:ignore
-
old new 12 12 *.nuget.props 13 13 *.nuget.targets 14 Plugin.cs
-
- Property svn:ignore
-
branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMAlgorithm.cs
r16899 r17002 29 29 using HeuristicLab.Selection; 30 30 using System.Collections.Generic; 31 using System.IO;32 31 using System.Linq; 33 32 using CancellationToken = System.Threading.CancellationToken; … … 35 34 36 35 namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { 37 [Item("Evolvment ModelsOfModels Algorithm", "EMM implementation")]36 [Item("Evolvment Models Of Models Algorithm (EMM) ", "EMM implementation")] 38 37 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 125)] 39 38 [StorableType("AD23B21F-089A-4C6C-AD2E-1B01E7939CF5")] 40 39 public class EMMAlgorithm : EvolvmentModelsOfModelsAlgorithmBase { 41 40 public EMMAlgorithm() : base() { } 42 protected EMMAlgorithm(EMMAlgorithm original, Cloner cloner) : base(original, cloner) { 43 //if (original.Map != null) { 44 // Map = cloner.Clone(original.Map); 45 //} 46 } 41 protected EMMAlgorithm(EMMAlgorithm original, Cloner cloner) : base(original, cloner) { } 47 42 public override IDeepCloneable Clone(Cloner cloner) { 48 43 return new EMMAlgorithm(this, cloner); … … 53 48 54 49 protected override void Run(CancellationToken cancellationToken) { 55 InfixExpressionParser parser = new InfixExpressionParser();56 var trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse);57 50 if (AlgorithmImplemetationType.Value == "Read") { 58 51 Map.MapRead(RandomParameter.Value, trees, "Map.txt"); 59 52 } else { 60 Map.MapCreationPrepare( RandomParameter.Value, trees, ClusterNumbersParameter.Value.Value);53 Map.MapCreationPrepare(trees); 61 54 Map.CreateMap(RandomParameter.Value, ClusterNumbersParameter.Value.Value); 62 // Map.WriteMapToTxtFile(RandomParameter.Value); 63 } 64 65 ClusterNumbersShowParameter.Value.Value = Map.K; 55 // Map.WriteMapToTxtFile(RandomParameter.Value); хайв этого не любит.. ворчит 56 } 57 ClusterNumbersShowParameter.Value.Value = Map.Map.Count; 66 58 67 59 if (AlgorithmImplemetationType.Value == "OnlyMap") { … … 74 66 if (!globalScope.Variables.ContainsKey("TreeModelMap")) 75 67 globalScope.Variables.Add(new Variable("TreeModelMap", Map)); 68 if (!globalScope.Variables.ContainsKey("Map")) 69 globalScope.Variables.Add(new Variable("Map", Map)); 76 70 EMMAlgorithmRun(cancellationToken); 77 71 } … … 84 78 bestSelector.QualityParameter.ActualName = "Quality"; 85 79 86 var populationSize = PopulationSize.Value;87 80 var maximumEvaluatedSolutions = MaximumEvaluatedSolutions.Value; 88 81 var crossover = Crossover; 89 82 var selector = Selector; 90 var groupSize = GroupSize.Value;91 83 var crossoverProbability = CrossoverProbability.Value; 92 84 var mutator = Mutator; … … 143 135 var op3 = executionContext.CreateChildOperation(evaluator, childScope); 144 136 ExecuteOperation(executionContext, innerToken, op3); 145 var qualities = (DoubleValue)childScope.Variables["Quality"].Value; 146 var childSolution = new EMMSolution(childScope); 147 // set child qualities 148 childSolution.Qualities = qualities; 137 //if (Problem.ProblemData is IRegressionProblemData problemData) { 138 // SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(Problem.SymbolicExpressionTreeInterpreter, (ISymbolicExpressionTree)childScope.Variables["SymbolicExpressionTree"].Value, problemData, problemData.TestIndices, true, 100 /*max iterration*/, true); 139 //} 149 140 ++EvaluatedSolutions; 150 141 Population.Add(new EMMSolution(childScope)); … … 152 143 Population.Add(new EMMSolution(selected.SubScopes[i])); 153 144 } 154 155 145 if (EvaluatedSolutions >= maximumEvaluatedSolutions) { 156 146 break; … … 158 148 159 149 } 150 if (Map is EMMSucsessMap) { 151 var population = new Dictionary<ISymbolicExpressionTree, double>(); 152 foreach (var individ in Population) { 153 var tree = (ISymbolicExpressionTree)(((IScope)individ.Individual).Variables["SymbolicExpressionTree"].Value); 154 population.Add(tree, individ.Qualities.Value); 155 } 156 Map.MapUpDate(population); 157 population.Clear(); 158 } 159 //List<object> A = new List<object>(); 160 //A.Add(new Scope ()); 161 //A.Add(new SymbolicExpressionTree()); 162 160 163 globalScope.SubScopes.Replace(Population.Select(x => (IScope)x.Individual)); 161 164 // run analyzer … … 170 173 171 174 // set the execution context for parameters to allow lookup 172 foreach (var parameter in Problem.Parameters.OfType<IValueParameter>()) { // 173 // we need all of these in order for the wiring of the operators to work 174 globalScope.Variables.Add(new Core.Variable(parameter.Name, parameter.Value)); 175 } 176 globalScope.Variables.Add(new Core.Variable("Results", Results)); // make results available as a parameter for analyzers etc. 175 foreach (var parameter in Problem.Parameters.OfType<IValueParameter>()) { 176 globalScope.Variables.Add(new Variable(parameter.Name, parameter.Value)); 177 } 178 globalScope.Variables.Add(new Variable("Results", Results)); // make results available as a parameter for analyzers etc. 177 179 178 180 var rand = RandomParameter.Value; … … 180 182 rand.Reset(Seed); 181 183 182 var populationSize = PopulationSize.Value;183 184 184 InitializePopulation(executionContext, cancellationToken, rand); 185 186 // initialize data structures for map clustering 187 //var models = new ItemList<ISymbolicExpressionTree>(Map.ModelSet); 188 //var map = new ItemList<ItemList<IntValue>>(); 189 //foreach (var list in Map.Map) { 190 // map.Add(new ItemList<IntValue>(list.Select(x => new IntValue(x)))); 191 //} 192 //var clusterNumber = new ItemList<IntValue>(Map.ClusterNumber.Select(x => new IntValue(x))); 193 //globalScope.Variables.Add(new Variable("Models", models)); 194 //globalScope.Variables.Add(new Variable("Map", map)); 195 //globalScope.Variables.Add(new Variable("ClusterNumber", clusterNumber)); 196 197 EvaluatedSolutions = populationSize; 185 EvaluatedSolutions = PopulationSize.Value; 186 198 187 base.Initialize(cancellationToken); 199 188 } 189 200 190 private void InitializePopulation(ExecutionContext executionContext, CancellationToken cancellationToken, IRandom random) { 191 Population = new List<IEMMSolution>(); 192 var evaluator = Problem.Evaluator; 201 193 var creator = Problem.SolutionCreator; 202 var evaluator = Problem.Evaluator;203 var populationSize = PopulationSize.Value;204 Population = new List<IEMMSolution>(populationSize);205 206 194 var parentScope = executionContext.Scope; //main scope for the next step work 207 // first, create all individuals208 for (int i = 0; i < populationSize; ++i) {195 // first, create all individuals 196 for (int i = 0; i < PopulationSize.Value; ++i) { 209 197 var childScope = new Scope(i.ToString()) { Parent = parentScope }; 210 198 ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(creator, childScope)); … … 212 200 var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value; 213 201 foreach (var node in tree.IterateNodesPostfix().OfType<TreeModelTreeNode>()) { 214 node.Tree = Map.NewModelForInizializtion(random, out int cluster, out int treeNumber); 215 node.SetLocalParameters(random, 0.5); 216 node.ClusterNumer = cluster; 217 node.TreeNumber = treeNumber; 202 Map.NodeManipulationForInizializtion(random, node); 218 203 } 219 204 parentScope.SubScopes.Add(childScope); 220 205 } 221 222 206 // then, evaluate them and update qualities 223 for (int i = 0; i < populationSize; ++i) {207 for (int i = 0; i < PopulationSize.Value; ++i) { 224 208 var childScope = parentScope.SubScopes[i]; 225 209 ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope)); 226 227 var qualities = (DoubleValue)childScope.Variables["Quality"].Value; 228 var solution = new EMMSolution(childScope); // Create solution and push individual inside 229 230 solution.Qualities = qualities; 231 Population.Add(solution); // push solution to Population 232 } 233 } 210 Population.Add(new EMMSolution(childScope)); // Create solution and push individual inside. push solution to Population 211 } 212 } 213 214 // next function was not tested in real work 234 215 private void LocalDecent(ISymbolicDataAnalysisSingleObjectiveProblem problem, CancellationToken cancellationToken, IScope childScope) { 235 216 int maxStepNumber = 100; 236 var creator = Problem.SolutionCreator; 237 var name = ((ISymbolicExpressionTreeCreator)creator).SymbolicExpressionTreeParameter.ActualName; 217 var name = ((ISymbolicExpressionTreeCreator)Problem.SolutionCreator).SymbolicExpressionTreeParameter.ActualName; 238 218 var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value; 239 var evaluator = problem.Evaluator;240 219 var oldTree = tree.Clone(); 241 ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation( evaluator, childScope));220 ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(problem.Evaluator, childScope)); 242 221 var rand = RandomParameter.Value; 243 222 if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed(); … … 259 238 return maxStepNumber; 260 239 } 261 void ModelChange(TreeModelTreeNode a, IRandom rand, CancellationToken cancellationToken, IScope childScope) {262 int treeNumber = a.TreeNumber;263 var oldSubTree = (ISymbolicExpressionTree) a.Tree.Clone();240 void ModelChange(TreeModelTreeNode tree, IRandom rand, CancellationToken cancellationToken, IScope childScope) { 241 int treeNumber = tree.TreeNumber; 242 var oldSubTree = (ISymbolicExpressionTree)tree.Tree.Clone(); 264 243 double oldQuality = ((DoubleValue)childScope.Variables["Quality"].Value).Value; 265 int cluster = Map.ClusterNumber[treeNumber]; 244 int cluster; 245 if (Map is EMMIslandMap map) 246 cluster = map.ClusterNumber[treeNumber]; 247 else cluster = treeNumber; 266 248 int newTreeNumber = rand.Next(Map.Map[cluster].Count); 267 a.Tree = (ISymbolicExpressionTree)Map.ModelSet[newTreeNumber].Clone();268 a.Tree.Root.ShakeLocalParameters(rand, 1);249 tree.Tree = (ISymbolicExpressionTree)Map.ModelSet[newTreeNumber].Clone(); 250 tree.Tree.Root.ShakeLocalParameters(rand, 1); 269 251 var evaluator = Problem.Evaluator; 270 252 ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope)); 271 253 double currentQuality = ((DoubleValue)childScope.Variables["Quality"].Value).Value; 272 254 if (oldQuality > currentQuality) { 273 a.Tree = (ISymbolicExpressionTree)oldSubTree.Clone();255 tree.Tree = (ISymbolicExpressionTree)oldSubTree.Clone(); 274 256 ((DoubleValue)childScope.Variables["Quality"].Value).Value = oldQuality; 275 257 }
Note: See TracChangeset
for help on using the changeset viewer.