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.Knapsack/3.3/KnapsackProblem.cs

    r6476 r6938  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    3736  [Creatable("Problems")]
    3837  [StorableClass]
    39   public sealed class KnapsackProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
     38  public sealed class KnapsackProblem : SingleObjectiveHeuristicOptimizationProblem<IKnapsackEvaluator, IBinaryVectorCreator>, IStorableContent {
    4039    public string Filename { get; set; }
    4140
    42     public override Image ItemImage {
    43       get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
    44     }
    45 
    4641    #region Parameter Properties
    47     public ValueParameter<BoolValue> MaximizationParameter {
    48       get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
    49     }
    50     IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
    51       get { return MaximizationParameter; }
    52     }
    5342    public ValueParameter<IntValue> KnapsackCapacityParameter {
    5443      get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; }
     
    6352      get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }
    6453    }
    65     public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
    66       get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
    67     }
    68     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
    69       get { return SolutionCreatorParameter; }
    70     }
    71     public ValueParameter<IKnapsackEvaluator> EvaluatorParameter {
    72       get { return (ValueParameter<IKnapsackEvaluator>)Parameters["Evaluator"]; }
    73     }
    74     IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
    75       get { return EvaluatorParameter; }
    76     }
    77     public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
    78       get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    79     }
    80     IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
    81       get { return BestKnownQualityParameter; }
    82     }
    8354    public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter {
    8455      get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
     
    8758
    8859    #region Properties
    89     public BoolValue Maximization {
    90       get { return MaximizationParameter.Value; }
    91       set { MaximizationParameter.Value = value; }
    92     }
    9360    public IntValue KnapsackCapacity {
    9461      get { return KnapsackCapacityParameter.Value; }
     
    10774      set { PenaltyParameter.Value = value; }
    10875    }
    109     public IBinaryVectorCreator SolutionCreator {
    110       get { return SolutionCreatorParameter.Value; }
    111       set { SolutionCreatorParameter.Value = value; }
    112     }
    113     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
    114       get { return SolutionCreatorParameter.Value; }
    115     }
    116     public IKnapsackEvaluator Evaluator {
    117       get { return EvaluatorParameter.Value; }
    118       set { EvaluatorParameter.Value = value; }
    119     }
    120     ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
    121       get { return EvaluatorParameter.Value; }
    122     }
    123     IEvaluator IHeuristicOptimizationProblem.Evaluator {
    124       get { return EvaluatorParameter.Value; }
    125     }
    126     public DoubleValue BestKnownQuality {
    127       get { return BestKnownQualityParameter.Value; }
    128       set { BestKnownQualityParameter.Value = value; }
    129     }
    13076    public BinaryVector BestKnownSolution {
    13177      get { return BestKnownSolutionParameter.Value; }
    13278      set { BestKnownSolutionParameter.Value = value; }
    13379    }
    134     public IEnumerable<IOperator> Operators {
    135       get { return operators.Cast<IOperator>(); }
    136     }
    13780    private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer {
    138       get { return operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); }
    139     }
    140     #endregion
    141 
    142     [Storable]
    143     private List<IOperator> operators;
     81      get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().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
    14497
    14598    [StorableConstructor]
     
    147100    private KnapsackProblem(KnapsackProblem original, Cloner cloner)
    148101      : base(original, cloner) {
    149       this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    150102      AttachEventHandlers();
    151103    }
     
    154106    }
    155107    public KnapsackProblem()
    156       : base() {
    157       RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
    158       KnapsackEvaluator evaluator = new KnapsackEvaluator();
    159 
    160       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Knapsack Problem is a maximization problem.", new BoolValue(true)));
     108      : base(new KnapsackEvaluator(), new RandomBinaryVectorCreator()) {
    161109      Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
    162110      Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
    163111      Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
    164112      Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1)));
    165       Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new Knapsack solutions.", creator));
    166       Parameters.Add(new ValueParameter<IKnapsackEvaluator>("Evaluator", "The operator which should be used to evaluate Knapsack solutions.", evaluator));
    167       Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Knapsack instance."));
    168113      Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance."));
    169114
    170       creator.BinaryVectorParameter.ActualName = "KnapsackSolution";
     115      Maximization.Value = true;
     116
     117      SolutionCreator.BinaryVectorParameter.ActualName = "KnapsackSolution";
    171118
    172119      InitializeRandomKnapsackInstance();
     
    180127
    181128    #region Events
    182     public event EventHandler SolutionCreatorChanged;
    183     private void OnSolutionCreatorChanged() {
    184       EventHandler handler = SolutionCreatorChanged;
    185       if (handler != null) handler(this, EventArgs.Empty);
    186     }
    187     public event EventHandler EvaluatorChanged;
    188     private void OnEvaluatorChanged() {
    189       EventHandler handler = EvaluatorChanged;
    190       if (handler != null) handler(this, EventArgs.Empty);
    191     }
    192     public event EventHandler OperatorsChanged;
    193     private void OnOperatorsChanged() {
    194       EventHandler handler = OperatorsChanged;
    195       if (handler != null) handler(this, EventArgs.Empty);
    196     }
    197     public event EventHandler Reset;
    198     private void OnReset() {
    199       EventHandler handler = Reset;
    200       if (handler != null) handler(this, EventArgs.Empty);
    201     }
    202 
    203     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
     129    protected override void OnSolutionCreatorChanged() {
     130      base.OnSolutionCreatorChanged();
    204131      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
    205132      ParameterizeSolutionCreator();
     
    207134      ParameterizeAnalyzer();
    208135      ParameterizeOperators();
    209       OnSolutionCreatorChanged();
     136    }
     137    protected override void OnEvaluatorChanged() {
     138      base.OnEvaluatorChanged();
     139      ParameterizeEvaluator();
     140      ParameterizeAnalyzer();
    210141    }
    211142    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
     
    214145      ParameterizeOperators();
    215146    }
    216     private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
    217       ParameterizeEvaluator();
    218       ParameterizeAnalyzer();
    219       OnEvaluatorChanged();
    220     }
    221     void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {
    222       ParameterizeEvaluator();
    223       ParameterizeAnalyzer();
    224     }
    225     void WeightsParameter_ValueChanged(object sender, EventArgs e) {
     147    private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {
     148      ParameterizeEvaluator();
     149      ParameterizeAnalyzer();
     150    }
     151    private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
    226152      ParameterizeEvaluator();
    227153      ParameterizeAnalyzer();
     
    230156      WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset);
    231157    }
    232     void WeightsValue_Reset(object sender, EventArgs e) {
     158    private void WeightsValue_Reset(object sender, EventArgs e) {
    233159      ParameterizeSolutionCreator();
    234160
     
    236162        ((IStringConvertibleArray)ValuesParameter.Value).Length = WeightsParameter.Value.Length;
    237163    }
    238     void ValuesParameter_ValueChanged(object sender, EventArgs e) {
     164    private void ValuesParameter_ValueChanged(object sender, EventArgs e) {
    239165      ParameterizeEvaluator();
    240166      ParameterizeAnalyzer();
     
    243169      ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset);
    244170    }
    245     void ValuesValue_Reset(object sender, EventArgs e) {
     171    private void ValuesValue_Reset(object sender, EventArgs e) {
    246172      ParameterizeSolutionCreator();
    247173
     
    249175        ((IStringConvertibleArray)WeightsParameter.Value).Length = ValuesParameter.Value.Length;
    250176    }
    251     void PenaltyParameter_ValueChanged(object sender, EventArgs e) {
    252       ParameterizeEvaluator();
    253     }
    254     void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
     177    private void PenaltyParameter_ValueChanged(object sender, EventArgs e) {
     178      ParameterizeEvaluator();
     179    }
     180    private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    255181      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
    256182      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
     
    265191      // BackwardsCompatibility3.3
    266192      #region Backwards compatible code (remove with 3.4)
    267       if (operators == null) InitializeOperators();
     193      if (Operators.Count > 0) InitializeOperators();
    268194      #endregion
    269195      AttachEventHandlers();
     
    271197
    272198    private void AttachEventHandlers() {
    273       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    274199      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
    275       EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    276200      KnapsackCapacityParameter.ValueChanged += new EventHandler(KnapsackCapacityParameter_ValueChanged);
    277201      WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
     
    319243    }
    320244    private void InitializeOperators() {
    321       operators = new List<IOperator>();
    322       operators.Add(new BestKnapsackSolutionAnalyzer());
     245      Operators.Add(new BestKnapsackSolutionAnalyzer());
    323246      ParameterizeAnalyzer();
    324247      foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
    325248        if (!(op is ISingleObjectiveMoveEvaluator) || (op is IKnapsackMoveEvaluator)) {
    326           operators.Add(op);
     249          Operators.Add(op);
    327250        }
    328251      }
Note: See TracChangeset for help on using the changeset viewer.