Exemplo n.º 1
0
        public override Solution Run()
        {
            do
            {
                BasicSchedule(OrderedTrainRuns);
                int subIter = 0;
                int nbConflicts;
                do
                {
                    nbConflicts = IterativeConflictSolver.ScheduleTrains(CurrentProblem, OrderedTrainRuns);
                    Log($"Iteration {Iteration}: {nbConflicts} conflicts.");
                } while (++subIter < SubIteration && nbConflicts > 0);

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

                // Reorder trains and start again (unless optimal has been reached)
                OrderedTrainRuns = CurrentSolution.TrainRunsDic.Values.OrderByDescending(tr => tr.ObjectiveValue).ToArray();

                // We need a way to identify that some ordering has been used already. Hash of the array ?
            } while (++Iteration < MaxIteration && (BestSolution == null || !BestSolution.IsOptimal));

            return(BestSolution ?? CurrentSolution);
        }
        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);
        }