Free cookie consent management tool by TermsFeed Policy Generator

Changeset 888


Ignore:
Timestamp:
12/02/08 21:30:34 (15 years ago)
Author:
gkronber
Message:

Refactored cloning in plugins HL.Routing and HL.Scheduling

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

Location:
branches/CloningRefactorBranch
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/CloningRefactorBranch/HeuristicLab.Routing.TSP/TSPTour.cs

    r2 r888  
    4848    }
    4949
     50    public TSPTour(TSPTour original) : this(original, new Dictionary<Guid, object>()) { }
     51    protected TSPTour(TSPTour original, IDictionary<Guid, object> clonedObjects)
     52      : base(original, clonedObjects) {
     53      this.myCoordinates = (DoubleMatrixData)Auxiliary.Clone(original.Coordinates, clonedObjects);
     54      this.myTour = (Permutation.Permutation)Auxiliary.Clone(original.Tour, clonedObjects);
     55    }
     56
    5057
    5158    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    52       TSPTour clone = (TSPTour)base.Clone(clonedObjects);
    53       clone.myCoordinates = (DoubleMatrixData)Auxiliary.Clone(Coordinates, clonedObjects);
    54       clone.myTour = Tour;
    55       return clone;
     59      return new TSPTour(this, clonedObjects);
    5660    }
    5761
  • branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/JSSPInjector.cs

    r120 r888  
    6161    }
    6262
     63    public JSSPInjector(JSSPInjector original) : this(original, new Dictionary<Guid, object>()) { }
     64    protected JSSPInjector(JSSPInjector original, IDictionary<Guid, object> clonedObjects)
     65      : base(original, clonedObjects) {
     66      this.operations = (ItemList)Auxiliary.Clone(original.operations, clonedObjects);
     67      this.jobs = (IntData)Auxiliary.Clone(original.jobs, clonedObjects);
     68      this.machines = (IntData)Auxiliary.Clone(original.machines, clonedObjects);
     69    }
     70
    6371    public override IView CreateView() {
    6472      return new JSSPInjectorView(this);
     
    7381
    7482    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    75       JSSPInjector clone = (JSSPInjector)base.Clone(clonedObjects);
    76       clone.operations = (ItemList)Auxiliary.Clone(operations, clonedObjects);
    77       clone.jobs = (IntData)Auxiliary.Clone(jobs, clonedObjects);
    78       clone.machines = (IntData)Auxiliary.Clone(machines, clonedObjects);
    79       return clone;
     83      return new JSSPInjector(this, clonedObjects);
    8084    }
    8185
  • branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/Operation.cs

    r2 r888  
    9999    }
    100100
     101    public Operation(Operation original) : this(original, new Dictionary<Guid, object>()) { }
     102    protected Operation(Operation original, IDictionary<Guid, object> clonedObjects)
     103      : base(original, clonedObjects) {
     104      this.duration = (IntData)Auxiliary.Clone(original.duration, clonedObjects);
     105      this.start = (IntData)Auxiliary.Clone(original.start, clonedObjects);
     106      this.operationIndex = (IntData)Auxiliary.Clone(original.operationIndex, clonedObjects);
     107      this.job = (IntData)Auxiliary.Clone(original.job, clonedObjects);
     108      this.machines = (IntArrayData)Auxiliary.Clone(original.machines, clonedObjects);
     109      this.predecessors = (ItemList)Auxiliary.Clone(original.predecessors, clonedObjects);
     110    }
     111
    101112    public override string ToString() {
    102113      if(this.Job == -1) {
     
    128139
    129140    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    130       Operation clone = new Operation();
    131       clonedObjects.Add(Guid, clone);
    132       clone.duration = (IntData)Auxiliary.Clone(duration, clonedObjects);
    133       clone.start = (IntData)Auxiliary.Clone(start, clonedObjects);
    134       clone.operationIndex = (IntData)Auxiliary.Clone(operationIndex, clonedObjects);
    135       clone.job = (IntData)Auxiliary.Clone(job, clonedObjects);
    136       clone.machines = (IntArrayData)Auxiliary.Clone(machines, clonedObjects);
    137       clone.predecessors = (ItemList)Auxiliary.Clone(predecessors, clonedObjects);
    138       return clone;
     141      return new Operation(this, clonedObjects);
    139142    }
    140143
  • branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/Schedule.cs

    r2 r888  
    4141    public Schedule() : this(0, 100) { }
    4242
     43    public Schedule(Schedule original) : this(original, new Dictionary<Guid, object>()) { }
     44    protected Schedule(Schedule original, IDictionary<Guid, object> clonedObjects)
     45      : base(original, clonedObjects) {
     46      this.schedule = new ScheduleTree[original.Machines];
     47      for (int i = 0; i < original.Machines; i++) {
     48        this.schedule[i] = (ScheduleTree)Auxiliary.Clone(original.schedule[i], clonedObjects);
     49      }
     50    }
     51
    4352    public int Machines {
    4453      get { return schedule.Length; }
     
    6473    }
    6574
    66     #region IStorable Members
     75   #region IStorable Members
    6776
    6877    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     
    8796
    8897    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    89       int timespan = 0;
    90       if ((schedule != null) && (schedule.Length > 0)) {
    91         timespan = schedule[0].Timespan;
    92       }
    93       Schedule clone = new Schedule(this.Machines, timespan);
    94       clonedObjects.Add(Guid, clone);
    95       clone.schedule = new ScheduleTree[this.Machines];
    96       for(int i = 0; i < this.Machines; i++) {
    97         clone.schedule[i] = (ScheduleTree)Auxiliary.Clone(schedule[i], clonedObjects);
    98       }
    99       return clone;
     98      return new Schedule(this, clonedObjects);
    10099    }
    101100
  • branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/ScheduleInjector.cs

    r361 r888  
    4747    }
    4848
     49    public ScheduleInjector(ScheduleInjector original) : this(original, new Dictionary<Guid, object>()) { }
     50    protected ScheduleInjector(ScheduleInjector original, IDictionary<Guid, object> clonedObjects)
     51      : base(original, clonedObjects) {
     52      if (original.schedule != null) {
     53        this.schedule = (Schedule)Auxiliary.Clone(original.schedule, clonedObjects);
     54      }
     55    }
     56
    4957    public override IOperation Apply(IScope scope) {
    5058      IntData machines = GetVariableValue<IntData>("Machines", scope, true);
     
    5664
    5765    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    58       ScheduleInjector clone = (ScheduleInjector)base.Clone(clonedObjects);
    59       if(schedule != null) {
    60         clone.schedule = (Schedule)Auxiliary.Clone(schedule, clonedObjects);
    61       }
    62       return clone;
     66      return new ScheduleInjector(this, clonedObjects);
    6367    }
    6468
    6569    #region IStorable Members
    66     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     70    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6771      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    68       if(schedule != null) {
     72      if (schedule != null) {
    6973        node.AppendChild(PersistenceManager.Persist("Schedule", schedule, document, persistedObjects));
    7074      }
    7175      return node;
    7276    }
    73     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     77    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    7478      base.Populate(node, restoredObjects);
    7579      XmlNode scheduleNode = node.SelectSingleNode("Schedule");
    76       if(scheduleNode != null) {
     80      if (scheduleNode != null) {
    7781        schedule = (Schedule)PersistenceManager.Restore(scheduleNode, restoredObjects);
    7882      }
  • branches/CloningRefactorBranch/HeuristicLab.Scheduling.JSSP/ScheduleTree.cs

    r385 r888  
    8888
    8989    public ScheduleTree() : this(10000) { }
     90
     91    public ScheduleTree(ScheduleTree original) : this(original, new Dictionary<Guid, object>()) {}
     92    protected ScheduleTree(ScheduleTree original, IDictionary<Guid, object> clonedObjects)
     93      : base(original, clonedObjects) {
     94      foreach (ScheduleTreeNode node in original.InOrder) {
     95        if (IsLeaf(node) && (node.Data.job > -1)) {
     96          this.InsertOperation(node.Data); // interestingly enough node.Data is not cloned! (gkronber Dec. 2nd, 2008)
     97          }
     98      }
     99    }
    90100
    91101    private int InsertOperation(Operation op) {
     
    243253
    244254    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    245       ScheduleTree clone = new ScheduleTree(timespan);
    246       clonedObjects.Add(Guid, clone);
    247       foreach (ScheduleTreeNode node in this.InOrder) {
    248         if (IsLeaf(node) && (node.Data.job > -1)) {
    249           clone.InsertOperation(node.Data);
    250         }
    251       }
    252       return clone;
     255      return new ScheduleTree(this, clonedObjects);
    253256    }
    254257
Note: See TracChangeset for help on using the changeset viewer.