Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/10 01:04:14 (14 years ago)
Author:
abeham
Message:

#1090

  • Fixed initialization of operators and made operator list storable
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

    r3797 r4098  
    126126      set { BestKnownSolutionParameter.Value = value; }
    127127    }
    128     private List<IOperator> operators;
    129128    public IEnumerable<IOperator> Operators {
    130129      get { return operators.Cast<IOperator>(); }
     
    135134    #endregion
    136135
    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() { }
    159141    public KnapsackProblem()
    160142      : base() {
     
    179161      ParameterizeEvaluator();
    180162
    181       Initialize();
    182     }
    183 
    184     [StorableConstructor]
    185     private KnapsackProblem(bool deserializing) : base() { }
     163      InitializeOperators();
     164      AttachEventHandlers();
     165    }
    186166
    187167    public override IDeepCloneable Clone(Cloner cloner) {
    188168      KnapsackProblem clone = (KnapsackProblem)base.Clone(cloner);
    189       clone.Initialize();
     169      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     170      clone.AttachEventHandlers();
    190171      return clone;
    191172    }
     
    274255    #region Helpers
    275256    [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
    278261      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    279262      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
     
    350333    }
    351334    #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    }
    352357  }
    353358}
Note: See TracChangeset for help on using the changeset viewer.