#include "layout.h"
#include "route.h"
#include "train.h"
+#include "trainroutemetric.h"
#include "trainrouteplanner.h"
#include "trainrouter.h"
#include "vehicle.h"
routed_trains.push_back(info);
}
- steps.push_back(RoutingStep());
- RoutingStep &start = steps.back();
+ queue.push_back(RoutingStep());
+ RoutingStep &start = queue.back();
for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
start.trains.push_back(TrainRoutingState(*i));
+ start.update_estimate();
}
void TrainRoutePlanner::plan()
{
- RoutingStep *goal = 0;
- for(list<RoutingStep>::iterator i=steps.begin(); i!=steps.end(); ++i)
+ const RoutingStep *goal = 0;
+ while(!queue.empty())
{
- if(i->is_goal())
+ const RoutingStep &step = get_step();
+ if(step.is_goal())
{
- goal = &*i;
+ goal = &step;
break;
}
- if(update_states(*i))
- {
- int next_train = find_next_train(*i);
- if(next_train>=0)
- add_steps(*i, next_train);
- }
+ add_steps(step);
}
if(goal)
create_routes(*goal);
}
-bool TrainRoutePlanner::update_states(RoutingStep &step)
-{
- RoutingStep next(&step);
- bool changes = false;
- for(vector<TrainRoutingState>::iterator i=next.trains.begin(); i!=next.trains.end(); ++i)
- {
- if(i->state==ARRIVED)
- continue;
-
- TrainState old_state = i->state;
- if(i->state==BLOCKED)
- i->state = MOVING;
-
- TrackIter next_track = i->track.next(i->path);
- if(!next_track)
- return false;
-
- for(vector<TrainRoutingState>::iterator j=next.trains.begin(); j!=next.trains.end(); ++j)
- if(j!=i)
- {
- if(j->track.track()==next_track.track())
- {
- unsigned other_exit = j->track.reverse(j->path).entry();
- if(next_track.entry()==other_exit)
- return false;
- }
- else if(!j->is_occupied(*next_track))
- continue;
-
- i->state = BLOCKED;
- }
-
- if(i->state!=old_state)
- changes = true;
- }
-
- if(changes)
- {
- list<RoutingStep>::iterator i;
- for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ;
- steps.insert(i, next);
- }
-
- return !changes;
-}
-
-int TrainRoutePlanner::find_next_train(RoutingStep &step)
+const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
{
- Time::TimeDelta min_dt;
- int next_train = -1;
- for(unsigned i=0; i<step.trains.size(); ++i)
- if(step.trains[i].state==MOVING)
- {
- Time::TimeDelta dt = step.trains[i].get_time_to_next_track();
- if(dt<min_dt || next_train<0)
- {
- min_dt = dt;
- next_train = i;
- }
- }
-
- return next_train;
+ steps.splice(steps.end(), queue, queue.begin());
+ return steps.back();
}
-void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
+void TrainRoutePlanner::add_steps(const RoutingStep &step)
{
- TrainRoutingState &train = step.trains[train_index];
- Time::TimeDelta dt = train.get_time_to_next_track();
- TrackIter next_track = train.track.next(train.path);
-
list<RoutingStep> new_steps;
-
- RoutingStep next(&step);
- next.advance(dt);
- TrainRouter &router = *train.info->router;
- if(train.waypoint<0 && router.is_destination(*train.track) && !router.is_destination(*next_track))
- {
- next.trains[train_index].state = ARRIVED;
- new_steps.push_back(next);
- }
- else
- {
- if(train.waypoint>=0 && router.is_waypoint(train.waypoint, *train.track) && !router.is_waypoint(train.waypoint, *next_track))
- {
- ++next.trains[train_index].waypoint;
- if(next.trains[train_index].waypoint>=static_cast<int>(router.get_n_waypoints()))
- next.trains[train_index].waypoint = -1;
- }
-
- next.trains[train_index].advance_track(0);
-
- const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
- for(unsigned i=0; next_entry_ep.paths>>i; ++i)
- if(next_entry_ep.has_path(i))
- {
- next.trains[train_index].path = i;
- if(next.is_viable())
- new_steps.push_back(next);
- }
-
- if(next_entry_ep.paths!=next_track->get_type().get_paths())
- {
- RoutingStep wait(&step);
- wait.advance(dt);
- wait.trains[train_index].state = WAITING;
- if(wait.is_viable())
- new_steps.push_back(wait);
- }
- }
-
+ step.create_successors(new_steps);
new_steps.sort();
- steps.merge(new_steps);
+ queue.merge(new_steps);
}
-void TrainRoutePlanner::create_routes(RoutingStep &goal)
+void TrainRoutePlanner::create_routes(const RoutingStep &goal)
{
for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
{
- i->route = new Route(i->train->get_layout());
- i->route->set_name("Router");
- i->route->set_temporary(true);
+ Route *route = new Route(i->train->get_layout());
+ route->set_name("Router");
+ route->set_temporary(true);
+ i->routes.push_front(route);
+
+ for(unsigned j=0; j<3; ++j)
+ i->track_history[j] = 0;
}
- for(RoutingStep *i=&goal; i; i=i->prev)
+ for(const RoutingStep *i=&goal; i; i=i->prev)
{
- for(vector<TrainRoutingState>::iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
+ for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
{
+ Track **history = j->info->track_history;
+ if(j->track.track()==history[0])
+ continue;
+
if(j->state==WAITING || j->state==BLOCKED)
j->info->waits.push_front(&*j);
- j->info->route->add_track(*j->track);
+
+ Route *route = j->info->routes.front();
+ if(route->has_track(*j->track))
+ {
+ route = new Route(j->info->train->get_layout());
+ route->set_name("Router");
+ route->set_temporary(true);
+ for(unsigned k=2; k>0; --k)
+ route->add_track(*history[k]);
+ j->info->routes.push_front(route);
+ }
+
+ if(history[0])
+ route->add_track(*history[0]);
+ for(unsigned k=2; k>0; --k)
+ history[k] = history[k-1];
+ history[0] = j->track.track();
}
}
for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
{
- i->router->set_route(i->route);
- TrainRoutingState *current_wait = 0;
- for(list<TrainRoutingState *>::iterator j=i->waits.begin(); j!=i->waits.end(); ++j)
+ for(list<Route *>::iterator j=i->routes.begin(); j!=i->routes.end(); ++j)
+ i->router->add_route(**j);
+ const TrainRoutingState *current_wait = 0;
+ for(list<const TrainRoutingState *>::const_iterator j=i->waits.begin(); j!=i->waits.end(); ++j)
if(!current_wait || (*j)->track.track()!=current_wait->track.track())
{
Block &block = (*j)->track.next()->get_block();
TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
train(&t),
speed(train->get_maximum_speed()),
- router(train->get_ai_of_type<TrainRouter>()),
- route(0)
+ router(train->get_ai_of_type<TrainRouter>())
{
// If no maximum speed is specified, use a sensible default
if(!speed)
occupied_tracks(0),
state(MOVING),
delay(info->router->get_departure_delay()),
- waypoint(info->router->get_n_waypoints() ? 0 : -1)
+ waypoint(info->router->get_n_waypoints() ? 0 : -1),
+ blocked_by(-1)
{
const Vehicle *veh = &info->train->get_vehicle(0);
// TODO margins
break;
iter = iter.next();
}
+
+ update_estimate();
}
TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState &other):
back_offset(other.back_offset),
state(other.state),
delay(other.delay),
- waypoint(other.waypoint)
+ waypoint(other.waypoint),
+ remaining_estimate(other.remaining_estimate),
+ blocked_by(other.blocked_by)
{
++occupied_tracks->refcount;
}
TrainRoutePlanner::TrainRoutingState::~TrainRoutingState()
{
- if(!--occupied_tracks->refcount)
+ if(occupied_tracks && !--occupied_tracks->refcount)
delete occupied_tracks;
}
return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay;
}
-bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const
+bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
{
OccupiedTrack *occ = occupied_tracks;
for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next)
return false;
}
+bool TrainRoutePlanner::TrainRoutingState::check_arrival()
+{
+ TrainRouter &router = *info->router;
+ TrackIter next_track = track.next(path);
+
+ if(waypoint<0 && router.is_destination(*track) && !router.is_destination(*next_track))
+ {
+ state = ARRIVED;
+ return true;
+ }
+ else if(waypoint>=0 && router.is_waypoint(waypoint, *track) && !router.is_waypoint(waypoint, *next_track))
+ {
+ ++waypoint;
+ if(waypoint>=static_cast<int>(router.get_n_waypoints()))
+ waypoint = -1;
+ }
+
+ return false;
+}
+
void TrainRoutePlanner::TrainRoutingState::advance(float distance)
{
offset += distance;
}
--occupied_tracks->n_tracks;
}
+
+ remaining_estimate -= (distance/info->speed)*Time::sec;
+ if(remaining_estimate<Time::zero)
+ remaining_estimate = Time::zero;
}
void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
offset = 0;
}
+void TrainRoutePlanner::TrainRoutingState::update_estimate()
+{
+ TrackIter iter = track.reverse(path);
+ float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
+ distance += track->get_type().get_path_length(path)-offset;
+ remaining_estimate = (distance/info->speed)*Time::sec+delay;
+}
+
TrainRoutePlanner::RoutingStep::RoutingStep():
prev(0)
{ }
-TrainRoutePlanner::RoutingStep::RoutingStep(RoutingStep *p):
+TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
time(p->time),
+ total_estimate(p->total_estimate),
trains(p->trains),
prev(p)
{ }
+void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_steps) const
+{
+ RoutingStep next(this);
+ if(next.update_states())
+ {
+ if(next.check_deadlocks())
+ return;
+
+ new_steps.push_back(next);
+ return;
+ }
+
+ int train_index = find_next_train();
+ if(train_index<0)
+ return;
+
+ TrainRoutingState &train = next.trains[train_index];
+
+ Time::TimeDelta dt = train.get_time_to_next_track();
+ next.advance(dt);
+
+ if(train.check_arrival())
+ {
+ new_steps.push_back(next);
+ return;
+ }
+
+ TrackIter next_track = train.track.next(train.path);
+ train.advance_track(0);
+
+ const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
+ for(unsigned i=0; next_entry_ep.paths>>i; ++i)
+ if(next_entry_ep.has_path(i))
+ {
+ train.path = i;
+ train.update_estimate();
+ next.update_estimate();
+ if(next.is_viable())
+ new_steps.push_back(next);
+ }
+
+ if(next_entry_ep.paths!=next_track->get_type().get_paths())
+ {
+ RoutingStep wait(this);
+ wait.advance(dt);
+ wait.trains[train_index].state = WAITING;
+ if(wait.is_viable())
+ new_steps.push_back(wait);
+ }
+}
+
+bool TrainRoutePlanner::RoutingStep::update_states()
+{
+ bool changes = false;
+ for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ if(i->state==ARRIVED)
+ continue;
+
+ TrainState old_state = i->state;
+
+ TrackIter next_track = i->track.next(i->path);
+ if(next_track)
+ {
+ i->blocked_by = get_occupant(*next_track);
+ if(i->blocked_by>=0)
+ i->state = BLOCKED;
+ else if(i->state==BLOCKED)
+ i->state = MOVING;
+ }
+ else
+ i->state = BLOCKED;
+
+ if(i->state!=old_state)
+ changes = true;
+ }
+
+ return changes;
+}
+
+bool TrainRoutePlanner::RoutingStep::check_deadlocks() const
+{
+ for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ if(i->state!=BLOCKED)
+ continue;
+
+ if(i->blocked_by<0)
+ return true;
+
+ int slow = i->blocked_by;
+ int fast = trains[slow].blocked_by;
+ while(fast>=0 && trains[fast].blocked_by>=0)
+ {
+ if(fast==slow)
+ return true;
+
+ slow = trains[slow].blocked_by;
+ fast = trains[trains[fast].blocked_by].blocked_by;
+ }
+ }
+
+ return false;
+}
+
+int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const
+{
+ for(unsigned i=0; i<trains.size(); ++i)
+ if(trains[i].is_occupying(track))
+ return i;
+
+ return -1;
+}
+
+int TrainRoutePlanner::RoutingStep::find_next_train() const
+{
+ Time::TimeDelta min_dt;
+ int next_train = -1;
+ for(unsigned i=0; i<trains.size(); ++i)
+ if(trains[i].state==MOVING)
+ {
+ Time::TimeDelta dt = trains[i].get_time_to_next_track();
+ if(dt<min_dt || next_train<0)
+ {
+ min_dt = dt;
+ next_train = i;
+ }
+ }
+
+ return next_train;
+}
+
void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
{
time += dt;
}
}
+void TrainRoutePlanner::RoutingStep::update_estimate()
+{
+ for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ if(i->remaining_estimate<Time::zero)
+ {
+ total_estimate = i->remaining_estimate;
+ return;
+ }
+
+ Time::TimeDelta t = time+i->remaining_estimate;
+ if(i==trains.begin() || t>total_estimate)
+ total_estimate = t;
+ }
+}
+
bool TrainRoutePlanner::RoutingStep::is_viable() const
{
+ if(total_estimate<Time::zero)
+ return false;
+
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(i->state==MOVING)
return true;
bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
{
- return time<other.time;
+ return total_estimate<other.total_estimate;
}
} // namespace R2C2