Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/26/11 23:36:10 (13 years ago)
Author:
abeham
Message:

#1663

  • Added an *Operators* parameter in Problem
  • Adapted KnapsackProblem, OnemaxProblem, SingleObjectiveTestFunctionProblem, TravelingSalesmanProblem, and VehicleRoutingProblem to derive from SingleObjectiveHeuristicOptimizationProblem<U, V>, renamed and marked old list as obsolete. Note that QuadraticAssignmentProblem, as well as the new DataAnalysisProblems already derived from that class. ArtificialAntProblem was not yet modified.
  • Added some test lists and adapted SamplesTest
  • Added plugin dependencies that the unit test was complaining about
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r6051 r6938  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing;
    2524using System.IO;
    2625using System.Linq;
     
    3837  [Creatable("Problems")]
    3938  [StorableClass]
    40   public sealed class TravelingSalesmanProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
     39  public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent {
    4140    public string Filename { get; set; }
    4241
    43     public override Image ItemImage {
    44       get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
    45     }
    46 
    4742    #region Parameter Properties
    48     public ValueParameter<BoolValue> MaximizationParameter {
    49       get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
    50     }
    51     IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
    52       get { return MaximizationParameter; }
    53     }
    5443    public ValueParameter<DoubleMatrix> CoordinatesParameter {
    5544      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
     
    6049    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
    6150      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
    62     }
    63     public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
    64       get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
    65     }
    66     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
    67       get { return SolutionCreatorParameter; }
    68     }
    69     public ValueParameter<ITSPEvaluator> EvaluatorParameter {
    70       get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
    71     }
    72     IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
    73       get { return EvaluatorParameter; }
    74     }
    75     public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
    76       get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    77     }
    78     IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
    79       get { return BestKnownQualityParameter; }
    8051    }
    8152    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
     
    9768      set { UseDistanceMatrixParameter.Value = value; }
    9869    }
    99     public IPermutationCreator SolutionCreator {
    100       get { return SolutionCreatorParameter.Value; }
    101       set { SolutionCreatorParameter.Value = value; }
    102     }
    103     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
    104       get { return SolutionCreatorParameter.Value; }
    105     }
    106     public ITSPEvaluator Evaluator {
    107       get { return EvaluatorParameter.Value; }
    108       set { EvaluatorParameter.Value = value; }
    109     }
    110     ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
    111       get { return EvaluatorParameter.Value; }
    112     }
    113     IEvaluator IHeuristicOptimizationProblem.Evaluator {
    114       get { return EvaluatorParameter.Value; }
    115     }
    116     public DoubleValue BestKnownQuality {
    117       get { return BestKnownQualityParameter.Value; }
    118       set { BestKnownQualityParameter.Value = value; }
    119     }
    12070    public Permutation BestKnownSolution {
    12171      get { return BestKnownSolutionParameter.Value; }
    12272      set { BestKnownSolutionParameter.Value = value; }
    12373    }
    124     public IEnumerable<IOperator> Operators {
    125       get { return operators; }
    126     }
    12774    private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
    128       get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
     75      get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
    12976    }
    13077    private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
    131       get { return operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
     78      get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
    13279    }
    13380    private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer {
    134       get { return operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
    135     }
    136     #endregion
    137 
    138     [Storable]
    139     private List<IOperator> operators;
     81      get { return Operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
     82    }
     83    #endregion
     84
     85    // BackwardsCompatibility3.3
     86    #region Backwards compatible code, remove with 3.4
     87    [Obsolete]
     88    [Storable(Name = "operators")]
     89    private IEnumerable<IOperator> oldOperators {
     90      get { return null; }
     91      set {
     92        if (value != null && value.Any())
     93          Operators.AddRange(value);
     94      }
     95    }
     96    #endregion
    14097
    14198    [StorableConstructor]
     
    143100    private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner)
    144101      : base(original, cloner) {
    145       this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    146102      AttachEventHandlers();
    147103    }
     
    150106    }
    151107    public TravelingSalesmanProblem()
    152       : base() {
    153       RandomPermutationCreator creator = new RandomPermutationCreator();
    154       TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
    155 
    156       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
     108      : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
     109
    157110      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
    158111      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    159112      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
    160       Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
    161       Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
    162       Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
    163113      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
    164114
     
    173123      });
    174124
    175       creator.PermutationParameter.ActualName = "TSPTour";
    176       evaluator.QualityParameter.ActualName = "TSPTourLength";
     125      SolutionCreator.PermutationParameter.ActualName = "TSPTour";
     126      Evaluator.QualityParameter.ActualName = "TSPTourLength";
    177127      ParameterizeSolutionCreator();
    178128      ParameterizeEvaluator();
     
    183133
    184134    #region Events
    185     public event EventHandler SolutionCreatorChanged;
    186     private void OnSolutionCreatorChanged() {
    187       EventHandler handler = SolutionCreatorChanged;
    188       if (handler != null) handler(this, EventArgs.Empty);
    189     }
    190     public event EventHandler EvaluatorChanged;
    191     private void OnEvaluatorChanged() {
    192       EventHandler handler = EvaluatorChanged;
    193       if (handler != null) handler(this, EventArgs.Empty);
    194     }
    195     public event EventHandler OperatorsChanged;
    196     private void OnOperatorsChanged() {
    197       EventHandler handler = OperatorsChanged;
    198       if (handler != null) handler(this, EventArgs.Empty);
    199     }
    200     public event EventHandler Reset;
    201     private void OnReset() {
    202       EventHandler handler = Reset;
    203       if (handler != null) handler(this, EventArgs.Empty);
    204     }
    205 
     135    protected override void OnSolutionCreatorChanged() {
     136      base.OnSolutionCreatorChanged();
     137      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     138      ParameterizeSolutionCreator();
     139      ParameterizeEvaluator();
     140      ParameterizeAnalyzers();
     141      ParameterizeOperators();
     142    }
     143    protected override void OnEvaluatorChanged() {
     144      base.OnEvaluatorChanged();
     145      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     146      ParameterizeEvaluator();
     147      UpdateMoveEvaluators();
     148      ParameterizeAnalyzers();
     149      ClearDistanceMatrix();
     150    }
    206151    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
    207152      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     
    217162      ClearDistanceMatrix();
    218163    }
    219     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    220       SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    221       ParameterizeSolutionCreator();
    222       ParameterizeEvaluator();
    223       ParameterizeAnalyzers();
    224       ParameterizeOperators();
    225       OnSolutionCreatorChanged();
    226     }
    227164    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
    228165      ParameterizeEvaluator();
     
    230167      ParameterizeOperators();
    231168    }
    232     private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
    233       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    234       ParameterizeEvaluator();
    235       UpdateMoveEvaluators();
    236       ParameterizeAnalyzers();
    237       ClearDistanceMatrix();
    238       OnEvaluatorChanged();
    239     }
    240169    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    241170      ParameterizeAnalyzers();
    242     }
    243     private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    244       string name = ((ILookupParameter<InversionMove>)sender).ActualName;
    245       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
    246         op.InversionMoveParameter.ActualName = name;
    247       }
    248     }
    249     private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    250       string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
    251       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    252         op.TranslocationMoveParameter.ActualName = name;
    253       }
    254171    }
    255172    #endregion
     
    277194      }
    278195
    279       if (operators == null) InitializeOperators();
     196      if (Operators.Count == 0) InitializeOperators();
    280197      #endregion
    281198      AttachEventHandlers();
     
    286203      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    287204      Coordinates.Reset += new EventHandler(Coordinates_Reset);
    288       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    289205      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    290       EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    291206      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    292207    }
    293208
    294209    private void InitializeOperators() {
    295       operators = new List<IOperator>();
    296       operators.Add(new BestTSPSolutionAnalyzer());
    297       operators.Add(new TSPAlleleFrequencyAnalyzer());
    298       operators.Add(new TSPPopulationDiversityAnalyzer());
    299       ParameterizeAnalyzers();
    300       operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
     210      Operators.Add(new BestTSPSolutionAnalyzer());
     211      Operators.Add(new TSPAlleleFrequencyAnalyzer());
     212      Operators.Add(new TSPPopulationDiversityAnalyzer());
     213      ParameterizeAnalyzers();
     214      Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
    301215      ParameterizeOperators();
    302216      UpdateMoveEvaluators();
    303       InitializeMoveGenerators();
    304     }
    305     private void InitializeMoveGenerators() {
    306       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
    307         if (op is IMoveGenerator) {
    308           op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
    309         }
    310       }
    311       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    312         if (op is IMoveGenerator) {
    313           op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
    314         }
    315       }
    316217    }
    317218    private void UpdateMoveEvaluators() {
    318       operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
     219      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
    319220      foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
    320221        if (op.EvaluatorType == Evaluator.GetType()) {
    321           operators.Add(op);
     222          Operators.Add(op);
    322223        }
    323224      ParameterizeOperators();
     
    400301        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    401302        op.PermutationParameter.Hidden = true;
    402       }
    403       string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
    404       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
    405         op.InversionMoveParameter.ActualName = inversionMove;
    406         op.InversionMoveParameter.Hidden = true;
    407       }
    408       string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
    409       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    410         op.TranslocationMoveParameter.ActualName = translocationMove;
    411         op.TranslocationMoveParameter.Hidden = true;
    412303      }
    413304      foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
Note: See TracChangeset for help on using the changeset viewer.