]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trainrouteplanner.cpp
Use cached path length from occupied_tracks
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/time/utils.h>
3 #include "catalogue.h"
4 #include "layout.h"
5 #include "route.h"
6 #include "train.h"
7 #include "trainroutemetric.h"
8 #include "trainrouteplanner.h"
9 #include "trainrouter.h"
10 #include "vehicle.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace R2C2 {
16
17 TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
18         goal(0),
19         path_switch_bias(15*Time::sec),
20         timeout(10*Time::sec),
21         result(PENDING),
22         thread(0)
23 {
24         const map<unsigned, Train *> &trains = layout.get_trains();
25         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
26         {
27                 TrainRoutingInfo info(*i->second);
28                 if(!info.waypoints.empty())
29                         routed_trains.push_back(info);
30         }
31 }
32
33 TrainRoutePlanner::~TrainRoutePlanner()
34 {
35         if(thread)
36         {
37                 thread->join();
38                 delete thread;
39         }
40 }
41
42 void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t)
43 {
44         timeout = t;
45 }
46
47 TrainRoutePlanner::Result TrainRoutePlanner::plan()
48 {
49         prepare_plan();
50         create_plan();
51         if(result==PENDING)
52                 finalize_plan();
53
54         return result;
55 }
56
57 void TrainRoutePlanner::plan_async()
58 {
59         if(thread)
60                 throw logic_error("already planning");
61
62         prepare_plan();
63         thread = new PlanningThread(*this);
64 }
65
66 TrainRoutePlanner::Result TrainRoutePlanner::check()
67 {
68         if(result==PENDING && goal)
69         {
70                 if(thread)
71                 {
72                         thread->join();
73                         delete thread;
74                         thread = 0;
75                 }
76                 finalize_plan();
77         }
78
79         return result;
80 }
81
82 const list<Route *> &TrainRoutePlanner::get_routes_for(const Train &train) const
83 {
84         return get_train_info(train).routes;
85 }
86
87 const list<TrainRouter::SequencePoint> &TrainRoutePlanner::get_sequence_for(const Train &train) const
88 {
89         return get_train_info(train).sequence;
90 }
91
92 const TrainRoutePlanner::TrainRoutingInfo &TrainRoutePlanner::get_train_info(const Train &train) const
93 {
94         for(vector<TrainRoutingInfo>::const_iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
95                 if(i->train==&train)
96                         return *i;
97
98         throw key_error(train.get_name());
99 }
100
101 const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
102 {
103         steps.splice(steps.end(), queue, queue.begin());
104         return steps.back();
105 }
106
107 void TrainRoutePlanner::prepare_plan()
108 {
109         steps.clear();
110         queue.clear();
111         goal = 0;
112         result = PENDING;
113
114         queue.push_back(RoutingStep());
115         RoutingStep &start = queue.back();
116         for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
117                 start.trains.push_back(TrainRoutingState(*i));
118         start.update_estimate();
119 }
120
121 void TrainRoutePlanner::create_plan()
122 {
123         Time::TimeStamp timeout_stamp = Time::now()+timeout;
124         unsigned count = 0;
125         while(!queue.empty())
126         {
127                 const RoutingStep &step = get_step();
128                 if(step.is_goal())
129                 {
130                         goal = &step;
131                         return;
132                 }
133
134                 add_steps(step);
135
136                 if(++count>=1000)
137                 {
138                         if(Time::now()>timeout_stamp)
139                                 break;
140                         count = 0;
141                 }
142         }
143
144         result = FAILED;
145 }
146
147 void TrainRoutePlanner::add_steps(const RoutingStep &step)
148 {
149         list<RoutingStep> new_steps;
150         step.create_successors(new_steps);
151         if(new_steps.empty())
152                 return;
153
154         new_steps.sort();
155         if(!queue.empty() && new_steps.front().cost_estimate<queue.front().cost_estimate+path_switch_bias)
156                 new_steps.front().preferred = true;
157         queue.merge(new_steps);
158 }
159
160 void TrainRoutePlanner::finalize_plan()
161 {
162         for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
163         {
164                 i->routes.clear();
165                 i->sequence.clear();
166                 for(unsigned j=0; j<2; ++j)
167                         i->track_history[j] = 0;
168         }
169
170         map<Track *, TrainRouter::SequencePoint *> sequenced_tracks;
171         unsigned sequence = steps.size();
172         for(const RoutingStep *i=goal; i; i=i->prev)
173                 for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
174                 {
175                         Track **history = j->info->track_history;
176                         // Don't process the same track again.
177                         if(j->track.track()==history[0])
178                                 continue;
179
180                         Route *route = 0;
181                         bool start_new_route = true;
182                         if(!j->info->routes.empty())
183                         {
184                                 /* If we already have a route and this track or any linked track is
185                                 in it, start a new one to avoid loops. */
186                                 route = j->info->routes.front();
187                                 start_new_route = route->has_track(*j->track);
188                                 if(!start_new_route)
189                                 {
190                                         unsigned nls = j->track->get_n_link_slots();
191                                         for(unsigned k=0; (!start_new_route && k<nls); ++k)
192                                         {
193                                                 Track *link = j->track->get_link(k);
194                                                 start_new_route = (link && link!=history[0] && route->has_track(*link));
195                                         }
196                                 }
197                         }
198
199                         if(start_new_route)
200                         {
201                                 route = new Route(j->info->train->get_layout());
202                                 route->set_name("Router");
203                                 route->set_temporary(true);
204                                 /* Have the routes overlap by two tracks to ensure that turnout
205                                 paths can be deduced. */
206                                 for(unsigned k=0; (k<2 && history[k]); ++k)
207                                         route->add_track(*history[k]);
208                                 j->info->routes.push_front(route);
209                         }
210
211                         route->add_track(*j->track.track());
212                         history[1] = history[0];
213                         history[0] = j->track.track();
214
215                         bool waitable = j->track.endpoint().paths!=j->track->get_type().get_paths();
216                         map<Track *, TrainRouter::SequencePoint *>::iterator k = sequenced_tracks.find(j->track.track());
217                         if(k!=sequenced_tracks.end())
218                         {
219                                 // Add a sequence point if another train uses this track afterwards.
220                                 if(!k->second->preceding_train)
221                                 {
222                                         k->second->preceding_train = j->info->train;
223                                         k->second->sequence_in = sequence;
224                                 }
225                                 j->info->sequence.push_front(TrainRouter::SequencePoint(j->track->get_block(), sequence));
226                                 if(waitable)
227                                         k->second = &j->info->sequence.front();
228                                 --sequence;
229                         }
230                         else if(waitable)
231                         {
232                                 /* Create a sequence point if it's possible to wait and let another
233                                 train past. */
234                                 j->info->sequence.push_front(TrainRouter::SequencePoint(j->track->get_block(), sequence));
235                                 sequenced_tracks[j->track.track()] = &j->info->sequence.front();
236                                 --sequence;
237                         }
238                 }
239
240         result = COMPLETE;
241 }
242
243
244 TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
245         train(&t),
246         length(0),
247         speed(train->get_maximum_speed()),
248         first_noncritical(train->get_last_critical_block().next().block()),
249         router(train->get_ai_of_type<TrainRouter>()),
250         has_duration(false)
251 {
252         if(unsigned n_wps = router->get_n_waypoints())
253         {
254                 waypoints.reserve(n_wps),
255                 metrics.reserve(n_wps);
256                 for(unsigned i=0; i<n_wps; ++i)
257                 {
258                         waypoints.push_back(router->get_waypoint(i));
259                         metrics.push_back(&router->get_metric(i));
260                 }
261                 has_duration = router->get_trip_duration();
262         }
263
264         unsigned n_vehs = train->get_n_vehicles();
265         for(unsigned i=0; i<n_vehs; ++i)
266                 length += train->get_vehicle(i).get_type().get_length();
267
268         // If no maximum speed is specified, use a sensible default
269         if(!speed)
270                 speed = 20*train->get_layout().get_catalogue().get_scale();
271 }
272
273
274 TrainRoutePlanner::OccupiedTrack::OccupiedTrack(Track &t, unsigned p, OccupiedTrack *n):
275         track(&t),
276         path_length(track->get_type().get_path_length(p)),
277         next(n),
278         n_tracks(next ? next->n_tracks+1 : 1),
279         refcount(1)
280 {
281         if(next)
282                 ++next->refcount;
283 }
284
285 TrainRoutePlanner::OccupiedTrack::OccupiedTrack(const OccupiedTrack &other):
286         track(other.track),
287         path_length(other.path_length),
288         next(other.next),
289         n_tracks(other.n_tracks),
290         refcount(1)
291 {
292         if(next)
293                 ++next->refcount;
294 }
295
296 TrainRoutePlanner::OccupiedTrack::~OccupiedTrack()
297 {
298         if(next && !--next->refcount)
299                 delete next;
300 }
301
302
303 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
304         info(&inf),
305         critical(true),
306         occupied_tracks(0),
307         state(MOVING),
308         delay(info->router->get_departure_delay()),
309         duration(info->router->get_trip_duration()),
310         waypoint(0),
311         blocked_by(-1)
312 {
313         const Vehicle *veh = &info->train->get_vehicle(0);
314         // TODO margins
315         TrackOffsetIter track_and_offs = veh->get_placement().get_position(VehiclePlacement::FRONT_BUFFER);
316         track = track_and_offs.track_iter();
317         offset = track_and_offs.offset();
318         path = track->get_active_path();
319
320         while(Vehicle *next = veh->get_link(1))
321                 veh = next;
322         track_and_offs = veh->get_placement().get_position(VehiclePlacement::BACK_BUFFER);
323         back_offset = track_and_offs.offset();
324
325         TrackIter iter = track_and_offs.track_iter();
326         while(1)
327         {
328                 occupied_tracks = new OccupiedTrack(*iter, iter->get_active_path(), occupied_tracks);
329                 if(iter.track()==track.track())
330                         break;
331                 iter = iter.next();
332         }
333
334         update_estimate();
335 }
336
337 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState &other):
338         info(other.info),
339         track(other.track),
340         path(other.path),
341         critical(other.critical),
342         occupied_tracks(other.occupied_tracks),
343         offset(other.offset),
344         back_offset(other.back_offset),
345         state(other.state),
346         delay(other.delay),
347         duration(other.duration),
348         waypoint(other.waypoint),
349         distance_traveled(other.distance_traveled),
350         remaining_estimate(other.remaining_estimate),
351         wait_time(other.wait_time),
352         estimated_wait(other.estimated_wait),
353         blocked_by(other.blocked_by)
354 {
355         ++occupied_tracks->refcount;
356 }
357
358 TrainRoutePlanner::TrainRoutingState::~TrainRoutingState()
359 {
360         if(occupied_tracks && !--occupied_tracks->refcount)
361                 delete occupied_tracks;
362 }
363
364 Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() const
365 {
366         return ((occupied_tracks->path_length-offset)/info->speed)*Time::sec+delay;
367 }
368
369 Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_pass(Track &trk) const
370 {
371         if(is_occupying(trk))
372         {
373                 float passed_length = 0;
374                 for(const OccupiedTrack *occ=occupied_tracks; (occ && occ->track!=&trk); occ=occ->next)
375                         passed_length += occ->path_length;
376                 return (max(info->length-passed_length, 0.0f)/info->speed)*Time::sec+delay;
377         }
378
379         for(unsigned wp=waypoint; wp<info->waypoints.size(); ++wp)
380         {
381                 float distance = info->metrics[wp]->get_distance_from(trk);
382                 if(distance>=0 && distance<remaining_estimate)
383                         return ((remaining_estimate-distance+info->length)/info->speed)*Time::sec+delay;
384         }
385
386         return Time::day;
387 }
388
389 bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
390 {
391         if(state==ARRIVED && !duration && info->has_duration)
392                 return false;
393
394         OccupiedTrack *occ = occupied_tracks;
395         for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next)
396                 if(occ->track==&trk)
397                         return true;
398         return false;
399 }
400
401 bool TrainRoutePlanner::TrainRoutingState::check_arrival()
402 {
403         TrackIter next_track = track.next(path);
404
405         // Check if we're about the exit the current waypoint's tracks.
406         const TrainRouter::Waypoint &wp = info->waypoints[waypoint];
407         if(wp.chain->has_track(*track) && !wp.chain->has_track(*next_track))
408                 if(wp.direction==TrackChain::UNSPECIFIED || track==wp.chain->iter_for(*track, wp.direction))
409                 {
410                         if(waypoint+1<info->waypoints.size())
411                                 ++waypoint;
412                         else
413                         {
414                                 state = ARRIVED;
415                                 return true;
416                         }
417                 }
418
419         // If we're entering the first non-critical block, clear the critical flag.
420         if(info->first_noncritical->has_track(*next_track))
421                 critical = false;
422
423         return false;
424 }
425
426 void TrainRoutePlanner::TrainRoutingState::advance(float distance)
427 {
428         offset += distance;
429         back_offset += distance;
430
431         // See if the tail end of the train has passed any sensors.
432         unsigned count_to_free = 0;
433         unsigned last_sensor_addr = 0;
434         float distance_after_sensor = 0;
435         OccupiedTrack *occ = occupied_tracks;
436         for(unsigned n=occupied_tracks->n_tracks; n>0; --n)
437         {
438                 if(unsigned saddr = occ->track->get_sensor_address())
439                 {
440                         if(saddr!=last_sensor_addr)
441                         {
442                                 count_to_free = 0;
443                                 distance_after_sensor = 0;
444                         }
445                         last_sensor_addr = saddr;
446                 }
447
448                 ++count_to_free;
449                 distance_after_sensor += occ->path_length;
450
451                 occ = occ->next;
452         }
453
454         // Free the last passed sensor and any tracks behind it.
455         if(count_to_free && back_offset>distance_after_sensor)
456         {
457                 back_offset -= distance_after_sensor;
458                 if(occupied_tracks->refcount>1)
459                 {
460                         --occupied_tracks->refcount;
461                         occupied_tracks = new OccupiedTrack(*occupied_tracks);
462                 }
463                 occupied_tracks->n_tracks -= count_to_free;
464         }
465
466         distance_traveled += distance;
467         remaining_estimate -= distance;
468 }
469
470 void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
471 {
472         if(delay>=dt)
473         {
474                 delay -= dt;
475                 return;
476         }
477
478         float secs = dt/Time::sec;
479         // There may be negative delay remaining after previous step.
480         if(delay)
481         {
482                 secs -= delay/Time::sec;
483                 delay = Time::zero;
484         }
485
486         if(duration)
487                 duration = max(duration-secs*Time::sec, Time::zero);
488
489         if(estimated_wait)
490                 estimated_wait = max(estimated_wait-secs*Time::sec, Time::zero);
491
492         if(state==MOVING)
493                 advance(info->speed*secs);
494         else if(state!=ARRIVED)
495                 wait_time += secs*Time::sec;
496 }
497
498 void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
499 {
500         float distance = occupied_tracks->path_length-offset;
501         track = track.next(path);
502         path = next_path;
503         occupied_tracks = new OccupiedTrack(*track, path, occupied_tracks);
504         advance(distance);
505         offset = 0;
506 }
507
508 void TrainRoutePlanner::TrainRoutingState::set_path(unsigned p)
509 {
510         path = p;
511         OccupiedTrack *next_occ = occupied_tracks->next;
512         if(!--occupied_tracks->refcount)
513                 delete occupied_tracks;
514         occupied_tracks = new OccupiedTrack(*track, path, next_occ);
515         update_estimate();
516 }
517
518 void TrainRoutePlanner::TrainRoutingState::update_estimate()
519 {
520         TrackIter iter = track.reverse(path);
521         remaining_estimate = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry());
522         if(remaining_estimate>=0)
523                 remaining_estimate += occupied_tracks->path_length-offset;
524 }
525
526 bool TrainRoutePlanner::TrainRoutingState::is_viable() const
527 {
528         if(remaining_estimate<0)
529                 return false;
530         if(critical && state==BLOCKED)
531                 return false;
532         return true;
533 }
534
535
536 TrainRoutePlanner::RoutingStep::RoutingStep():
537         preferred(false),
538         prev(0)
539 { }
540
541 TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
542         time(p->time),
543         cost_estimate(p->cost_estimate),
544         preferred(false),
545         trains(p->trains),
546         prev(p)
547 { }
548
549 void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_steps) const
550 {
551         RoutingStep next(this);
552         if(next.update_states() && next.check_deadlocks())
553                 return;
554
555         int train_index = find_next_train();
556         if(train_index<0)
557                 return;
558
559         TrainRoutingState &train = next.trains[train_index];
560
561         Time::TimeDelta dt = train.get_time_to_next_track();
562         next.advance(dt);
563
564         /* Check arrival after the train has advanced to the end of its current track
565         so travel time and occupied tracks will be correct. */
566         if(train.check_arrival())
567         {
568                 new_steps.push_back(next);
569                 return;
570         }
571
572         train.advance_track(0);
573
574         const TrackType::Endpoint &entry_ep = train.track.endpoint();
575         if(train.critical)
576         {
577                 /* Only create a successor step matching the currently set path for a
578                 critical track. */
579                 unsigned critical_path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path());
580                 create_successor(next, train_index, critical_path, new_steps);
581         }
582         else
583         {
584                 // Create successor steps for all possible paths through the new track.
585                 for(unsigned i=0; entry_ep.paths>>i; ++i)
586                         if(entry_ep.has_path(i))
587                                 create_successor(next, train_index, i, new_steps);
588         }
589
590         if(entry_ep.paths!=train.track->get_type().get_paths() && !train.critical)
591         {
592                 /* Create a waiting state before the track if there's at least one path
593                 that doesn't pass through the entry endpoint. */
594                 RoutingStep wait(this);
595                 wait.advance(dt);
596                 wait.trains[train_index].state = WAITING;
597
598                 Time::TimeDelta estimated_wait = Time::day;
599                 for(unsigned i=0; i<wait.trains.size(); ++i)
600                         if(i!=static_cast<unsigned>(train_index) && wait.trains[i].state!=ARRIVED)
601                         {
602                                 Time::TimeDelta ttp = wait.trains[i].get_time_to_pass(*train.track);
603                                 estimated_wait = min(estimated_wait, ttp);
604                         }
605                 wait.trains[train_index].estimated_wait = estimated_wait;
606
607                 wait.update_estimate();
608                 if(wait.is_viable())
609                         new_steps.push_back(wait);
610         }
611 }
612
613 void TrainRoutePlanner::RoutingStep::create_successor(RoutingStep &next, unsigned train_index, unsigned path, list<RoutingStep> &new_steps)
614 {
615         TrainRoutingState &train = next.trains[train_index];
616
617         train.set_path(path);
618         next.update_estimate();
619         if(next.is_viable())
620                 new_steps.push_back(next);
621 }
622
623 bool TrainRoutePlanner::RoutingStep::update_states()
624 {
625         bool changes = false;
626         for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
627         {
628                 if(i->state==ARRIVED)
629                         continue;
630
631                 TrainState old_state = i->state;
632
633                 TrackIter next_track = i->track.next(i->path);
634                 if(next_track)
635                 {
636                         i->blocked_by = get_occupant(*next_track);
637                         if(i->blocked_by>=0)
638                         {
639                                 /* If the train is still traversing its last critical track, the
640                                 flag needs to be cleared here to pass viability test. */
641                                 if(i->info->first_noncritical->has_track(*next_track))
642                                         i->critical = false;
643
644                                 if(i->state!=BLOCKED)
645                                         i->estimated_wait = trains[i->blocked_by].get_time_to_pass(*next_track);
646
647                                 /* Trains in the WAITING state will also transition to BLOCKED and
648                                 then to MOVING when the other train has passed. */
649                                 i->state = BLOCKED;
650                         }
651                         else if(i->state==BLOCKED)
652                         {
653                                 i->estimated_wait = Time::zero;
654                                 i->state = MOVING;
655                         }
656                 }
657                 else
658                         i->state = BLOCKED;
659
660                 if(i->state!=old_state)
661                         changes = true;
662         }
663
664         return changes;
665 }
666
667 bool TrainRoutePlanner::RoutingStep::check_deadlocks() const
668 {
669         for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
670         {
671                 if(i->state!=BLOCKED)
672                         continue;
673
674                 // A train blocked by end of track is always considered a deadlock.
675                 if(i->blocked_by<0)
676                         return true;
677
678                 /* Use the tortoise and hare algorithm to check if trains are blocked
679                 cyclically (A blocks B, which blocks ..., which blocks A). */
680                 int slow = i->blocked_by;
681                 int fast = trains[slow].blocked_by;
682                 while(fast>=0 && trains[fast].blocked_by>=0)
683                 {
684                         if(fast==slow)
685                                 return true;
686
687                         slow = trains[slow].blocked_by;
688                         fast = trains[trains[fast].blocked_by].blocked_by;
689                 }
690         }
691
692         return false;
693 }
694
695 int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const
696 {
697         for(unsigned i=0; i<trains.size(); ++i)
698                 if(trains[i].is_occupying(track))
699                         return i;
700
701         return -1;
702 }
703
704 int TrainRoutePlanner::RoutingStep::find_next_train() const
705 {
706         Time::TimeDelta min_dt;
707         int next_train = -1;
708         for(unsigned i=0; i<trains.size(); ++i)
709                 if(trains[i].state==MOVING)
710                 {
711                         Time::TimeDelta dt = trains[i].get_time_to_next_track();
712                         if(dt<min_dt || next_train<0)
713                         {
714                                 min_dt = dt;
715                                 next_train = i;
716                         }
717                 }
718
719         return next_train;
720 }
721
722 void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
723 {
724         time += dt;
725         for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
726                 i->advance(dt);
727 }
728
729 void TrainRoutePlanner::RoutingStep::update_estimate()
730 {
731         cost_estimate = Time::zero;
732         for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
733                 if(i->remaining_estimate>=0)
734                         cost_estimate += i->wait_time+i->estimated_wait+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec;
735 }
736
737 bool TrainRoutePlanner::RoutingStep::is_viable() const
738 {
739         for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
740                 if(!i->is_viable())
741                         return false;
742
743         for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
744                 if(i->state==MOVING)
745                         return true;
746
747         return false;
748 }
749
750 bool TrainRoutePlanner::RoutingStep::is_goal() const
751 {
752         for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
753                 if(i->state!=ARRIVED)
754                         return false;
755         return true;
756 }
757
758 bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
759 {
760         if(preferred!=other.preferred)
761                 return preferred>other.preferred;
762         return cost_estimate<other.cost_estimate;
763 }
764
765
766 TrainRoutePlanner::PlanningThread::PlanningThread(TrainRoutePlanner &p):
767         planner(p)
768 {
769         launch();
770 }
771
772 void TrainRoutePlanner::PlanningThread::main()
773 {
774         planner.create_plan();
775 }
776
777 } // namespace R2C2