Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13005


Ignore:
Timestamp:
10/15/15 12:18:42 (9 years ago)
Author:
ascheibe
Message:

#2377 applied and improved bburlacus patch

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis/3.3/PopulationSimilarityAnalysis/PopulationSimilarityAnalyzer.cs

    r12076 r13005  
    3939  public class PopulationSimilarityAnalyzer : SingleSuccessorOperator, IAnalyzer, ISimilarityBasedOperator {
    4040    private const string DiversityResultNameParameterName = "DiversityResultsName";
     41    private const string ExecuteInParallelParameterName = "ExecuteInParallel";
     42    private const string MaxDegreeOfParallelismParameterName = "MaxDegreeOfParallelism";
    4143
    4244    #region Backwards compatible code, remove with 3.4
     
    7173      get { return (FixedValueParameter<StringValue>)Parameters[DiversityResultNameParameterName]; }
    7274    }
     75    public IFixedValueParameter<BoolValue> ExecuteInParallelParameter {
     76      get { return (IFixedValueParameter<BoolValue>)Parameters[ExecuteInParallelParameterName]; }
     77    }
     78    public IFixedValueParameter<IntValue> MaxDegreeOfParallelismParameter {
     79      get { return (IFixedValueParameter<IntValue>)Parameters[MaxDegreeOfParallelismParameterName]; }
     80    }
    7381
    7482    public string DiversityResultName {
     
    7785    }
    7886
     87    public bool ExecuteInParallel {
     88      get { return ExecuteInParallelParameter.Value.Value; }
     89      set { ExecuteInParallelParameter.Value.Value = value; }
     90    }
     91
     92    public int MaxDegreeOfParallelism {
     93      get { return MaxDegreeOfParallelismParameter.Value.Value; }
     94      set { MaxDegreeOfParallelismParameter.Value.Value = value; }
     95    }
     96
    7997    [StorableConstructor]
    8098    protected PopulationSimilarityAnalyzer(bool deserializing) : base(deserializing) { }
    81     protected PopulationSimilarityAnalyzer(PopulationSimilarityAnalyzer original, Cloner cloner) : base(original, cloner) { }
     99
     100    protected PopulationSimilarityAnalyzer(PopulationSimilarityAnalyzer original, Cloner cloner)
     101      : base(original, cloner) {
     102      RegisterParametersEventHandlers();
     103    }
     104
    82105    public PopulationSimilarityAnalyzer(IEnumerable<ISolutionSimilarityCalculator> validSimilarityCalculators)
    83106      : base() {
     
    89112      Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "PopulationDiversityAnalyzerUpdateCounter"));
    90113      Parameters.Add(new FixedValueParameter<StringValue>(DiversityResultNameParameterName, "Specifies how the diversity results should be named.", new StringValue("PopulationDiversity")));
     114      Parameters.Add(new FixedValueParameter<BoolValue>(ExecuteInParallelParameterName, "Specifies whether similarity calculations should be parallelized.", new BoolValue(false)));
     115      Parameters.Add(new FixedValueParameter<IntValue>(MaxDegreeOfParallelismParameterName, "Specifies the maximum number of threads when calculating similarities in parallel.", new IntValue(-1)));
    91116
    92117      var similarityCalculators = SimilarityCalculatorParameter.ValidValues;
    93       foreach (var sc in validSimilarityCalculators)
     118      foreach (var sc in validSimilarityCalculators) {
    94119        similarityCalculators.Add(sc);
    95 
     120      }
    96121
    97122      ResultsParameter.Hidden = true;
    98123      UpdateCounterParameter.Hidden = true;
     124      ExecuteInParallelParameter.Hidden = true;
     125      MaxDegreeOfParallelismParameter.Hidden = true;
     126
     127      RegisterParametersEventHandlers();
     128    }
     129
     130    private void RegisterParametersEventHandlers() {
     131      ExecuteInParallelParameter.Value.ValueChanged += Value_ValueChanged;
     132      MaxDegreeOfParallelismParameter.Value.ValueChanged += Value_ValueChanged;
     133    }
     134
     135    private void Value_ValueChanged(object sender, EventArgs e) {
     136      var similarityCalculators = SimilarityCalculatorParameter.ValidValues;
     137      foreach (var similarityCalculator in similarityCalculators) {
     138        similarityCalculator.ExecuteInParallel = ExecuteInParallel;
     139        similarityCalculator.MaxDegreeOfParallelism = MaxDegreeOfParallelism;
     140      }
    99141    }
    100142
     
    113155        Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
    114156
    115       SimilarityCalculatorParameter.ValidValues.Add(oldSimilarityCalculator);
     157      if (oldSimilarityCalculator != null)
     158        SimilarityCalculatorParameter.ValidValues.Add(oldSimilarityCalculator);
     159
     160      if (!Parameters.ContainsKey(ExecuteInParallelParameterName)) {
     161        Parameters.Add(new FixedValueParameter<BoolValue>(ExecuteInParallelParameterName,
     162          "Specifies whether similarity calculations should be parallelized.", new BoolValue(false)));
     163        ExecuteInParallelParameter.Hidden = true;
     164      }
     165      if (!Parameters.ContainsKey(MaxDegreeOfParallelismParameterName)) {
     166        Parameters.Add(new FixedValueParameter<IntValue>(MaxDegreeOfParallelismParameterName,
     167          "Specifies the maximum number of threads when calculating similarities in parallel.", new IntValue(-1)));
     168        MaxDegreeOfParallelismParameter.Hidden = true;
     169      }
     170
     171      RegisterParametersEventHandlers();
    116172      #endregion
    117173    }
  • trunk/sources/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs

    r12129 r13005  
    2222using System;
    2323using System.Linq;
     24using System.Threading.Tasks;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    4142    [Storable]
    4243    public string QualityVariableName { get; set; }
     44    [Storable]
     45    public bool ExecuteInParallel { get; set; }
     46    [Storable]
     47    public int MaxDegreeOfParallelism { get; set; }
    4348    #endregion
    4449
     
    4853    protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner)
    4954      : base(original, cloner) {
    50       this.SolutionVariableName = original.SolutionVariableName;
    51       this.QualityVariableName = original.QualityVariableName;
    52     }
    53     protected SolutionSimilarityCalculator() : base() { }
     55      SolutionVariableName = original.SolutionVariableName;
     56      QualityVariableName = original.QualityVariableName;
     57      ExecuteInParallel = original.ExecuteInParallel;
     58      MaxDegreeOfParallelism = original.MaxDegreeOfParallelism;
     59    }
     60
     61    protected SolutionSimilarityCalculator() : base() {
     62      ExecuteInParallel = false;
     63      MaxDegreeOfParallelism = -1;
     64    }
     65
     66    [StorableHook(HookType.AfterDeserialization)]
     67    private void AfterDeserialization() {
     68      if (MaxDegreeOfParallelism == 0) {
     69        ExecuteInParallel = false;
     70        MaxDegreeOfParallelism = -1;
     71      }
     72    }
    5473
    5574    public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) {
     
    7594
    7695    public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) {
    77       if (solutionCrowd == null)
     96      if (solutionCrowd == null) {
    7897        throw new ArgumentException("Cannot calculate similarity because the provided crowd is null.");
    79 
     98      }
    8099      var individuals = solutionCrowd.SubScopes;
    81100
    82       if (!individuals.Any())
     101      if (!individuals.Any()) {
    83102        throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty.");
     103      }
    84104
    85105      var similarityMatrix = new double[individuals.Count][];
    86       for (int i = 0; i < individuals.Count; i++) similarityMatrix[i] = new double[individuals.Count];
    87 
    88       if (IsCommutative) {
    89         for (int i = 0; i < individuals.Count; i++) {
    90           for (int j = i; j < individuals.Count; j++) {
    91             similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     106      for (int i = 0; i < individuals.Count; i++) {
     107        similarityMatrix[i] = new double[individuals.Count];
     108      }
     109
     110      if (ExecuteInParallel) {
     111        var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = MaxDegreeOfParallelism };
     112        if (IsCommutative) {
     113          Parallel.For(0, individuals.Count, parallelOptions, i => {
     114            for (int j = i; j < individuals.Count; j++) {
     115              similarityMatrix[i][j] =
     116                similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     117            }
     118          });
     119        } else {
     120          Parallel.For(0, individuals.Count, parallelOptions, i => {
     121            for (int j = i; j < individuals.Count; j++) {
     122              similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     123              if (i == j) continue;
     124              similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
     125            }
     126          });
     127        }
     128      } else {
     129        if (IsCommutative) {
     130          for (int i = 0; i < individuals.Count; i++) {
     131            for (int j = i; j < individuals.Count; j++) {
     132              similarityMatrix[i][j] =
     133                similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     134            }
    92135          }
    93         }
    94       } else {
    95         for (int i = 0; i < individuals.Count; i++) {
    96           for (int j = i; j < individuals.Count; j++) {
    97             similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
    98             if (i == j) continue;
    99             similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
     136        } else {
     137          for (int i = 0; i < individuals.Count; i++) {
     138            for (int j = i; j < individuals.Count; j++) {
     139              similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     140              if (i == j) continue;
     141              similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
     142            }
    100143          }
    101144        }
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISolutionSimilarityCalculator.cs

    r12085 r13005  
    3030    string SolutionVariableName { get; set; }
    3131    string QualityVariableName { get; set; }
     32    bool ExecuteInParallel { get; set; }
     33    int MaxDegreeOfParallelism { get; set; }
    3234
    3335    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.