Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11802 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.0 KB
RevLine 
[8022]1using System;
2#region License Information
3/* HeuristicLab
[11171]4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8022]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]
[10291]34  public class LAPEvaluator : InstrumentedOperator, ILAPEvaluator {
[8022]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) { }
[10291]49    public LAPEvaluator()
50      : base() {
[8022]51      Parameters.Add(new LookupParameter<DoubleMatrix>("Costs", LinearAssignmentProblem.CostsDescription));
52      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The assignment solution to evaluate."));
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 LAPEvaluator(this, cloner);
58    }
59
[10291]60    public override IOperation InstrumentedApply() {
[8022]61      var costs = CostsParameter.ActualValue;
62      var assignment = AssignmentParameter.ActualValue;
63      if (costs == null || assignment == null) throw new InvalidOperationException(Name + ": Cannot find Costs or Assignment.");
64
65      int len = assignment.Length;
66      double quality = 0;
67      for (int i = 0; i < len; i++) {
68        quality += costs[i, assignment[i]];
69      }
70
71      QualityParameter.ActualValue = new DoubleValue(quality);
[10291]72      return base.InstrumentedApply();
[8022]73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.