Changeset 3634


Ignore:
Timestamp:
05/05/10 14:28:22 (12 years ago)
Author:
swagner
Message:

Worked on best solution analysis for the TSP (#999)

Location:
trunk/sources
Files:
4 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/HeuristicLab.Parameters-3.3.csproj

    r3413 r3634  
    8686    <None Include="HeuristicLabParametersPlugin.cs.frame" />
    8787    <Compile Include="ConstrainedValueParameter.cs" />
     88    <Compile Include="SubScopesSubScopesLookupParameter.cs" />
    8889    <Compile Include="OptionalConstrainedValueParameter.cs" />
    8990    <Compile Include="ValueParameter.cs" />
  • trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs

    r3479 r3634  
    2727namespace HeuristicLab.Parameters {
    2828  /// <summary>
    29   /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
     29  /// A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.
    3030  /// </summary>
    3131  [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]
  • trunk/sources/HeuristicLab.Parameters/3.3/SubScopesSubScopesLookupParameter.cs

    r3626 r3634  
    2727namespace HeuristicLab.Parameters {
    2828  /// <summary>
    29   /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
     29  /// A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the sub-scopes of the current scope.
    3030  /// </summary>
    31   [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]
     31  [Item("SubScopesSubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the sub-scopes of the current scope.")]
    3232  [StorableClass]
    33   public class SubScopesLookupParameter<T> : LookupParameter<ItemArray<T>> where T : class, IItem {
    34     public SubScopesLookupParameter() : base() { }
    35     public SubScopesLookupParameter(string name) : base(name) { }
    36     public SubScopesLookupParameter(string name, string description) : base(name, description) { }
    37     public SubScopesLookupParameter(string name, string description, string actualName) : base(name, description, actualName) { }
     33  public class SubScopesSubScopesLookupParameter<T> : LookupParameter<ItemArray<ItemArray<T>>> where T : class, IItem {
     34    public SubScopesSubScopesLookupParameter() : base() { }
     35    public SubScopesSubScopesLookupParameter(string name) : base(name) { }
     36    public SubScopesSubScopesLookupParameter(string name, string description) : base(name, description) { }
     37    public SubScopesSubScopesLookupParameter(string name, string description, string actualName) : base(name, description, actualName) { }
    3838
    3939    protected override IItem GetActualValue() {
    40       string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
     40      string name = LookupParameter<ItemArray<ItemArray<T>>>.TranslateName(Name, ExecutionContext);
    4141      IScope scope = ExecutionContext.Scope;
    42       ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count);
     42      ItemArray<ItemArray<T>> values = new ItemArray<ItemArray<T>>(scope.SubScopes.Count);
    4343      IVariable var;
    4444      T value;
    4545
    4646      for (int i = 0; i < values.Length; i++) {
    47         scope.SubScopes[i].Variables.TryGetValue(name, out var);
    48         if (var != null) {
    49           value = var.Value as T;
    50           if ((var.Value != null) && (value == null))
    51             throw new InvalidOperationException(
    52               string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
    53                             name,
    54                             typeof(T).GetPrettyName())
    55             );
    56           values[i] = value;
     47        values[i] = new ItemArray<T>(scope.SubScopes[i].SubScopes.Count);
     48        for (int j = 0; j < values[i].Length; j++) {
     49          scope.SubScopes[i].SubScopes[j].Variables.TryGetValue(name, out var);
     50          if (var != null) {
     51            value = var.Value as T;
     52            if ((var.Value != null) && (value == null))
     53              throw new InvalidOperationException(
     54                string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
     55                              name,
     56                              typeof(T).GetPrettyName())
     57              );
     58            values[i][j] = value;
     59          }
    5760        }
    5861      }
     
    6063    }
    6164    protected override void SetActualValue(IItem value) {
    62       ItemArray<T> values = value as ItemArray<T>;
     65      ItemArray<ItemArray<T>> values = value as ItemArray<ItemArray<T>>;
    6366      if (values == null)
    6467        throw new InvalidOperationException(
     
    6770        );
    6871
    69       string name = LookupParameter<ItemArray<T>>.TranslateName(Name, ExecutionContext);
     72      string name = LookupParameter<ItemArray<ItemArray<T>>>.TranslateName(Name, ExecutionContext);
    7073      IScope scope = ExecutionContext.Scope;
    7174      IVariable var;
    7275
    7376      for (int i = 0; i < values.Length; i++) {
    74         scope.SubScopes[i].Variables.TryGetValue(name, out var);
    75         if (var != null) var.Value = values[i];
    76         else scope.SubScopes[i].Variables.Add(new Variable(name, values[i]));
     77        for (int j = 0; j < values[i].Length; j++) {
     78          scope.SubScopes[i].SubScopes[j].Variables.TryGetValue(name, out var);
     79          if (var != null) var.Value = values[i][j];
     80          else scope.SubScopes[i].SubScopes[j].Variables.Add(new Variable(name, values[i][j]));
     81        }
    7782      }
    7883    }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/BestTSPSolutionAnalyzer.cs

    r3618 r3634  
    3636  [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
    3737  [StorableClass]
    38   public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IPopulationAnalyzer {
     38  public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, ISolutionAnalyzer {
    3939    public LookupParameter<DoubleMatrix> CoordinatesParameter {
    4040      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    4141    }
    42     public SubScopesLookupParameter<Permutation> PermutationParameter {
    43       get { return (SubScopesLookupParameter<Permutation>)Parameters["Permutation"]; }
     42    public LookupParameter<Permutation> PermutationParameter {
     43      get { return (LookupParameter<Permutation>)Parameters["Permutation"]; }
    4444    }
    45     public SubScopesLookupParameter<DoubleValue> QualityParameter {
    46       get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
     45    public LookupParameter<DoubleValue> QualityParameter {
     46      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
    4747    }
    4848    public LookupParameter<PathTSPTour> BestSolutionParameter {
     
    5656      : base() {
    5757      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    58       Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
    59       Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
     58      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be analyzed."));
     59      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the TSP solution which should be analyzed."));
    6060      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
    61       Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
     61      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the TSP solution should be stored."));
    6262    }
    6363
    6464    public override IOperation Apply() {
    6565      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
    66       ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
    67       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
     66      Permutation permutation = PermutationParameter.ActualValue;
     67      DoubleValue quality = QualityParameter.ActualValue;
    6868      ResultCollection results = ResultsParameter.ActualValue;
    69 
    70       int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    7169
    7270      PathTSPTour tour = BestSolutionParameter.ActualValue;
    7371      if (tour == null) {
    74         tour = new PathTSPTour(coordinates, permutations[i], qualities[i]);
     72        tour = new PathTSPTour(coordinates, permutation, quality);
    7573        BestSolutionParameter.ActualValue = tour;
    7674        results.Add(new Result("Best TSP Solution", tour));
    7775      } else {
    78         if (tour.Quality.Value > qualities[i].Value) {
     76        if (tour.Quality.Value > quality.Value) {
    7977          tour.Coordinates = coordinates;
    80           tour.Permutation = permutations[i];
    81           tour.Quality = qualities[i];
     78          tour.Permutation = permutation;
     79          tour.Quality = quality;
    8280          results["Best TSP Solution"].Value = tour;
    8381        }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/MultiPopulationBestTSPSolutionAnalyzer.cs

    r3626 r3634  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    2930using HeuristicLab.Parameters;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using System.Collections.Generic;
    3133
    3234namespace HeuristicLab.Problems.TravelingSalesman {
     
    3436  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
    3537  /// </summary>
    36   [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
     38  [Item("MultiPopulationBestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
    3739  [StorableClass]
    38   public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IPopulationAnalyzer {
     40  public sealed class MultiPopulationBestTSPSolutionAnalyzer : SingleSuccessorOperator, IMultiPopulationAnalyzer {
    3941    public LookupParameter<DoubleMatrix> CoordinatesParameter {
    4042      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    4143    }
    42     public SubScopesLookupParameter<Permutation> PermutationParameter {
    43       get { return (SubScopesLookupParameter<Permutation>)Parameters["Permutation"]; }
     44    public SubScopesSubScopesLookupParameter<Permutation> PermutationParameter {
     45      get { return (SubScopesSubScopesLookupParameter<Permutation>)Parameters["Permutation"]; }
    4446    }
    45     public SubScopesLookupParameter<DoubleValue> QualityParameter {
    46       get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
     47    public SubScopesSubScopesLookupParameter<DoubleValue> QualityParameter {
     48      get { return (SubScopesSubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
    4749    }
    4850    public LookupParameter<PathTSPTour> BestSolutionParameter {
     
    5355    }
    5456
    55     public BestTSPSolutionAnalyzer()
     57    public MultiPopulationBestTSPSolutionAnalyzer()
    5658      : base() {
    5759      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    58       Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
    59       Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
     60      Parameters.Add(new SubScopesSubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
     61      Parameters.Add(new SubScopesSubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
    6062      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
    6163      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
     
    6466    public override IOperation Apply() {
    6567      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
    66       ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
    67       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
     68      ItemArray<ItemArray<Permutation>> permutations = PermutationParameter.ActualValue;
     69      ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
    6870      ResultCollection results = ResultsParameter.ActualValue;
    6971
    70       int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
     72      DoubleValue bestQuality = new DoubleValue(double.MaxValue);
     73      Permutation bestPermutation = null;
     74
     75      for (int i = 0; i < qualities.Length; i++) {
     76        for (int j = 0; j < qualities[i].Length; j++) {
     77          if (qualities[i][j].Value < bestQuality.Value) {
     78            bestQuality = qualities[i][j];
     79            bestPermutation = permutations[i][j];
     80          }
     81        }
     82      }
    7183
    7284      PathTSPTour tour = BestSolutionParameter.ActualValue;
    7385      if (tour == null) {
    74         tour = new PathTSPTour(coordinates, permutations[i], qualities[i]);
     86        tour = new PathTSPTour(coordinates, bestPermutation, bestQuality);
    7587        BestSolutionParameter.ActualValue = tour;
    7688        results.Add(new Result("Best TSP Solution", tour));
    7789      } else {
    78         if (tour.Quality.Value > qualities[i].Value) {
     90        if (tour.Quality.Value > bestQuality.Value) {
    7991          tour.Coordinates = coordinates;
    80           tour.Permutation = permutations[i];
    81           tour.Quality = qualities[i];
     92          tour.Permutation = bestPermutation;
     93          tour.Quality = bestQuality;
    8294          results["Best TSP Solution"].Value = tour;
    8395        }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/PopulationBestTSPSolutionAnalyzer.cs

    r3626 r3634  
    3434  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
    3535  /// </summary>
    36   [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
     36  [Item("PopulationBestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
    3737  [StorableClass]
    38   public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IPopulationAnalyzer {
     38  public sealed class PopulationBestTSPSolutionAnalyzer : SingleSuccessorOperator, IPopulationAnalyzer {
    3939    public LookupParameter<DoubleMatrix> CoordinatesParameter {
    4040      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
     
    5353    }
    5454
    55     public BestTSPSolutionAnalyzer()
     55    public PopulationBestTSPSolutionAnalyzer()
    5656      : base() {
    5757      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/HeuristicLab.Problems.TravelingSalesman-3.3.csproj

    r3616 r3634  
    8484  </ItemGroup>
    8585  <ItemGroup>
     86    <Compile Include="Analyzers\MultiPopulationBestTSPSolutionAnalyzer.cs" />
     87    <Compile Include="Analyzers\PopulationBestTSPSolutionAnalyzer.cs" />
    8688    <Compile Include="Analyzers\BestTSPSolutionAnalyzer.cs" />
    8789    <Compile Include="Evaluators\TSPEuclideanPathEvaluator.cs" />
Note: See TracChangeset for help on using the changeset viewer.