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
Location:
branches/ProblemRefactoring
Files:
2 added
1 deleted
20 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.DotSettings
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs

    r13396 r13404  
    5252    }
    5353    #endregion
    54 
    5554
    5655    public int Length {
     
    108107        typeof (IBinaryVectorMoveOperator),
    109108        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
     109        typeof (IBinaryVectorSolutionOperator),
     110        typeof (IBinaryVectorSolutionsOperator)
    110111      };
    111112    }
     
    129130      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
    130131      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
     132      ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>());
     133      ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>());
    131134    }
    132135
     
    134137    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
    135138      foreach (var creator in creators) {
    136         creator.BinaryVectorParameter.ActualName = Name;
    137139        creator.LengthParameter.ActualName = LengthParameter.Name;
    138140      }
     
    145147    }
    146148    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
    147       foreach (var manipulator in manipulators) {
    148         manipulator.BinaryVectorParameter.ActualName = Name;
    149       }
     149      // binary vector manipulators don't contain additional parameters besides the solution parameter
    150150    }
    151151    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
    152       foreach (var moveOperator in moveOperators) {
    153         moveOperator.BinaryVectorParameter.ActualName = Name;
    154       }
     152      // binary vector move operators don't contain additional parameters besides the solution parameter
    155153    }
    156154    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
     
    160158    }
    161159    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
    162       foreach (var shakingOperator in shakingOperators) {
    163         shakingOperator.BinaryVectorParameter.ActualName = Name;
    164       }
     160      // binary vector shaking operators don't contain additional parameters besides the solution parameter
     161    }
     162    private void ConfigureSolutionOperators(IEnumerable<IBinaryVectorSolutionOperator> solutionOperators) {
     163      foreach (var solutionOperator in solutionOperators)
     164        solutionOperator.BinaryVectorParameter.ActualName = Name;
     165    }
     166    private void ConfigureSolutionsOperators(IEnumerable<IBinaryVectorSolutionsOperator> solutionsOperators) {
     167      foreach (var solutionsOperator in solutionsOperators)
     168        solutionsOperator.BinaryVectorsParameter.ActualName = Name;
    165169    }
    166170    #endregion
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/HeuristicLab.Encodings.BinaryVectorEncoding-3.3.csproj

    r11961 r13404  
    127127    <Compile Include="BinaryVector.cs" />
    128128    <Compile Include="Interfaces\IBinaryVectorMultiNeighborhoodShakingOperator.cs" />
     129    <Compile Include="Interfaces\IBinaryVectorSolutionsOperator.cs" />
     130    <Compile Include="Interfaces\IBinaryVectorSolutionOperator.cs" />
     131    <Compile Include="Interfaces\IOneBitflipMoveQualityOperator.cs" />
    129132    <Compile Include="Interfaces\IOneBitflipMoveOperator.cs" />
    130133    <Compile Include="Interfaces\IBinaryVectorCreator.cs" />
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorCreator.cs

    r13336 r13404  
    2828  /// An interface which represents an operator for creating vectors of bool-valued data.
    2929  /// </summary>
    30   public interface IBinaryVectorCreator : IBinaryVectorOperator, ISolutionCreator<BinaryVector> {
     30  public interface IBinaryVectorCreator : IBinaryVectorSolutionOperator, ISolutionCreator<BinaryVector> {
    3131    IValueLookupParameter<IntValue> LengthParameter { get; }
    32     ILookupParameter<BinaryVector> BinaryVectorParameter { get; }
    3332  }
    3433}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorManipulator.cs

    r12012 r13404  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    2322using HeuristicLab.Optimization;
    2423
     
    2726  /// An interface which represents an operator for manipulating vectors of bool-valued data.
    2827  /// </summary>
    29   public interface IBinaryVectorManipulator : IBinaryVectorOperator, IManipulator {
    30     ILookupParameter<BinaryVector> BinaryVectorParameter { get; }
     28  public interface IBinaryVectorManipulator : IBinaryVectorSolutionOperator, IManipulator {
    3129  }
    3230}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorMoveOperator.cs

    r12012 r13404  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    2322using HeuristicLab.Optimization;
    2423
    2524namespace HeuristicLab.Encodings.BinaryVectorEncoding {
    26   public interface IBinaryVectorMoveOperator : IBinaryVectorOperator, IMoveOperator {
    27     ILookupParameter<BinaryVector> BinaryVectorParameter { get; }
     25  public interface IBinaryVectorMoveOperator : IBinaryVectorSolutionOperator, IMoveOperator {
    2826  }
    2927}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorMultiNeighborhoodShakingOperator.cs

    r12012 r13404  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    2322
    2423namespace HeuristicLab.Encodings.BinaryVectorEncoding {
    25   public interface IBinaryVectorMultiNeighborhoodShakingOperator : IBinaryVectorOperator {
    26     ILookupParameter<BinaryVector> BinaryVectorParameter { get; }
     24  public interface IBinaryVectorMultiNeighborhoodShakingOperator : IBinaryVectorSolutionOperator {
    2725  }
    2826}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IOneBitflipMoveQualityOperator.cs

    r13402 r13404  
    2121
    2222using HeuristicLab.Core;
     23using HeuristicLab.Data;
    2324
    2425namespace HeuristicLab.Encodings.BinaryVectorEncoding {
    25   public interface IOneBitflipMoveOperator : IBinaryVectorMoveOperator {
    26     ILookupParameter<OneBitflipMove> OneBitflipMoveParameter { get; }
     26  public interface IOneBitflipMoveQualityOperator : IOneBitflipMoveOperator {
     27    ILookupParameter<DoubleValue> MoveQualityParameter { get; }
    2728  }
    2829}
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveMaker.cs

    r12012 r13404  
    3131  [Item("OneBitflipMoveMaker", "Peforms a one bitflip move on a given BitVector and updates the quality.")]
    3232  [StorableClass]
    33   public class OneBitflipMoveMaker : SingleSuccessorOperator, IOneBitflipMoveOperator, IMoveMaker, ISingleObjectiveOperator {
     33  public class OneBitflipMoveMaker : SingleSuccessorOperator, IOneBitflipMoveQualityOperator, IMoveMaker, ISingleObjectiveOperator {
    3434    public override bool CanChangeName {
    3535      get { return false; }
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveTabuChecker.cs

    r12012 r13404  
    3131  [Item("OneBitflipMoveTabuChecker", "Prevents peforming a one bitflip move again.")]
    3232  [StorableClass]
    33   public class OneBitflipMoveTabuChecker : SingleSuccessorOperator, IOneBitflipMoveOperator, ITabuChecker {
     33  public class OneBitflipMoveTabuChecker : SingleSuccessorOperator, IOneBitflipMoveQualityOperator, ITabuChecker {
    3434    public override bool CanChangeName {
    3535      get { return false; }
  • branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveTabuMaker.cs

    r12012 r13404  
    2929  [Item("OneBitflipMoveTabuMaker", "Declares a given one bitflip move as tabu, by adding its attributes to the tabu list. It also removes the oldest entry in the tabu list when its size is greater than tenure.")]
    3030  [StorableClass]
    31   public class OneBitflipMoveTabuMaker : TabuMaker, IOneBitflipMoveOperator {
     31  public class OneBitflipMoveTabuMaker : TabuMaker, IOneBitflipMoveQualityOperator {
    3232    public ILookupParameter<BinaryVector> BinaryVectorParameter {
    3333      get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs

    r13361 r13404  
    2828    where TSolution : class, ISolution {
    2929    bool Maximization { get; }
    30     double Evaluate(TSolution individual, IRandom random);
    31     void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random);
    32     IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random);
     30    double Evaluate(TSolution solution, IRandom random);
     31    void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results, IRandom random);
     32    IEnumerable<TSolution> GetNeighbors(TSolution solution, IRandom random);
    3333    bool IsBetter(double quality, double bestQuality);
    3434  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r13396 r13404  
    100100
    101101    public abstract bool Maximization { get; }
    102     public abstract double Evaluate(TSolution individual, IRandom random);
    103     public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
    104     public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
     102    public abstract double Evaluate(TSolution solution, IRandom random);
     103    public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
     104    public virtual IEnumerable<TSolution> GetNeighbors(TSolution solution, IRandom random) {
    105105      return Enumerable.Empty<TSolution>();
    106106    }
  • branches/ProblemRefactoring/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r12012 r13404  
    8585      : base(original, cloner) {
    8686      actualName = original.actualName;
     87      this.Hidden = original.Hidden;
    8788      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    8889      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     
    9192      : base("Anonymous", typeof(T)) {
    9293      this.actualName = Name;
    93       this.Hidden = true;
     94      this.Hidden = false;
    9495      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9596      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     
    9899      : base(name, typeof(T)) {
    99100      this.actualName = Name;
    100       this.Hidden = true;
     101      this.Hidden = false;
    101102      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    102103      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     
    105106      : base(name, description, typeof(T)) {
    106107      this.actualName = Name;
    107       this.Hidden = true;
     108      this.Hidden = false;
    108109      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    109110      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     
    112113      : base(name, description, typeof(T)) {
    113114      this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
    114       this.Hidden = true;
     115      this.Hidden = false;
    115116      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    116117      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
  • 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),
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/HeuristicLab.Problems.Knapsack-3.3.csproj

    r11623 r13404  
    114114  </ItemGroup>
    115115  <ItemGroup>
    116     <Compile Include="Evaluators\KnapsackEvaluator.cs" />
    117116    <Compile Include="Improvers\KnapsackImprovementOperator.cs" />
    118117    <Compile Include="Interfaces\IKnapsackEvaluator.cs" />
     118    <Compile Include="Interfaces\IKnapsackBitflipMoveEvaluator.cs" />
    119119    <Compile Include="Interfaces\IKnapsackMoveEvaluator.cs" />
    120120    <Compile Include="KnapsackProblem.cs" />
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Improvers/KnapsackImprovementOperator.cs

    r12012 r13404  
    125125      int evaluatedSolutions = 0;
    126126      int j = sol.Length - 1;
    127       while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values)
    128                               .SumWeights.Value > KnapsackCapacity.Value && j >= 0) {
     127      var totalWeight = 0.0;
     128      for (var i = 0; i < sol.Length; i++) {
     129        if (sol[i]) totalWeight += Weights[i];
     130      }
     131      evaluatedSolutions++;
     132
     133      while (totalWeight > KnapsackCapacity.Value && j >= 0) {
     134        totalWeight -= Weights[order[j]];
    129135        sol[order[j--]] = false;
    130         evaluatedSolutions++;
    131136      }
    132137
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Interfaces/IKnapsackBitflipMoveEvaluator.cs

    r13402 r13404  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    23 using HeuristicLab.Data;
    24 using HeuristicLab.Optimization;
     22using HeuristicLab.Encodings.BinaryVectorEncoding;
    2523
    2624namespace HeuristicLab.Problems.Knapsack {
    27   public interface IKnapsackMoveEvaluator : ISingleObjectiveMoveEvaluator, IMoveOperator {
    28     ILookupParameter<IntValue> KnapsackCapacityParameter { get; }
    29     ILookupParameter<DoubleValue> PenaltyParameter { get; }
    30     ILookupParameter<IntArray> WeightsParameter { get; }
    31     ILookupParameter<IntArray> ValuesParameter { get; }
    32   }
     25  public interface IKnapsackOneBitflipMoveEvaluator : IKnapsackMoveEvaluator, IOneBitflipMoveOperator { }
    3326}
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Interfaces/IKnapsackMoveEvaluator.cs

    r12012 r13404  
    2727  public interface IKnapsackMoveEvaluator : ISingleObjectiveMoveEvaluator, IMoveOperator {
    2828    ILookupParameter<IntValue> KnapsackCapacityParameter { get; }
    29     ILookupParameter<DoubleValue> PenaltyParameter { get; }
    3029    ILookupParameter<IntArray> WeightsParameter { get; }
    3130    ILookupParameter<IntArray> ValuesParameter { get; }
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

    r13364 r13404  
    3232using HeuristicLab.Parameters;
    3333using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    34 using HeuristicLab.PluginInfrastructure;
    3534
    3635namespace HeuristicLab.Problems.Knapsack {
     
    3837  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 200)]
    3938  [StorableClass]
    40   public sealed class KnapsackProblem : SingleObjectiveHeuristicOptimizationProblem<IKnapsackEvaluator, IBinaryVectorCreator>,
    41     ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>, IStorableContent {
    42     public string Filename { get; set; }
     39  public sealed class KnapsackProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
     40    public override bool Maximization { get { return true; } }
    4341
    4442    #region Parameter Properties
     
    5250      get { return (ValueParameter<IntArray>)Parameters["Values"]; }
    5351    }
    54     public ValueParameter<DoubleValue> PenaltyParameter {
    55       get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }
    56     }
    5752    public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter {
    5853      get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
     
    6156
    6257    #region Properties
    63     public IntValue KnapsackCapacity {
    64       get { return KnapsackCapacityParameter.Value; }
    65       set { KnapsackCapacityParameter.Value = value; }
     58    public int KnapsackCapacity {
     59      get { return KnapsackCapacityParameter.Value.Value; }
     60      set { KnapsackCapacityParameter.Value.Value = value; }
    6661    }
    6762    public IntArray Weights {
     
    7368      set { ValuesParameter.Value = value; }
    7469    }
    75     public DoubleValue Penalty {
    76       get { return PenaltyParameter.Value; }
    77       set { PenaltyParameter.Value = value; }
    78     }
    7970    public BinaryVector BestKnownSolution {
    8071      get { return BestKnownSolutionParameter.Value; }
     
    8374    private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer {
    8475      get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); }
    85     }
    86     #endregion
    87 
    88     // BackwardsCompatibility3.3
    89     #region Backwards compatible code, remove with 3.4
    90     [Obsolete]
    91     [Storable(Name = "operators")]
    92     private IEnumerable<IOperator> oldOperators {
    93       get { return null; }
    94       set {
    95         if (value != null && value.Any())
    96           Operators.AddRange(value);
    97       }
    9876    }
    9977    #endregion
     
    10583      RegisterEventHandlers();
    10684    }
     85    public KnapsackProblem()
     86      : base(new BinaryVectorEncoding("Selection")) {
     87      Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(1)));
     88      Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
     89      Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
     90      Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance."));
     91
     92      InitializeRandomKnapsackInstance();
     93
     94      InitializeOperators();
     95      RegisterEventHandlers();
     96    }
     97
     98    public override double Evaluate(BinaryVector solution, IRandom random) {
     99      var totalWeight = 0.0;
     100      var totalValue = 0.0;
     101      for (var i = 0; i < solution.Length; i++) {
     102        if (!solution[i]) continue;
     103        totalWeight += Weights[i];
     104        totalValue += Values[i];
     105      }
     106      return totalWeight > KnapsackCapacity ? KnapsackCapacity - totalWeight : totalValue;
     107    }
     108
    107109    public override IDeepCloneable Clone(Cloner cloner) {
    108110      return new KnapsackProblem(this, cloner);
    109111    }
    110     public KnapsackProblem()
    111       : base(new KnapsackEvaluator(), new RandomBinaryVectorCreator()) {
    112       Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
    113       Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
    114       Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
    115       Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1)));
    116       Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance."));
    117 
    118       Maximization.Value = true;
    119       MaximizationParameter.Hidden = true;
    120 
    121       SolutionCreator.BinaryVectorParameter.ActualName = "KnapsackSolution";
    122 
    123       InitializeRandomKnapsackInstance();
    124 
    125       ParameterizeSolutionCreator();
    126       ParameterizeEvaluator();
    127 
    128       InitializeOperators();
     112
     113    [StorableHook(HookType.AfterDeserialization)]
     114    private void AfterDeserialization() {
    129115      RegisterEventHandlers();
    130116    }
    131117
     118    private void RegisterEventHandlers() {
     119      Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     120      KnapsackCapacityParameter.ValueChanged += KnapsackCapacityParameter_ValueChanged;
     121      WeightsParameter.ValueChanged += WeightsParameter_ValueChanged;
     122      WeightsParameter.Value.Reset += WeightsValue_Reset;
     123      ValuesParameter.ValueChanged += ValuesParameter_ValueChanged;
     124      ValuesParameter.Value.Reset += ValuesValue_Reset;
     125      // TODO: There is no even to detect if the parameter itself was changed
     126      Encoding.LengthParameter.ValueChanged += Encoding_LengthParameter_ValueChanged;
     127    }
     128
    132129    #region Events
     130    protected override void OnEncodingChanged() {
     131      base.OnEncodingChanged();
     132      Parameterize();
     133    }
    133134    protected override void OnSolutionCreatorChanged() {
    134135      base.OnSolutionCreatorChanged();
    135       SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
    136       ParameterizeSolutionCreator();
    137       ParameterizeEvaluator();
    138       ParameterizeAnalyzer();
    139       ParameterizeOperators();
     136      Parameterize();
    140137    }
    141138    protected override void OnEvaluatorChanged() {
    142139      base.OnEvaluatorChanged();
    143       ParameterizeEvaluator();
    144       ParameterizeAnalyzer();
    145     }
    146     private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
    147       ParameterizeEvaluator();
    148       ParameterizeAnalyzer();
    149       ParameterizeOperators();
     140      Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     141      Parameterize();
     142    }
     143    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     144      Parameterize();
    150145    }
    151146    private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {
    152       ParameterizeEvaluator();
    153       ParameterizeAnalyzer();
     147      Parameterize();
    154148    }
    155149    private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
    156       ParameterizeEvaluator();
    157       ParameterizeAnalyzer();
    158       ParameterizeSolutionCreator();
    159 
    160       WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset);
     150      Parameterize();
     151      WeightsParameter.Value.Reset += WeightsValue_Reset;
    161152    }
    162153    private void WeightsValue_Reset(object sender, EventArgs e) {
    163       ParameterizeSolutionCreator();
    164 
    165       if (WeightsParameter.Value != null && ValuesParameter.Value != null)
    166         ((IStringConvertibleArray)ValuesParameter.Value).Length = WeightsParameter.Value.Length;
     154      if (WeightsParameter.Value != null && ValuesParameter.Value != null) {
     155        ((IStringConvertibleArray)ValuesParameter.Value).Length = Weights.Length;
     156        Encoding.Length = Weights.Length;
     157      }
     158      Parameterize();
    167159    }
    168160    private void ValuesParameter_ValueChanged(object sender, EventArgs e) {
    169       ParameterizeEvaluator();
    170       ParameterizeAnalyzer();
    171       ParameterizeSolutionCreator();
    172 
    173       ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset);
     161      Parameterize();
     162      ValuesParameter.Value.Reset += ValuesValue_Reset;
    174163    }
    175164    private void ValuesValue_Reset(object sender, EventArgs e) {
    176       ParameterizeSolutionCreator();
    177 
    178       if (WeightsParameter.Value != null && ValuesParameter.Value != null)
    179         ((IStringConvertibleArray)WeightsParameter.Value).Length = ValuesParameter.Value.Length;
    180     }
    181     private void PenaltyParameter_ValueChanged(object sender, EventArgs e) {
    182       ParameterizeEvaluator();
    183     }
    184     private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    185       string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
    186       foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
    187         op.OneBitflipMoveParameter.ActualName = name;
    188       }
     165      if (WeightsParameter.Value != null && ValuesParameter.Value != null) {
     166        ((IStringConvertibleArray)WeightsParameter.Value).Length = Values.Length;
     167        Encoding.Length = Values.Length;
     168      }
     169      Parameterize();
     170    }
     171    private void Encoding_LengthParameter_ValueChanged(object sender, EventArgs e) {
     172      if (Weights.Length != Encoding.Length) {
     173        ((IStringConvertibleArray)WeightsParameter.Value).Length = Encoding.Length;
     174      }
     175      if (Values.Length != Encoding.Length) {
     176        ((IStringConvertibleArray)ValuesParameter.Value).Length = Encoding.Length;
     177      }
     178      Parameterize();
    189179    }
    190180    #endregion
    191181
    192182    #region Helpers
    193     [StorableHook(HookType.AfterDeserialization)]
    194     private void AfterDeserialization() {
    195       // BackwardsCompatibility3.3
    196       #region Backwards compatible code (remove with 3.4)
    197       if (Operators.Count == 0) InitializeOperators();
    198       #endregion
    199       RegisterEventHandlers();
    200     }
    201 
    202     private void RegisterEventHandlers() {
    203       SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
    204       KnapsackCapacityParameter.ValueChanged += new EventHandler(KnapsackCapacityParameter_ValueChanged);
    205       WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
    206       WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset);
    207       ValuesParameter.ValueChanged += new EventHandler(ValuesParameter_ValueChanged);
    208       ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset);
    209       PenaltyParameter.ValueChanged += new EventHandler(PenaltyParameter_ValueChanged);
    210     }
    211     private void ParameterizeSolutionCreator() {
    212       if (SolutionCreator.LengthParameter.Value == null ||
    213         SolutionCreator.LengthParameter.Value.Value != WeightsParameter.Value.Length)
    214         SolutionCreator.LengthParameter.Value = new IntValue(WeightsParameter.Value.Length);
    215     }
    216     private void ParameterizeEvaluator() {
    217       if (Evaluator is KnapsackEvaluator) {
    218         KnapsackEvaluator knapsackEvaluator =
    219           (KnapsackEvaluator)Evaluator;
    220         knapsackEvaluator.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    221         knapsackEvaluator.BinaryVectorParameter.Hidden = true;
    222         knapsackEvaluator.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
    223         knapsackEvaluator.KnapsackCapacityParameter.Hidden = true;
    224         knapsackEvaluator.WeightsParameter.ActualName = WeightsParameter.Name;
    225         knapsackEvaluator.WeightsParameter.Hidden = true;
    226         knapsackEvaluator.ValuesParameter.ActualName = ValuesParameter.Name;
    227         knapsackEvaluator.ValuesParameter.Hidden = true;
    228         knapsackEvaluator.PenaltyParameter.ActualName = PenaltyParameter.Name;
    229         knapsackEvaluator.PenaltyParameter.Hidden = true;
    230       }
    231     }
    232     private void ParameterizeAnalyzer() {
    233       if (BestKnapsackSolutionAnalyzer != null) {
    234         BestKnapsackSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
    235         BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true;
    236         BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
    237         BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true;
    238         BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
    239         BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true;
    240         BestKnapsackSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    241         BestKnapsackSolutionAnalyzer.BinaryVectorParameter.Hidden = true;
    242         BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
    243         BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.Hidden = true;
    244         BestKnapsackSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
    245         BestKnapsackSolutionAnalyzer.WeightsParameter.Hidden = true;
    246         BestKnapsackSolutionAnalyzer.ValuesParameter.ActualName = ValuesParameter.Name;
    247         BestKnapsackSolutionAnalyzer.ValuesParameter.Hidden = true;
    248       }
    249     }
    250183    private void InitializeOperators() {
    251184      Operators.Add(new KnapsackImprovementOperator());
     
    258191      Operators.Add(new BestKnapsackSolutionAnalyzer());
    259192      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
    260       ParameterizeAnalyzer();
    261       foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
    262         if (!(op is ISingleObjectiveMoveEvaluator) || (op is IKnapsackMoveEvaluator)) {
    263           Operators.Add(op);
    264         }
    265       }
    266       ParameterizeOperators();
    267       InitializeMoveGenerators();
    268     }
    269     private void InitializeMoveGenerators() {
    270       foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
    271         if (op is IMoveGenerator) {
    272           op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
    273         }
    274       }
    275     }
    276     private void ParameterizeOperators() {
    277       foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
    278         op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    279         op.ParentsParameter.Hidden = true;
    280         op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    281         op.ChildParameter.Hidden = true;
    282       }
    283       foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
    284         op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    285         op.BinaryVectorParameter.Hidden = true;
    286       }
    287       foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
    288         op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    289         op.BinaryVectorParameter.Hidden = true;
    290       }
    291       foreach (IKnapsackMoveEvaluator op in Operators.OfType<IKnapsackMoveEvaluator>()) {
     193
     194      Parameterize();
     195    }
     196    private void Parameterize() {
     197      var operators = new List<IItem>();
     198
     199      if (BestKnapsackSolutionAnalyzer != null) {
     200        operators.Add(BestKnapsackSolutionAnalyzer);
     201        BestKnapsackSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     202        BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true;
     203        BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
     204        BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true;
     205        BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
     206        BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true;
     207        BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
     208        BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.Hidden = true;
     209        BestKnapsackSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
     210        BestKnapsackSolutionAnalyzer.WeightsParameter.Hidden = true;
     211        BestKnapsackSolutionAnalyzer.ValuesParameter.ActualName = ValuesParameter.Name;
     212        BestKnapsackSolutionAnalyzer.ValuesParameter.Hidden = true;
     213      }
     214      foreach (var op in Operators.OfType<IKnapsackMoveEvaluator>()) {
     215        operators.Add(op);
    292216        op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
    293217        op.KnapsackCapacityParameter.Hidden = true;
    294         op.PenaltyParameter.ActualName = PenaltyParameter.Name;
    295         op.PenaltyParameter.Hidden = true;
    296218        op.WeightsParameter.ActualName = WeightsParameter.Name;
    297219        op.WeightsParameter.Hidden = true;
    298220        op.ValuesParameter.ActualName = ValuesParameter.Name;
    299221        op.ValuesParameter.Hidden = true;
    300       }
    301       foreach (IBinaryVectorMultiNeighborhoodShakingOperator op in Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()) {
    302         op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
    303         op.BinaryVectorParameter.Hidden = true;
    304       }
    305       foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
    306         op.SolutionParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
     222
     223        var bitflipMoveEval = op as IKnapsackOneBitflipMoveEvaluator;
     224        if (bitflipMoveEval != null) {
     225          foreach (var moveOp in Encoding.Operators.OfType<IOneBitflipMoveQualityOperator>()) {
     226            moveOp.MoveQualityParameter.ActualName = bitflipMoveEval.MoveQualityParameter.ActualName;
     227            moveOp.MoveQualityParameter.Hidden = true;
     228          }
     229        }
     230      }
     231      foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
     232        operators.Add(op);
     233        op.SolutionParameter.ActualName = Encoding.Name;
    307234        op.SolutionParameter.Hidden = true;
    308235      }
    309       foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
    310         op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
     236      foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) {
     237        operators.Add(op);
     238        op.ParentsParameter.ActualName = Encoding.Name;
    311239        op.ParentsParameter.Hidden = true;
    312240      }
    313       foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>()) {
    314         op.SolutionVariableName = SolutionCreator.BinaryVectorParameter.ActualName;
     241      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
     242        operators.Add(op);
     243        op.SolutionVariableName = Encoding.Name;
    315244        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
    316245      }
     246
     247      if (operators.Count > 0) Encoding.ConfigureOperators(Operators);
    317248    }
    318249    #endregion
    319250
    320251    private void InitializeRandomKnapsackInstance() {
    321       System.Random rand = new System.Random();
    322 
    323       int itemCount = rand.Next(10, 100);
     252      var sysrand = new System.Random();
     253
     254      var itemCount = sysrand.Next(10, 100);
    324255      Weights = new IntArray(itemCount);
    325256      Values = new IntArray(itemCount);
     
    328259
    329260      for (int i = 0; i < itemCount; i++) {
    330         int value = rand.Next(1, 10);
    331         int weight = rand.Next(1, 10);
     261        var value = sysrand.Next(1, 10);
     262        var weight = sysrand.Next(1, 10);
    332263
    333264        Values[i] = value;
     
    336267      }
    337268
    338       int capacity = (int)Math.Round(0.7 * totalWeight);
    339       KnapsackCapacity = new IntValue(capacity);
     269      KnapsackCapacity = (int)Math.Round(0.7 * totalWeight);
    340270    }
    341271  }
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/MoveEvaluators/KnapsackMoveEvaluator.cs

    r12012 r13404  
    2525using HeuristicLab.Encodings.BinaryVectorEncoding;
    2626using HeuristicLab.Operators;
    27 using HeuristicLab.Optimization;
    2827using HeuristicLab.Parameters;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4746    public ILookupParameter<IntValue> KnapsackCapacityParameter {
    4847      get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
    49     }
    50     public ILookupParameter<DoubleValue> PenaltyParameter {
    51       get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
    5248    }
    5349    public ILookupParameter<IntArray> WeightsParameter {
  • branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/MoveEvaluators/KnapsackOneBitflipMoveEvaluator.cs

    r12012 r13404  
    5151
    5252    public override IOperation Apply() {
    53       BinaryVector binaryVector = BinaryVectorParameter.ActualValue;
    54       OneBitflipMove move = OneBitflipMoveParameter.ActualValue;
     53      var move = OneBitflipMoveParameter.ActualValue;
     54      var solution = BinaryVectorParameter.ActualValue;
     55      var weights = WeightsParameter.ActualValue;
     56      var values = ValuesParameter.ActualValue;
     57      var capacity = KnapsackCapacityParameter.ActualValue.Value;
    5558
    56       BinaryVector newSolution = new BinaryVector(binaryVector);
    57       newSolution[move.Index] = !newSolution[move.Index];
     59      var totalWeight = !solution[move.Index] ? weights[move.Index] : 0.0;
     60      var totalValue = !solution[move.Index] ? values[move.Index] : 0.0; ;
     61      for (var i = 0; i < solution.Length; i++) {
     62        if (i == move.Index || !solution[i]) continue;
     63        totalWeight += weights[i];
     64        totalValue += values[i];
     65      }
    5866
    59       DoubleValue quality = KnapsackEvaluator.Apply(newSolution,
    60         KnapsackCapacityParameter.ActualValue,
    61         PenaltyParameter.ActualValue,
    62         WeightsParameter.ActualValue,
    63         ValuesParameter.ActualValue).Quality;
    64 
    65       double moveQuality = quality.Value;
     67      var moveQuality = totalWeight > capacity ? capacity - totalWeight : totalValue;
    6668
    6769      if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
Note: See TracChangeset for help on using the changeset viewer.