[15661] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16003] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15661] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[16834] | 24 | using HEAL.Attic;
|
---|
[15661] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[16011] | 29 | using HeuristicLab.Optimization;
|
---|
[15661] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Problems.Instances;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.PermutationProblems {
|
---|
| 34 | [Item("Linear Ordering Problem (LOP)", "Represents a Linear Ordering Problem")]
|
---|
| 35 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
|
---|
[16834] | 36 | [StorableType("A84D3AAE-8AD2-4502-ACD3-70E62F12841B")]
|
---|
[15661] | 37 | public sealed class LinearOrderingProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IProblemInstanceConsumer<LOPData>, IProblemInstanceExporter<LOPData>, IStorableContent {
|
---|
| 38 | private static readonly LOPData DefaultInstance = new LOPData() {
|
---|
| 39 | Name = "Linaer Ordering Problem (LOP)",
|
---|
| 40 | Description = "The default instance of the LOP in HeuristicLab",
|
---|
| 41 | Dimension = 4,
|
---|
| 42 | Matrix = new double[,] {
|
---|
| 43 | {0 ,3, 6 ,6},
|
---|
| 44 | {2 ,0, 8 ,4},
|
---|
| 45 | {4 ,2, 0 ,4},
|
---|
| 46 | {5 ,3, 8 ,0}
|
---|
| 47 | }
|
---|
| 48 | };
|
---|
| 49 |
|
---|
[16834] | 50 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
[15661] | 51 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 52 | }
|
---|
[16834] | 53 | public Permutation BestKnownSolution {
|
---|
[15661] | 54 | get { return BestKnownSolutionParameter.Value; }
|
---|
[16834] | 55 | set {
|
---|
[15661] | 56 | BestKnownSolutionParameter.Value = value;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[16834] | 59 | public ValueParameter<DoubleMatrix> MatrixParameter {
|
---|
[16014] | 60 | get { return (ValueParameter<DoubleMatrix>)Parameters["Matrix"]; }
|
---|
| 61 | }
|
---|
[16834] | 62 | public DoubleMatrix Matrix {
|
---|
[15661] | 63 | get { return MatrixParameter.Value; }
|
---|
| 64 | set { MatrixParameter.Value = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override bool Maximization { get { return true; } }
|
---|
| 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
[16834] | 70 | private LinearOrderingProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[15661] | 71 | private LinearOrderingProblem(LinearOrderingProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
| 72 | public LinearOrderingProblem() {
|
---|
| 73 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this LOP instance."));
|
---|
[16014] | 74 | Parameters.Add(new ValueParameter<DoubleMatrix>("Matrix", "The matrix which contains the corresponding LOP-values"));
|
---|
[15661] | 75 |
|
---|
| 76 | Load(DefaultInstance);
|
---|
| 77 | EvaluatorParameter.GetsCollected = false;
|
---|
| 78 | EvaluatorParameter.Hidden = true;
|
---|
| 79 |
|
---|
[16014] | 80 | Evaluator.QualityParameter.ActualName = "Superdiagonal";
|
---|
[15661] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new LinearOrderingProblem(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void Load(LOPData data) {
|
---|
[16014] | 88 | if (data.Matrix.GetLength(0) != data.Matrix.GetLength(1)) {
|
---|
| 89 | throw new ArgumentException("Matrix must be square");
|
---|
| 90 | }
|
---|
[15661] | 91 | if (data.BestKnownQuality.HasValue) {
|
---|
| 92 | BestKnownQuality = data.BestKnownQuality.Value;
|
---|
| 93 | }
|
---|
| 94 | Name = data.Name;
|
---|
| 95 | Description = data.Description;
|
---|
| 96 | Matrix = new DoubleMatrix(data.Matrix);
|
---|
| 97 | Encoding.Length = Matrix.Columns;
|
---|
| 98 |
|
---|
| 99 | if (data.BestKnownPermutation != null) {
|
---|
| 100 | int[] permut = data.BestKnownPermutation;
|
---|
| 101 | //Clean up if the first index = 1
|
---|
| 102 | if (!permut.Contains(0)) { permut = permut.Select(v => v - 1).ToArray(); }
|
---|
| 103 |
|
---|
[16014] | 104 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, permut);
|
---|
| 105 | BestKnownQuality = Evaluate(new Permutation(PermutationTypes.Absolute, permut), Matrix);
|
---|
[15661] | 106 | }
|
---|
| 107 | }
|
---|
| 108 | public LOPData Export() {
|
---|
| 109 | var result = new LOPData {
|
---|
| 110 | Name = Name,
|
---|
| 111 | Description = Description,
|
---|
| 112 | BestKnownQuality = BestKnownQuality,
|
---|
| 113 | BestKnownPermutation = BestKnownSolution.ToArray(),
|
---|
| 114 | Dimension = Matrix.Rows,
|
---|
| 115 | Matrix = Matrix.CloneAsMatrix()
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | return result;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
[16014] | 122 | return Evaluate(individual.Permutation(), Matrix);
|
---|
[15661] | 123 | }
|
---|
[16014] | 124 | private double Evaluate(Permutation permutation, DoubleMatrix matrix) {
|
---|
[15661] | 125 | double sum = 0;
|
---|
| 126 | for (int i = 1; i < matrix.Columns; i++) {
|
---|
| 127 | for (int j = 0; j < i; j++) {
|
---|
| 128 | sum += matrix[permutation[j], permutation[i]];
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | return sum;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|