In this paper, we study the real-time train assignment problem (RT-TAP) that arises from the high percentage of unreliable arrival times of freight trains and the large quantity of last-minute parking requests at railway yards. In the RT-TAP, the reassignment of trains to the yard is triggered every time a train arrives at the railway yard and needs to be assigned (event-based optimization). After
introducing a problem formulation for the RT-TAP problem, we prove that RT-TAP is NP-Hard. In particular, the RT-TAP is modeled as a mixed integer program that strives to minimize the total weighted delay of trains. Because of its computational complexity and the time-critical nature of this problem, we introduce two real-time solution methods: (a) a problem-specific genetic algorithm (GA), (b) and a first-scheduled first-served (FSFS) heuristic. In small instances, we show that the GA returns a globally optimal solution which is identical to the solution of exact optimization methods. In larger problem instances, the heuristic approaches of FSFS and GA are tested at the Waalhaven Zuid railway yard in the Netherlands using two months of operational data. In the experimental results, the GA solutions reduce the average delays by more than 4 minutes compared to the solutions of the FSFS heuristic.