#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Parameters;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
using HeuristicLab.Random;
using System;
using System.Collections.Generic;
namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
[Item("NetworkMap", "A map of models of models of models")]
[StorableType("C200ECC2-6D33-4468-A538-580B07D75B3C")]
public class EMMNetworkMap : EMMMapBase {
private const string NegbourTypeParameterName = "NegbourType";
private const string NegbourNumberParameterName = "NegbourNumber";
public IFixedValueParameter NegbourTypeParameter {
get { return (IFixedValueParameter)Parameters[NegbourTypeParameterName]; }
}
public IValueParameter NegbourNumberParameter {
get { return (IValueParameter)Parameters[NegbourNumberParameterName]; }
}
public StringValue NegbourType {
get { return NegbourTypeParameter.Value; }
set { NegbourTypeParameter.Value.Value = value.Value; }
}
public IntValue NegbourNumber {
get { return NegbourNumberParameter.Value; }
set { NegbourNumberParameter.Value.Value = value.Value; }
}
#region constructors
[StorableConstructor]
protected EMMNetworkMap(StorableConstructorFlag _) : base(_) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new EMMNetworkMap(this, cloner);
}
public EMMNetworkMap() : base() {
Parameters.Add(new ValueParameter(NegbourNumberParameterName, "The parameter for FullMap type of map creation algorithm. Use one from: 10, 20.", new IntValue(10)));
Parameters.Add(new FixedValueParameter(NegbourTypeParameterName, "The parameter for FullMap type of map creation algorithm. Use one from: Percent, Number.", new StringValue("Number")));
MapParameterUpdate();
ModelSet = new List();
}
public EMMNetworkMap(EMMNetworkMap original, Cloner cloner) : base(original, cloner) { NegbourNumber = original.NegbourNumber; }
#endregion
#region Map Transformation
override public void CreateMap(IRandom random) {
MapParameterUpdate();
if (Map != null) {
Map.Clear();
}
ApplyNetworkMapCreationAlgorithm(random, ModelSetPreparation.CalculateDistances(ModelSet), Map, NegbourNumber.Value);
}
override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
MapParameterUpdate();
if (Map != null) {
Map.Clear();
}
ApplyNetworkMapCreationAlgorithm(random, ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, NegbourNumber.Value);
}
override public void MapRead(IEnumerable trees) {
base.MapRead(trees);
string fileName = ("Map" + DistanceParametr + ".txt");
Map = FileComuncations.IntMatrixFromFileRead(fileName);
NegbourNumber.Value = Map[0].Count;
}
public static void ApplyNetworkMapCreationAlgorithm(IRandom random, double[,] distances, List> map, int neghboorNumber = 10) {
int mapSize = distances.GetLength(0);
List currentList = new List();
for (int i = 0; i < mapSize; i++) {
map.Add(new List());
for (int j = 0; j < mapSize; j++) {
currentList.Add(distances[i, j]);
}
map[i].Add(HelpFunctions.ChooseMinElementIndex(currentList));
while (map[i].Count < neghboorNumber) {
map[i].Add(HelpFunctions.ChooseMinElementIndex(currentList, i, map[i]));
}
currentList.Clear();
}
}
protected void MapParameterUpdate() {
switch (NegbourType.Value) {
case "Percent": NegbourNumber.Value = Convert.ToInt32((Convert.ToDouble(ModelSet.Count)) * (Convert.ToDouble(NegbourNumber.Value)) / 100.0); break;
case "Number": NegbourNumber.Value = NegbourNumber.Value; break;
default: NegbourNumber.Value = NegbourNumber.Value; break;
}
}
#endregion
#region Dialog with surroundings
public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
treeNumber = Map[parentTreeNumber].SampleRandom(random);
return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
}
override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
var newTree = NewModelForInizializtion(random, out treeNumber);
return newTree;
}
#endregion
}
}