Free cookie consent management tool by TermsFeed Policy Generator

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

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

  • merged to trunk
Location:
trunk/sources
Files:
5 edited
6 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
  • trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPEvaluator.cs

    r5838 r6342  
    6969    }
    7070
     71    public static double Impact(int facility, Permutation assignment, DoubleMatrix weights, DoubleMatrix distances) {
     72      double impact = 0;
     73      for (int i = 0; i < assignment.Length; i++) {
     74        impact += weights[facility, i] * distances[assignment[facility], assignment[i]];
     75        impact += weights[i, facility] * distances[assignment[i], assignment[facility]];
     76      }
     77      return impact;
     78    }
     79
    7180    public override IOperation Apply() {
    7281      Permutation assignment = PermutationParameter.ActualValue;
  • trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj

    r5950 r6342  
    109109  <ItemGroup>
    110110    <Compile Include="Analyzers\BestQAPSolutionAnalyzer.cs" />
     111    <Compile Include="Analyzers\QAPAlleleFrequencyAnalyzer.cs" />
     112    <Compile Include="Analyzers\QAPPopulationDiversityAnalyzer.cs" />
    111113    <Compile Include="Evaluators\QAPSwap2MoveEvaluator.cs" />
    112114    <Compile Include="Evaluators\QAPEvaluator.cs" />
     
    118120    <Compile Include="Interfaces\IQAPEvaluator.cs" />
    119121    <Compile Include="Interfaces\IQAPMoveEvaluator.cs" />
     122    <Compile Include="LocalImprovement\QAPExhaustiveSwap2LocalImprovement.cs" />
    120123    <Compile Include="Parsers\QAPLIBSolutionParser.cs" />
    121124    <Compile Include="Parsers\QAPLIBParser.cs" />
     
    386389  </ItemGroup>
    387390  <ItemGroup>
     391    <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
     392      <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
     393      <Name>HeuristicLab.Analysis-3.3</Name>
     394    </ProjectReference>
    388395    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    389396      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     
    432439  </ItemGroup>
    433440  <ItemGroup>
    434     <Content Include="Data\tai100a.sln" />
    435     <Content Include="Data\tai25a.sln" />
    436     <Content Include="Data\tai50a.sln" />
     441    <EmbeddedResource Include="Data\tai100a.sln" />
     442    <EmbeddedResource Include="Data\tai25a.sln" />
     443    <EmbeddedResource Include="Data\tai50a.sln" />
     444  </ItemGroup>
     445  <ItemGroup>
     446    <EmbeddedResource Include="Data\esc32a.sln" />
     447  </ItemGroup>
     448  <ItemGroup>
     449    <EmbeddedResource Include="Data\sko49.sln" />
    437450  </ItemGroup>
    438451  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r6088 r6342  
    4949
    5050    #region Parameter Properties
     51    public IValueParameter<ItemList<Permutation>> BestKnownSolutionsParameter {
     52      get { return (IValueParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]; }
     53    }
    5154    public IValueParameter<Permutation> BestKnownSolutionParameter {
    5255      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
     
    6164
    6265    #region Properties
     66    public ItemList<Permutation> BestKnownSolutions {
     67      get { return BestKnownSolutionsParameter.Value; }
     68      set { BestKnownSolutionsParameter.Value = value; }
     69    }
    6370    public Permutation BestKnownSolution {
    6471      get { return BestKnownSolutionParameter.Value; }
     
    8895      get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); }
    8996    }
     97
     98    private QAPAlleleFrequencyAnalyzer QAPAlleleFrequencyAnalyzer {
     99      get { return Operators.OfType<QAPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
     100    }
     101
     102    private QAPPopulationDiversityAnalyzer QAPPopulationDiversityAnalyzer {
     103      get { return Operators.OfType<QAPPopulationDiversityAnalyzer>().FirstOrDefault(); }
     104    }
    90105    #endregion
    91106
     
    98113    public QuadraticAssignmentProblem()
    99114      : base(new QAPEvaluator(), new RandomPermutationCreator()) {
     115      Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
    100116      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
    101117      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
     
    130146    public override IDeepCloneable Clone(Cloner cloner) {
    131147      return new QuadraticAssignmentProblem(this, cloner);
     148    }
     149
     150    [StorableHook(HookType.AfterDeserialization)]
     151    private void AfterDeserialization() {
     152      // BackwardsCompatibility3.3
     153      #region Backwards compatible code, remove with 3.4
     154      /*if (Parameters.ContainsKey("BestKnownSolution")) {
     155        Permutation solution = ((IValueParameter<Permutation>)Parameters["BestKnownSolution"]).Value;
     156        Parameters.Remove("BestKnownSolution");
     157        Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
     158        if (solution != null) {
     159          BestKnownSolutions = new ItemList<Permutation>();
     160          BestKnownSolutions.Add(solution);
     161        }
     162      }*/
     163      if (!Parameters.ContainsKey("BestKnownSolutions")) {
     164        Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
     165      }
     166      if (Parameters.ContainsKey("DistanceMatrix")) {
     167        DoubleMatrix bla = ((ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]).Value;
     168        Parameters.Remove("DistanceMatrix");
     169        Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "bla", bla));
     170      }
     171      AttachEventHandlers();
     172      #endregion
    132173    }
    133174
     
    217258
    218259    #region Helpers
    219     [StorableHook(HookType.AfterDeserialization)]
    220     private void AfterDeserializationHook() {
    221       AttachEventHandlers();
    222     }
    223 
    224260    private void AttachEventHandlers() {
    225261      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     
    238274      Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>());
    239275      Operators.Add(new BestQAPSolutionAnalyzer());
     276      Operators.Add(new QAPAlleleFrequencyAnalyzer());
     277      Operators.Add(new QAPPopulationDiversityAnalyzer());
     278      Operators.Add(new QAPExhaustiveSwap2LocalImprovement());
    240279      ParameterizeAnalyzers();
    241280      ParameterizeOperators();
     
    262301        BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
    263302        BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
    264         BestQAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
     303        BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
    265304        BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     305      }
     306      if (QAPAlleleFrequencyAnalyzer != null) {
     307        QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     308        QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
     309        QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
     310        QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     311        QAPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
     312        QAPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     313        QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
     314      }
     315      if (QAPPopulationDiversityAnalyzer != null) {
     316        QAPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     317        QAPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     318        QAPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
     319        QAPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    266320      }
    267321    }
     
    291345      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())
    292346        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     347
     348      QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault();
     349      if (localOpt != null) {
     350        localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     351        localOpt.DistancesParameter.ActualName = DistancesParameter.Name;
     352        localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name;
     353        localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     354        localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
     355      }
    293356    }
    294357
     
    315378      Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
    316379      BestKnownQuality = null;
    317       BestKnownSolution = null;
     380      BestKnownSolutions = null;
    318381      OnReset();
    319382    }
     
    348411            if (solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) {
    349412              BestKnownQuality = new DoubleValue(solParser.Quality);
     413              BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) });
    350414              BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
    351415            } else {
    352416              BestKnownQuality = new DoubleValue(solParser.Quality);
     417              BestKnownSolutions = null;
    353418              BestKnownSolution = null;
    354419            }
    355420          } else {
    356421            BestKnownQuality = new DoubleValue(solParser.Quality);
     422            BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) });
    357423            BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
    358424          }
     
    360426      } else {
    361427        BestKnownQuality = null;
     428        BestKnownSolutions = null;
    362429        BestKnownSolution = null;
    363430      }
Note: See TracChangeset for help on using the changeset viewer.