Changeset 17002


Ignore:
Timestamp:
06/03/19 14:15:11 (3 weeks 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
Files:
3 added
2 deleted
18 edited
1 moved

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      }
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseAlgorithm.cs

    r16899 r17002  
    3535using System;
    3636using System.Collections.Generic;
     37using System.IO;
    3738using System.Linq;
    3839using CancellationToken = System.Threading.CancellationToken;
     
    5657    [Storable]
    5758    protected ExecutionState previousExecutionState;
     59    [Storable]
     60    protected IEnumerable<ISymbolicExpressionTree> trees;
    5861
    5962    [Storable]
     
    7780    private const string ClusterNumbersParameterName = "ClusterNumbers";
    7881    private const string ClusterNumbersShowParameterName = "ClusterNumbersShow";
    79     private const string AlgorithmImplementationTypeParameterName = "AlgorithmImplemetationType";
     82    private const string AlgorithmImplementationTypeParameterName = "AlgorithmImplementationType";
    8083    private const string MapParameterName = "Map";
    8184    private const string NegbourTypeParameterName = "NegbourType";
     
    250253      ParameterizeSelectors();
    251254
     255      //begin hack ...
     256      InputFile.ValueChanged += InputFile_ValueChanged;
     257      InfixExpressionParser parser = new InfixExpressionParser();
     258      trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse).ToArray();
     259      // end hack
     260
    252261      ProblemChanged += EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged;
    253262      MapParameterUpdate();
    254     }
     263
     264    }
     265
     266    // also hack vvvvvvvvv
     267    private void InputFile_ValueChanged(object sender, EventArgs e) {
     268      InfixExpressionParser parser = new InfixExpressionParser();
     269      trees = File.ReadAllLines(InputFileParameter.Value.Value).Select(parser.Parse);
     270    }
     271    // remove again ^^^^^^^^^
    255272
    256273    private void EvolvmentModelsOfModelsAlgorithmBase_ProblemChanged(object sender, EventArgs e) {
     
    271288      {
    272289        new EMMIslandMap(),
    273         new EMMNetworkMap(neghboorNumber)
     290        new EMMNetworkMap(neghboorNumber),
     291        new EMMDisatanceMap(),
     292        new EMMRankMap(),
     293        new EMMSucsessMap (),
     294        new EMMZeroMap ()
    274295      };
    275296      foreach (var t in mapTypes) {
     
    314335        globalScope = cloner.Clone(original.globalScope);
    315336      }
     337
     338      // hack
     339      trees = original.trees.Select(x => cloner.Clone(x)).ToArray();
    316340    }
    317341
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseMap.cs

    r16899 r17002  
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2626using HeuristicLab.Problems.DataAnalysis.Symbolic;
     27using HeuristicLab.Random;
    2728using System.Collections.Generic;
    2829using System.IO;
     
    3233  [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
    3334  public abstract class EMMMapBase<T> : Item where T : class {
     35    #region data members
    3436    [Storable]
    3537    public List<T> ModelSet { get; set; }
    3638    [Storable]
    37     public List<int> ClusterNumber { get; set; }
    38     [Storable]
    3939    public List<List<int>> Map { get; set; }
    40     [Storable]
    41     public double[,] Distances { get; set; }
    42     [Storable]
    43     public int K { get; protected set; }
    44 
    45     protected void CalculateDistances() {
    46       if (ModelSet is List<ISymbolicExpressionTree> set) {
    47         Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true);
    48       } else { /// for future work
    49         for (int i = 0; i < ModelSet.Count - 1; i++) {
    50           for (int j = 0; j <= i; j++) {
    51             Distances[i, j] = 0;
    52           }
    53         }
    54       }
    55       for (int i = 0; i < ModelSet.Count - 1; i++) {
    56         for (int j = i + 1; j < ModelSet.Count; j++) {
    57           Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
    58         }
    59       }
    60 
    61     }
    62     public abstract void CreateMap(IRandom random, int k);
    63     public abstract T NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber);
    64     public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
    65       treeNumber = random.Next(ModelSet.Count);
    66       cluster = ClusterNumber[treeNumber];
    67       if (ModelSet[treeNumber] is ISymbolicExpressionTree model)
    68         return (ISymbolicExpressionTree)(model.Clone());
    69       return new SymbolicExpressionTree();
    70     }
    71 
     40    #endregion
     41    #region constructors
    7242    [StorableConstructor]
    7343    protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
    74     protected EMMMapBase() : this(1) { }
    75     public EMMMapBase(int k) {
    76       K = k;
    77       ClusterNumber = new List<int>();
     44    public EMMMapBase() {
    7845      Map = new List<List<int>>();
    7946    }
     
    8451        else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it
    8552      }
    86       if (original.ClusterNumber != null) {
    87         ClusterNumber = original.ClusterNumber.ToList();
    88       }
    8953      if (original.Map != null) {
    9054        Map = original.Map.Select(x => x.ToList()).ToList();
    9155      }
    92       K = original.K;
    9356    }
    94     //public EMMMapBase(IRandom random, IEnumerable<T> trees, int k) : this(k) {
    95     //  // constructor that should be used in case of creation of a new map from the start point
    96     //  ModelSet = trees.ToList();
    97     //  CalculateDistances();
    98     //  CreateMap(random, k);
    99     //}
    100     //public EMMMapBase(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") : this(1) {
    101     //  // constructor that shoud be used in case of using of "old" map, that was previously created and now shoud be readed from file
    102     //  ModelSet = trees.ToList();
    103     //  MapFromFileRead(fileName);
    104     //  K = Map.Count;
    105     //  MapPreparation();
    106     //}
    107     public void MapCreationPrepare(IRandom random, IEnumerable<T> trees, int k) {
     57    #endregion
     58    #region map creation functions
     59    protected double[,] CalculateDistances() {
     60      double[,] distances;
     61      if (ModelSet is List<ISymbolicExpressionTree> set) {
     62        distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true);
     63      } else { /// for future work
     64        distances = new double[ModelSet.Count, ModelSet.Count];
     65        for (int i = 0; i < ModelSet.Count - 1; i++) {
     66          for (int j = 0; j <= i; j++) {
     67            distances[i, j] = 0;
     68          }
     69        }
     70      }
     71      for (int i = 0; i < ModelSet.Count - 1; i++) {
     72        for (int j = i + 1; j < ModelSet.Count; j++) {
     73          distances[j, i] = distances[i, j] = 1 - distances[i, j];
     74        }
     75      }
     76      return distances;
     77    }
     78    public abstract void CreateMap(IRandom random, int k);
     79    public void MapCreationPrepare(IEnumerable<T> trees) {
    10880      ModelSet = trees.ToList();
    109       CalculateDistances();
    110       CreateMap(random, k);
    11181    }
     82
     83    protected void MapSizeCheck(int k) {
     84      if (Map != null) Map.Clear();
     85      else Map = new List<List<int>>();
     86      if (Map.Count != k) {
     87        if (Map.Count != 0) {
     88          Map.Clear();
     89        }
     90        for (int i = 0; i < k; i++) {
     91          Map.Add(new List<int>());
     92        }
     93      }
     94    }
     95    #endregion
     96    #region map and files
    11297    public void MapRead(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") {
    11398      ModelSet = trees.ToList();
    11499      MapFromFileRead(fileName);
    115       K = Map.Count;
    116       MapPreparation();
     100      if (this is EMMIslandMap island) { island.ClusterNumbersCalculate(); }
    117101      if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; }
    118102    }
     
    126110      fileName2 += s;
    127111      fileName2 += ".txt";
    128       File.WriteAllLines(fileName, MapToSee());
     112      File.WriteAllLines(fileName2, MapToSee());
    129113    }
    130114    public string[] MapToString() { // Function that preapre Map to printing in .txt File: create a set of strings for future reading by computer
    131115      string[] s;
    132       s = new string[K];
    133       for (int i = 0; i < K; i++) {
     116      s = new string[Map.Count];
     117      for (int i = 0; i < Map.Count; i++) {
    134118        s[i] = "";
    135119        for (int j = 0; j < Map[i].Count; j++) {
     
    146130      s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model";
    147131      for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
    148         s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + ",";
     132        s[i] = "";
     133        if (this is EMMIslandMap island) {
     134          s[i] += island.ClusterNumber[i - 1].ToString() + ",";
     135        }
     136        s[i] += (i - 1).ToString() + ",";
    149137        if (ModelSet[i - 1] is ISymbolicExpressionTree model) {
    150138          s[i] += fmt.Format(model);
     
    164152      }
    165153    }
    166     protected void MapSizeCheck() {
    167       if (Map != null) Map.Clear();
    168       else Map = new List<List<int>>();
    169       if (Map.Count != K) {
    170         if (Map.Count != 0) {
    171           Map.Clear();
    172         }
    173         for (int i = 0; i < K; i++) {
    174           Map.Add(new List<int>());
     154    #endregion
     155
     156    #region map use functions
     157    public abstract T NewModelForInizializtionNotTree(IRandom random, out int treeNumber);
     158    public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int treeNumber) {
     159      treeNumber = random.Next(ModelSet.Count);
     160      if (ModelSet[treeNumber] is ISymbolicExpressionTree model)
     161        return (ISymbolicExpressionTree)(model.Clone());
     162      return new SymbolicExpressionTree();
     163    }
     164    public void NodeManipulationForInizializtion(IRandom random, TreeModelTreeNode node) {
     165      node.Tree = NewModelForInizializtion(random, out int treeNumber);
     166      node.SetLocalParameters(random, 0.5);
     167      node.TreeNumber = treeNumber;
     168    }
     169    public abstract ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber);
     170    public virtual void NodeForMutationChange(IRandom random, TreeModelTreeNode treeNode) {
     171      int treeNumber = treeNode.TreeNumber;
     172      int treeNumber2 = treeNode.TreeNumber;
     173      treeNode.Tree = new SymbolicExpressionTree(NewModelForMutation(random, out treeNumber, treeNumber2).Root);
     174      treeNode.TreeNumber = treeNumber;
     175      SetLocalParametersForTree(random, 0.5, treeNode.Tree);
     176    }
     177    public void SetLocalParametersForTree(IRandom random, double shakingFactor, ISymbolicExpressionTree tree) {
     178      foreach (var node in tree.IterateNodesPrefix().Where(x => x.HasLocalParameters)) {
     179        if (node is VariableTreeNode variableTreeNode) {
     180          var symbol = variableTreeNode.Symbol;
     181          variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma);
     182        } else {
     183          node.ResetLocalParameters(random);
    175184        }
    176185      }
    177186    }
    178     protected void MapPreparation() {
    179       for (int i = 0; i < Map.Count; i++) {
    180         for (int j = 0; j < Map[i].Count; j++) {
    181           ClusterNumber.Add(0);
    182         }
    183       }
    184       for (int i = 0; i < Map.Count; i++) {
    185         for (int j = 0; j < Map[i].Count; j++) {
    186           ClusterNumber[Map[i][j]] = i;
    187         }
    188       }
    189     }
     187    public virtual void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) { }
     188    #endregion
    190189  }
    191190}
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMDisatanceMap.cs

    r16899 r17002  
    1 using HEAL.Attic;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using HEAL.Attic;
    223using HeuristicLab.Common;
    324using HeuristicLab.Core;
    425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     26using System;
    527using System.Collections.Generic;
    628
     
    931  [StorableType("456692FB-2149-4359-8106-45D59D2D7FA0")]
    1032  public class EMMDisatanceMap : EMMMapBase<ISymbolicExpressionTree> {
    11 
    12     // Under the work now. Do not take into account =)
     33    [Storable]
     34    public List<List<double>> Probabilities { get; set; }
    1335    #region conctructors
    1436    [StorableConstructor]
    1537    protected EMMDisatanceMap(StorableConstructorFlag _) : base(_) { }
    16     public EMMDisatanceMap() : this(1) { }
    1738    public override IDeepCloneable Clone(Cloner cloner) {
    1839      return new EMMDisatanceMap(this, cloner);
    1940    }
    20     public EMMDisatanceMap(int k) : base(k) { ModelSet = new List<ISymbolicExpressionTree>(); }
     41    public EMMDisatanceMap() : base() { ModelSet = new List<ISymbolicExpressionTree>(); }
    2142    public EMMDisatanceMap(EMMDisatanceMap original, Cloner cloner) : base(original, cloner) { }
    2243    #endregion
    23     #region MapApplayFunctions
     44    #region MapCreation
    2445    override public void CreateMap(IRandom random, int k) {
    25       K = Map.Count;
    26       ApplyDistanceMapCreationAlgorithm(random, Distances, Map, K);
    27       for (int i = 0; i < Map.Count; i++) {
    28         ClusterNumber.Add(i);
     46
     47      Probabilities = new List<List<double>>();
     48      MapSizeCheck(ModelSet.Count);
     49      ApplyDistanceMapCreationAlgorithm(random, CalculateDistances(), Map, Probabilities);
     50    }
     51    public static void ApplyDistanceMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<List<double>> probabilities) {
     52      int mapSize = distances.GetLength(0);
     53      for (int t = 0; t < mapSize; t++) {
     54        probabilities.Add(new List<double>());
     55        double tempSum = 0;
     56        for (int i = 0; i < mapSize; i++) {
     57          tempSum += Math.Log(distances[i, t]);
     58        }
     59        for (int i = 0; i < mapSize; i++) {
     60          if (distances[i, t].IsAlmost(0))
     61            continue;
     62          map[t].Add(i);
     63          probabilities[t].Add(Math.Log(distances[i, t]) / tempSum);
     64        }
    2965      }
    3066    }
    31     public static void ApplyDistanceMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, int k, int neghboorNumber = 10) {
    32       int mapSize = distances.GetLength(0);
    33       List<double> currentList = new List<double>();
    34       for (int i = 0; i < mapSize; i++) {
    35         map.Add(new List<int>());
    36         for (int j = 0; j < mapSize; j++) {
    37           currentList.Add(distances[i, j]);
    38         }
    39         map[i].Add(HelpFunctions.ChooseMinElementIndex(currentList));
    40         while (map[i].Count < neghboorNumber) {
    41           map[i].Add(HelpFunctions.ChooseMinElementIndex(currentList, i, map[i]));
    42         }
    43         currentList.Clear();
    44       }
     67    #endregion
     68    #region MapApplayFunctions
     69    public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
     70      treeNumber = HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities[parentTreeNumber]);
     71      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
    4572    }
    46 
    47     override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber) {
    48       return NewModelForInizializtion(random, out cluster, out treeNumber);
     73    override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
     74      return NewModelForInizializtion(random, out treeNumber);
    4975    }
    5076    #endregion
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMIslandMap.cs

    r16899 r17002  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     26using HeuristicLab.Random;
    2627using System.Collections.Generic;
     28using System.Linq;
    2729
    2830namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
     
    3032  [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
    3133  public class EMMIslandMap : EMMMapBase<ISymbolicExpressionTree> {
     34    [Storable]
     35    public List<int> ClusterNumber { get; set; }  // May be only Island Map really need it
    3236    #region conctructors
    3337    [StorableConstructor]
    3438    protected EMMIslandMap(StorableConstructorFlag _) : base(_) { }
    35     public EMMIslandMap() : this(1) { }
     39    public EMMIslandMap() {
     40      ModelSet = new List<ISymbolicExpressionTree>();
     41      ClusterNumber = new List<int>();
     42    }
    3643    public override IDeepCloneable Clone(Cloner cloner) {
    3744      return new EMMIslandMap(this, cloner);
    3845    }
    39     public EMMIslandMap(int k) : base(k) { ModelSet = new List<ISymbolicExpressionTree>(); }
    40     public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) { }
    41     // public EMMIslandMap(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, int k, int neghboorNumber) : base(random, trees, k) { }
    42     //public EMMIslandMap(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, string fileName = "Map.txt") : base(random, trees, fileName) { }
     46    public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) {
     47      if (original.ClusterNumber != null) {
     48        ClusterNumber = original.ClusterNumber.ToList();
     49      }
     50    }
    4351    #endregion
    4452    #region MapApplayFunctions
    4553    override public void CreateMap(IRandom random, int k) {
    46       K = EMModelsClusterizationAlgorithm.ApplyClusteringAlgorithm(random, Distances, ClusterNumber, k);
    47       MapSizeCheck();
     54
     55      k = EMModelsClusterizationAlgorithm.ApplyClusteringAlgorithm(random, CalculateDistances(), ClusterNumber, k);
     56      MapSizeCheck(k);
    4857      for (int i = 0; i < ModelSet.Count; i++) {
    4958        Map[ClusterNumber[i]].Add(i);
    5059      }
    5160    }
    52     override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber) {
    53       return NewModelForInizializtion(random, out cluster, out treeNumber);
     61    override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
     62      return NewModelForInizializtion(random, out treeNumber);
     63    }
     64
     65    public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
     66      if (parentTreeNumber == -10) {
     67        treeNumber = random.Next(ModelSet.Count);
     68      } else {
     69        treeNumber = Map[ClusterNumber[parentTreeNumber]].SampleRandom(random);
     70      }
     71      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
     72    }
     73    public void ClusterNumbersCalculate() {  // May be it should be transported to Child Clase (IslandMap)
     74      for (int i = 0; i < Map.Count; i++) {
     75        for (int j = 0; j < Map[i].Count; j++) {
     76          ClusterNumber.Add(0);
     77        }
     78      }
     79      for (int i = 0; i < Map.Count; i++) {
     80        for (int j = 0; j < Map[i].Count; j++) {
     81          ClusterNumber[Map[i][j]] = i;
     82        }
     83      }
    5484    }
    5585    #endregion
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMultyPointsMutator.cs

    r16899 r17002  
    2727using HeuristicLab.Parameters;
    2828using HeuristicLab.Problems.DataAnalysis.Symbolic;
    29 using HeuristicLab.Random;
    3029using System.Collections.Generic;
    3130using System.Linq;
     
    4443    }
    4544    public List<ISymbolicExpressionTree> ModelSet => MapParameter.ActualValue.ModelSet;
    46     public List<int> ClusterNumber => MapParameter.ActualValue.ClusterNumber;
    4745    public List<List<int>> Map => MapParameter.ActualValue.Map;
    4846    public PercentValue MutationProbability => MutationProbabilityParameter.ActualValue;
     
    6159
    6260    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
    63       EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map, MutationProbability);
     61      EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, Map, MutationProbability);
    6462    }
    6563    private static bool SymbolTypeCheck(ISymbol nSymbol, IEnumerable<ISymbol> allSymbols) {
     
    7068      return false;
    7169    }
    72     public static void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<int> clusterNumber, List<List<int>> map, PercentValue mutationProbability) {
     70    public void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<List<int>> map, PercentValue mutationProbability) {
    7371
    7472      List<ISymbol> allowedSymbols = new List<ISymbol>();
     
    9391            }
    9492            int pTemp = random.Next(map.Count);
    95             if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.ClusterNumer; } //remeber the cluster number
     93            if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.TreeNumber; } //remeber the cluster number
    9694            var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); // set up probabilities
    9795
     
    102100
    103101            if (node is TreeModelTreeNode treeNode2) { // make rigth mutation for tree model
    104               treeNode2.TreeNumber = map[pTemp].SampleRandom(random);
    105               treeNode2.Tree = (ISymbolicExpressionTree)modelSet[treeNode2.TreeNumber].Clone();
    106               treeNode2.ClusterNumer = pTemp;
    107               treeNode2.SetLocalParameters(random, 1);
     102              treeNode2.TreeNumber = pTemp;
     103              MapParameter.ActualValue.NodeForMutationChange(random, treeNode2);
    108104            } else if (node.HasLocalParameters) { // make local parametrs set up for other node types
    109105              node.ResetLocalParameters(random);
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMultyPointsMutatorNodeTypeSaving.cs

    r16899 r17002  
    2727using HeuristicLab.Parameters;
    2828using HeuristicLab.Problems.DataAnalysis.Symbolic;
    29 using HeuristicLab.Random;
    3029using System.Collections.Generic;
    3130using System.Linq;
     
    4241    }
    4342    public List<ISymbolicExpressionTree> ModelSet => MapParameter.ActualValue.ModelSet;
    44     public List<int> ClusterNumber => MapParameter.ActualValue.ClusterNumber;
    4543    public List<List<int>> Map => MapParameter.ActualValue.Map;
    4644
     
    6462
    6563    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
    66       EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map, MutationProbability);
     64      EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, Map, MutationProbability);
    6765    }
    6866    private static bool SymbolTypeCheck(ISymbol nSymbol, IEnumerable<ISymbol> allSymbols) {
     
    7371      return false;
    7472    }
    75     public static void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<int> clusterNumber, List<List<int>> map, PercentValue mutationProbability) {
     73    public void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<List<int>> map, PercentValue mutationProbability) {
    7674
    7775      List<ISymbol> allowedSymbols = new List<ISymbol>();
     
    9593              allowedSymbols.Add(node.Symbol);
    9694            }
    97             int pTemp = random.Next(map.Count);
    98             if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.ClusterNumer; } //remeber the cluster number
     95            int pTemp = random.Next(modelSet.Count);
     96            if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.TreeNumber; } //remeber the cluster number
    9997            var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); // set up probabilities
    10098
     
    105103
    106104            if (node is TreeModelTreeNode treeNode2) { // make rigth mutation for tree model
    107               treeNode2.TreeNumber = map[pTemp].SampleRandom(random);
    108               treeNode2.Tree = (ISymbolicExpressionTree)modelSet[treeNode2.TreeNumber].Clone();
    109               treeNode2.ClusterNumer = pTemp;
    110               treeNode2.SetLocalParameters(random, 1);
     105              treeNode2.TreeNumber = pTemp;
     106              MapParameter.ActualValue.NodeForMutationChange(random, treeNode2);
    111107            } else if (node.HasLocalParameters) { // make local parametrs set up for other node types
    112108              node.ResetLocalParameters(random);
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMutators.cs

    r16899 r17002  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
    25 using HeuristicLab.Data;
    2625using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2726using HeuristicLab.Parameters;
    2827using HeuristicLab.Problems.DataAnalysis.Symbolic;
    29 using HeuristicLab.Random;
    3028using System.Collections.Generic;
    3129using System.Linq;
     
    4341
    4442    public List<ISymbolicExpressionTree> ModelSet => MapParameter.ActualValue.ModelSet;
    45     public List<int> ClusterNumber => MapParameter.ActualValue.ClusterNumber;
    4643    public List<List<int>> Map => MapParameter.ActualValue.Map;
    4744
     
    5855
    5956    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
    60       EMMMutatorsPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map);
     57      EMMMutatorsPart(random, symbolicExpressionTree, ModelSet, Map);
    6158    }
    62     public static void EMMMutatorsPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<int> clusterNumber, List<List<int>> map) {
     59    public void EMMMutatorsPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<List<int>> map) {
    6360      List<ISymbol> allowedSymbols = new List<ISymbol>();
    6461      ISymbolicExpressionTreeNode parent;
     
    109106
    110107        if (newNode is TreeModelTreeNode treeNode) {
    111           int p = random.Next(map.Count);
    112           if (child is TreeModelTreeNode chNode) { p = chNode.ClusterNumer; }
    113           treeNode.TreeNumber = map[p].SampleRandom(random);
    114           treeNode.Tree = (ISymbolicExpressionTree)modelSet[treeNode.TreeNumber].Clone();
    115           treeNode.ClusterNumer = p;
    116           treeNode.SetLocalParameters(random, 0.5);
     108          int p = random.Next(modelSet.Count);
     109          if (child is TreeModelTreeNode chNode) { p = chNode.TreeNumber; }
     110          treeNode.TreeNumber = p;
     111          MapParameter.ActualValue.NodeForMutationChange(random, treeNode);
    117112        } else if (newNode.HasLocalParameters)
    118113          newNode.ResetLocalParameters(random);
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMNetworkMap.cs

    r16899 r17002  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     26using HeuristicLab.Random;
    2627using System.Collections.Generic;
    2728
     
    4243    }
    4344    public EMMNetworkMap(EMMNetworkMap original, Cloner cloner) : base(original, cloner) { NeghboorNumber = original.NeghboorNumber; }
    44     //public EMMNetworkMap(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, int k, int neghboorNumber = 10) : this(neghboorNumber) {
    45     //  // constructor that should be used in case of creation of a new map from the start point
    46     //  ModelSet = trees.ToList();
    47     //  CalculateDistances();
    48     //  NeghboorNumber = neghboorNumber; // this shoud be done before map creation, so it is impossible to create tree wisout it
    49     //  CreateMap(random, k);
    50     //}
    51     //public EMMNetworkMap(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, string fileName = "Map.txt") : base(random, trees, fileName) {
    52     //  NeghboorNumber = Map[0].Count;
    53     //}
    5445    #endregion
    5546    #region MapTransformation
    5647    override public void CreateMap(IRandom random, int k) {
    57       K = Map.Count;
    58       ApplyNetworkMapCreationAlgorithm(random, Distances, Map, K, NeghboorNumber);
    59       for (int i = 0; i < Map.Count; i++) {
    60         ClusterNumber.Add(i);
    61       }
     48      ApplyNetworkMapCreationAlgorithm(random, CalculateDistances(), Map, NeghboorNumber);
    6249    }
    63     public static void ApplyNetworkMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, int k, int neghboorNumber = 10) {
     50    public static void ApplyNetworkMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, int neghboorNumber = 10) {
    6451      int mapSize = distances.GetLength(0);
    6552      List<double> currentList = new List<double>();
     
    7865    #endregion
    7966    #region Dialog with surroudings
    80     override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber) {
    81       var newTree = NewModelForInizializtion(random, out cluster, out treeNumber);
     67    public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
     68      treeNumber = Map[parentTreeNumber].SampleRandom(random);
     69      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
     70    }
     71    override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
     72      var newTree = NewModelForInizializtion(random, out treeNumber);
    8273      return newTree;
    8374    }
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMSolution.cs

    r16734 r17002  
    4545      Individual = individual;
    4646    }
    47 
     47    public EMMSolution(IScope scope) {
     48      Individual = scope;
     49      Qualities = (DoubleValue)scope.Variables["Quality"].Value;
     50    }
    4851    public EMMSolution(IItem individual, DoubleValue qualities) {
    4952      Individual = individual;
     
    6265
    6366    protected EMMSolution(EMMSolution original, Cloner cloner) : base(original, cloner) {
    64       Qualities = original.Qualities;
    65       Individual = (IItem)original.Individual.Clone(cloner);
     67      Qualities = cloner.Clone(original.Qualities);
     68      Individual = cloner.Clone(original.Individual);
    6669    }
    6770
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/HelpFunctions.cs

    r16899 r17002  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21using HeuristicLab.Core;
    222using System.Collections.Generic;
    323using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    6 using HEAL.Attic;
    724
    825namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    926  class HelpFunctions {
     27    public static int OneElementFromListProportionalSelection(IRandom random, List<double> list) {
     28      double selectedQuality = random.NextDouble() * list.Sum();
     29      int index = 0;
     30      double currentQuality = list[index];
     31      while ((currentQuality < selectedQuality) && (index < list.Count)) {
     32        index++;
     33        currentQuality += list[index];
     34      }
     35      return index;
     36    }
    1037    public static int ChooseMinElementIndex(List<double> distances, int currentElement, List<int> previousNumbers) {
    1138      double minValue = 100;
     
    2855      return minElementNumber;
    2956    }
     57
    3058    public static bool CheckNumberIsInList(int number, List<int> priviousNumber) {
    3159      foreach (var pNum in priviousNumber) {
     
    4674      return minElementNumber;
    4775    }
     76    public static int ChooseMaxElementIndex(List<double> averageClusterDistance) {
     77      double maxValue = averageClusterDistance[0];
     78      int maxElementNumber = 0;
     79      for (int i = 1; i < averageClusterDistance.Count(); i++) {
     80        if (averageClusterDistance[i] > maxValue) {
     81          maxValue = averageClusterDistance[i];
     82          maxElementNumber = i;
     83        }
     84      }
     85      return maxElementNumber;
     86    }
     87    public static double CheckSocialKatre(double socialKarteValue, double value, double stepValue) {
     88      if (value > (socialKarteValue + stepValue))
     89        return stepValue;
     90      else if (value > socialKarteValue)
     91        return (value - socialKarteValue);
     92      else return 0;
     93    }
     94    public static void ProbabilitiesUpDate(List<List<double>> sucsessStatistics, List<double> probabilities) {
     95
     96      var averageQuality = new List<double>();
     97      foreach (var variant in sucsessStatistics) {
     98        if (variant[0] > 0.005) {
     99          averageQuality.Add(variant[1] / variant[0]);
     100        } else { averageQuality.Add(0); }
     101      }
     102      int bestModelNumber = ChooseMaxElementIndex(averageQuality);
     103      double socialKarte = 1.0 / (probabilities.Count * 20.0); // parameters of method
     104      double stepValue = socialKarte / 5.0;
     105      double totalChangeValue = 0, changeValue = 0;
     106      for (int i = 0; i < probabilities.Count; i++) {
     107        changeValue = CheckSocialKatre(socialKarte, probabilities[i], stepValue);
     108        totalChangeValue += changeValue;
     109        probabilities[i] -= changeValue;
     110      }
     111      probabilities[bestModelNumber] += totalChangeValue;
     112    }
    48113  }
    49114}
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/HeuristicLab.Algorithms.EvolvmentModelsOfModels.csproj

    r16899 r17002  
    128128    <Compile Include="EMMBaseAlgorithm.cs" />
    129129    <Compile Include="EMMNetworkMap.cs" />
     130    <Compile Include="EMMSucsessMap.cs" />
     131    <Compile Include="EMMZeroMap.cs" />
    130132    <Compile Include="HelpFunctions.cs" />
     133    <Compile Include="ModelClustersFrequencyAnalyzer.cs" />
    131134    <Compile Include="Plugin.cs" />
    132135    <Compile Include="Properties\AssemblyInfo.cs" />
     136    <Compile Include="EMMRankMap.cs" />
    133137  </ItemGroup>
    134138  <ItemGroup>
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/ModelClustersFrequencyAnalyzer.cs

    r16899 r17002  
    2121
    2222using HEAL.Attic;
     23using HeuristicLab.Algorithms.EvolvmentModelsOfModels;
    2324using HeuristicLab.Analysis;
    2425using HeuristicLab.Common;
     
    4344
    4445    #region parameter properties
    45     [Storable]
     46    private const string MapParameterName = "Map";
    4647    public ILookupParameter<DataTable> ModelClustersFrequencyParameter {
    4748      get { return (ILookupParameter<DataTable>)Parameters[ModelClustersFrequencyParameterName]; }
    4849    }
    49     [Storable]
    5050    public IValueLookupParameter<BoolValue> AggregateModelClustersParameter {
    5151      get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateModelClustersParameterName]; }
    5252    }
     53    public ILookupParameter<EMMMapBase<ISymbolicExpressionTree>> MapParameter {
     54      get { return (ILookupParameter<EMMMapBase<ISymbolicExpressionTree>>)Parameters[MapParameterName]; }
     55    }
     56
    5357    #endregion
    5458    #region properties
     
    7175      Parameters.Add(new LookupParameter<DataTable>(ModelClustersFrequencyParameterName, "The relative Model Clusters reference frequencies aggregated over all trees in the population."));
    7276      Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateModelClustersParameterName, "Switch that determines whether all references to factor Model Clusters should be aggregated regardless of the value. Turn off to analyze all factor variable references with different values separately.", new BoolValue(true)));
     77      Parameters.Add(new LookupParameter<EMMMapBase<ISymbolicExpressionTree>>(MapParameterName));
    7378    }
    7479
     
    103108      int numberOfValues = datatable.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
    104109
    105       foreach (var pair in CalculateModelClustersFrequency(expressions)) {
     110      foreach (var pair in CalculateModelClustersFrequency(expressions, MapParameter.ActualValue)) {
    106111        if (!datatable.Rows.ContainsKey(pair.Key)) {
    107112          // initialize a new row for the variable and pad with zeros
     
    120125    }
    121126
    122     public static IEnumerable<KeyValuePair<string, double>> CalculateModelClustersFrequency(IEnumerable<ISymbolicExpressionTree> trees) {
     127    public static IEnumerable<KeyValuePair<string, double>> CalculateModelClustersFrequency(IEnumerable<ISymbolicExpressionTree> trees, EMMMapBase<ISymbolicExpressionTree> map) {
    123128      var modelClustersFrequency = trees
    124           .SelectMany(t => GetModelClustersReferences(t))
     129          .SelectMany(t => GetModelClustersReferences(t, map))
    125130          .GroupBy(pair => pair.Key, pair => pair.Value)
    126           .ToDictionary(g => g.Key, g => (double)g.Sum());
     131          .ToDictionary(g => g.Key, g => g.Sum());
    127132
    128133      double totalNumberOfSymbols = modelClustersFrequency.Values.Sum();
     
    132137    }
    133138
    134     private static IEnumerable<KeyValuePair<string, int>> GetModelClustersReferences(ISymbolicExpressionTree tree) {
    135       Dictionary<string, int> references = new Dictionary<string, int>();
    136       foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) {
    137         string referenceId = "Cluster " + treeNode.ClusterNumer;
    138         if (references.ContainsKey(referenceId)) {
    139           references[referenceId]++;
     139    private static IEnumerable<KeyValuePair<string, double>> GetModelClustersReferences(ISymbolicExpressionTree tree, EMMMapBase<ISymbolicExpressionTree> map) {
     140      Dictionary<string, double> references = new Dictionary<string, double>();
     141      if (map is EMMIslandMap island) {
     142        foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) {
     143          string referenceId = "no";
     144
     145          referenceId = "Cluster " + island.ClusterNumber[treeNode.TreeNumber];
     146          if (references.ContainsKey(referenceId)) {
     147            int a = (int)references[referenceId];
     148            a++;
     149            references[referenceId] = a;
     150          } else {
     151            references[referenceId] = 1;
     152          }
     153        }
     154      } else {
     155        if (map is EMMSucsessMap sMap) {
     156          for (int i = 0; i < map.ModelSet.Count; i++) {
     157            string referenceId = "Tree Probability" + i;
     158            references[referenceId] = sMap.Probabilities[i];
     159          }
    140160        } else {
    141           references[referenceId] = 1;
     161          string referenceId = "no";
     162          references[referenceId] = 0;
    142163        }
    143164      }
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Plugin.cs.frame

    r16722 r17002  
    2828  [Plugin("HeuristicLab.Algorithms.EMMAlgorithm", "3.3.15.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Algorithms.EMMAlgorithm-3.3.dll", PluginFileType.Assembly)]
     30  [PluginDependency("HeuristicLab.Algorithms.DataAnalysis", "3.4")]
    3031  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    3132  [PluginDependency("HeuristicLab.Collections", "3.3")]
     
    3334  [PluginDependency("HeuristicLab.Core", "3.3")]
    3435  [PluginDependency("HeuristicLab.Data", "3.3")]
     36  [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")]
    3537  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3638  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    37   [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    3839  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    39   [PluginDependency("HeuristicLab.Persistence", "3.3")]
     40  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic", "3.4")]
    4041  [PluginDependency("HeuristicLab.Random", "3.3")]
    4142  [PluginDependency("HeuristicLab.Selection", "3.3")]
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/ModelsFrequencyAnalyzer.cs

    r16899 r17002  
    4343
    4444    #region parameter properties
    45     [Storable]
     45
    4646    public ILookupParameter<DataTable> ModelFrequencyParameter {
    4747      get { return (ILookupParameter<DataTable>)Parameters[ModelsFrequencyParameterName]; }
    4848    }
    49     [Storable]
     49
    5050    public IValueLookupParameter<BoolValue> AggregateModelParameter {
    5151      get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateModelParameterName]; }
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/TerminalNodesFrequencyAnalyzer.cs

    r16899 r17002  
    4343
    4444    #region parameter properties
    45     [Storable]
    4645    public ILookupParameter<DataTable> TerminalNodesFrequencyParameter {
    4746      get { return (ILookupParameter<DataTable>)Parameters[TerminalNodesFrequencyParameterName]; }
    4847    }
    49     [Storable]
    5048    public IValueLookupParameter<BoolValue> AggregateTerminalNodesParameter {
    5149      get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateTerminalNodesParameterName]; }
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16760 r17002  
    216216    <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeVariablesNumberAnalyzer.cs" />
    217217    <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeComplexcityAnalyzer.cs" />
    218     <Compile Include="Analyzers\ModelClustersFrequencyAnalyzer.cs" />
    219218    <Compile Include="Analyzers\ModelsFrequencyAnalyzer.cs" />
    220219    <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" />
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/ModelTreeNode.cs

    r16899 r17002  
    3939      set { tree = value; }
    4040    }
    41     int clusterNumer = -10;
    42     int treeNumber = 0;
    43     public int ClusterNumer {
    44       get { return clusterNumer; }
    45       set { clusterNumer = value; }
    46     }
     41    [Storable]
     42    int treeNumber = -10;
    4743    public int TreeNumber {
    4844      get { return treeNumber; }
     
    5147
    5248    [StorableConstructor]
    53     private TreeModelTreeNode(StorableConstructorFlag _) : base(_) {
    54       ClusterNumer = -10;
    55     }
     49    private TreeModelTreeNode(StorableConstructorFlag _) : base(_) { }
    5650
    5751    private TreeModelTreeNode(TreeModelTreeNode original, Cloner cloner)
    5852      : base(original, cloner) {
    5953      tree = (ISymbolicExpressionTree)original.tree.Clone(cloner);
    60       ClusterNumer = original.ClusterNumer;
    6154      TreeNumber = original.TreeNumber;
    6255    }
    6356
    6457    private TreeModelTreeNode() : base() { }
    65     public TreeModelTreeNode(TreeModel treeModelSymbol) : base(treeModelSymbol) {
    66       ClusterNumer = -10;
    67     }
     58    public TreeModelTreeNode(TreeModel treeModelSymbol) : base(treeModelSymbol) { }
    6859
    6960    public override bool HasLocalParameters {
Note: See TracChangeset for help on using the changeset viewer.