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.VehicleRouting/3.3/VehicleRoutingProblem.cs

    r6441 r6938  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing;
    2524using System.Linq;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
    2827using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.PermutationEncoding;
    3028using HeuristicLab.Optimization;
    3129using HeuristicLab.Parameters;
     
    4038  [Creatable("Problems")]
    4139  [StorableClass]
    42   public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
     40  public sealed class VehicleRoutingProblem : SingleObjectiveHeuristicOptimizationProblem<IVRPEvaluator, IVRPCreator>, IStorableContent {
    4341    public string Filename { get; set; }
    4442
    45     public override Image ItemImage {
    46       get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
    47     }
    48 
    4943    #region Parameter Properties
    50     public ValueParameter<BoolValue> MaximizationParameter {
    51       get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
    52     }
    53     IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
    54       get { return MaximizationParameter; }
    55     }
    5644    public ValueParameter<DoubleMatrix> CoordinatesParameter {
    5745      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
     
    8169      get { return (ValueParameter<DoubleArray>)Parameters["ServiceTime"]; }
    8270    }
    83     ValueParameter<IVRPCreator> SolutionCreatorParameter {
    84       get { return (ValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
    85     }
    86     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
    87       get { return SolutionCreatorParameter; }
    88     }
    89     ValueParameter<IVRPEvaluator> EvaluatorParameter {
    90       get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
    91     }
    92     IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
    93       get { return EvaluatorParameter; }
    94     }
    9571    public IValueParameter<DoubleValue> FleetUsageFactorParameter {
    9672      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
     
    10783    public IValueParameter<DoubleValue> TardinessPenaltyParameter {
    10884      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
    109     }
    110     public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
    111       get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    112     }
    113     IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
    114       get { return BestKnownQualityParameter; }
    11585    }
    11686    public OptionalValueParameter<IVRPEncoding> BestKnownSolutionParameter {
     
    156126      set { ServiceTimeParameter.Value = value; }
    157127    }
    158     public DoubleValue BestKnownQuality {
    159       get { return BestKnownQualityParameter.Value; }
    160       set { BestKnownQualityParameter.Value = value; }
    161     }
    162128    public IVRPEncoding BestKnownSolution {
    163129      get { return BestKnownSolutionParameter.Value; }
    164130      set { BestKnownSolutionParameter.Value = value; }
    165131    }
    166     public IVRPCreator SolutionCreator {
    167       get { return SolutionCreatorParameter.Value; }
    168       set { SolutionCreatorParameter.Value = value; }
    169     }
    170     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
    171       get { return SolutionCreatorParameter.Value; }
    172     }
    173     public IVRPEvaluator Evaluator {
    174       get { return EvaluatorParameter.Value; }
    175       set { EvaluatorParameter.Value = value; }
    176     }
    177     ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
    178       get { return EvaluatorParameter.Value; }
    179     }
    180     IEvaluator IHeuristicOptimizationProblem.Evaluator {
    181       get { return EvaluatorParameter.Value; }
    182     }
    183     public IEnumerable<IOperator> Operators {
    184       get { return operators; }
    185     }
    186132    private BestVRPSolutionAnalyzer BestVRPSolutionAnalyzer {
    187       get { return operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }
     133      get { return Operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }
    188134    }
    189135    private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer {
    190       get { return operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
     136      get { return Operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }
    191137    }
    192138    #endregion
    193139
    194     [Storable]
    195     private List<IOperator> operators;
     140    // BackwardsCompatibility3.3
     141    #region Backwards compatible code, remove with 3.4
     142    [Obsolete]
     143    [Storable(Name = "operators")]
     144    private IEnumerable<IOperator> oldOperators {
     145      get { return null; }
     146      set {
     147        if (value != null && value.Any())
     148          Operators.AddRange(value);
     149      }
     150    }
     151    #endregion
    196152
    197153    [StorableConstructor]
     
    199155    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
    200156      : base(original, cloner) {
    201       operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    202157      AttachEventHandlers();
    203158    }
    204159    public VehicleRoutingProblem()
    205       : base() {
    206       IVRPCreator creator = new RandomCreator();
    207       IVRPEvaluator evaluator = new VRPEvaluator();
    208 
    209       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
     160      : base(new VRPEvaluator(), new RandomCreator()) {
    210161      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
    211162      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     
    217168      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
    218169      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
    219       Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
    220170      Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
    221171      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
     
    225175      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
    226176
    227       Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", creator));
    228       Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", evaluator));
    229 
    230       creator.VRPToursParameter.ActualName = "VRPTours";
    231       evaluator.QualityParameter.ActualName = "VRPQuality";
     177      SolutionCreator.VRPToursParameter.ActualName = "VRPTours";
     178      Evaluator.QualityParameter.ActualName = "VRPQuality";
    232179
    233180      InitializeRandomVRPInstance();
     
    245192
    246193    #region Events
    247     public event EventHandler SolutionCreatorChanged;
    248     private void OnSolutionCreatorChanged() {
    249       EventHandler handler = SolutionCreatorChanged;
    250       if (handler != null) handler(this, EventArgs.Empty);
    251     }
    252     public event EventHandler EvaluatorChanged;
    253     private void OnEvaluatorChanged() {
    254       EventHandler handler = EvaluatorChanged;
    255       if (handler != null) handler(this, EventArgs.Empty);
    256     }
    257     public event EventHandler OperatorsChanged;
    258     private void OnOperatorsChanged() {
    259       EventHandler handler = OperatorsChanged;
    260       if (handler != null) handler(this, EventArgs.Empty);
    261     }
    262     public event EventHandler Reset;
    263     private void OnReset() {
    264       EventHandler handler = Reset;
    265       if (handler != null) handler(this, EventArgs.Empty);
    266     }
    267     void VehiclesValue_ValueChanged(object sender, EventArgs e) {
     194    protected override void OnSolutionCreatorChanged() {
     195      base.OnSolutionCreatorChanged();
     196      ParameterizeSolutionCreator();
     197      ParameterizeEvaluator();
     198      ParameterizeAnalyzer();
     199      ParameterizeOperators();
     200    }
     201    protected override void OnEvaluatorChanged() {
     202      base.OnEvaluatorChanged();
     203      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     204      ParameterizeEvaluator();
     205      UpdateMoveEvaluators();
     206      ParameterizeAnalyzer();
     207    }
     208    private void VehiclesValue_ValueChanged(object sender, EventArgs e) {
    268209      ParameterizeSolutionCreator();
    269210    }
     
    287228      BestKnownSolution = null;
    288229    }
    289     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    290       ParameterizeSolutionCreator();
    291       ParameterizeEvaluator();
    292       ParameterizeAnalyzer();
    293       ParameterizeOperators();
    294       OnSolutionCreatorChanged();
    295     }
    296     private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
    297       ParameterizeEvaluator();
    298       ParameterizeAnalyzer();
    299       ParameterizeOperators();
    300     }
    301     private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
    302       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    303       ParameterizeEvaluator();
    304       UpdateMoveEvaluators();
    305       ParameterizeAnalyzer();
    306       OnEvaluatorChanged();
    307     }
    308230    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    309231      ParameterizeAnalyzer();
    310     }
    311     private void TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    312       string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
    313       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    314         op.TranslocationMoveParameter.ActualName = name;
    315       }
    316232    }
    317233
     
    453369      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
    454370
    455       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    456 
    457       EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    458371      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    459372
     
    494407
    495408    private void InitializeOperators() {
    496       operators = new List<IOperator>();
    497       operators.Add(new BestVRPSolutionAnalyzer());
    498       operators.Add(new BestAverageWorstVRPToursAnalyzer());
     409      Operators.Add(new BestVRPSolutionAnalyzer());
     410      Operators.Add(new BestAverageWorstVRPToursAnalyzer());
    499411      ParameterizeAnalyzer();
    500       operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
     412      Operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));
    501413      ParameterizeOperators();
    502414      UpdateMoveEvaluators();
    503       InitializeMoveGenerators();
    504     }
    505     private void InitializeMoveGenerators() {
    506       foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>()) {
    507         if (op is IMoveGenerator) {
    508           op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(TranslocationMoveParameter_ActualNameChanged);
    509         }
    510       }
    511 
    512415    }
    513416    private void UpdateMoveEvaluators() {
Note: See TracChangeset for help on using the changeset viewer.