public override Solution Run()
        {
            do
            {
                BasicSchedule(OrderedTrainRuns);

                var conflicts = CurrentSolution.GetConflicts();
                foreach (var kvp in conflicts)
                {
                    CurrentSolution.TrainRunsDic[kvp.Key].Order = kvp.Value.Count;
                    if (CurrentSolution.TrainRunsDic[kvp.Key].Route.Graph.PossiblePathsOrderedByPenalty.Length > 1)
                    {
                        Log($"Train {kvp.Key} has {CurrentSolution.TrainRunsDic[kvp.Key].Route.Graph.PossiblePathsOrderedByPenalty.Length-1} alternative routes");
                    }
                }

                // Schedule all trains with simple algorithm
                var subIter     = 0;
                var nbConflicts = 0;
                do
                {
                    nbConflicts = IterativeConflictSolver.ScheduleTrains(CurrentProblem, CurrentSolution.TrainRunsDic.Values.OrderByDescending(tr => tr.Order));
                    // Log($"Iteration {subIter}: {nbConflicts} conflicts.");
                } while (++subIter < SubIteration && nbConflicts > 0);

                if (nbConflicts == 0) // Only consider admissible solution
                {
                    CurrentSolution.ComputeObjectiveFunction();
                    Log($"Iteration {Iteration}, Objective value {CurrentSolution.ObjectiveValue}");
                    CompareWithBest(CurrentSolution);
                }

                if (BestSolution == null || !BestSolution.IsOptimal)
                {
                    // Is there some route alternative
                    foreach (var kvp in conflicts)
                    {
                        if (CurrentSolution.TrainRunsDic[kvp.Key].SelectNextPath())
                        {
                            Log($"Train {kvp.Key}, changing route");
                            break;
                        }
                    }
                }
            } while (++Iteration < MaxIteration && (BestSolution == null || !BestSolution.IsOptimal));

            return(BestSolution ?? CurrentSolution);
        }
예제 #2
0
        public override Solution Run()
        {
            do
            {
                // Assign path randomly
                CurrentSolution.AssignRandomPaths();

                // Simple Schedule
                BasicSchedule(CurrentSolution.TrainRuns.OrderBy(tr => tr.Train.MinEntryEarliest));

                // Detect conflicts
                var conflicts = CurrentSolution.GetConflicts();
                foreach (var c in conflicts)
                {
                    CurrentSolution.TrainRunsDic[c.Key].IsScheduled = false;
                }

                // Apply simple algorithm for trains with no conflicts
                var usedResources = new UsedResourceCollection();
                DummySolver.ScheduleTrains(CurrentProblem, CurrentSolution.TrainRuns.Where(tr => tr.IsScheduled).OrderBy(tr => tr.Train.MinEntryEarliest), usedResources);

                // Schedule conflicted trains
                DummySolver.ScheduleTrains(CurrentProblem, CurrentSolution.TrainRuns.Where(tr => !tr.IsScheduled).OrderBy(tr => tr.Train.MinEntryEarliest), usedResources);

                var validation = CurrentSolution.Validate();
                if (CurrentSolution.IsAdmissible) // Only consider admissible solution
                {
                    Log($"Compute objective function ... ");
                    CurrentSolution.ComputeObjectiveFunction();
                    CompareWithBest(CurrentSolution);
                }
                else
                {
                    Log(validation);
                }
            } while (++Iteration < MaxIteration && !BestSolution.IsOptimal);

            return(BestSolution);
        }