Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17005


Ignore:
Timestamp:
06/11/19 20:59:44 (5 years ago)
Author:
gkronber
Message:

#2994: merged r16944:16997 from trunk to branch

Location:
branches/2994-AutoDiffForIntervals
Files:
1 deleted
30 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/2994-AutoDiffForIntervals

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Common/3.3/Content/ContentManager.cs

    r16565 r17005  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using System.Threading;
     26using System.Threading.Tasks;
     27using HEAL.Attic;
    2428
    2529namespace HeuristicLab.Common {
    2630  public abstract class ContentManager {
     31    public class Info {
     32      public Info() { }
     33
     34      public Info(string filename, TimeSpan duration) {
     35        Filename = filename;
     36        Duration = duration;
     37      }
     38
     39      public Info(string filename, SerializationInfo serInfo) {
     40        Filename = filename;
     41        Duration = serInfo.Duration;
     42        UnknownTypeGuids = serInfo.UnknownTypeGuids;
     43        NumberOfSerializedObjects = serInfo.NumberOfSerializedObjects;
     44        SerializedTypes = serInfo.SerializedTypes;       
     45      }
     46
     47      public TimeSpan Duration { get; internal set; }
     48      public IEnumerable<Guid> UnknownTypeGuids { get; internal set; } = Enumerable.Empty<Guid>();
     49      public int NumberOfSerializedObjects { get; internal set; }
     50      public IEnumerable<Type> SerializedTypes { get; internal set; } = Enumerable.Empty<Type>();
     51      public string Filename { get; internal set; } = string.Empty;
     52    }
     53
    2754    private static ContentManager instance;
    2855
     
    3663
    3764    public static IStorableContent Load(string filename) {
     65      return Load(filename, out var _);
     66    }
     67
     68    public static IStorableContent Load(string filename, out Info serializationInfo) {
    3869      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    39       IStorableContent content = instance.LoadContent(filename);
    40       content.Filename = filename;
     70      IStorableContent content = instance.LoadContent(filename, out serializationInfo);
     71     
     72      if (content != null) content.Filename = filename;
    4173      return content;
    4274    }
    43     public static void LoadAsync(string filename, Action<IStorableContent, Exception> loadingCompletedCallback) {
     75
     76
     77    public static Task LoadAsync(string filename, Action<IStorableContent, Exception> loadingCompletedCallback) {
     78      return LoadAsync(filename, (content, error, info) => loadingCompletedCallback(content, error)); // drop info
     79    }
     80
     81    public static async Task LoadAsync(string filename, Action<IStorableContent, Exception, Info> loadingCompletedCallback) {
    4482      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
    45       var func = new Func<string, IStorableContent>(instance.LoadContent);
    46       func.BeginInvoke(filename, delegate (IAsyncResult result) {
    47         Exception error = null;
    48         IStorableContent content = null;
    49         try {
    50           content = func.EndInvoke(result);
    51           content.Filename = filename;
    52         } catch (Exception ex) {
    53           error = ex;
    54         }
    55         loadingCompletedCallback(content, error);
    56       }, null);
     83
     84      Exception error = null;
     85      IStorableContent result = null;
     86      Info serializationInfo = null;
     87      try {
     88        result = await Task.Run(() => {
     89          var content = instance.LoadContent(filename, out serializationInfo);
     90          if(content!=null) content.Filename = filename;
     91          return content;
     92        });
     93      } catch(Exception ex) {
     94        error = ex;
     95      }
     96      loadingCompletedCallback(result, error, serializationInfo);
    5797    }
    58     protected abstract IStorableContent LoadContent(string filename);
     98
     99    protected abstract IStorableContent LoadContent(string filename, out Info serializationInfo);
    59100
    60101    public static void Save(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken = default(CancellationToken)) {
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core.Views

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core.Views/3.3/Clipboard.cs

    r16565 r17005  
    2424using System.Collections.Generic;
    2525using System.IO;
    26 using System.IO.Compression;
    2726using System.Linq;
    2827using System.Threading;
     
    3130using HeuristicLab.Common;
    3231using HeuristicLab.MainForm;
    33 using HeuristicLab.Persistence.Default.Xml;
    3432using HeuristicLab.PluginInfrastructure;
    3533
     
    157155      foreach (string filename in items) {
    158156        try {
    159           var ser = new ProtoBufSerializer();
    160           T item = (T)ser.Deserialize(filename);
     157          T item = (T)ContentManager.Load(filename);
    161158          OnItemLoaded(item, progressBar.Maximum / items.Length);
    162         } catch (Exception) {
    163           try {
    164             // try old format if protobuf deserialization fails
    165             T item = XmlParser.Deserialize<T>(filename);
    166             OnItemLoaded(item, progressBar.Maximum / items.Length);
    167           } catch (Exception) { }
     159        } catch(Exception) {
     160          // ignore if loading a clipboad item fails.
    168161        }
    169162      }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core.Views/3.3/HeuristicLab.Core.Views-3.3.csproj

    r16658 r17005  
    375375      <Private>False</Private>
    376376    </ProjectReference>
    377     <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
    378       <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
    379       <Name>HeuristicLab.Persistence-3.3</Name>
    380       <Private>False</Private>
    381     </ProjectReference>
    382377    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    383378      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core.Views/3.3/Plugin.cs.frame

    r16658 r17005  
    3535  [PluginDependency("HeuristicLab.MainForm", "3.3")]
    3636  [PluginDependency("HeuristicLab.MainForm.WindowsForms", "3.3")]
    37   [PluginDependency("HeuristicLab.Persistence", "3.3")]
    3837  public class HeuristicLabCoreViewsPlugin : PluginBase {
    3938  }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Core/3.3/PersistenceContentManager.cs

    r16943 r17005  
    2525using HEAL.Attic;
    2626using System;
     27using System.Diagnostics;
    2728
    2829namespace HeuristicLab.Core {
     
    3031    public PersistenceContentManager() : base() { }
    3132
    32     protected override IStorableContent LoadContent(string filename) {
    33       // first try to load using the new persistence format
    34       var ser = new ProtoBufSerializer();
    35       try {
    36         return (IStorableContent)ser.Deserialize(filename, out SerializationInfo info);
    37       } catch (Exception e) {
    38         try {
    39           // try old format if new format fails
    40           return XmlParser.Deserialize<IStorableContent>(filename);
    41         } catch (Exception e2) {
    42           throw new AggregateException($"Cannot open file {filename}", e, e2);
    43         }
     33    protected override IStorableContent LoadContent(string filename, out Info info) {
     34      bool useOldPersistence = XmlParser.CanOpen(filename);
     35      IStorableContent content = null;
     36      if (useOldPersistence) {
     37        var sw = new Stopwatch();
     38        sw.Start();
     39        content = XmlParser.Deserialize<IStorableContent>(filename);
     40        sw.Stop();
     41        info = new Info(filename, sw.Elapsed);
     42      } else {
     43        var ser = new ProtoBufSerializer();
     44        content = (IStorableContent)ser.Deserialize(filename, out SerializationInfo serInfo);
     45        info = new Info(filename, serInfo);
    4446      }
     47      if (content == null) throw new PersistenceException($"Cannot deserialize root element of {filename}");
     48      return content;
    4549    }
     50
    4651
    4752    protected override void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken) {
  • branches/2994-AutoDiffForIntervals/HeuristicLab.DataPreprocessing.Views

  • branches/2994-AutoDiffForIntervals/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs

    r16565 r17005  
    411411        var foundIndices = new List<int>();
    412412        for (int idx = 0; idx < filteredRows.Length; ++idx) {
    413           var notFilteredThusFound = !filteredRows[idx];
    414           if (notFilteredThusFound) {
     413          if (filteredRows[idx]) {
    415414            foundIndices.Add(idx);
    416415          }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.ExtLibs

  • branches/2994-AutoDiffForIntervals/HeuristicLab.ExtLibs/HeuristicLab.OrTools/7.0.0/HeuristicLab.OrTools-7.0.0/HeuristicLab.OrTools-7.0.0.csproj

    r16910 r17005  
    4343  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    4444    <DebugSymbols>true</DebugSymbols>
    45     <OutputPath>bin\x64\Debug\</OutputPath>
     45    <OutputPath>..\..\..\..\bin\</OutputPath>
    4646    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4747    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     
    5252  </PropertyGroup>
    5353  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    54     <OutputPath>bin\x64\Release\</OutputPath>
     54    <OutputPath>..\..\..\..\bin\</OutputPath>
    5555    <DefineConstants>TRACE</DefineConstants>
    5656    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     
    6363  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    6464    <DebugSymbols>true</DebugSymbols>
    65     <OutputPath>bin\x86\Debug\</OutputPath>
     65    <OutputPath>..\..\..\..\bin\</OutputPath>
    6666    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6767    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     
    7272  </PropertyGroup>
    7373  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    74     <OutputPath>bin\x86\Release\</OutputPath>
     74    <OutputPath>..\..\..\..\bin\</OutputPath>
    7575    <DefineConstants>TRACE</DefineConstants>
    7676    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer/3.3/FileManager.cs

    r16565 r17005  
    2323using System.Collections.Generic;
    2424using System.IO;
     25using System.Linq;
    2526using System.Threading;
    2627using System.Windows.Forms;
     
    7374    }
    7475
    75     private static void LoadingCompleted(IStorableContent content, Exception error) {
     76    private static void LoadingCompleted(IStorableContent content, Exception error, ContentManager.Info info) {
    7677      try {
    7778        if (error != null) throw error;
     79        if (info!=null && info.UnknownTypeGuids.Any()) {
     80          var message = "Unknown type guids: " + string.Join(Environment.NewLine, info.UnknownTypeGuids);
     81          MessageBox.Show((Control)MainFormManager.MainForm, message, $"File {info.Filename} not restored completely", MessageBoxButtons.OK, MessageBoxIcon.Information);
     82        }
    7883        IView view = MainFormManager.MainForm.ShowContent(content);
    7984        if (view == null)
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer/3.3/HeuristicLab.Optimizer-3.3.csproj

    r16658 r17005  
    312312      <Private>False</Private>
    313313    </ProjectReference>
    314     <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
    315       <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
    316       <Name>HeuristicLab.Persistence-3.3</Name>
    317       <Private>False</Private>
    318     </ProjectReference>
    319314    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    320315      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer/3.3/Plugin.cs.frame

    r16658 r17005  
    4343  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    4444  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    45   [PluginDependency("HeuristicLab.Persistence", "3.3")]
    4645  [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]
    4746  public class HeuristicLabOptimizerPlugin : PluginBase {
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer/3.3/StartPage.cs

    r16565 r17005  
    3030using HeuristicLab.Core;
    3131using HeuristicLab.MainForm;
    32 using HeuristicLab.Persistence.Default.Xml;
    3332
    3433namespace HeuristicLab.Optimizer {
     
    121120        using (var stream = assembly.GetManifestResourceStream(name)) {
    122121          WriteStreamToTempFile(stream, path); // create a file in a temporary folder (persistence cannot load these files directly from the stream)
    123           var item = XmlParser.Deserialize<INamedItem>(path);
     122          var item = (INamedItem)ContentManager.Load(path);
    124123          OnSampleLoaded(item, group, 1.0 / count);
    125124        }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Persistence

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r16565 r17005  
    288288      }
    289289    }
     290
     291    /// <summary>
     292    /// Checks if the given file can be opened as <see cref="ZipArchive" />.
     293    /// </summary>
     294    /// <param name="filename">The filename.</param>
     295    /// <returns><see langword="true" /> if the file can be opened as <see cref="ZipArchive" />; otherwise, <see langword="false" />.</returns>
     296    public static bool CanOpen(string filename) {
     297      try {
     298        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
     299          using (ZipArchive zip = new ZipArchive(fs)) {
     300            return true;
     301          }
     302        }
     303      } catch (InvalidDataException) {
     304        return false;
     305      }
     306    }
    290307  }
    291308}
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs

    r16565 r17005  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Diagnostics;
    25 using System.Linq;
     24using HEAL.Attic;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    2928using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3029using HeuristicLab.Parameters;
    31 using HEAL.Attic;
    3230
    3331namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    3634  public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
    3735    private const string StrictSimilarityParameterName = "StrictSimilarity";
     36    private const string AverageSimilarityParameterName = "AverageSimilarity";
    3837
    3938    private readonly object locker = new object();
    4039
     40    private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
     41
    4142    public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
    4243      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
     44    }
     45
     46    public ILookupParameter<DoubleValue> AverageSimilarityParameter {
     47      get { return (ILookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; }
    4348    }
    4449
     
    5863    public PearsonRSquaredAverageSimilarityEvaluator() : base() {
    5964      Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
     65      Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName));
    6066    }
    6167
    62     public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity
     68    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity
    6369
    6470    public override IOperation InstrumentedApply() {
     
    7379        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    7480      }
    75       double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);
    76       QualitiesParameter.ActualValue = new DoubleArray(qualities);
    77       return base.InstrumentedApply();
    78     }
    7981
    80     public double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, int decimalPlaces) {
    81       double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
    82       if (decimalPlaces >= 0)
    83         r2 = Math.Round(r2, decimalPlaces);
     82      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
    8483
    85       var variables = ExecutionContext.Scope.Variables;
    86       if (!variables.ContainsKey("AverageSimilarity")) {
    87         lock (locker) {
    88           CalculateAverageSimilarities(ExecutionContext.Scope.Parent.SubScopes.Where(x => x.Variables.ContainsKey("SymbolicExpressionTree")).ToArray(), StrictSimilarity);
     84      if (DecimalPlaces >= 0)
     85        r2 = Math.Round(r2, DecimalPlaces);
    8986
     87      lock (locker) {
     88        if (AverageSimilarityParameter.ActualValue == null) {
     89          var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent);
     90          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
     91          SimilarityCalculator.Execute(context, CancellationToken);
    9092        }
    9193      }
     94      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
    9295
    93       double avgSim = ((DoubleValue)variables["AverageSimilarity"].Value).Value;
    94       return new double[2] { r2, avgSim };
     96      QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
     97      return base.InstrumentedApply();
    9598    }
    9699
    97100    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
    98101      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
     102      AverageSimilarityParameter.ExecutionContext = context;
    99103      EstimationLimitsParameter.ExecutionContext = context;
    100104      ApplyLinearScalingParameter.ExecutionContext = context;
    101       // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.
    102105
    103       double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
     106      var estimationLimits = EstimationLimitsParameter.ActualValue;
     107      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
     108
     109      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
     110
     111      lock (locker) {
     112        if (AverageSimilarityParameter.ActualValue == null) {
     113          var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent);
     114          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
     115          SimilarityCalculator.Execute(context, CancellationToken);
     116        }
     117      }
     118      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
    104119
    105120      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
     
    107122      ApplyLinearScalingParameter.ExecutionContext = null;
    108123
    109       return quality;
    110     }
    111 
    112     private readonly Stopwatch sw = new Stopwatch();
    113     public void CalculateAverageSimilarities(IScope[] treeScopes, bool strict) {
    114       var trees = treeScopes.Select(x => (ISymbolicExpressionTree)x.Variables["SymbolicExpressionTree"].Value).ToArray();
    115       var similarityMatrix = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(trees, simplify: false, strict: strict);
    116 
    117       for (int i = 0; i < treeScopes.Length; ++i) {
    118         var scope = treeScopes[i];
    119         var avgSimilarity = 0d;
    120         for (int j = 0; j < trees.Length; ++j) {
    121           if (i == j) continue;
    122           avgSimilarity += similarityMatrix[i, j];
    123         }
    124         avgSimilarity /= trees.Length - 1;
    125 
    126         if (scope.Variables.ContainsKey("AverageSimilarity")) {
    127           ((DoubleValue)scope.Variables["AverageSimilarity"].Value).Value = avgSimilarity;
    128         } else {
    129           scope.Variables.Add(new Core.Variable("AverageSimilarity", new DoubleValue(avgSimilarity)));
    130         }
    131       }
     124      return new[] { r2, avgSimilarity };
    132125    }
    133126  }
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers

    • Property svn:ignore set to
      SymbolicDataAnalysisBuildingBlockAnalyzer.cs
      SymbolicDataAnalysisHashBasedDiversityAnalyzer.cs
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs

    r16565 r17005  
    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
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
    4 
     25using HEAL.Attic;
    526using HeuristicLab.Common;
    627using HeuristicLab.Core;
     
    829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    930using HeuristicLab.Parameters;
    10 using HEAL.Attic;
    1131using HeuristicLab.Random;
    1232using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions;
    1333
    1434namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    15   [Item("DiversityCrossover", "Simple crossover operator prioritizing internal nodes according to the given probability.")]
     35  [Item("DiversityCrossover", "Simple crossover operator preventing swap between subtrees with the same hash value.")]
    1636  [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")]
    1737  public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     
    2040    private const string WindowingParameterName = "Windowing";
    2141    private const string ProportionalSamplingParameterName = "ProportionalSampling";
     42    private const string StrictHashingParameterName = "StrictHashing";
    2243
    2344    private static readonly Func<byte[], ulong> hashFunction = HashUtil.JSHash;
     
    3556      get { return (IValueLookupParameter<BoolValue>)Parameters[ProportionalSamplingParameterName]; }
    3657    }
     58
     59    public IFixedValueParameter<BoolValue> StrictHashingParameter {
     60      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictHashingParameterName]; }
     61    }
    3762    #endregion
    3863
     
    4974      get { return ProportionalSamplingParameter.ActualValue; }
    5075    }
     76
     77    bool StrictHashing {
     78      get { return StrictHashingParameter.Value.Value; }
     79    }
    5180    #endregion
     81
     82
     83    [StorableHook(HookType.AfterDeserialization)]
     84    private void AfterDeserialization() {
     85      if (!Parameters.ContainsKey(StrictHashingParameterName)) {
     86        Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
     87      }
     88    }
    5289
    5390    public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() {
     
    5693      Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false)));
    5794      Parameters.Add(new ValueLookupParameter<BoolValue>(ProportionalSamplingParameterName, "Select cutpoints proportionally using probabilities as weights instead of randomly.", new BoolValue(true)));
     95      Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
    5896    }
    5997
     
    72110    }
    73111
    74     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false) {
    75       var leafCrossoverPointProbability = 1 - internalCrossoverPointProbability;
    76 
    77       var nodes0 = ActualRoot(parent0).MakeNodes().Sort(hashFunction);
    78       var nodes1 = ActualRoot(parent1).MakeNodes().Sort(hashFunction);
     112    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false, bool strictHashing = false) {
     113      var nodes0 = ActualRoot(parent0).MakeNodes(strictHashing).Sort(hashFunction);
     114      var nodes1 = ActualRoot(parent1).MakeNodes(strictHashing).Sort(hashFunction);
    79115
    80116      IList<HashNode<ISymbolicExpressionTreeNode>> sampled0;
     
    126162      var proportionalSampling = ProportionalSampling.Value;
    127163
    128       return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling);
     164      return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling, StrictHashing);
    129165    }
    130166
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs

    r16565 r17005  
    2121
    2222using System;
    23 using System.Collections.Generic;
     23using System.Linq;
    2424
    2525namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    3838      public SimplifyAction Simplify;
    3939
    40       public IComparer<T> Comparer;
     40      //public IComparer<T> Comparer;
    4141
    4242      public bool IsLeaf => Arity == 0;
    4343
    44       public HashNode(IComparer<T> comparer) {
    45         Comparer = comparer;
    46       }
    47 
    48       private HashNode() { }
     44      //public HashNode(IComparer<T> comparer) {
     45      //  Comparer = comparer;
     46      //}
     47
     48      //public HashNode() { }
    4949
    5050      public int CompareTo(HashNode<T> other) {
    51         var res = Comparer.Compare(Data, other.Data);
    52         return res == 0 ? CalculatedHashValue.CompareTo(other.CalculatedHashValue) : res;
     51        return CalculatedHashValue.CompareTo(other.CalculatedHashValue);
    5352      }
    5453
     
    103102
    104103    public static HashNode<T>[] Simplify<T>(this HashNode<T>[] nodes, Func<byte[], ulong> hashFunction) where T : class {
    105       var reduced = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction);
    106 
    107       for (int i = 0; i < reduced.Length; ++i) {
    108         var node = reduced[i];
    109         if (node.IsLeaf) {
    110           continue;
    111         }
    112         node.Simplify?.Invoke(ref reduced, i);
    113       }
    114       // detect if anything was simplified
    115       var count = 0;
    116       foreach (var node in reduced) {
    117         if (!node.Enabled) { ++count; }
    118       }
    119       if (count == 0) {
    120         return reduced;
    121       }
    122 
    123       var simplified = new HashNode<T>[reduced.Length - count];
    124       int j = 0;
    125       foreach (var node in reduced) {
    126         if (node.Enabled) {
    127           simplified[j++] = node;
    128         }
    129       }
    130       return simplified.UpdateNodeSizes().Reduce().Sort(hashFunction);
     104      bool simplified = false;
     105      nodes = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction);
     106      do {
     107        if (simplified) {
     108          simplified = false;
     109          nodes = nodes.Where(x => x.Enabled).ToArray().UpdateNodeSizes().Reduce().Sort(hashFunction);
     110        }
     111
     112        for (int i = 0; i < nodes.Length; ++i) {
     113          var node = nodes[i];
     114          if (node.IsLeaf) {
     115            continue;
     116          }
     117          node.Simplify?.Invoke(ref nodes, i);
     118          for (int j = i - node.Size; j < i; ++j) {
     119            // detect if anything was simplified
     120            if (!nodes[j].Enabled) {
     121              simplified = true;
     122              break;
     123            }
     124          }
     125        }
     126      } while (simplified);
     127      return nodes.UpdateNodeSizes().Sort(hashFunction);
    131128    }
    132129
     
    207204    }
    208205
    209     private static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
     206    public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
    210207      int count = 0;
    211208      for (int i = 0; i < nodes.Length; ++i) {
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r16478 r17005  
    3838    private static readonly Constant constant = new Constant();
    3939
    40     private static readonly ISymbolicExpressionTreeNodeComparer comparer = new SymbolicExpressionTreeNodeComparer();
    4140    private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0);
    4241
     
    7473      }
    7574      var hash = (ulong)name.GetHashCode();
    76       var hashNode = new HashNode<ISymbolicExpressionTreeNode>(comparer) {
     75      var hashNode = new HashNode<ISymbolicExpressionTreeNode> {
    7776        Data = node,
    7877        Arity = node.SubtreeCount,
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16674 r17005  
    132132  <ItemGroup>
    133133    <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" />
    134     <Compile Include="Analyzers\SymbolicDataAnalysisBuildingBlockAnalyzer.cs" />
    135134    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" />
    136135    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
     
    173172    <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs" />
    174173    <Compile Include="Selectors\DiversitySelector.cs" />
     174    <Compile Include="SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs" />
    175175    <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" />
    176176    <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" />
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Tests

  • branches/2994-AutoDiffForIntervals/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/PersistenceConsistencyChecks.cs

    r16911 r17005  
    1818      var dict = new Dictionary<Guid, string>();
    1919      var duplicates = new Dictionary<string, string>();
    20       //get all non-generic and instantiable classes which implement IContentView
    2120      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(object))) {
    2221        var attr = StorableTypeAttribute.GetStorableTypeAttribute(type);
Note: See TracChangeset for help on using the changeset viewer.