Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16734


Ignore:
Timestamp:
04/01/19 13:03:13 (6 years ago)
Author:
msemenki
Message:

#2988: Add Model Symbol Frequency Analyzer and Model's Clusters Frequency Analyzer. Fix Bag's with Keys. Fix changing during mutation for Variables Types in SubModels .

Location:
branches/2988_ModelsOfModels2
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMAlgorithm.cs

    r16722 r16734  
    3737  [Item("EvolvmentModelsOfModels Algorithm ", "EMM implementation")]
    3838  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 125)]
    39   [StorableType("D02C50E5-8325-496F-8DEA-C23651756846")]
     39  [StorableType("AD23B21F-089A-4C6C-AD2E-1B01E7939CF5")]
    4040  public class EMMAlgorithm : EvolvmentModelsOfModelsAlgorithmBase {
    4141    public EMMMapTreeModel Map { get; private set; }
     
    5757      InfixExpressionParser parser = new InfixExpressionParser();
    5858      var trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse);
    59       Map = new EMMMapTreeModel(RandomParameter.Value, trees, ClusterNumbersParameter.Value.Value);
    60       ClusterNumbersParameter.Value.Value = Map.K;
    61       if (previousExecutionState != ExecutionState.Paused) {
    62         InitializeAlgorithm(cancellationToken);
    63       }
    64       globalScope.Variables.Add(new Variable("TreeModelMap", Map));
    65       EMMEvolutionaryAlgorithmRun(cancellationToken);
     59      // this.Problem.SymbolicExpressionTreeGrammar;
     60      /*   Problem.ProblemData.Dataset.ColumnNames.Take(2).ToList();
     61         trees.First().Root.Grammar.ContainsSymbol((IVariable)a).
     62           = this.Problem.SymbolicExpressionTreeGrammar;*/
     63      int flag = 1;
     64      switch (flag) {
     65        case 0: // for case when we want only create map, and do  not want made somting also.
     66          Map = new EMMMapTreeModel(RandomParameter.Value, trees, ClusterNumbersParameter.Value.Value);
     67          ClusterNumbersParameter.Value.Value = Map.K;
     68          File.WriteAllLines("Map.txt", Map.MapToString());
     69          File.WriteAllLines("MapToSee.txt", Map.MapToSee());
     70          globalScope = new Scope("Global Scope");
     71          executionContext = new ExecutionContext(null, this, globalScope);
     72          break;
     73        case 1: // for case when we want read existed map and work with it;
     74          Map = new EMMMapTreeModel(RandomParameter.Value, trees);
     75          ClusterNumbersParameter.Value.Value = Map.K;
     76          if (previousExecutionState != ExecutionState.Paused) {
     77            InitializeAlgorithm(cancellationToken);
     78          }
     79          globalScope.Variables.Add(new Variable("TreeModelMap", Map));
     80          EMMEvolutionaryAlgorithmRun(cancellationToken);
     81          break;
     82
     83        default: //for case of usial from zero step starting
     84          Map = new EMMMapTreeModel(RandomParameter.Value, trees, ClusterNumbersParameter.Value.Value);
     85          ClusterNumbersParameter.Value.Value = Map.K;
     86          if (previousExecutionState != ExecutionState.Paused) {
     87            InitializeAlgorithm(cancellationToken);
     88          }
     89          globalScope.Variables.Add(new Variable("TreeModelMap", Map));
     90          EMMEvolutionaryAlgorithmRun(cancellationToken);
     91          break;
     92      }
     93
    6694    }
    6795    private void EMMEvolutionaryAlgorithmRun(CancellationToken cancellationToken) {
     
    201229        foreach (var node in tree.IterateNodesPostfix().OfType<TreeModelTreeNode>()) {
    202230          node.Tree = Map.NewModelForInizializtion(random, out int cluster, out int treeNumber);
    203           node.Tree.Root.ShakeLocalParameters(random, 0.5);
     231          node.SetLocalParameters(random, 0.5);
    204232          node.ClusterNumer = cluster;
    205233          node.TreeNumber = treeNumber;
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseAlgorithm.cs

    r16722 r16734  
    4040namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    4141  [Item("MOEADAlgorithmBase", "Base class for all MOEA/D algorithm variants.")]
    42   [StorableType("DD721D8A-A4DE-44C1-8D83-7888E8433B5F")]
     42  [StorableType("A56A396B-965A-4ADE-8A2B-AE3A45F9C239")]
    4343  public abstract class EvolvmentModelsOfModelsAlgorithmBase : FixedDataAnalysisAlgorithm<ISymbolicDataAnalysisSingleObjectiveProblem> {
    4444    #region data members
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMapBase.cs

    r16722 r16734  
    2525
    2626namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    27   [StorableType("8A5818D6-2B74-4148-AFBC-D36CE6256406")]
     27  [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
    2828  public abstract class EMMMapBase<T> : Item where T : class {
    2929    public List<T> ModelSet { get; set; }
     
    3434
    3535    protected abstract void CalculateDistances();
    36     public abstract void CreateMap(IRandom random, int k);
     36    protected abstract void CreateMap(IRandom random, int k);
    3737    public abstract T NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber);
    3838
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMapTreeModel.cs

    r16722 r16734  
    2626using HeuristicLab.Problems.DataAnalysis.Symbolic;
    2727using System.Collections.Generic;
     28using System.IO;
    2829using System.Linq;
    2930
    3031namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    31   [StorableType("A9AE93F0-E589-44D0-AD34-0E3AA358D669")]
    3232  [Item("TreeModelMap", "A map of models of models of models")]
     33  [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
    3334  public class EMMMapTreeModel : EMMMapBase<ISymbolicExpressionTree> {
    3435    #region conctructors
     
    6263      CreateMap(random, k);
    6364    }
     65    public EMMMapTreeModel(IRandom random, IEnumerable<ISymbolicExpressionTree> trees) : this(1) {
     66      ModelSet = trees.ToList();
     67      string input = File.ReadAllText("Map.txt");
     68
     69      int i = 0;
     70      foreach (var row in input.Split('\n')) {
     71        Map.Add(new List<int>());
     72        foreach (var col in row.Trim().Split(' ')) {
     73          Map[i].Add(int.Parse(col.Trim()));
     74        }
     75        i++;
     76      }
     77      K = Map.Count;
     78      MapPreparation();
     79    }
    6480    public override IDeepCloneable Clone(Cloner cloner) {
    6581      return new EMMMapTreeModel(this, cloner);
     
    7591      }
    7692    }
    77     override public void CreateMap(IRandom random, int k) {
     93    override protected void CreateMap(IRandom random, int k) {
    7894      K = k;
    7995      //Clusterization
     
    96112      }
    97113    }
     114    protected void MapPreparation() {
     115      for (int i = 0; i < Map.Count; i++) {
     116        for (int j = 0; j < Map[i].Count; j++) {
     117          ClusterNumber.Add(0);
     118        }
     119      }
     120      for (int i = 0; i < Map.Count; i++) {
     121        for (int j = 0; j < Map[i].Count; j++) {
     122          ClusterNumber[Map[i][j]] = i;
     123        }
     124      }
     125    }
    98126    #endregion
    99127    #region Dialog with surroudings
     
    103131      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
    104132    }
     133    public string[] MapToString() {
     134      string[] s;
     135      s = new string[K];
     136      for (int i = 0; i < K; i++) {
     137        s[i] = "";
     138        for (int j = 0; j < Map[i].Count; j++) {
     139          s[i] += Map[i][j].ToString();
     140          s[i] += " ";
     141        }
     142      }
     143      return s;
     144    }
     145    public string[] MapToSee() {
     146      var fmt = new InfixExpressionFormatter();
     147      string[] s;
     148      s = new string[(ModelSet.Count) + 1];
     149      s[0] = "ClusterNumber" + "," + "Modfelnumber" + "," + "Model";
     150      for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
     151        s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + "," + fmt.Format(ModelSet[i - 1]);
     152      }
     153      return s;
     154    }
    105155
    106156    #endregion
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMutators.cs

    r16722 r16734  
    3232
    3333namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    34   [StorableType("92D37E9F-8726-4524-AF8F-F3787641F7B9")]
    3534  [Item("EMMChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
     35  [StorableType("990D3946-7F06-48B4-B8DB-F8E308D6304D")]
    3636  public sealed class EMMMutators : SymbolicExpressionTreeManipulator {
    3737    private const int MAX_TRIES = 100;
     
    125125            int p = random.Next(map.Count);
    126126            if (child is TreeModelTreeNode chNode) // in real life never bacame true. It need some cheking
    127               p = chNode.ClusterNumer;
     127            { p = chNode.ClusterNumer; }
    128128            treeNode.TreeNumber = map[p].SampleRandom(random).Value;
    129129            treeNode.Tree = (ISymbolicExpressionTree)modelSet[treeNode.TreeNumber].Clone();
    130             treeNode.Tree.Root.ShakeLocalParameters(random, 0.5);
     130            treeNode.SetLocalParameters(random, 0.5);
    131131          } else
    132132            newNode.ResetLocalParameters(random);
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMSolution.cs

    r16722 r16734  
    2626// Can be deleted potnetinaly
    2727namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
     28  [StorableType("AB38211D-5F52-4420-A606-1C3CB58BA27C")]
    2829  public interface IEMMSolution : IItem {
    2930    IItem Individual { get; set; }
     
    3132  }
    3233  [Item("EMMSolution", "Represents a solution inside the EMM population")]
    33   [StorableType("708BDF29-AD3F-4AFC-83AE-551A27FF45A0")]
     34  [StorableType("C0E63430-5000-4592-BBE4-2D3E0EE1AE3F")]
    3435  public class EMMSolution : Item, IEMMSolution {
    3536    [Storable]
     
    6970  }
    7071  [Item("EMMSolution", "Represents a solution inside the EMM population")]
    71   [StorableType("36929CD1-63A5-4270-8C65-2A8177BCA0AE")]
     72  [StorableType("559EA31B-2263-4233-900C-EC62120EE580")]
    7273  public class EMMSolution<T> : EMMSolution where T : class, IItem {
    7374    public new T Individual {
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Plugin.cs

    r16722 r16734  
    2626  /// Plugin class for HeuristicLab.Algorithms.EMMAlgorithm plugin.
    2727  /// </summary>
    28   [Plugin("HeuristicLab.Algorithms.EMMAlgorithm", "3.3.15.16565")]
     28  [Plugin("HeuristicLab.Algorithms.EMMAlgorithm", "3.3.15.16722")]
    2929  [PluginFile("HeuristicLab.Algorithms.EMMAlgorithm-3.3.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Analysis", "3.3")]
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs

    r16565 r16734  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
     22using HEAL.Attic;
    2523using HeuristicLab.Analysis;
    2624using HeuristicLab.Common;
     
    3028using HeuristicLab.Optimization;
    3129using HeuristicLab.Parameters;
    32 using HEAL.Attic;
     30using System;
     31using System.Collections.Generic;
     32using System.Linq;
    3333
    3434namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16722 r16734  
    216216    <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer.cs" />
    217217    <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeComplexcityAnalyzer.cs" />
     218    <Compile Include="Analyzers\ModelClustersFrequencyAnalyzer.cs" />
     219    <Compile Include="Analyzers\ModelsFrequencyAnalyzer.cs" />
    218220    <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" />
    219221    <Compile Include="Analyzers\SymbolicDataAnalysisBuildingBlockAnalyzer.cs" />
     
    232234      <SubType>Code</SubType>
    233235    </Compile>
     236    <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" />
    234237    <Compile Include="Converters\LinearModelToTreeConverter.cs" />
    235238    <Compile Include="Converters\TreeSimplifier.cs" />
     
    260263    <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" />
    261264    <Compile Include="SymbolicDataAnalysisExpressionPruningOperator.cs" />
    262     <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" />
    263265    <Compile Include="Analyzers\SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs" />
    264266    <Compile Include="Creators\MultiSymbolicDataAnalysisExpressionCreator.cs" />
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/ModelTreeNode.cs

    r16722 r16734  
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2626using HeuristicLab.Random;
    27 using System.Collections.Generic;
    28 
     27using System.Linq;
    2928
    3029namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    31   [StorableType("38C37AED-5B9D-488E-8785-D8FF541D9B4C")]
     30  [StorableType("44B25E73-6C4D-404C-93E5-42B7D2807CF7")]
    3231  public sealed class TreeModelTreeNode : SymbolicExpressionTreeTerminalNode {
    3332    public new TreeModel Symbol {
     
    7170      get { return true; }
    7271    }
    73     //public override void ResetLocalParameters(IRandom random, List<ISymbolicExpressionTree> modelSet, List<List<int>> map, List<int> cLusterNumber) {
    74     //  base.ResetLocalParameters(random);
    75     //  if (ClusterNumer < 0) {
    76     //    //Tree = map.NewModelForInizializtion(ClusterNumer, random);
    77     //    var selectedNodelNumber = random.Next(modelSet.Count);
    78     //    ClusterNumer = cLusterNumber[selectedNodelNumber];
    79     //    TreeNumber = selectedNodelNumber;
    80     //    Tree = (ISymbolicExpressionTree)modelSet[selectedNodelNumber].Clone();
    81     //  } else {
    82     //    //New Model For Mutation Adding
    83     //    var selectedNodelNumber = random.Next(map[ClusterNumer].Count);
    84     //    TreeNumber = selectedNodelNumber;
    85     //    Tree = (ISymbolicExpressionTree)modelSet[map[ClusterNumer][selectedNodelNumber]].Clone();
    86     //  }
    87 
    88     //}
    89 
    9072    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
    9173      base.ShakeLocalParameters(random, shakingFactor);
    92       List<ISymbolicExpressionTreeNode> parametricNodes = new List<ISymbolicExpressionTreeNode>();
    93       Tree.Root.ForEachNodePostfix(n => {
    94         if (n.HasLocalParameters) parametricNodes.Add(n);
    95       });
    96       if (parametricNodes.Count > 0) {
     74      var parametricNodes = Tree.IterateNodesPrefix().Where(x => x.HasLocalParameters).ToList();
     75      if (parametricNodes.Any()) {
    9776        var selectedPoint = parametricNodes.SampleRandom(random);
    98         selectedPoint.ShakeLocalParameters(random, shakingFactor);
     77        if (selectedPoint is VariableTreeNode variableTreeNode) {
     78          var symbol = variableTreeNode.Symbol;
     79          variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma);
     80        } else {
     81          selectedPoint.ShakeLocalParameters(random, shakingFactor);
     82        }
     83
    9984      }
    10085    }
    101 
     86    public void SetLocalParameters(IRandom random, double shakingFactor) {
     87      foreach (var node in Tree.IterateNodesPrefix().Where(x => x.HasLocalParameters)) {
     88        if (node is VariableTreeNode variableTreeNode) {
     89          var symbol = variableTreeNode.Symbol;
     90          variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma);
     91        } else {
     92          node.ResetLocalParameters(random);
     93        }
     94      }
     95    }
    10296    public override IDeepCloneable Clone(Cloner cloner) {
    10397      return new TreeModelTreeNode(this, cloner);
     
    106100    public override string ToString() {
    107101
    108       string s = "model" +" " +TreeNumber.ToString()+"\n " + new InfixExpressionFormatter().Format(tree);
     102      string s = "model" + " " + TreeNumber.ToString() + "\n " + new InfixExpressionFormatter().Format(tree);
    109103      return s;
    110104    }
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/TreeModel.cs

    r16722 r16734  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
     22using HEAL.Attic;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HEAL.Attic;
    2725namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    28   [StorableType("F8A6AD96-28D9-4BEC-8392-8B7BA824B085")]
     26  [StorableType("6F628649-7C71-49F3-862A-0BAB4BADF964")]
    2927  public sealed class TreeModel : Symbol {
    3028
     
    3937    [StorableConstructor]
    4038    private TreeModel(StorableConstructorFlag _) : base(_) { }
    41     private TreeModel(TreeModel original, Cloner cloner) : base(original, cloner) {   }
    42     public TreeModel() : base("TreeModel", "Represents a TreeModel.") {    }
     39    private TreeModel(TreeModel original, Cloner cloner) : base(original, cloner) { }
     40    public TreeModel() : base("TreeModel", "Represents a TreeModel.") { }
    4341    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    44       return new TreeModelTreeNode(this); } 
     42      return new TreeModelTreeNode(this);
     43    }
    4544
    4645    public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset for help on using the changeset viewer.