Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.LinearOrdering/3.3/LinearOrderingProblem.cs @ 15541

Last change on this file since 15541 was 15541, checked in by fholzing, 6 years ago

#2864: CleanUp of old code, added LOP benchmark instances

File size: 10.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Optimization;
32using System.Collections.Generic;
33using HeuristicLab.Problems.Instances.Types;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.LinearOrdering
37{
38    [Item("Linear Ordering Problem (LOP)", "Represents a Linear Ordering Problem")]
39    [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 999)]
40    [StorableClass]
41    public sealed class LinearOrderingProblem : SingleObjectiveHeuristicOptimizationProblem<ILOPEvaluator, IPermutationCreator>, IProblemInstanceConsumer<LOPData>, IProblemInstanceExporter<LOPData>, IStorableContent
42    {
43        #region Default Instance
44        private static readonly LOPData DefaultInstance = new LOPData()
45        {
46            Name = "Linaer Ordering Problem (LOP)",
47            Description = "The default instance of the LOP in HeuristicLab",
48            Dimension = 4,
49            Matrix = new double[,] {
50                {0 ,3, 6 ,6},
51                {2 ,0, 8 ,4},
52                {4 ,2, 0 ,4},
53                {5 ,3, 8 ,0}
54            }
55        };
56        #endregion
57
58        public string Filename { get; set; }
59
60        #region Parameter Properties
61        public OptionalValueParameter<Permutation> BestKnownSolutionParameter
62        {
63            get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
64        }
65
66        public OptionalValueParameter<DoubleMatrix> MatrixParameter
67        {
68            get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Matrix"]; }
69        }
70        #endregion
71
72        #region Properties
73        public Permutation BestKnownSolution
74        {
75            get { return BestKnownSolutionParameter.Value; }
76            set
77            {
78                BestKnownSolutionParameter.Value = value;
79                if (BestKnownSolutionChanged != null) { OnBestKnownSolutionChanged(); }
80            }
81        }
82        public DoubleMatrix Matrix
83        {
84            get { return MatrixParameter.Value; }
85            set { MatrixParameter.Value = value; }
86        }
87        #endregion
88
89        public event EventHandler BestKnownSolutionChanged;
90        private void OnBestKnownSolutionChanged()
91        {
92            var changed = BestKnownSolutionChanged;
93            if (changed != null)
94                changed(this, EventArgs.Empty);
95        }
96
97        // BackwardsCompatibility3.3
98        #region Backwards compatible code, remove with 3.4
99        [Obsolete]
100        [Storable(Name = "operators")]
101        private IEnumerable<IOperator> oldOperators
102        {
103            get { return null; }
104            set
105            {
106                if (value != null && value.Any())
107                    Operators.AddRange(value);
108            }
109        }
110        #endregion
111
112        [StorableConstructor]
113        private LinearOrderingProblem(bool deserializing) : base(deserializing) { }
114        private LinearOrderingProblem(LinearOrderingProblem original, Cloner cloner)
115          : base(original, cloner)
116        {
117        }
118        public LinearOrderingProblem()
119          : base(new SuperdiagonalMatrixTriangulationEvaluator(), new RandomPermutationCreator())
120        {
121            Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this LOP instance."));
122            Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Matrix", "The matrix which contains the corresponding LOP-values"));
123
124            Load(DefaultInstance);
125            EvaluatorParameter.GetsCollected = false;
126            EvaluatorParameter.Hidden = true;
127
128            Maximization.Value = true;
129            MaximizationParameter.Hidden = true;
130
131            SolutionCreator.PermutationParameter.ActualName = "LOPOrder";
132            Evaluator.QualityParameter.ActualName = "SuperDiagonale";
133            ParameterizeSolutionCreator();
134            ParameterizeEvaluator();
135
136            InitializeOperators();
137        }
138
139        public override IDeepCloneable Clone(Cloner cloner)
140        {
141            return new LinearOrderingProblem(this, cloner);
142        }
143
144        [StorableHook(HookType.AfterDeserialization)]
145        private void AfterDeserialization()
146        {
147        }
148
149        #region Problem Instance Handling
150        public void Load(LOPData data)
151        {
152            BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
153            Name = data.Name;
154            Description = data.Description;
155            Matrix = new DoubleMatrix(data.Matrix);
156
157            if (data.BestKnownPermutation != null)
158            {
159                int[] permut = data.BestKnownPermutation;
160                //Clean up if the first index = 1
161                if (!permut.Contains(0)) { permut = permut.Select(v => v - 1).ToArray(); }
162                double bestKnownQuality = MatrixTriangulationEvaluator.Apply(
163                    new SuperdiagonalMatrixTriangulationEvaluator(),
164                    new DoubleMatrix(data.Matrix),
165                    new Permutation(PermutationTypes.Absolute, permut)
166                );
167
168                BestKnownSolution = new Permutation(PermutationTypes.Absolute, data.BestKnownPermutation);
169                BestKnownQuality = new DoubleValue(bestKnownQuality);
170            }
171
172            ParameterizeSolutionCreator();
173        }
174
175        public LOPData Export()
176        {
177            var result = new LOPData
178            {
179                Name = Name,
180                Description = Description,
181                BestKnownQuality = BestKnownQuality.Value,
182                BestKnownPermutation = BestKnownSolution.ToArray(),
183                Dimension = Matrix.Rows,
184                Matrix = Matrix.CloneAsMatrix()
185            };
186
187            return result;
188        }
189        #endregion
190
191        #region Helpers
192        private void InitializeOperators()
193        {
194            Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
195            ParameterizeOperators();
196            UpdateMoveEvaluators();
197        }
198
199
200        private void ParameterizeOperators()
201        {
202            foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>())
203            {
204                op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
205                op.ParentsParameter.Hidden = true;
206                op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
207                op.ChildParameter.Hidden = true;
208            }
209            foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>())
210            {
211                op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
212                op.PermutationParameter.Hidden = true;
213            }
214            foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>())
215            {
216                op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
217                op.PermutationParameter.Hidden = true;
218            }
219            foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())
220            {
221                op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
222                op.PermutationParameter.Hidden = true;
223            }
224            foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>())
225            {
226                op.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
227                op.SolutionParameter.Hidden = true;
228            }
229            foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>())
230            {
231                op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
232                op.ParentsParameter.Hidden = true;
233            }
234            foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>())
235            {
236                op.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
237                op.QualityVariableName = Evaluator.QualityParameter.ActualName;
238            }
239        }
240        #endregion
241
242        private void UpdateMoveEvaluators()
243        {
244            ParameterizeOperators();
245            OnOperatorsChanged();
246        }
247
248        private void ParameterizeSolutionCreator()
249        {
250            SolutionCreator.LengthParameter.Value = new IntValue(Matrix.Columns);
251
252            SolutionCreator.LengthParameter.Hidden = SolutionCreator.LengthParameter.Value != null;
253            SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
254            SolutionCreator.PermutationTypeParameter.Hidden = true;
255        }
256        private void ParameterizeEvaluator()
257        {
258            if (Evaluator is IMatrixTriangulationEvaluator)
259            {
260                IMatrixTriangulationEvaluator evaluator = (IMatrixTriangulationEvaluator)Evaluator;
261                evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
262                evaluator.PermutationParameter.Hidden = true;
263            }
264        }
265    }
266}
Note: See TracBrowser for help on using the repository browser.