Changeset 13404 for branches/ProblemRefactoring
- Timestamp:
- 11/26/15 09:30:43 (9 years ago)
- 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 5 5 *.vs10x 6 6 Plugin.cs 7 *.DotSettings
-
- Property svn:ignore
-
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs
r13396 r13404 52 52 } 53 53 #endregion 54 55 54 56 55 public int Length { … … 108 107 typeof (IBinaryVectorMoveOperator), 109 108 typeof (IBinaryVectorMultiNeighborhoodShakingOperator), 109 typeof (IBinaryVectorSolutionOperator), 110 typeof (IBinaryVectorSolutionsOperator) 110 111 }; 111 112 } … … 129 130 ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>()); 130 131 ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()); 132 ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>()); 133 ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>()); 131 134 } 132 135 … … 134 137 private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) { 135 138 foreach (var creator in creators) { 136 creator.BinaryVectorParameter.ActualName = Name;137 139 creator.LengthParameter.ActualName = LengthParameter.Name; 138 140 } … … 145 147 } 146 148 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 150 150 } 151 151 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 155 153 } 156 154 private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) { … … 160 158 } 161 159 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; 165 169 } 166 170 #endregion -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/HeuristicLab.Encodings.BinaryVectorEncoding-3.3.csproj
r11961 r13404 127 127 <Compile Include="BinaryVector.cs" /> 128 128 <Compile Include="Interfaces\IBinaryVectorMultiNeighborhoodShakingOperator.cs" /> 129 <Compile Include="Interfaces\IBinaryVectorSolutionsOperator.cs" /> 130 <Compile Include="Interfaces\IBinaryVectorSolutionOperator.cs" /> 131 <Compile Include="Interfaces\IOneBitflipMoveQualityOperator.cs" /> 129 132 <Compile Include="Interfaces\IOneBitflipMoveOperator.cs" /> 130 133 <Compile Include="Interfaces\IBinaryVectorCreator.cs" /> -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorCreator.cs
r13336 r13404 28 28 /// An interface which represents an operator for creating vectors of bool-valued data. 29 29 /// </summary> 30 public interface IBinaryVectorCreator : IBinaryVector Operator, ISolutionCreator<BinaryVector> {30 public interface IBinaryVectorCreator : IBinaryVectorSolutionOperator, ISolutionCreator<BinaryVector> { 31 31 IValueLookupParameter<IntValue> LengthParameter { get; } 32 ILookupParameter<BinaryVector> BinaryVectorParameter { get; }33 32 } 34 33 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorManipulator.cs
r12012 r13404 20 20 #endregion 21 21 22 using HeuristicLab.Core;23 22 using HeuristicLab.Optimization; 24 23 … … 27 26 /// An interface which represents an operator for manipulating vectors of bool-valued data. 28 27 /// </summary> 29 public interface IBinaryVectorManipulator : IBinaryVectorOperator, IManipulator { 30 ILookupParameter<BinaryVector> BinaryVectorParameter { get; } 28 public interface IBinaryVectorManipulator : IBinaryVectorSolutionOperator, IManipulator { 31 29 } 32 30 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorMoveOperator.cs
r12012 r13404 20 20 #endregion 21 21 22 using HeuristicLab.Core;23 22 using HeuristicLab.Optimization; 24 23 25 24 namespace HeuristicLab.Encodings.BinaryVectorEncoding { 26 public interface IBinaryVectorMoveOperator : IBinaryVectorOperator, IMoveOperator { 27 ILookupParameter<BinaryVector> BinaryVectorParameter { get; } 25 public interface IBinaryVectorMoveOperator : IBinaryVectorSolutionOperator, IMoveOperator { 28 26 } 29 27 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorMultiNeighborhoodShakingOperator.cs
r12012 r13404 20 20 #endregion 21 21 22 using HeuristicLab.Core;23 22 24 23 namespace HeuristicLab.Encodings.BinaryVectorEncoding { 25 public interface IBinaryVectorMultiNeighborhoodShakingOperator : IBinaryVectorOperator { 26 ILookupParameter<BinaryVector> BinaryVectorParameter { get; } 24 public interface IBinaryVectorMultiNeighborhoodShakingOperator : IBinaryVectorSolutionOperator { 27 25 } 28 26 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IOneBitflipMoveQualityOperator.cs
r13402 r13404 21 21 22 22 using HeuristicLab.Core; 23 using HeuristicLab.Data; 23 24 24 25 namespace HeuristicLab.Encodings.BinaryVectorEncoding { 25 public interface IOneBitflipMove Operator : IBinaryVectorMoveOperator {26 ILookupParameter< OneBitflipMove> OneBitflipMoveParameter { get; }26 public interface IOneBitflipMoveQualityOperator : IOneBitflipMoveOperator { 27 ILookupParameter<DoubleValue> MoveQualityParameter { get; } 27 28 } 28 29 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveMaker.cs
r12012 r13404 31 31 [Item("OneBitflipMoveMaker", "Peforms a one bitflip move on a given BitVector and updates the quality.")] 32 32 [StorableClass] 33 public class OneBitflipMoveMaker : SingleSuccessorOperator, IOneBitflipMove Operator, IMoveMaker, ISingleObjectiveOperator {33 public class OneBitflipMoveMaker : SingleSuccessorOperator, IOneBitflipMoveQualityOperator, IMoveMaker, ISingleObjectiveOperator { 34 34 public override bool CanChangeName { 35 35 get { return false; } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveTabuChecker.cs
r12012 r13404 31 31 [Item("OneBitflipMoveTabuChecker", "Prevents peforming a one bitflip move again.")] 32 32 [StorableClass] 33 public class OneBitflipMoveTabuChecker : SingleSuccessorOperator, IOneBitflipMove Operator, ITabuChecker {33 public class OneBitflipMoveTabuChecker : SingleSuccessorOperator, IOneBitflipMoveQualityOperator, ITabuChecker { 34 34 public override bool CanChangeName { 35 35 get { return false; } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/OneBitflipMoveTabuMaker.cs
r12012 r13404 29 29 [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.")] 30 30 [StorableClass] 31 public class OneBitflipMoveTabuMaker : TabuMaker, IOneBitflipMove Operator {31 public class OneBitflipMoveTabuMaker : TabuMaker, IOneBitflipMoveQualityOperator { 32 32 public ILookupParameter<BinaryVector> BinaryVectorParameter { 33 33 get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs
r13361 r13404 28 28 where TSolution : class, ISolution { 29 29 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); 33 33 bool IsBetter(double quality, double bestQuality); 34 34 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r13396 r13404 100 100 101 101 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) { 105 105 return Enumerable.Empty<TSolution>(); 106 106 } -
branches/ProblemRefactoring/HeuristicLab.Parameters/3.3/LookupParameter.cs
r12012 r13404 85 85 : base(original, cloner) { 86 86 actualName = original.actualName; 87 this.Hidden = original.Hidden; 87 88 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 88 89 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); … … 91 92 : base("Anonymous", typeof(T)) { 92 93 this.actualName = Name; 93 this.Hidden = true;94 this.Hidden = false; 94 95 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 95 96 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); … … 98 99 : base(name, typeof(T)) { 99 100 this.actualName = Name; 100 this.Hidden = true;101 this.Hidden = false; 101 102 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 102 103 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); … … 105 106 : base(name, description, typeof(T)) { 106 107 this.actualName = Name; 107 this.Hidden = true;108 this.Hidden = false; 108 109 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 109 110 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); … … 112 113 : base(name, description, typeof(T)) { 113 114 this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName; 114 this.Hidden = true;115 this.Hidden = false; 115 116 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 116 117 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs
r12012 r13404 36 36 [Item("BestKnapsackSolutionAnalyzer", "An operator for analyzing the best solution for a Knapsack problem.")] 37 37 [StorableClass] 38 public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, I Analyzer, ISingleObjectiveOperator {38 public class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IBinaryVectorSolutionsOperator, IAnalyzer, ISingleObjectiveOperator { 39 39 public virtual bool EnabledByDefault { 40 40 get { return true; } 41 41 } 42 42 43 public LookupParameter<BoolValue> MaximizationParameter {44 get { return ( LookupParameter<BoolValue>)Parameters["Maximization"]; }43 public ILookupParameter<BoolValue> MaximizationParameter { 44 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 45 45 } 46 public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {47 get { return ( ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }46 public IScopeTreeLookupParameter<BinaryVector> BinaryVectorsParameter { 47 get { return (IScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVectors"]; } 48 48 } 49 public LookupParameter<IntValue> KnapsackCapacityParameter {50 get { return ( LookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }49 public ILookupParameter<IntValue> KnapsackCapacityParameter { 50 get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; } 51 51 } 52 public LookupParameter<IntArray> WeightsParameter {53 get { return ( LookupParameter<IntArray>)Parameters["Weights"]; }52 public ILookupParameter<IntArray> WeightsParameter { 53 get { return (ILookupParameter<IntArray>)Parameters["Weights"]; } 54 54 } 55 public LookupParameter<IntArray> ValuesParameter {56 get { return ( LookupParameter<IntArray>)Parameters["Values"]; }55 public ILookupParameter<IntArray> ValuesParameter { 56 get { return (ILookupParameter<IntArray>)Parameters["Values"]; } 57 57 } 58 public ScopeTreeLookupParameter<DoubleValue> QualityParameter {59 get { return ( ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }58 public IScopeTreeLookupParameter<DoubleValue> QualityParameter { 59 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 60 60 } 61 public LookupParameter<KnapsackSolution> BestSolutionParameter {62 get { return ( LookupParameter<KnapsackSolution>)Parameters["BestSolution"]; }61 public ILookupParameter<KnapsackSolution> BestSolutionParameter { 62 get { return (ILookupParameter<KnapsackSolution>)Parameters["BestSolution"]; } 63 63 } 64 public ValueLookupParameter<ResultCollection> ResultsParameter {65 get { return ( ValueLookupParameter<ResultCollection>)Parameters["Results"]; }64 public IValueLookupParameter<ResultCollection> ResultsParameter { 65 get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; } 66 66 } 67 public LookupParameter<DoubleValue> BestKnownQualityParameter {68 get { return ( LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }67 public ILookupParameter<DoubleValue> BestKnownQualityParameter { 68 get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 69 69 } 70 public LookupParameter<BinaryVector> BestKnownSolutionParameter {71 get { return ( LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }70 public ILookupParameter<BinaryVector> BestKnownSolutionParameter { 71 get { return (ILookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; } 72 72 } 73 73 … … 78 78 : base() { 79 79 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.")); 81 81 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.")); 82 82 Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items.")); … … 95 95 96 96 public override IOperation Apply() { 97 ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;98 ItemArray<DoubleValue>qualities = QualityParameter.ActualValue;99 ResultCollectionresults = ResultsParameter.ActualValue;100 boolmax = MaximizationParameter.ActualValue.Value;101 DoubleValuebestKnownQuality = 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; 102 102 103 103 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; 107 106 108 107 if (bestKnownQuality == null || … … 113 112 } 114 113 115 KnapsackSolutionsolution = BestSolutionParameter.ActualValue;114 var solution = BestSolutionParameter.ActualValue; 116 115 if (solution == null) { 117 116 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 114 114 </ItemGroup> 115 115 <ItemGroup> 116 <Compile Include="Evaluators\KnapsackEvaluator.cs" />117 116 <Compile Include="Improvers\KnapsackImprovementOperator.cs" /> 118 117 <Compile Include="Interfaces\IKnapsackEvaluator.cs" /> 118 <Compile Include="Interfaces\IKnapsackBitflipMoveEvaluator.cs" /> 119 119 <Compile Include="Interfaces\IKnapsackMoveEvaluator.cs" /> 120 120 <Compile Include="KnapsackProblem.cs" /> -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Improvers/KnapsackImprovementOperator.cs
r12012 r13404 125 125 int evaluatedSolutions = 0; 126 126 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]]; 129 135 sol[order[j--]] = false; 130 evaluatedSolutions++;131 136 } 132 137 -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Interfaces/IKnapsackBitflipMoveEvaluator.cs
r13402 r13404 20 20 #endregion 21 21 22 using HeuristicLab.Core; 23 using HeuristicLab.Data; 24 using HeuristicLab.Optimization; 22 using HeuristicLab.Encodings.BinaryVectorEncoding; 25 23 26 24 namespace 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 { } 33 26 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/Interfaces/IKnapsackMoveEvaluator.cs
r12012 r13404 27 27 public interface IKnapsackMoveEvaluator : ISingleObjectiveMoveEvaluator, IMoveOperator { 28 28 ILookupParameter<IntValue> KnapsackCapacityParameter { get; } 29 ILookupParameter<DoubleValue> PenaltyParameter { get; }30 29 ILookupParameter<IntArray> WeightsParameter { get; } 31 30 ILookupParameter<IntArray> ValuesParameter { get; } -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r13364 r13404 32 32 using HeuristicLab.Parameters; 33 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.PluginInfrastructure;35 34 36 35 namespace HeuristicLab.Problems.Knapsack { … … 38 37 [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 200)] 39 38 [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; } } 43 41 44 42 #region Parameter Properties … … 52 50 get { return (ValueParameter<IntArray>)Parameters["Values"]; } 53 51 } 54 public ValueParameter<DoubleValue> PenaltyParameter {55 get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }56 }57 52 public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter { 58 53 get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; } … … 61 56 62 57 #region Properties 63 public IntValueKnapsackCapacity {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; } 66 61 } 67 62 public IntArray Weights { … … 73 68 set { ValuesParameter.Value = value; } 74 69 } 75 public DoubleValue Penalty {76 get { return PenaltyParameter.Value; }77 set { PenaltyParameter.Value = value; }78 }79 70 public BinaryVector BestKnownSolution { 80 71 get { return BestKnownSolutionParameter.Value; } … … 83 74 private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer { 84 75 get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); } 85 }86 #endregion87 88 // BackwardsCompatibility3.389 #region Backwards compatible code, remove with 3.490 [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 }98 76 } 99 77 #endregion … … 105 83 RegisterEventHandlers(); 106 84 } 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 107 109 public override IDeepCloneable Clone(Cloner cloner) { 108 110 return new KnapsackProblem(this, cloner); 109 111 } 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() { 129 115 RegisterEventHandlers(); 130 116 } 131 117 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 132 129 #region Events 130 protected override void OnEncodingChanged() { 131 base.OnEncodingChanged(); 132 Parameterize(); 133 } 133 134 protected override void OnSolutionCreatorChanged() { 134 135 base.OnSolutionCreatorChanged(); 135 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); 136 ParameterizeSolutionCreator(); 137 ParameterizeEvaluator(); 138 ParameterizeAnalyzer(); 139 ParameterizeOperators(); 136 Parameterize(); 140 137 } 141 138 protected override void OnEvaluatorChanged() { 142 139 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(); 150 145 } 151 146 private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) { 152 ParameterizeEvaluator(); 153 ParameterizeAnalyzer(); 147 Parameterize(); 154 148 } 155 149 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; 161 152 } 162 153 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(); 167 159 } 168 160 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; 174 163 } 175 164 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(); 189 179 } 190 180 #endregion 191 181 192 182 #region Helpers 193 [StorableHook(HookType.AfterDeserialization)]194 private void AfterDeserialization() {195 // BackwardsCompatibility3.3196 #region Backwards compatible code (remove with 3.4)197 if (Operators.Count == 0) InitializeOperators();198 #endregion199 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 }250 183 private void InitializeOperators() { 251 184 Operators.Add(new KnapsackImprovementOperator()); … … 258 191 Operators.Add(new BestKnapsackSolutionAnalyzer()); 259 192 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); 292 216 op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name; 293 217 op.KnapsackCapacityParameter.Hidden = true; 294 op.PenaltyParameter.ActualName = PenaltyParameter.Name;295 op.PenaltyParameter.Hidden = true;296 218 op.WeightsParameter.ActualName = WeightsParameter.Name; 297 219 op.WeightsParameter.Hidden = true; 298 220 op.ValuesParameter.ActualName = ValuesParameter.Name; 299 221 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; 307 234 op.SolutionParameter.Hidden = true; 308 235 } 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; 311 239 op.ParentsParameter.Hidden = true; 312 240 } 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; 315 244 op.QualityVariableName = Evaluator.QualityParameter.ActualName; 316 245 } 246 247 if (operators.Count > 0) Encoding.ConfigureOperators(Operators); 317 248 } 318 249 #endregion 319 250 320 251 private void InitializeRandomKnapsackInstance() { 321 System.Randomrand = 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); 324 255 Weights = new IntArray(itemCount); 325 256 Values = new IntArray(itemCount); … … 328 259 329 260 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); 332 263 333 264 Values[i] = value; … … 336 267 } 337 268 338 int capacity = (int)Math.Round(0.7 * totalWeight); 339 KnapsackCapacity = new IntValue(capacity); 269 KnapsackCapacity = (int)Math.Round(0.7 * totalWeight); 340 270 } 341 271 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/MoveEvaluators/KnapsackMoveEvaluator.cs
r12012 r13404 25 25 using HeuristicLab.Encodings.BinaryVectorEncoding; 26 26 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization;28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 47 46 public ILookupParameter<IntValue> KnapsackCapacityParameter { 48 47 get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; } 49 }50 public ILookupParameter<DoubleValue> PenaltyParameter {51 get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }52 48 } 53 49 public ILookupParameter<IntArray> WeightsParameter { -
branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/MoveEvaluators/KnapsackOneBitflipMoveEvaluator.cs
r12012 r13404 51 51 52 52 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; 55 58 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 } 58 66 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; 66 68 67 69 if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
Note: See TracChangeset
for help on using the changeset viewer.