- Timestamp:
- 07/25/10 01:04:14 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r3797 r4098 126 126 set { BestKnownSolutionParameter.Value = value; } 127 127 } 128 private List<IOperator> operators;129 128 public IEnumerable<IOperator> Operators { 130 129 get { return operators.Cast<IOperator>(); } … … 135 134 #endregion 136 135 137 private void InitializeRandomKnapsackInstance() { 138 System.Random rand = new System.Random(); 139 140 int itemCount = rand.Next(10, 100); 141 Weights = new IntArray(itemCount); 142 Values = new IntArray(itemCount); 143 144 double totalWeight = 0; 145 146 for (int i = 0; i < itemCount; i++ ) { 147 int value = rand.Next(1, 10); 148 int weight = rand.Next(1, 10); 149 150 Values[i] = value; 151 Weights[i] = weight; 152 totalWeight += weight; 153 } 154 155 int capacity = (int)Math.Round(0.7 * totalWeight); 156 KnapsackCapacity = new IntValue(capacity); 157 } 158 136 [Storable] 137 private List<IOperator> operators; 138 139 [StorableConstructor] 140 private KnapsackProblem(bool deserializing) : base() { } 159 141 public KnapsackProblem() 160 142 : base() { … … 179 161 ParameterizeEvaluator(); 180 162 181 Initialize(); 182 } 183 184 [StorableConstructor] 185 private KnapsackProblem(bool deserializing) : base() { } 163 InitializeOperators(); 164 AttachEventHandlers(); 165 } 186 166 187 167 public override IDeepCloneable Clone(Cloner cloner) { 188 168 KnapsackProblem clone = (KnapsackProblem)base.Clone(cloner); 189 clone.Initialize(); 169 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 170 clone.AttachEventHandlers(); 190 171 return clone; 191 172 } … … 274 255 #region Helpers 275 256 [StorableHook(HookType.AfterDeserialization)] 276 private void Initialize() { 277 InitializeOperators(); 257 private void AttachEventHandlers() { 258 // Start BackwardsCompatibility3.3 (remove with 3.4) 259 if (operators == null) InitializeOperators(); 260 // End BackwardsCompatibility3.3 278 261 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 279 262 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); … … 350 333 } 351 334 #endregion 335 336 private void InitializeRandomKnapsackInstance() { 337 System.Random rand = new System.Random(); 338 339 int itemCount = rand.Next(10, 100); 340 Weights = new IntArray(itemCount); 341 Values = new IntArray(itemCount); 342 343 double totalWeight = 0; 344 345 for (int i = 0; i < itemCount; i++) { 346 int value = rand.Next(1, 10); 347 int weight = rand.Next(1, 10); 348 349 Values[i] = value; 350 Weights[i] = weight; 351 totalWeight += weight; 352 } 353 354 int capacity = (int)Math.Round(0.7 * totalWeight); 355 KnapsackCapacity = new IntValue(capacity); 356 } 352 357 } 353 358 }
Note: See TracChangeset
for help on using the changeset viewer.