Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11087


Ignore:
Timestamp:
07/04/14 16:31:30 (10 years ago)
Author:
abeham
Message:

#2131: Fixed maximization in LAP (by using negative cost matrix)

Location:
trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblem.cs

    r9456 r11087  
    3333
    3434namespace HeuristicLab.Problems.LinearAssignment {
    35   [Item("LinearAssignmentProblem", "In the linear assignment problem (LAP) an assignment of workers to jobs has to be found such that each worker is assigned to exactly one job, each job is assigned to exactly one worker and the sum of the resulting costs needs to be minimal.")]
     35  [Item("LinearAssignmentProblem", "In the linear assignment problem (LAP) an assignment of workers to jobs has to be found such that each worker is assigned to exactly one job, each job is assigned to exactly one worker and the sum of the resulting costs is minimal (or maximal).")]
    3636  [Creatable("Problems")]
    3737  [StorableClass]
     
    153153      }
    154154    }
     155    private void Costs_Reset(object sender, EventArgs e) {
     156      Parameterize();
     157    }
    155158    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
    156159      Parameterize();
     
    167170      Costs.RowsChanged += new EventHandler(Costs_RowsChanged);
    168171      Costs.ColumnsChanged += new EventHandler(Costs_ColumnsChanged);
     172      Costs.Reset += new EventHandler(Costs_Reset);
    169173      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    170174    }
     
    178182    private void Parameterize() {
    179183      SolutionCreator.LengthParameter.Value = new IntValue(Costs.Rows);
    180       SolutionCreator.LengthParameter.Hidden = false;
     184      SolutionCreator.LengthParameter.Hidden = true;
     185      SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
     186      SolutionCreator.PermutationTypeParameter.Hidden = true;
    181187      Evaluator.CostsParameter.ActualName = CostsParameter.Name;
    182188      Evaluator.CostsParameter.Hidden = true;
  • trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LinearAssignmentProblemSolver.cs

    r9456 r11087  
    3434    private const int UNASSIGNED = -1;
    3535
     36    public IValueLookupParameter<BoolValue> MaximizationParameter {
     37      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     38    }
    3639    public ILookupParameter<DoubleMatrix> CostsParameter {
    3740      get { return (ILookupParameter<DoubleMatrix>)Parameters["Costs"]; }
     
    4952    public LinearAssignmentProblemSolver()
    5053      : base() {
     54      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the costs should be maximized or minimized."));
    5155      Parameters.Add(new LookupParameter<DoubleMatrix>("Costs", LinearAssignmentProblem.CostsDescription));
    5256      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The assignment solution to create."));
     
    5862    }
    5963
     64    [StorableHook(HookType.AfterDeserialization)]
     65    private void AfterDeserialization() {
     66      // BackwardsCompatibility3.3
     67      #region Backwards compatible code, remove with 3.4
     68      if (!Parameters.ContainsKey("Maximization"))
     69        Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the costs should be maximized or minimized."));
     70      #endregion
     71    }
     72
    6073    public override IOperation Apply() {
    6174      var costs = CostsParameter.ActualValue;
     75      var maximization = MaximizationParameter.ActualValue.Value;
     76      if (maximization) {
     77        costs = (DoubleMatrix)costs.Clone();
     78        for (int i = 0; i < costs.Rows; i++)
     79          for (int j = 0; j < costs.Rows; j++)
     80            costs[i, j] = -costs[i, j];
     81      }
    6282      double quality;
    6383      var solution = Solve(costs, out quality);
    6484
    6585      AssignmentParameter.ActualValue = new Permutation(PermutationTypes.Absolute, solution);
     86      if (maximization) quality = -quality;
    6687      QualityParameter.ActualValue = new DoubleValue(quality);
    6788
Note: See TracChangeset for help on using the changeset viewer.