Пример #1
0
        /// <summary>
        /// Calculates the transfers.
        /// </summary>
        /// <param name="timetable">The timetable.</param>
        /// <param name="line">The line.</param>
        /// <param name="startTime">The start time.</param>
        /// <returns></returns>
        private CurrentState calculateTransfers(Timetable timetable, TrainLineVariable line, Time startTime)
        {
            // Changes, in case we need to revert the state (it is worse than the current one).
            List <Change> changes = new List <Change>();

            //////SET SELECED LINES
            // Set the start time for the given line.
            changes.Add(new Change(line, line.StartTime));
            line.StartTime = startTime;

            ///SET ALL CONNECTED LINES
            // Set the appropriate start time for each connected variable.
            foreach (TrainLineVariable connectedVariable in line.ConnectedLinesVariable)
            {
                // Set the start for variable.
                changes.Add(new Change(line, line.StartTime));
                connectedVariable.StartTime = startTime - connectedVariable.Line.ConnectedLineShift;
                connectedVariable.StartTime = PeriodUtil.normalizeTime(connectedVariable.StartTime, connectedVariable.Period);
            }

            // Compute the factor.
            int factor = Timetable.calculateTimetableRatingValue(timetable);

            // Current state returned.
            return(new CurrentState(changes, factor));
        }
Пример #2
0
 /// <summary>
 /// Adds the variable line.
 /// </summary>
 /// <param name="line">The variable train line.</param>
 public void addVariableLine(TrainLineVariable line)
 {
     if (!doesVariableLineExist(variableLines, line.LineNumber))
     {
         variableLines.Add(line);
     }
 }
        /// <summary>
        /// Creates the train line variables independent on solution.
        /// </summary>
        /// <param name="solution">The solution.</param>
        /// <param name="trainLineMap">The train line map.</param>
        /// <returns>Train line variables.</returns>
        private static List <TrainLineVariable> createTrainLineVariablesIndependentOnSolution(Solution solution, List <TrainLine> trainLineMap)
        {
            // retreive all lines from train line cache
            List <TrainLine> allLines = TrainLineCache.getInstance().getCacheContent();
            // new train lines variable
            List <TrainLineVariable> trainLineVariables = new List <TrainLineVariable>();

            // if not all lines already contained, otherwise return
            if (allLines.Count != trainLineMap.Count)
            {
                foreach (TrainLine line in allLines)
                {
                    // if line is not contained in trian line map
                    if (!containsTrainLine(trainLineMap, line))
                    {
                        // create new line of timetable
                        TrainLineVariable tlv = new TrainLineVariable(line);
                        // set independent start time
                        tlv.StartTime = Time.MinValue;
                        // add
                        trainLineVariables.Add(tlv);
                    }
                }
            }

            return(trainLineVariables);
        }
        /// <summary>
        /// Creates the train line variables dependent on solution.
        /// </summary>
        /// <param name="solution">The solution.</param>
        /// <param name="trainLineMap">The train line map.</param>
        /// <returns>Train line variables.</returns>
        private static List <TrainLineVariable> createTrainLineVariablesDependentOnSolution(Solution solution, List <TrainLine> trainLineMap)
        {
            // new train lines variable
            List <TrainLineVariable> newTrainLineVariables = new List <TrainLineVariable>();

            // loop index
            int index = 0;

            // for all mapped line at solution
            foreach (TrainLine line in trainLineMap)
            {
                // create a trainLine
                TrainLineVariable tlv = new TrainLineVariable(line);
                //initialize fields

                Time normalizeTime = PeriodUtil.normalizeTime(Time.ToTime(solution.SolutionVector[index].Minute), line.Period);
                tlv.StartTime = normalizeTime;
                //tlv.RatingValue = solution.SolutionFactor;

                // add to the list
                newTrainLineVariables.Add(tlv);
                // increment index
                index++;
            }

            return(newTrainLineVariables);
        }
Пример #5
0
        /// <summary>
        /// Calculates the transfer.
        /// </summary>
        /// <param name="timetable">The timetable.</param>
        /// <param name="transfer">The transfer.</param>
        /// <returns></returns>
        public static int calculateTransfer(Timetable timetable, Transfer transfer)
        {
            // result rating value
            int ratingValue;

            TrainLineVariable onLine  = timetable.getVariableLineOnSelect(transfer.OnLine.LineNumber);
            TrainLineVariable offLine = timetable.getVariableLineOnSelect(transfer.OffLine.LineNumber);

            // varline startime, departure from start of line, connected line shif of line
            Time arrivalTime   = offLine.StartTime + offLine.arrivalToStopAtIndex(transfer.TrainStopIndexOffLine);
            Time departureTime = onLine.StartTime + onLine.departureFromStopAtIndex(transfer.TrainStopIndexOnLine);

            normalizeTransferTime(ref departureTime, ref arrivalTime, transfer.Station.MinimalTransferTime, (int)onLine.Period, (int)offLine.Period);
            ratingValue = transfer.evaluateTransferFunction(departureTime - arrivalTime);
            return(ratingValue);
        }
