Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.PermutationProblems/3.3/LinearOrderingProblem.cs

Last change on this file was 16834, checked in by fholzing, 5 years ago

#2864: Made the branch compile again (References, new Persistence, ...)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.Instances;
32
33namespace HeuristicLab.Problems.PermutationProblems {
34  [Item("Linear Ordering Problem (LOP)", "Represents a Linear Ordering Problem")]
35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
36  [StorableType("A84D3AAE-8AD2-4502-ACD3-70E62F12841B")]
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
50    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
51      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
52    }
53    public Permutation BestKnownSolution {
54      get { return BestKnownSolutionParameter.Value; }
55      set {
56        BestKnownSolutionParameter.Value = value;
57      }
58    }
59    public ValueParameter<DoubleMatrix> MatrixParameter {
60      get { return (ValueParameter<DoubleMatrix>)Parameters["Matrix"]; }
61    }
62    public DoubleMatrix Matrix {
63      get { return MatrixParameter.Value; }
64      set { MatrixParameter.Value = value; }
65    }
66
67    public override bool Maximization { get { return true; } }
68
69    [StorableConstructor]
70    private LinearOrderingProblem(StorableConstructorFlag _) : base(_) { }
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."));
74      Parameters.Add(new ValueParameter<DoubleMatrix>("Matrix", "The matrix which contains the corresponding LOP-values"));
75
76      Load(DefaultInstance);
77      EvaluatorParameter.GetsCollected = false;
78      EvaluatorParameter.Hidden = true;
79
80      Evaluator.QualityParameter.ActualName = "Superdiagonal";
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new LinearOrderingProblem(this, cloner);
85    }
86
87    public void Load(LOPData data) {
88      if (data.Matrix.GetLength(0) != data.Matrix.GetLength(1)) {
89        throw new ArgumentException("Matrix must be square");
90      }
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
104        BestKnownSolution = new Permutation(PermutationTypes.Absolute, permut);
105        BestKnownQuality = Evaluate(new Permutation(PermutationTypes.Absolute, permut), Matrix);
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) {
122      return Evaluate(individual.Permutation(), Matrix);
123    }
124    private double Evaluate(Permutation permutation, DoubleMatrix matrix) {
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}
Note: See TracBrowser for help on using the repository browser.