Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/18/12 02:53:03 (12 years ago)
Author:
abeham
Message:

#1855:

  • Transformed LAP into a SingleObjectiveHeuristicOptimizationProblem
  • Added HungarianAlgorithm as separate algorithm for solving the problem
Location:
trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3
Files:
7 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/HeuristicLab.Problems.LinearAssignment-3.3.csproj

    r7874 r8022  
    112112  </ItemGroup>
    113113  <ItemGroup>
     114    <Compile Include="HungarianAlgorithm.cs" />
     115    <Compile Include="LAPAssignment.cs" />
    114116    <None Include="Plugin.cs.frame" />
     117    <Compile Include="Analyzers\BestLAPSolutionAnalyzer.cs" />
     118    <Compile Include="Interfaces\ILAPEvaluator.cs" />
     119    <Compile Include="LAPEvaluator.cs" />
    115120    <Compile Include="LinearAssignmentProblem.cs" />
    116121    <Compile Include="LinearAssignmentProblemSolver.cs" />
     
    123128  </ItemGroup>
    124129  <ItemGroup>
     130    <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
     131      <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
     132      <Name>HeuristicLab.Analysis-3.3</Name>
     133      <Private>False</Private>
     134    </ProjectReference>
    125135    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    126136      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     
    146156      <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
    147157      <Name>HeuristicLab.Data-3.3</Name>
     158      <Private>False</Private>
     159    </ProjectReference>
     160    <ProjectReference Include="..\..\HeuristicLab.Encodings.PermutationEncoding\3.3\HeuristicLab.Encodings.PermutationEncoding-3.3.csproj">
     161      <Project>{DBECB8B0-B166-4133-BAF1-ED67C3FD7FCA}</Project>
     162      <Name>HeuristicLab.Encodings.PermutationEncoding-3.3</Name>
    148163      <Private>False</Private>
    149164    </ProjectReference>
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblem.cs

    r7934 r8022  
    2222using System;
    2323using System.Drawing;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Encodings.PermutationEncoding;
    2729using HeuristicLab.Optimization;
    2830using HeuristicLab.Parameters;
    2931using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.PluginInfrastructure;
    3033
    3134namespace HeuristicLab.Problems.LinearAssignment {
     
    3336  [Creatable("Problems")]
    3437  [StorableClass]
    35   public sealed class LinearAssignmentProblem : Problem {
     38  public sealed class LinearAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<ILAPEvaluator, IPermutationCreator> {
     39    public static readonly string CostsDescription = "The cost matrix that describes the assignment of rows to columns.";
     40
    3641    public override Image ItemImage {
    3742      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
     
    4247      get { return (IValueParameter<DoubleMatrix>)Parameters["Costs"]; }
    4348    }
    44     public IValueParameter<IntArray> SolutionParameter {
    45       get { return (IValueParameter<IntArray>)Parameters["Solution"]; }
     49    public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
     50      get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
    4651    }
    47     public IValueParameter<DoubleValue> QualityParameter {
    48       get { return (IValueParameter<DoubleValue>)Parameters["Quality"]; }
     52    public IValueParameter<Permutation> BestKnownSolutionParameter {
     53      get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
    4954    }
    5055    #endregion
     
    5560      set { CostsParameter.Value = value; }
    5661    }
    57     public IntArray Solution {
    58       get { return SolutionParameter.Value; }
    59       set { SolutionParameter.Value = value; }
     62    public ItemSet<Permutation> BestKnownSolutions {
     63      get { return BestKnownSolutionsParameter.Value; }
     64      set { BestKnownSolutionsParameter.Value = value; }
    6065    }
    61     public DoubleValue Quality {
    62       get { return QualityParameter.Value; }
    63       set { QualityParameter.Value = value; }
     66    public Permutation BestKnownSolution {
     67      get { return BestKnownSolutionParameter.Value; }
     68      set { BestKnownSolutionParameter.Value = value; }
    6469    }
     70    #endregion
    6571
    66     #endregion
     72    [Storable]
     73    private BestLAPSolutionAnalyzer bestLAPSolutionAnalyzer;
    6774
    6875    [StorableConstructor]
     
    7077    private LinearAssignmentProblem(LinearAssignmentProblem original, Cloner cloner)
    7178      : base(original, cloner) {
     79      this.bestLAPSolutionAnalyzer = cloner.Clone(original.bestLAPSolutionAnalyzer);
    7280      AttachEventHandlers();
    7381    }
    7482    public LinearAssignmentProblem()
    75       : base() {
    76       Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", "The cost matrix that describes the assignment of rows to columns.", new DoubleMatrix(3, 3)));
    77       Parameters.Add(new OptionalValueParameter<IntArray>("Solution", "An optimal solution.", null));
    78       Parameters.Add(new OptionalValueParameter<DoubleValue>("Quality", "The solution quality.", null));
    79 
     83      : base(new LAPEvaluator(), new RandomPermutationCreator()) {
     84      Parameters.Add(new ValueParameter<DoubleMatrix>("Costs", CostsDescription, new DoubleMatrix(3, 3)));
     85      Parameters.Add(new OptionalValueParameter<ItemSet<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));
     86      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));
     87     
    8088      ((ValueParameter<DoubleMatrix>)CostsParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
    8189
     90      bestLAPSolutionAnalyzer = new BestLAPSolutionAnalyzer();
     91      SolutionCreator.PermutationParameter.ActualName = "Assignment";
     92      InitializeOperators();
     93      Parameterize();
    8294      AttachEventHandlers();
    8395    }
     
    88100
    89101    #region Events
     102    protected override void OnEvaluatorChanged() {
     103      base.OnEvaluatorChanged();
     104      Parameterize();
     105    }
     106    protected override void OnOperatorsChanged() {
     107      base.OnOperatorsChanged();
     108      Parameterize();
     109    }
     110    protected override void OnSolutionCreatorChanged() {
     111      base.OnSolutionCreatorChanged();
     112      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     113      Parameterize();
     114    }
    90115    private void Costs_RowsChanged(object sender, EventArgs e) {
    91       if (Costs.Rows != Costs.Columns)
     116      if (Costs.Rows != Costs.Columns) {
    92117        ((IStringConvertibleMatrix)Costs).Columns = Costs.Rows;
     118        Parameterize();
     119      }
    93120    }
    94121    private void Costs_ColumnsChanged(object sender, EventArgs e) {
    95       if (Costs.Rows != Costs.Columns)
     122      if (Costs.Rows != Costs.Columns) {
    96123        ((IStringConvertibleMatrix)Costs).Rows = Costs.Columns;
     124        Parameterize();
     125      }
     126    }
     127    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
     128      Parameterize();
    97129    }
    98130    #endregion
     
    107139      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
    108140      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
     141      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     142    }
     143
     144    private void InitializeOperators() {
     145      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
     146      Operators.RemoveAll(x => x is IMoveOperator);
     147      Operators.Add(bestLAPSolutionAnalyzer);
     148    }
     149
     150    private void Parameterize() {
     151      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
     152      SolutionCreator.LengthParameter.Hidden = false;
     153      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
     154      Evaluator.CostsParameter.Hidden = true;
     155      Evaluator.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     156      Evaluator.AssignmentParameter.Hidden = true;
     157
     158      foreach (var op in Operators.OfType<IPermutationCrossover>()) {
     159        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     160        op.ParentsParameter.Hidden = true;
     161        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     162        op.ChildParameter.Hidden = true;
     163      }
     164
     165      foreach (var op in Operators.OfType<IPermutationManipulator>()) {
     166        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     167        op.PermutationParameter.Hidden = true;
     168      }
     169
     170      foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
     171        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     172        op.PermutationParameter.Hidden = true;
     173      }
     174
     175      bestLAPSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     176      bestLAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
     177      bestLAPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
     178      bestLAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
     179      bestLAPSolutionAnalyzer.CostsParameter.ActualName = CostsParameter.Name;
     180      bestLAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     181      bestLAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    109182    }
    110183    #endregion
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblemSolver.cs

    r7873 r8022  
    2121
    2222using HeuristicLab.Common;
     23using HeuristicLab.Core;
    2324using HeuristicLab.Data;
     25using HeuristicLab.Encodings.PermutationEncoding;
     26using HeuristicLab.Operators;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2429
    2530namespace HeuristicLab.Problems.LinearAssignment {
    26   public static class LinearAssignmentProblemSolver {
     31  [Item("LinearAssignmentProblemSolver", "Uses the hungarian algorithm to solve linear assignment problems.")]
     32  [StorableClass]
     33  public sealed class LinearAssignmentProblemSolver : SingleSuccessorOperator {
    2734    private const int UNASSIGNED = -1;
     35
     36    public ILookupParameter<DoubleMatrix> CostsParameter {
     37      get { return (ILookupParameter<DoubleMatrix>)Parameters["Costs"]; }
     38    }
     39    public ILookupParameter<Permutation> AssignmentParameter {
     40      get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
     41    }
     42    public ILookupParameter<DoubleValue> QualityParameter {
     43      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
     44    }
     45
     46    [StorableConstructor]
     47    private LinearAssignmentProblemSolver(bool deserializing) : base(deserializing) { }
     48    private LinearAssignmentProblemSolver(LinearAssignmentProblemSolver original, Cloner cloner) : base(original, cloner) { }
     49    public LinearAssignmentProblemSolver()
     50      : base() {
     51      Parameters.Add(new LookupParameter<DoubleMatrix>("Costs", LinearAssignmentProblem.CostsDescription));
     52      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The assignment solution to create."));
     53      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the solution."));
     54    }
     55
     56    public override IDeepCloneable Clone(Cloner cloner) {
     57      return new LinearAssignmentProblemSolver(this, cloner);
     58    }
     59
     60    public override IOperation Apply() {
     61      var costs = CostsParameter.ActualValue;
     62      double quality;
     63      var solution = Solve(costs, out quality);
     64
     65      AssignmentParameter.ActualValue = new Permutation(PermutationTypes.Absolute, solution);
     66      QualityParameter.ActualValue = new DoubleValue(quality);
     67
     68      return base.Apply();
     69    }
    2870
    2971    /// <summary>
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/Plugin.cs.frame

    r7873 r8022  
    2525  [Plugin("HeuristicLab.Problems.LinearAssignment", "3.3.6.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.LinearAssignment-3.3.dll", PluginFileType.Assembly)]
     27  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    2728  [PluginDependency("HeuristicLab.Collections", "3.3")]
    2829  [PluginDependency("HeuristicLab.Common", "3.3")]
     
    3031  [PluginDependency("HeuristicLab.Core", "3.3")]
    3132  [PluginDependency("HeuristicLab.Data", "3.3")]
     33  [PluginDependency("HeuristicLab.Encodings.PermutationEncoding", "3.3")]
     34  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3235  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    3336  [PluginDependency("HeuristicLab.Parameters", "3.3")]
Note: See TracChangeset for help on using the changeset viewer.