Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/30/11 18:07:32 (14 years ago)
Author:
abeham
Message:

#1465, #1469, #1470, #1494, #1496, #1497, #1539, #1487

  • merged to trunk
Location:
trunk/sources
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/BestQAPSolutionAnalyzer.cs

    r5933 r6342  
    6161      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    6262    }
     63    public LookupParameter<ItemList<Permutation>> BestKnownSolutionsParameter {
     64      get { return (LookupParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]; }
     65    }
    6366    public LookupParameter<Permutation> BestKnownSolutionParameter {
    6467      get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
     
    8184      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best QAP solution should be stored."));
    8285      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this QAP instance."));
     86      Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions (there may be multiple) of this QAP instance."));
    8387      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this QAP instance."));
     88    }
     89
     90    [StorableHook(HookType.AfterDeserialization)]
     91    private void AfterDeserialization() {
     92      // BackwardsCompatibility3.3
     93      #region Backwards compatible code, remove with 3.4
     94      if (!Parameters.ContainsKey("BestKnownSolutions")) {
     95        Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance."));
     96      }
     97      #endregion
    8498    }
    8599
     
    93107      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    94108
    95       int i = -1;
    96       if (!max)
    97         i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    98       else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
     109      var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
     110      if (max) sorted = sorted.Reverse().ToArray();
     111      int i = sorted.First().index;
    99112
    100       if (bestKnownQuality == null ||
    101           max && qualities[i].Value > bestKnownQuality.Value ||
    102           !max && qualities[i].Value < bestKnownQuality.Value) {
     113      if (bestKnownQuality == null
     114          || max && qualities[i].Value > bestKnownQuality.Value
     115          || !max && qualities[i].Value < bestKnownQuality.Value) {
     116        // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
    103117        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
    104118        BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
     119        BestKnownSolutionsParameter.ActualValue = new ItemList<Permutation>();
     120        BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[i].Clone());
     121      } else if (bestKnownQuality.Value == qualities[i].Value) {
     122        // if we matched the best-known quality we'll try to set the best-known solution if it isn't null
     123        // and try to add it to the pool of best solutions if it is different
     124        if (BestKnownSolutionParameter.ActualValue == null)
     125          BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
     126        if (BestKnownSolutionsParameter.ActualValue == null)
     127          BestKnownSolutionsParameter.ActualValue = new ItemList<Permutation>();
     128        PermutationEqualityComparer comparer = new PermutationEqualityComparer();
     129        foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns
     130          if (!max && k.Value > qualities[i].Value
     131            || max && k.Value < qualities[i].Value) break; // stop when we reached a solution worse than the best-known quality
     132          Permutation p = permutations[k.index];
     133          bool alreadyPresent = false;
     134          foreach (Permutation p2 in BestKnownSolutionsParameter.ActualValue) {
     135            if (comparer.Equals(p, p2)) {
     136              alreadyPresent = true;
     137              break;
     138            }
     139          }
     140          if (!alreadyPresent)
     141            BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[k.index].Clone());
     142        }
    105143      }
    106144
Note: See TracChangeset for help on using the changeset viewer.