Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/15/20 13:53:11 (4 years ago)
Author:
mkommend
Message:

#2971: Added first draft of results implementation and problem adaptation.

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3/HeuristicLab.Problems.LinearAssignment-3.3.csproj

    r17530 r17745  
    192192      <Private>False</Private>
    193193    </ProjectReference>
     194    <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
     195      <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
     196      <Name>HeuristicLab.Random-3.3</Name>
     197    </ProjectReference>
    194198  </ItemGroup>
    195199  <ItemGroup>
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3/HungarianAlgorithm.cs

    r17530 r17745  
    2727using HeuristicLab.Encodings.PermutationEncoding;
    2828using HeuristicLab.Optimization;
     29using HeuristicLab.Random;
    2930
    3031namespace HeuristicLab.Problems.LinearAssignment {
     
    6364      var assignment = LinearAssignmentProblemSolver.Solve(Problem.Costs, out double quality);
    6465
    65       Problem.Analyze(new[] { new Permutation(PermutationTypes.Absolute, assignment) },
    66         new[] { quality }, Results, null);
     66      var context = new SingleObjectiveSolutionContext<Permutation>(new Permutation(PermutationTypes.Absolute, assignment));
     67      context.EvaluationResult = new SingleObjectiveEvaluationResult(quality);
     68
     69      var random = new FastRandom(42);
     70      Problem.Analyze(new[] { context }, random);
    6771    }
    6872  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblem.cs

    r17696 r17745  
    136136    }
    137137
    138     public override void Analyze(Permutation[] permutations, double[] qualities, ResultCollection results, IRandom random) {
    139       base.Analyze(permutations, qualities, results, random);
    140 
    141       var costs = Costs;
    142       var rowNames = RowNames;
    143       var columnNames = ColumnNames;
    144       bool max = Maximization;
    145 
    146       var sorted = qualities.Select((x, index) => new { Index = index, Quality = x }).OrderBy(x => x.Quality).ToArray();
    147       if (max) Array.Reverse(sorted);
    148       int i = sorted[0].Index;
    149       var best = Tuple.Create(permutations[i], qualities[i]);
    150 
    151       if (double.IsNaN(BestKnownQuality) || IsBetter(best.Item2, BestKnownQuality)) {
    152         // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
    153         BestKnownQuality = best.Item2;
    154         BestKnownSolution = (Permutation)best.Item1.Clone();
    155         BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
    156         BestKnownSolutions.Add((Permutation)best.Item1.Clone());
    157       } else if (BestKnownQuality == best.Item2) {
    158         // if we matched the best-known quality we'll try to set the best-known solution if it isn't null
    159         // and try to add it to the pool of best solutions if it is different
    160         if (BestKnownSolution == null) BestKnownSolution = (Permutation)best.Item1.Clone();
    161         if (BestKnownSolutions == null) BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
    162 
    163         foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns
    164           if (IsBetter(best.Item2, k.Quality)) break; // stop when we reached a solution worse than the best-known quality
    165           var p = permutations[k.Index];
    166           if (!BestKnownSolutions.Contains(p))
    167             BestKnownSolutions.Add((Permutation)permutations[k.Index].Clone());
    168         }
    169       }
    170 
    171       LAPAssignment solution = BestLAPSolutionParameter.ActualValue;
    172       if (solution == null) {
    173         solution = new LAPAssignment(costs, rowNames, columnNames, (Permutation)best.Item1.Clone(), new DoubleValue(best.Item2));
    174         BestLAPSolutionParameter.ActualValue = solution;
    175       } else {
    176         if (IsBetter(best.Item2, solution.Quality.Value)) {
    177           solution.Costs = costs;
    178           solution.Assignment = (Permutation)best.Item1.Clone();
    179           solution.Quality.Value = best.Item2;
    180           if (rowNames != null)
    181             solution.RowNames = rowNames;
    182           else solution.RowNames = null;
    183           if (columnNames != null)
    184             solution.ColumnNames = columnNames;
    185           else solution.ColumnNames = null;
    186         }
    187       }
     138    public override void Analyze(ISingleObjectiveSolutionContext<Permutation>[] solutionContexts, IRandom random) {
     139      base.Analyze(solutionContexts, random);
     140
     141      //TODO reimplement code below using results directly
     142      //var costs = Costs;
     143      //var rowNames = RowNames;
     144      //var columnNames = ColumnNames;
     145      //bool max = Maximization;
     146
     147      //var sorted = qualities.Select((x, index) => new { Index = index, Quality = x }).OrderBy(x => x.Quality).ToArray();
     148      //if (max) Array.Reverse(sorted);
     149      //int i = sorted[0].Index;
     150      //var best = Tuple.Create(permutations[i], qualities[i]);
     151
     152      //if (double.IsNaN(BestKnownQuality) || IsBetter(best.Item2, BestKnownQuality)) {
     153      //  // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
     154      //  BestKnownQuality = best.Item2;
     155      //  BestKnownSolution = (Permutation)best.Item1.Clone();
     156      //  BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
     157      //  BestKnownSolutions.Add((Permutation)best.Item1.Clone());
     158      //} else if (BestKnownQuality == best.Item2) {
     159      //  // if we matched the best-known quality we'll try to set the best-known solution if it isn't null
     160      //  // and try to add it to the pool of best solutions if it is different
     161      //  if (BestKnownSolution == null) BestKnownSolution = (Permutation)best.Item1.Clone();
     162      //  if (BestKnownSolutions == null) BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
     163
     164      //  foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns
     165      //    if (IsBetter(best.Item2, k.Quality)) break; // stop when we reached a solution worse than the best-known quality
     166      //    var p = permutations[k.Index];
     167      //    if (!BestKnownSolutions.Contains(p))
     168      //      BestKnownSolutions.Add((Permutation)permutations[k.Index].Clone());
     169      //  }
     170      //}
     171
     172      //LAPAssignment solution = BestLAPSolutionParameter.ActualValue;
     173      //if (solution == null) {
     174      //  solution = new LAPAssignment(costs, rowNames, columnNames, (Permutation)best.Item1.Clone(), new DoubleValue(best.Item2));
     175      //  BestLAPSolutionParameter.ActualValue = solution;
     176      //} else {
     177      //  if (IsBetter(best.Item2, solution.Quality.Value)) {
     178      //    solution.Costs = costs;
     179      //    solution.Assignment = (Permutation)best.Item1.Clone();
     180      //    solution.Quality.Value = best.Item2;
     181      //    if (rowNames != null)
     182      //      solution.RowNames = rowNames;
     183      //    else solution.RowNames = null;
     184      //    if (columnNames != null)
     185      //      solution.ColumnNames = columnNames;
     186      //    else solution.ColumnNames = null;
     187      //  }
     188      //}
    188189
    189190    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.LinearAssignment/3.3/Plugin.cs.frame

    r17226 r17745  
    3636  [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    3737  [PluginDependency("HeuristicLab.Parameters", "3.3")]
     38  [PluginDependency("HeuristicLab.Random", "3.3")]
    3839  [PluginDependency("HeuristicLab.Attic", "1.0")]
    3940  public class HeuristicLabProblemsLinearAssignmentPlugin : PluginBase {
Note: See TracChangeset for help on using the changeset viewer.