Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/06/11 20:47:56 (12 years ago)
Author:
abeham
Message:

#1619

  • reverted changes to trunk
  • readded instances as embedded resources
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r6939 r6952  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Drawing;
    2425using System.IO;
    2526using System.Linq;
    2627using System.Reflection;
    27 using HeuristicLab.Collections;
    2828using HeuristicLab.Common;
    2929using HeuristicLab.Core;
     
    9393    }
    9494
    95     private ObservableList<string> instances = new ObservableList<string>();
    96     public ObservableList<string> Instances {
    97       get { return instances; }
     95    public IEnumerable<string> Instances {
     96      get {
     97        return Assembly.GetExecutingAssembly()
     98          .GetManifestResourceNames()
     99          .Where(x => x.EndsWith(".dat"))
     100          .OrderBy(x => x)
     101          .Select(x => x.Replace(".dat", String.Empty))
     102          .Select(x => x.Replace(InstancePrefix, String.Empty));
     103      }
    98104    }
    99105    #endregion
     
    103109    private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
    104110      : base(original, cloner) {
    105       instances = new ObservableList<string>(original.instances);
    106111      AttachEventHandlers();
    107112    }
     
    412417      OnReset();
    413418    }
     419
     420    public void LoadInstanceFromEmbeddedResource(string instance) {
     421      using (Stream stream = Assembly.GetExecutingAssembly()
     422        .GetManifestResourceStream(InstancePrefix + instance + ".dat")) {
     423        QAPLIBParser datParser = new QAPLIBParser();
     424        datParser.Parse(stream);
     425        if (datParser.Error != null) throw datParser.Error;
     426        Distances = new DoubleMatrix(datParser.Distances);
     427        Weights = new DoubleMatrix(datParser.Weights);
     428        Name = instance;
     429        Description = "Loaded embedded instance " + instance + " of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
     430
     431        bool solutionExists = Assembly.GetExecutingAssembly()
     432          .GetManifestResourceNames()
     433          .Where(x => x.EndsWith(instance + ".sln"))
     434          .Any();
     435
     436        if (solutionExists) {
     437          using (Stream solStream = Assembly.GetExecutingAssembly()
     438            .GetManifestResourceStream(InstancePrefix + instance + ".sln")) {
     439            QAPLIBSolutionParser slnParser = new QAPLIBSolutionParser();
     440            slnParser.Parse(solStream, true);
     441            if (slnParser.Error != null) throw slnParser.Error;
     442
     443            BestKnownQuality = new DoubleValue(slnParser.Quality);
     444            BestKnownSolution = new Permutation(PermutationTypes.Absolute, slnParser.Assignment);
     445            BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
     446            BestKnownSolutions.Add((Permutation)BestKnownSolution.Clone());
     447
     448            if (!BestKnownQuality.Value.IsAlmost(QAPEvaluator.Apply(BestKnownSolution, Weights, Distances))) {
     449              // the solution doesn't result in the given quality, maybe indices and values are inverted
     450              // try parsing again, this time inverting them
     451              solStream.Seek(0, SeekOrigin.Begin);
     452              slnParser.Reset();
     453              slnParser.Parse(solStream, false);
     454              if (slnParser.Error != null) throw slnParser.Error;
     455
     456              BestKnownQuality = new DoubleValue(slnParser.Quality);
     457              BestKnownSolution = new Permutation(PermutationTypes.Absolute, slnParser.Assignment);
     458              BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
     459              BestKnownSolutions.Add((Permutation)BestKnownSolution.Clone());
     460
     461              if (!BestKnownQuality.Value.IsAlmost(QAPEvaluator.Apply(BestKnownSolution, Weights, Distances))) {
     462                // if the solution still doesn't result in the given quality, remove it and only take the quality
     463                BestKnownSolution = null;
     464                BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
     465              }
     466            }
     467          }
     468        }
     469      }
     470      OnReset();
     471    }
    414472  }
    415473}
Note: See TracChangeset for help on using the changeset viewer.