Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2864: First commit of new branch of Permutation based benchmark problems.

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