1 | using System;
|
---|
2 | #region License Information
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2015 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 |
|
---|
23 | using HeuristicLab.Operators;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.LinearAssignment {
|
---|
32 | [Item("LAPEvaluator", "Evaluates a solution to the linear assignment problem.")]
|
---|
33 | [StorableType("D58F39FD-C614-4E41-A555-52B277A0DF26")]
|
---|
34 | public class LAPEvaluator : InstrumentedOperator, 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()
|
---|
50 | : base() {
|
---|
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 |
|
---|
60 | public override IOperation InstrumentedApply() {
|
---|
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);
|
---|
72 | return base.InstrumentedApply();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|