Changeset 6938 for trunk/sources/HeuristicLab.Problems.Knapsack
- Timestamp:
- 10/26/11 23:36:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r6476 r6938 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 37 36 [Creatable("Problems")] 38 37 [StorableClass] 39 public sealed class KnapsackProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {38 public sealed class KnapsackProblem : SingleObjectiveHeuristicOptimizationProblem<IKnapsackEvaluator, IBinaryVectorCreator>, IStorableContent { 40 39 public string Filename { get; set; } 41 40 42 public override Image ItemImage {43 get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }44 }45 46 41 #region Parameter Properties 47 public ValueParameter<BoolValue> MaximizationParameter {48 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }49 }50 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {51 get { return MaximizationParameter; }52 }53 42 public ValueParameter<IntValue> KnapsackCapacityParameter { 54 43 get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; } … … 63 52 get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; } 64 53 } 65 public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {66 get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }67 }68 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {69 get { return SolutionCreatorParameter; }70 }71 public ValueParameter<IKnapsackEvaluator> EvaluatorParameter {72 get { return (ValueParameter<IKnapsackEvaluator>)Parameters["Evaluator"]; }73 }74 IParameter IHeuristicOptimizationProblem.EvaluatorParameter {75 get { return EvaluatorParameter; }76 }77 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {78 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }79 }80 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {81 get { return BestKnownQualityParameter; }82 }83 54 public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter { 84 55 get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; } … … 87 58 88 59 #region Properties 89 public BoolValue Maximization {90 get { return MaximizationParameter.Value; }91 set { MaximizationParameter.Value = value; }92 }93 60 public IntValue KnapsackCapacity { 94 61 get { return KnapsackCapacityParameter.Value; } … … 107 74 set { PenaltyParameter.Value = value; } 108 75 } 109 public IBinaryVectorCreator SolutionCreator {110 get { return SolutionCreatorParameter.Value; }111 set { SolutionCreatorParameter.Value = value; }112 }113 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {114 get { return SolutionCreatorParameter.Value; }115 }116 public IKnapsackEvaluator Evaluator {117 get { return EvaluatorParameter.Value; }118 set { EvaluatorParameter.Value = value; }119 }120 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {121 get { return EvaluatorParameter.Value; }122 }123 IEvaluator IHeuristicOptimizationProblem.Evaluator {124 get { return EvaluatorParameter.Value; }125 }126 public DoubleValue BestKnownQuality {127 get { return BestKnownQualityParameter.Value; }128 set { BestKnownQualityParameter.Value = value; }129 }130 76 public BinaryVector BestKnownSolution { 131 77 get { return BestKnownSolutionParameter.Value; } 132 78 set { BestKnownSolutionParameter.Value = value; } 133 79 } 134 public IEnumerable<IOperator> Operators {135 get { return operators.Cast<IOperator>(); }136 }137 80 private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer { 138 get { return operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); } 139 } 140 #endregion 141 142 [Storable] 143 private List<IOperator> operators; 81 get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); } 82 } 83 #endregion 84 85 // BackwardsCompatibility3.3 86 #region Backwards compatible code, remove with 3.4 87 [Obsolete] 88 [Storable(Name = "operators")] 89 private IEnumerable<IOperator> oldOperators { 90 get { return null; } 91 set { 92 if (value != null && value.Any()) 93 Operators.AddRange(value); 94 } 95 } 96 #endregion 144 97 145 98 [StorableConstructor] … … 147 100 private KnapsackProblem(KnapsackProblem original, Cloner cloner) 148 101 : base(original, cloner) { 149 this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();150 102 AttachEventHandlers(); 151 103 } … … 154 106 } 155 107 public KnapsackProblem() 156 : base() { 157 RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator(); 158 KnapsackEvaluator evaluator = new KnapsackEvaluator(); 159 160 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Knapsack Problem is a maximization problem.", new BoolValue(true))); 108 : base(new KnapsackEvaluator(), new RandomBinaryVectorCreator()) { 161 109 Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0))); 162 110 Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5))); 163 111 Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5))); 164 112 Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1))); 165 Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new Knapsack solutions.", creator));166 Parameters.Add(new ValueParameter<IKnapsackEvaluator>("Evaluator", "The operator which should be used to evaluate Knapsack solutions.", evaluator));167 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Knapsack instance."));168 113 Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance.")); 169 114 170 creator.BinaryVectorParameter.ActualName = "KnapsackSolution"; 115 Maximization.Value = true; 116 117 SolutionCreator.BinaryVectorParameter.ActualName = "KnapsackSolution"; 171 118 172 119 InitializeRandomKnapsackInstance(); … … 180 127 181 128 #region Events 182 public event EventHandler SolutionCreatorChanged; 183 private void OnSolutionCreatorChanged() { 184 EventHandler handler = SolutionCreatorChanged; 185 if (handler != null) handler(this, EventArgs.Empty); 186 } 187 public event EventHandler EvaluatorChanged; 188 private void OnEvaluatorChanged() { 189 EventHandler handler = EvaluatorChanged; 190 if (handler != null) handler(this, EventArgs.Empty); 191 } 192 public event EventHandler OperatorsChanged; 193 private void OnOperatorsChanged() { 194 EventHandler handler = OperatorsChanged; 195 if (handler != null) handler(this, EventArgs.Empty); 196 } 197 public event EventHandler Reset; 198 private void OnReset() { 199 EventHandler handler = Reset; 200 if (handler != null) handler(this, EventArgs.Empty); 201 } 202 203 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 129 protected override void OnSolutionCreatorChanged() { 130 base.OnSolutionCreatorChanged(); 204 131 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); 205 132 ParameterizeSolutionCreator(); … … 207 134 ParameterizeAnalyzer(); 208 135 ParameterizeOperators(); 209 OnSolutionCreatorChanged(); 136 } 137 protected override void OnEvaluatorChanged() { 138 base.OnEvaluatorChanged(); 139 ParameterizeEvaluator(); 140 ParameterizeAnalyzer(); 210 141 } 211 142 private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) { … … 214 145 ParameterizeOperators(); 215 146 } 216 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 217 ParameterizeEvaluator(); 218 ParameterizeAnalyzer(); 219 OnEvaluatorChanged(); 220 } 221 void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) { 222 ParameterizeEvaluator(); 223 ParameterizeAnalyzer(); 224 } 225 void WeightsParameter_ValueChanged(object sender, EventArgs e) { 147 private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) { 148 ParameterizeEvaluator(); 149 ParameterizeAnalyzer(); 150 } 151 private void WeightsParameter_ValueChanged(object sender, EventArgs e) { 226 152 ParameterizeEvaluator(); 227 153 ParameterizeAnalyzer(); … … 230 156 WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset); 231 157 } 232 void WeightsValue_Reset(object sender, EventArgs e) {158 private void WeightsValue_Reset(object sender, EventArgs e) { 233 159 ParameterizeSolutionCreator(); 234 160 … … 236 162 ((IStringConvertibleArray)ValuesParameter.Value).Length = WeightsParameter.Value.Length; 237 163 } 238 void ValuesParameter_ValueChanged(object sender, EventArgs e) {164 private void ValuesParameter_ValueChanged(object sender, EventArgs e) { 239 165 ParameterizeEvaluator(); 240 166 ParameterizeAnalyzer(); … … 243 169 ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset); 244 170 } 245 void ValuesValue_Reset(object sender, EventArgs e) {171 private void ValuesValue_Reset(object sender, EventArgs e) { 246 172 ParameterizeSolutionCreator(); 247 173 … … 249 175 ((IStringConvertibleArray)WeightsParameter.Value).Length = ValuesParameter.Value.Length; 250 176 } 251 void PenaltyParameter_ValueChanged(object sender, EventArgs e) {252 ParameterizeEvaluator(); 253 } 254 void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {177 private void PenaltyParameter_ValueChanged(object sender, EventArgs e) { 178 ParameterizeEvaluator(); 179 } 180 private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) { 255 181 string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName; 256 182 foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) { … … 265 191 // BackwardsCompatibility3.3 266 192 #region Backwards compatible code (remove with 3.4) 267 if ( operators == null) InitializeOperators();193 if (Operators.Count > 0) InitializeOperators(); 268 194 #endregion 269 195 AttachEventHandlers(); … … 271 197 272 198 private void AttachEventHandlers() { 273 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);274 199 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); 275 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);276 200 KnapsackCapacityParameter.ValueChanged += new EventHandler(KnapsackCapacityParameter_ValueChanged); 277 201 WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged); … … 319 243 } 320 244 private void InitializeOperators() { 321 operators = new List<IOperator>(); 322 operators.Add(new BestKnapsackSolutionAnalyzer()); 245 Operators.Add(new BestKnapsackSolutionAnalyzer()); 323 246 ParameterizeAnalyzer(); 324 247 foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) { 325 248 if (!(op is ISingleObjectiveMoveEvaluator) || (op is IKnapsackMoveEvaluator)) { 326 operators.Add(op);249 Operators.Add(op); 327 250 } 328 251 }
Note: See TracChangeset
for help on using the changeset viewer.