Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/26/15 09:30:43 (9 years ago)
Author:
abeham
Message:

#2521:

  • Adapted Knapsack problem to new problem infrastructure
  • Introduced additional interfaces in binary vector encoding
  • Improved KnapsackImprovementOperator which requires less evaluated solutions in case of an infeasible start solution

Loosely related change:

  • All LookupParameters are now shown by default
  • Wiring code should make sure that wired parameters are hidden
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs

    r12012 r13404  
    3636  [Item("BestKnapsackSolutionAnalyzer", "An operator for analyzing the best solution for a Knapsack problem.")]
    3737  [StorableClass]
    38   public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
     38  public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IBinaryVectorSolutionsOperator, IAnalyzer, ISingleObjectiveOperator {
    3939    public virtual bool EnabledByDefault {
    4040      get { return true; }
    4141    }
    4242
    43     public LookupParameter<BoolValue> MaximizationParameter {
    44       get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
     43    public ILookupParameter<BoolValue> MaximizationParameter {
     44      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
    4545    }
    46     public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
    47       get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
     46    public IScopeTreeLookupParameter<BinaryVector> BinaryVectorsParameter {
     47      get { return (IScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVectors"]; }
    4848    }
    49     public LookupParameter<IntValue> KnapsackCapacityParameter {
    50       get { return (LookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
     49    public ILookupParameter<IntValue> KnapsackCapacityParameter {
     50      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
    5151    }
    52     public LookupParameter<IntArray> WeightsParameter {
    53       get { return (LookupParameter<IntArray>)Parameters["Weights"]; }
     52    public ILookupParameter<IntArray> WeightsParameter {
     53      get { return (ILookupParameter<IntArray>)Parameters["Weights"]; }
    5454    }
    55     public LookupParameter<IntArray> ValuesParameter {
    56       get { return (LookupParameter<IntArray>)Parameters["Values"]; }
     55    public ILookupParameter<IntArray> ValuesParameter {
     56      get { return (ILookupParameter<IntArray>)Parameters["Values"]; }
    5757    }
    58     public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    59       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     58    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     59      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    6060    }
    61     public LookupParameter<KnapsackSolution> BestSolutionParameter {
    62       get { return (LookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
     61    public ILookupParameter<KnapsackSolution> BestSolutionParameter {
     62      get { return (ILookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }
    6363    }
    64     public ValueLookupParameter<ResultCollection> ResultsParameter {
    65       get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
     64    public IValueLookupParameter<ResultCollection> ResultsParameter {
     65      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
    6666    }
    67     public LookupParameter<DoubleValue> BestKnownQualityParameter {
    68       get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
     67    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
     68      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    6969    }
    70     public LookupParameter<BinaryVector> BestKnownSolutionParameter {
    71       get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
     70    public ILookupParameter<BinaryVector> BestKnownSolutionParameter {
     71      get { return (ILookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
    7272    }
    7373
     
    7878      : base() {
    7979      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
    80       Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The Knapsack solutions from which the best solution should be visualized."));
     80      Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVectors", "The Knapsack solutions from which the best solution should be visualized."));
    8181      Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack."));
    8282      Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items."));
     
    9595
    9696    public override IOperation Apply() {
    97       ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;
    98       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    99       ResultCollection results = ResultsParameter.ActualValue;
    100       bool max = MaximizationParameter.ActualValue.Value;
    101       DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
     97      var binaryVectors = BinaryVectorsParameter.ActualValue;
     98      var qualities = QualityParameter.ActualValue;
     99      var results = ResultsParameter.ActualValue;
     100      var max = MaximizationParameter.ActualValue.Value;
     101      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
    102102
    103103      int i = -1;
    104       if (!max)
    105         i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    106       else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
     104      i = !max ? qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index
     105               : qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
    107106
    108107      if (bestKnownQuality == null ||
     
    113112      }
    114113
    115       KnapsackSolution solution = BestSolutionParameter.ActualValue;
     114      var solution = BestSolutionParameter.ActualValue;
    116115      if (solution == null) {
    117116        solution = new KnapsackSolution((BinaryVector)binaryVectors[i].Clone(), new DoubleValue(qualities[i].Value),
Note: See TracChangeset for help on using the changeset viewer.