Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/03/19 14:15:11 (5 years ago)
Author:
msemenki
Message:

#2988:
Class HelpFuction get new static functions that are used in different Map’s classes and possible in other classes.
Branch was adapted to Hive.
New version of class structure for Maps:

  1. 3 new variants of maps (RankMap, SuccessMap and ZeroMap) are added.
  2. BaseMap class was simplified, some class members were deleted and other were transported to child class, because some of them are not used in all kinds of maps.
  3. Functions between base class and child class were divided in other way.
  4. Mutation operators were adapted to work with new class structure. Now mutation make less work for ModelNodes than previously.
  5. ModelNode and Model symbols were simplified. They should not take into account a map type.
  6. Models frequency analyzers were adapted for new variants of maps.
  7. EMMAlgorithm class was adapted to new maps
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  
        1212*.nuget.props
        1313*.nuget.targets
         14Plugin.cs
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMAlgorithm.cs

    r16899 r17002  
    2929using HeuristicLab.Selection;
    3030using System.Collections.Generic;
    31 using System.IO;
    3231using System.Linq;
    3332using CancellationToken = System.Threading.CancellationToken;
     
    3534
    3635namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    37   [Item("EvolvmentModelsOfModels Algorithm ", "EMM implementation")]
     36  [Item("Evolvment Models Of Models Algorithm (EMM) ", "EMM implementation")]
    3837  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 125)]
    3938  [StorableType("AD23B21F-089A-4C6C-AD2E-1B01E7939CF5")]
    4039  public class EMMAlgorithm : EvolvmentModelsOfModelsAlgorithmBase {
    4140    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) { }
    4742    public override IDeepCloneable Clone(Cloner cloner) {
    4843      return new EMMAlgorithm(this, cloner);
     
    5348
    5449    protected override void Run(CancellationToken cancellationToken) {
    55       InfixExpressionParser parser = new InfixExpressionParser();
    56       var trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse);
    5750      if (AlgorithmImplemetationType.Value == "Read") {
    5851        Map.MapRead(RandomParameter.Value, trees, "Map.txt");
    5952      } else {
    60         Map.MapCreationPrepare(RandomParameter.Value, trees, ClusterNumbersParameter.Value.Value);
     53        Map.MapCreationPrepare(trees);
    6154        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;
    6658
    6759      if (AlgorithmImplemetationType.Value == "OnlyMap") {
     
    7466        if (!globalScope.Variables.ContainsKey("TreeModelMap"))
    7567          globalScope.Variables.Add(new Variable("TreeModelMap", Map));
     68        if (!globalScope.Variables.ContainsKey("Map"))
     69          globalScope.Variables.Add(new Variable("Map", Map));
    7670        EMMAlgorithmRun(cancellationToken);
    7771      }
     
    8478      bestSelector.QualityParameter.ActualName = "Quality";
    8579
    86       var populationSize = PopulationSize.Value;
    8780      var maximumEvaluatedSolutions = MaximumEvaluatedSolutions.Value;
    8881      var crossover = Crossover;
    8982      var selector = Selector;
    90       var groupSize = GroupSize.Value;
    9183      var crossoverProbability = CrossoverProbability.Value;
    9284      var mutator = Mutator;
     
    143135            var op3 = executionContext.CreateChildOperation(evaluator, childScope);
    144136            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            //}
    149140            ++EvaluatedSolutions;
    150141            Population.Add(new EMMSolution(childScope));
     
    152143            Population.Add(new EMMSolution(selected.SubScopes[i]));
    153144          }
    154 
    155145          if (EvaluatedSolutions >= maximumEvaluatedSolutions) {
    156146            break;
     
    158148
    159149        }
     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
    160163        globalScope.SubScopes.Replace(Population.Select(x => (IScope)x.Individual));
    161164        // run analyzer
     
    170173
    171174      // 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.
    177179
    178180      var rand = RandomParameter.Value;
     
    180182      rand.Reset(Seed);
    181183
    182       var populationSize = PopulationSize.Value;
    183 
    184184      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
    198187      base.Initialize(cancellationToken);
    199188    }
     189
    200190    private void InitializePopulation(ExecutionContext executionContext, CancellationToken cancellationToken, IRandom random) {
     191      Population = new List<IEMMSolution>();
     192      var evaluator = Problem.Evaluator;
    201193      var creator = Problem.SolutionCreator;
    202       var evaluator = Problem.Evaluator;
    203       var populationSize = PopulationSize.Value;
    204       Population = new List<IEMMSolution>(populationSize);
    205 
    206194      var parentScope = executionContext.Scope; //main scope for the next step work
    207       // first, create all individuals
    208       for (int i = 0; i < populationSize; ++i) {
     195                                                // first, create all individuals
     196      for (int i = 0; i < PopulationSize.Value; ++i) {
    209197        var childScope = new Scope(i.ToString()) { Parent = parentScope };
    210198        ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(creator, childScope));
     
    212200        var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value;
    213201        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);
    218203        }
    219204        parentScope.SubScopes.Add(childScope);
    220205      }
    221 
    222206      // then, evaluate them and update qualities
    223       for (int i = 0; i < populationSize; ++i) {
     207      for (int i = 0; i < PopulationSize.Value; ++i) {
    224208        var childScope = parentScope.SubScopes[i];
    225209        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
    234215    private void LocalDecent(ISymbolicDataAnalysisSingleObjectiveProblem problem, CancellationToken cancellationToken, IScope childScope) {
    235216      int maxStepNumber = 100;
    236       var creator = Problem.SolutionCreator;
    237       var name = ((ISymbolicExpressionTreeCreator)creator).SymbolicExpressionTreeParameter.ActualName;
     217      var name = ((ISymbolicExpressionTreeCreator)Problem.SolutionCreator).SymbolicExpressionTreeParameter.ActualName;
    238218      var tree = (ISymbolicExpressionTree)childScope.Variables[name].Value;
    239       var evaluator = problem.Evaluator;
    240219      var oldTree = tree.Clone();
    241       ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope));
     220      ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(problem.Evaluator, childScope));
    242221      var rand = RandomParameter.Value;
    243222      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
     
    259238      return maxStepNumber;
    260239    }
    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();
    264243      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;
    266248      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);
    269251      var evaluator = Problem.Evaluator;
    270252      ExecuteOperation(executionContext, cancellationToken, executionContext.CreateChildOperation(evaluator, childScope));
    271253      double currentQuality = ((DoubleValue)childScope.Variables["Quality"].Value).Value;
    272254      if (oldQuality > currentQuality) {
    273         a.Tree = (ISymbolicExpressionTree)oldSubTree.Clone();
     255        tree.Tree = (ISymbolicExpressionTree)oldSubTree.Clone();
    274256        ((DoubleValue)childScope.Variables["Quality"].Value).Value = oldQuality;
    275257      }
Note: See TracChangeset for help on using the changeset viewer.