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

Last change on this file since 16011 was 16011, checked in by fholzing, 14 months ago

#2864: Double-checked the namespaces and ordering of imports

File size: 5.4 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
37  public sealed class LinearOrderingProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IProblemInstanceConsumer<LOPData>, IProblemInstanceExporter<LOPData>, IStorableContent {
38    #region Fields
39    private static readonly LOPData DefaultInstance = new LOPData() {
40      Name = "Linaer Ordering Problem (LOP)",
41      Description = "The default instance of the LOP in HeuristicLab",
42      Dimension = 4,
43      Matrix = new double[,] {
44                {0 ,3, 6 ,6},
45                {2 ,0, 8 ,4},
46                {4 ,2, 0 ,4},
47                {5 ,3, 8 ,0}
48            }
49    };
50    public event EventHandler BestKnownSolutionChanged;
51    #endregion
52
53    #region Getter/Setter
54    public OptionalValueParameter<Permutation> BestKnownSolutionParameter
55    {
56      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
57    }
58    public OptionalValueParameter<DoubleMatrix> MatrixParameter
59    {
60      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Matrix"]; }
61    }
62    public Permutation BestKnownSolution
63    {
64      get { return BestKnownSolutionParameter.Value; }
65      set
66      {
67        BestKnownSolutionParameter.Value = value;
68        if (BestKnownSolutionChanged != null) { OnBestKnownSolutionChanged(); }
69      }
70    }
71    public DoubleMatrix Matrix
72    {
73      get { return MatrixParameter.Value; }
74      set { MatrixParameter.Value = value; }
75    }
76
77    public override bool Maximization { get { return true; } }
78    #endregion
79
80    #region Ctor
81    [StorableConstructor]
82    private LinearOrderingProblem(bool deserializing) : base(deserializing) { }
83    private LinearOrderingProblem(LinearOrderingProblem original, Cloner cloner) : base(original, cloner) { }
84    public LinearOrderingProblem() {
85      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this LOP instance."));
86      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Matrix", "The matrix which contains the corresponding LOP-values"));
87
88      Load(DefaultInstance);
89      EvaluatorParameter.GetsCollected = false;
90      EvaluatorParameter.Hidden = true;
91
92      Evaluator.QualityParameter.ActualName = "SuperDiagonale";
93    }
94    #endregion
95
96    #region Methods
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new LinearOrderingProblem(this, cloner);
100    }
101
102    public void Load(LOPData data) {
103      if (data.BestKnownQuality.HasValue) {
104        BestKnownQuality = data.BestKnownQuality.Value;
105      }
106      Name = data.Name;
107      Description = data.Description;
108      Matrix = new DoubleMatrix(data.Matrix);
109      Encoding.Length = Matrix.Columns;
110
111      if (data.BestKnownPermutation != null) {
112        int[] permut = data.BestKnownPermutation;
113        //Clean up if the first index = 1
114        if (!permut.Contains(0)) { permut = permut.Select(v => v - 1).ToArray(); }
115
116        BestKnownSolution = new Permutation(PermutationTypes.Absolute, data.BestKnownPermutation);
117        BestKnownQuality = Evaluate(permut, Matrix);
118      }
119    }
120
121    public LOPData Export() {
122      var result = new LOPData {
123        Name = Name,
124        Description = Description,
125        BestKnownQuality = BestKnownQuality,
126        BestKnownPermutation = BestKnownSolution.ToArray(),
127        Dimension = Matrix.Rows,
128        Matrix = Matrix.CloneAsMatrix()
129      };
130
131      return result;
132    }
133
134    public override double Evaluate(Individual individual, IRandom random) {
135      return Evaluate(individual.Permutation().ToArray(), Matrix);
136    }
137
138    #endregion
139
140    #region Helper Methods
141    private void OnBestKnownSolutionChanged() {
142      BestKnownSolutionChanged?.Invoke(this, EventArgs.Empty);
143    }
144
145    private double Evaluate(int[] permutation, DoubleMatrix matrix) {
146      double sum = 0;
147      for (int i = 1; i < matrix.Columns; i++) {
148        for (int j = 0; j < i; j++) {
149          sum += matrix[permutation[j], permutation[i]];
150        }
151      }
152
153      return sum;
154    }
155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.