Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.LinearAssignment/3.3/LAPEvaluator.cs @ 8624

Last change on this file since 8624 was 8022, checked in by abeham, 12 years ago

#1855:

  • Transformed LAP into a SingleObjectiveHeuristicOptimizationProblem
  • Added HungarianAlgorithm as separate algorithm for solving the problem
File size: 3.0 KB
RevLine 
[8022]1using System;
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using HeuristicLab.Operators;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Encodings.PermutationEncoding;
30
31namespace HeuristicLab.Problems.LinearAssignment {
32  [Item("LAPEvaluator", "Evaluates a solution to the linear assignment problem.")]
33  [StorableClass]
34  public class LAPEvaluator : SingleSuccessorOperator, ILAPEvaluator {
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    protected LAPEvaluator(bool deserializing) : base(deserializing) { }
48    protected LAPEvaluator(LAPEvaluator original, Cloner cloner) : base(original, cloner) { }
49    public LAPEvaluator() : base() {
50      Parameters.Add(new LookupParameter<DoubleMatrix>("Costs", LinearAssignmentProblem.CostsDescription));
51      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The assignment solution to evaluate."));
52      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the solution."));
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new LAPEvaluator(this, cloner);
57    }
58
59    public override IOperation Apply() {
60      var costs = CostsParameter.ActualValue;
61      var assignment = AssignmentParameter.ActualValue;
62      if (costs == null || assignment == null) throw new InvalidOperationException(Name + ": Cannot find Costs or Assignment.");
63
64      int len = assignment.Length;
65      double quality = 0;
66      for (int i = 0; i < len; i++) {
67        quality += costs[i, assignment[i]];
68      }
69
70      QualityParameter.ActualValue = new DoubleValue(quality);
71      return base.Apply();
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.