Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/SimulationModel/PDAction.cs @ 12897

Last change on this file since 12897 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.PDPSimulation.DomainModel;
30
31namespace HeuristicLab.PDPSimulation {
32  [StorableClass]
33  public abstract class PDAction: Item {
34    [Storable]
35    private Guid baseObjectId;
36
37    public Guid BaseObjectId {
38      get { return baseObjectId; }
39    }
40
41    [Storable]
42    protected PDAction successor;
43
44    public PDAction Successor {
45      get { return successor; }
46    }
47
48    [Storable]
49    protected bool started;
50
51    public void AppendAction(PDAction action, bool recursive) {
52      if (!action.BaseObjectId.Equals(this.BaseObjectId))
53        throw new Exception("Can only append actions of the same object");
54     
55      PDAction predecessor = this;
56      while (predecessor.successor != null &&
57            (recursive || predecessor.ActionCompleted()))
58        predecessor = predecessor.successor;
59
60      predecessor.successor = action;
61    }
62
63    public bool IsCompleted {
64      get {
65        return ActionCompleted() &&
66          (successor == null || successor.IsCompleted);
67      }
68    }
69
70    protected abstract bool ActionInterruptable();
71
72    public bool Interruptable() {
73      PDAction action = this;
74
75      while (action != null && action.ActionCompleted())
76        action = action.successor;
77
78      return action == null || !action.started || action.ActionInterruptable();
79    }
80
81    public abstract bool ActionCompleted();
82
83    protected abstract double Perform(BaseObject baseObject, double simulationTime, double tickTime, List<PDChange> changes);
84
85    public List<PDChange> Perform(BaseObject baseObject, double simulationTime, double timeStep) {
86      List<PDChange> changes = new List<PDChange>();
87
88      lock (this) {
89        double startTime = simulationTime;
90
91        double tickTime = timeStep;
92        bool complete = ActionCompleted();
93        if (!complete && tickTime > 0) {
94          started = true;
95          simulationTime = Perform(baseObject, simulationTime, tickTime, changes);
96          complete = ActionCompleted();
97        }
98
99        PDAction next = successor;
100        while (complete && next != null) {
101          tickTime = timeStep - (simulationTime - startTime);
102
103          complete = next.ActionCompleted();
104          if (!complete && tickTime > 0) {
105            next.started = true;
106            simulationTime = next.Perform(baseObject, simulationTime, tickTime, changes);
107            complete = next.ActionCompleted();
108          }
109
110          next = next.successor;
111        }
112      }
113
114      return changes;
115    }
116
117    public virtual bool Obsoletes(PDAction action) {
118      return this.BaseObjectId.Equals(action.BaseObjectId);
119    }
120
121    protected PDAction(Guid baseObjectId) {
122      this.baseObjectId = baseObjectId;
123    }
124
125    [StorableConstructor]
126    protected PDAction(bool deserializing) : base(deserializing) { }
127    protected PDAction(PDAction original, Cloner cloner)
128      : base(original, cloner) {
129        baseObjectId = original.baseObjectId;
130        successor = cloner.Clone<PDAction>(original.successor);
131        started = original.started;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.