[8022] | 1 | using 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 |
|
---|
| 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 | [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 | }
|
---|