[8022] | 1 | using System;
|
---|
| 2 | #region License Information
|
---|
| 3 | /* HeuristicLab
|
---|
[16565] | 4 | * Copyright (C) 2002-2019 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 |
|
---|
| 23 | using HeuristicLab.Operators;
|
---|
[16565] | 24 | using HEAL.Attic;
|
---|
[8022] | 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.")]
|
---|
[16565] | 33 | [StorableType("DB86EA88-EE30-4E57-8447-D24B7B6ACB8A")]
|
---|
[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]
|
---|
[16565] | 47 | protected LAPEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[8022] | 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 | }
|
---|