Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/25/11 14:37:13 (14 years ago)
Author:
abeham
Message:

#1330

  • Added working version of QAP
Location:
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBParser.cs

    r5562 r5563  
    2121
    2222    public bool Parse(string file) {
     23      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
     24        return Parse(stream);
     25      }
     26    }
     27
     28    /// <summary>
     29    /// Reads from the given stream data which is expected to be in the QAPLIB format.
     30    /// </summary>
     31    /// <remarks>
     32    /// The stream is not closed or disposed. The caller has to take care of that.
     33    /// </remarks>
     34    /// <param name="stream">The stream to read data from.</param>
     35    /// <returns>True if the file was successfully read or false otherwise.</returns>
     36    public bool Parse(Stream stream) {
    2337      Error = null;
    2438      try {
    25         StreamReader reader = new StreamReader(file);
     39        StreamReader reader = new StreamReader(stream);
    2640        Size = int.Parse(reader.ReadLine());
    2741        Distances = new double[Size, Size];
     
    6579        return false;
    6680      }
    67 
    6881    }
    6982  }
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Plugin.cs.frame

    r5562 r5563  
    2323
    2424namespace HeuristicLab.Problems.QuadraticAssignment {
    25   [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.0.$WCREV$")]
     25  [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.3.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.QuadraticAssignment-3.3.dll", PluginFileType.Assembly)]
    2727  [PluginDependency("HeuristicLab.Collections", "3.3.3")]
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r5562 r5563  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Drawing;
    2425using System.IO;
     
    3940  [StorableClass]
    4041  public sealed class QuadraticAssignmentProblem : SingleObjectiveProblem<IQAPEvaluator, IPermutationCreator> {
     42    private static string InstancePrefix = "HeuristicLab.Problems.QuadraticAssignment.Data.";
     43
    4144    public override Image ItemImage {
    4245      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
     
    8285      get { return UseDistanceMatrixParameter.Value; }
    8386      set { UseDistanceMatrixParameter.Value = value; }
     87    }
     88
     89    public IEnumerable<string> EmbeddedInstances {
     90      get {
     91        return Assembly.GetExecutingAssembly()
     92          .GetManifestResourceNames()
     93          .Where(x => x.EndsWith(".dat"))
     94          .OrderBy(x => x)
     95          .Select(x => x.Replace(".dat", String.Empty))
     96          .Select(x => x.Replace(InstancePrefix, String.Empty));
     97      }
    8498    }
    8599    #endregion
     
    93107    public QuadraticAssignmentProblem()
    94108      : base() {
    95       RandomPermutationCreator solutionCreator = new RandomPermutationCreator();
    96       solutionCreator.LengthParameter.Value = new IntValue(5);
    97       solutionCreator.PermutationParameter.ActualName = "Assignment";
    98       QAPEvaluator evaluator = new QAPEvaluator();
    99 
    100       Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new solutions.", solutionCreator));
    101       Parameters.Add(new ValueParameter<IQAPEvaluator>("Evaluator", "The operator which should be used to evaluate solutions.", evaluator));
    102109      Parameters.Add(new ValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
    103110      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations."));
     
    105112      Parameters.Add(new ValueParameter<DoubleMatrix>("DistanceMatrix", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates.", new DoubleMatrix(5, 5)));
    106113      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "Defaults to true, set to false only when the number of facilities is very high and a distance matrix would become too large (>1000 facilities).", new BoolValue(true)));
     114
     115      Maximization = new BoolValue(false);
    107116
    108117      Coordinates = new DoubleMatrix(new double[,] {
     
    130139      });
    131140
    132       ParameterizeSolutionCreator();
    133       ParameterizeEvaluator();
     141      RandomPermutationCreator solutionCreator = new RandomPermutationCreator();
     142      solutionCreator.LengthParameter.Value = new IntValue(5);
     143      solutionCreator.PermutationParameter.ActualName = "Assignment";
     144      QAPEvaluator evaluator = new QAPEvaluator();
     145
     146      SolutionCreatorParameter.Value = solutionCreator;
     147      EvaluatorParameter.Value = evaluator;
    134148
    135149      InitializeOperators();
     
    186200    private void InitializeOperators() {
    187201      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
     202      ParameterizeOperators();
    188203    }
    189204    private void ParameterizeSolutionCreator() {
    190       SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
    191       SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
     205      if (SolutionCreator != null) {
     206        SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
     207        SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
     208      }
    192209    }
    193210    private void ParameterizeEvaluator() {
    194       Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    195       Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
    196       Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
    197       Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    198       Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
     211      if (Evaluator != null) {
     212        Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     213        Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
     214        Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
     215        Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     216        Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
     217      }
    199218    }
    200219    private void ParameterizeOperators() {
     
    209228        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    210229      }
    211       string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
    212       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
    213         op.InversionMoveParameter.ActualName = inversionMove;
    214       string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
    215       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
    216         op.TranslocationMoveParameter.ActualName = translocationMove;
    217     }
    218     #endregion
    219 
    220     public void ImportFromQAPLib(string filename) {
     230      if (Operators.OfType<IMoveGenerator>().Any()) {
     231        string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
     232        foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
     233          op.InversionMoveParameter.ActualName = inversionMove;
     234        string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
     235        foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
     236          op.TranslocationMoveParameter.ActualName = translocationMove;
     237      }
     238    }
     239    #endregion
     240
     241    public void ImportFileInstance(string filename) {
    221242      QAPLIBParser parser = new QAPLIBParser();
    222243      parser.Parse(filename);
     244      Coordinates = new DoubleMatrix();
    223245      DistanceMatrix = new DoubleMatrix(parser.Distances);
    224246      Weights = new DoubleMatrix(parser.Weights);
     
    228250      OnReset();
    229251    }
     252
     253    public void LoadEmbeddedInstance(string instance) {
     254      using (Stream stream = Assembly.GetExecutingAssembly()
     255        .GetManifestResourceStream(InstancePrefix + instance + ".dat")) {
     256        QAPLIBParser parser = new QAPLIBParser();
     257        parser.Parse(stream);
     258        Coordinates = new DoubleMatrix();
     259        DistanceMatrix = new DoubleMatrix(parser.Distances);
     260        Weights = new DoubleMatrix(parser.Weights);
     261        UseDistanceMatrix.Value = true;
     262        Name = "Quadratic Assignment Problem (loaded instance " + instance + ")";
     263        Description = "Loaded embedded problem data of instance " + instance + ".";
     264        OnReset();
     265      }
     266    }
    230267  }
    231268}
Note: See TracChangeset for help on using the changeset viewer.