/// <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); }
public static Time operator +(Time time1, Time time2) { int min1 = time1.ToMinutes(); int min2 = time2.ToMinutes(); return(Time.ToTime(min1 + min2)); }
public static Time normalizeTime(Time time, Period period) { int minutes = time.ToMinutes(); minutes %= (int)period; if (minutes < 0) { minutes += (int)period; } return(Time.ToTime(minutes)); }
public static Time operator -(Time time1, Time time2) { Time newTime = new Time(); if (time1 >= time2) { int min1 = time1.ToMinutes(); int min2 = time2.ToMinutes(); newTime = Time.ToTime(min1 - min2); } return(newTime); }
private void saveStopsInformation() { foreach (ListViewItem lvi in listViewListOfStops.Items) { // if there where changes of the train stop, save it if (lvi.Tag.Equals(CHANGED)) { // train stop at order int order = Convert.ToInt32(lvi.SubItems[1].Text); TrainStop stop = trainLine.getTrainStopOrderedAt(order); stop.TimeStayingAtStation = Time.ToTime(lvi.SubItems[4].Text); stop.TimeFromPreviousStop = Time.ToTime(lvi.SubItems[5].Text); stop.KmFromPreviousStop = Convert.ToInt32(lvi.SubItems[6].Text); } } this.trainLine.updateRelativeTrainStopInformation(); }
/// <summary> /// Reads station's information from file. /// </summary> /// <returns>The stations.</returns> public static List <StationDetail> readTrainStationFromFile(String fileName) { const int MANDATORY = 2; const int ALL = 4; List <String[]> listOfStrings = readFromFile(fileName); List <StationDetail> stationDetails = new List <StationDetail>(); if (listOfStrings == null) { return(null); } if (listOfStrings.Count.Equals(0)) { return(stationDetails); } // loop over all items in list of strings foreach (String[] strings in listOfStrings) { //if there is only 2 items if (strings.Length.Equals(MANDATORY)) { stationDetails.Add(new StationDetail(strings[0], Convert.ToInt32(strings[1]))); } else if (strings.Length.Equals(ALL)) { StationDetail details = new StationDetail(strings[0], Convert.ToInt32(strings[1])); if (strings[2] != "") { details.MinimalTransferTime = Time.ToTime(Convert.ToInt32(strings[2])); } if (strings[3] != "") { details.Town = strings[3]; } stationDetails.Add(details); } } return(stationDetails); }
/// <summary> /// Normalizes the transfer time. /// </summary> /// <param name="timeDeparture">The time departure.</param> /// <param name="timeArrival">The time arrival.</param> /// <param name="minimalTransferTime">The minimal transfer time.</param> /// <param name="onPeriod">The on period.</param> /// <param name="offPeriod">The off period.</param> private static void normalizeTransferTime(ref Time timeDeparture, ref Time timeArrival, Time minimalTransferTime, int onPeriod, int offPeriod) { // if departure is before arrival, we need to find closest time departure // that satisfied the condition(departure>arrival) if (timeDeparture < timeArrival + minimalTransferTime) { while (timeDeparture < timeArrival + minimalTransferTime) { // addConstraint new period of train timeDeparture += Time.ToTime(onPeriod); } } else { // if arrival is before departure, we need to find closest time arrival // that satisfied the cond(departure>arrival) but not the cond(departure>arrival+nextPeriod) while (timeDeparture > timeArrival + Time.ToTime(offPeriod) + minimalTransferTime) { timeArrival += Time.ToTime(offPeriod); } } }
/// <summary> /// Randomizes the timetable. /// Set random strat time for all train lines of timetable. /// </summary> public void randomizeTimetable() { DateTime now = DateTime.Now; // output for controlling random numbers //FileStream fs = new FileStream("randomNumbers" + now.Month + now.Day + now.Hour + now.Minute + ".tmp", FileMode.Create); //StreamWriter sw = new StreamWriter(fs); foreach (TrainLineVariable line in variableLines) { int randomInt = random.Next(0, (int)line.Period); Time time = Time.ToTime(randomInt); line.StartTime = time; // controll information // sw.WriteLine(String.Format("{0,8}", time)); } //sw.Close(); //fs.Close(); }
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); }
private void saveDetailsOfStation() { // if the value of inhabitation was changed if (inhabitationValueChanged()) { // set up the new value of inhabitation trainStation.Inhabitation = Convert.ToInt32(textBoxInhabitation.Text); // and according that update townCategory trainStation.updateTownCategory(); } // otherwise if category was changed else if (townCategoryValueChanged()) { // set up the new value of townCategory trainStation.TownCategory = (TownCategory)comboBoxCategory.SelectedValue; // and according that update value of inhabitation trainStation.updateInhabitation(); } // save information about the town trainStation.Town = textBoxTown.Text; trainStation.MinimalTransferTime = Time.ToTime(textBoxMinimalTransferTime.Text); }
/// <summary> /// Creates the train stops. /// </summary> /// <param name="data">The data.</param> /// <param name="line">The line.</param> /// <returns>The train stops.</returns> private static List <TrainStop> createTrainStops(List <String[]> data, TrainLine line, out Time originalDepartureTime) { List <TrainStop> trainStops = new List <TrainStop>(); TrainStationCache stationCache = TrainStationCache.getInstance(); Int16 orderInLine = 0; String nameStation; Time departure; Time arrival; int kmFromStart; Time timeStart = Time.ToTime(data[0][2]);//Time.MinValue; // set original departure time originalDepartureTime = new Time(timeStart); Time timePrev = Time.MinValue; int kmPrev = 0; // foreach string[] createConstraintSet specific trainStop foreach (String[] str in data) { // if not correct number of stop details, take next stop if (str.Length != NUMBER_OF_STOP_DETAILS) { continue; } // createConstraintSet new stop TrainStop stop = new TrainStop(); // extract information about stop nameStation = str[0]; arrival = Time.ToTime(str[1]); departure = Time.ToTime(str[2]); kmFromStart = Convert.ToInt32(str[3]); // try toStation find out if throughStation already exists, if not, createConstraintSet appropriate throughStation if (!stationCache.doesStationExist(nameStation)) { // calculate new ID for new throughStation int id = stationCache.getCacheContent().Count; stationCache.addTrainStation(new TrainStation(id, nameStation)); // download new throughStation //stationCache = } // find appropriate throughStation TrainStation trainStation = stationCache.getCacheContentOnName(nameStation); // addConstraint linked line_ toStation throughStation trainStation.addTrainLine(line); stop.KmFromPreviousStop = kmFromStart - kmPrev; stop.KmFromStart = kmFromStart; stop.OrderInTrainLine = orderInLine; stop.TrainStation = trainStation; stop.TimeArrival = arrival - timeStart; stop.TimeDeparture = departure - timeStart; // if no arrival time_, count with departure if (arrival.Equals(Time.MinValue)) { stop.TimeFromPreviousStop = stop.TimeDeparture - timePrev; } //if arrival time_, use it else { stop.TimeFromPreviousStop = stop.TimeArrival - timePrev; } // set time_ and km for next stop timePrev = stop.TimeDeparture; kmPrev = stop.KmFromStart; orderInLine++; // addConstraint into final stops trainStops.Add(stop); } return(trainStops); }
/// <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); }