Пример #6
0
        /// <summary>
        /// Finds the variable line from specified lines.
        /// </summary>
        /// <param name="lines">The lines.</param>
        /// <param name="lineNumber">The line number.</param>
        /// <returns>The variable train line.</returns>
        public static TrainLineVariable findVariableLine(List <TrainLineVariable> lines, int lineNumber)
        {
            TrainLineVariable wantedLine = null;

            // loop over all lines
            foreach (TrainLineVariable line in lines)
            {
                // if lines' number are equal
                if (line.LineNumber.Equals(lineNumber))
                {
                    // we found the line
                    wantedLine = line;
                    // and escape the loop
                    break;
                }
            }
            return(wantedLine);
        }
Пример #7
0
        private void updateConnectedLineShift(TrainLineVariable varLine)
        {
            // create choosen time window
            Time timeWindowLowerBound = new Time(TimeWindowLowerBoundHour, 0);

            // calculate in minutes

            // normalize with respect to the choosen window
            int shift = varLine.Line.OriginalDeparture.ToMinutes() - timeWindowLowerBound.ToMinutes();

            // move it into that window
            while (shift < 0)
            {
                shift += (int)varLine.Period;
            }
            while (shift >= (int)varLine.Period)
            {
                shift -= (int)varLine.Period;
            }

            // store the relative
            varLine.Line.ConnectedLineShift = Time.ToTime(shift);
        }
Пример #8
0
        /// <summary>
        /// Updates the connected train line variables.
        /// </summary>
        /// <param name="varLines">The variable lines.</param>
        private void updateConnectedTrainLineVariables(List <TrainLineVariable> varLines)
        {
            // loop over all varLines
            foreach (TrainLineVariable varLine in varLines)
            {
                // update (set) relative shift within equivalent group
                updateConnectedLineShift(varLine);

                // loop over all connected line for line related with varLine
                foreach (TrainLine line in varLine.Line.getConnectedLines())
                {
                    // update the connections
                    if (line.LineNumber < varLine.LineNumber)
                    {
                        // find appropriate varLine
                        TrainLineVariable connectedVariableLine = findVariableLine(varLines, line.LineNumber);

                        // add this var line into list of related var line
                        connectedVariableLine.ConnectedLinesVariable.Add(varLine);
                        varLine.ConnectedLinesVariable.Add(connectedVariableLine);
                    }
                }
            }
        }
Пример #9
0
 public Change(TrainLineVariable variable, Time oldTime)
     : this()
 {
     this.changedVariable = variable;
     this.oldStartTime    = oldTime;
 }
Пример #10
0
        /// <summary>
        /// Generates the timetable.
        /// </summary>
        /// <returns></returns>
        public Timetable generateTimetable()
        {
            // generate randomizedTimetable
            Timetable timetable = generateRandomizedTimetable();
            // createConstraintSet temporary holder for AvailableLines during calculation
            List <TrainLineVariable> availableLines = cloneVariableLines(timetable.getVariableLines());
            // createConstraintSet temporary holder for AvailableLines during calculation
            List <TrainLineVariable> stableLines = new List <TrainLineVariable>();

            // random class for randomizing choice of trainlines
            Random random = new Random();

            // local variables
            //int theBestTimetableRatingValue = int.MaxValue;
            //int progressiveChanges = 0;

            // Find the best new state.
            CurrentState state = new CurrentState(new List <Change>(), int.MaxValue);

            // loop until if availableLines are not empty AND stableLine are not complete
            while (!availableLines.Count.Equals(0) &&
                   !stableLines.Count.Equals(timetable.TrainLines.Count))
            {
                Boolean improved = false;

                // choose available line randomly
                TrainLineVariable selectedLine = availableLines[random.Next(0, availableLines.Count - 1)];

                // loop over whole interval of line
                for (int i = 0; i < (int)selectedLine.Period; i++)
                {
                    // calculate transfers
                    CurrentState newState = calculateTransfers(timetable, selectedLine, Time.ToTime(i));
                    if (newState.Factor > state.Factor)
                    {
                        // If the current one is better forget the computed one.
                        newState.Revert();
                    }
                    else if (newState.Factor != state.Factor)
                    {
                        // The computed one should be preserved.
                        state    = newState;
                        improved = true;
                    }
                }

                // If the state was improved.
                if (improved)
                {
                    selectedLine.ProgressiveChanges += 1;
                    // clear stableLines queue
                    stableLines.Clear();
                    // reset availableLines
                    availableLines = cloneVariableLines(timetable.getVariableLines());
                }

                // add selected line into list of stableLines
                stableLines.Add(selectedLine);
                // add also all connected lines with selected line
                stableLines.AddRange(selectedLine.ConnectedLinesVariable);
                // remove line off availableLines
                availableLines.Remove(selectedLine);
                // remove all connected lines with selected line
                foreach (TrainLineVariable connectedVar in selectedLine.ConnectedLinesVariable)
                {
                    availableLines.Remove(connectedVar);
                }
            }

            timetable.RatingValue = state.Factor;
            //int ratingValue = Timetable.calculateTimetableRatingValue(timetable);
            timetable.calculateProgressiveChanges();

            //LogUtil.printTimetableTransfersEvaluation(timetable, FinalInput.getInstance().getCacheContent());

            return(timetable);
        }