1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022016 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 


22  using System;


23  using System.Linq;


24  using HeuristicLab.Common;


25  using HeuristicLab.Core;


26  using HeuristicLab.Data;


27  using HeuristicLab.Encodings.PermutationEncoding;


28  using HeuristicLab.Parameters;


29  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


30  using HeuristicLab.PluginInfrastructure;


31  using HeuristicLab.Optimization;


32  using System.Collections.Generic;


33  using HeuristicLab.Problems.Instances.Types;


34  using HeuristicLab.Problems.Instances;


35 


36  namespace HeuristicLab.Problems.PermutationProblems {


37  [Item("Linear Ordering Problem (LOP)", "Represents a Linear Ordering Problem")]


38  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]


39  [StorableClass]


40  public sealed class LinearOrderingProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IProblemInstanceConsumer<LOPData>, IProblemInstanceExporter<LOPData>, IStorableContent {


41  #region Fields


42  private static readonly LOPData DefaultInstance = new LOPData() {


43  Name = "Linaer Ordering Problem (LOP)",


44  Description = "The default instance of the LOP in HeuristicLab",


45  Dimension = 4,


46  Matrix = new double[,] {


47  {0 ,3, 6 ,6},


48  {2 ,0, 8 ,4},


49  {4 ,2, 0 ,4},


50  {5 ,3, 8 ,0}


51  }


52  };


53  public event EventHandler BestKnownSolutionChanged;


54  #endregion


55 


56  #region Getter/Setter


57  public OptionalValueParameter<Permutation> BestKnownSolutionParameter


58  {


59  get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }


60  }


61  public OptionalValueParameter<DoubleMatrix> MatrixParameter


62  {


63  get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Matrix"]; }


64  }


65  public Permutation BestKnownSolution


66  {


67  get { return BestKnownSolutionParameter.Value; }


68  set


69  {


70  BestKnownSolutionParameter.Value = value;


71  if (BestKnownSolutionChanged != null) { OnBestKnownSolutionChanged(); }


72  }


73  }


74  public DoubleMatrix Matrix


75  {


76  get { return MatrixParameter.Value; }


77  set { MatrixParameter.Value = value; }


78  }


79 


80  public override bool Maximization { get { return true; } }


81  #endregion


82 


83  #region Ctor


84  [StorableConstructor]


85  private LinearOrderingProblem(bool deserializing) : base(deserializing) { }


86  private LinearOrderingProblem(LinearOrderingProblem original, Cloner cloner) : base(original, cloner) { }


87  public LinearOrderingProblem() {


88  Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this LOP instance."));


89  Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Matrix", "The matrix which contains the corresponding LOPvalues"));


90 


91  Load(DefaultInstance);


92  EvaluatorParameter.GetsCollected = false;


93  EvaluatorParameter.Hidden = true;


94 


95  Evaluator.QualityParameter.ActualName = "SuperDiagonale";


96  }


97  #endregion


98 


99  #region Methods


100 


101  public override IDeepCloneable Clone(Cloner cloner) {


102  return new LinearOrderingProblem(this, cloner);


103  }


104 


105  public void Load(LOPData data) {


106  if (data.BestKnownQuality.HasValue) {


107  BestKnownQuality = data.BestKnownQuality.Value;


108  }


109  Name = data.Name;


110  Description = data.Description;


111  Matrix = new DoubleMatrix(data.Matrix);


112  Encoding.Length = Matrix.Columns;


113 


114  if (data.BestKnownPermutation != null) {


115  int[] permut = data.BestKnownPermutation;


116  //Clean up if the first index = 1


117  if (!permut.Contains(0)) { permut = permut.Select(v => v  1).ToArray(); }


118 


119  BestKnownSolution = new Permutation(PermutationTypes.Absolute, data.BestKnownPermutation);


120  BestKnownQuality = Evaluate(permut, Matrix);


121  }


122  }


123 


124  public LOPData Export() {


125  var result = new LOPData {


126  Name = Name,


127  Description = Description,


128  BestKnownQuality = BestKnownQuality,


129  BestKnownPermutation = BestKnownSolution.ToArray(),


130  Dimension = Matrix.Rows,


131  Matrix = Matrix.CloneAsMatrix()


132  };


133 


134  return result;


135  }


136 


137  public override double Evaluate(Individual individual, IRandom random) {


138  return Evaluate(individual.Permutation().ToArray(), Matrix);


139  }


140 


141  #endregion


142 


143  #region Helper Methods


144  private void OnBestKnownSolutionChanged() {


145  BestKnownSolutionChanged?.Invoke(this, EventArgs.Empty);


146  }


147 


148  private double Evaluate(int[] permutation, DoubleMatrix matrix) {


149  double sum = 0;


150  for (int i = 1; i < matrix.Columns; i++) {


151  for (int j = 0; j < i; j++) {


152  sum += matrix[permutation[j], permutation[i]];


153  }


154  }


155 


156  return sum;


157  }


158  #endregion


159  }


160  }

