public static void readSchedule() { var fileStream = scheduleFilename; using (StreamReader sr = new StreamReader(scheduleFilename, System.Text.Encoding.Default)) { string line; while ((line = sr.ReadLine()) != null) { string[] items = line.Split(new char[] { ';' }); DateTime application_time = new DateTime(); string[] time = items[0].Split(new char[] { ':' }); application_time = application_time.AddHours(int.Parse(time[0])); application_time = application_time.AddMinutes(int.Parse(time[1])); bool is_arriving = true; if (items[4] == "DEPARTURE") { is_arriving = false; } durations required_time_interval = new durations(); foreach (durations t in timeDurations) { if (items[3].StartsWith(t.type)) { required_time_interval = t; } } //items[0] - время; items[1] - номер рейса; items[2] - авиакомпаниия; items[3] - тип самолета; items[4] - отправление или посадка airplanes.Add(new Airplane(application_time, items[1], items[2], is_arriving, required_time_interval)); } } Airplane.generateDelays(); }
public static bool isOkShedule() { // если в какой-то момент времени нужно больше свободных полос, чем существует, то вернет ложь DateTime time = currentTime; do { int count = 0; foreach (Airplane airplane in airplanes) { if (airplane.isOnRunway(time)) { count++; } } if (count > runwaysAmount) { return(false); } time = time.AddMinutes(GenerateDelay.integral_step); }while (time < currentTime.AddDays(1)); // тут надо сразу распихать по полосам самолеты, они отсортированы по времени for (int i = 1; i <= runwaysAmount; i++) { runways.Add(i, new Runway()); } foreach (Airplane plane in airplanes) { DateTime begin = new DateTime(2, 1, plane.applicationTime.Day, plane.applicationTime.Hour, plane.applicationTime.Minute, 0, 0); DateTime end = begin.AddMinutes(plane.getRequiredTimeInterval()); for (int i = 1; i <= runwaysAmount; i++) { if (runways[i].countAirplanes() == 0) { Airplane now_plane = new Airplane(begin, plane.flight, plane.companyName, plane.isArriving, plane.timeIntervals, i); now_plane.runwayNumber = i; plane.runwayNumber = i; runways[i].addAirplane(begin, now_plane); break; } else if (runways[i].is_free(begin, end)) { Airplane now_plane = new Airplane(begin, plane.flight, plane.companyName, plane.isArriving, plane.timeIntervals, i); now_plane.runwayNumber = i; plane.runwayNumber = i; runways[i].addAirplane(begin, now_plane); break; } } } Airplane.generateDelays(); return(true); }
public static void generateSchedule() { // выдает только заведомо выполнимое расписание // важными являются только время подачи заявки, тип самолета и уникальный номер рейса // сначала выберем количество самолетов того типа, что занимает полосу дольше всех runways.Clear(); airplanes.Clear(); List <Airplane> ranges = new List <Airplane>(); DateTime time = currentTime; foreach (durations d in timeDurations) { Airplane plane1 = new Airplane(time, Convert.ToString(ranges.Count()), "A_Airlines", true, d, -2); if (ranges.Count() == 0) { ranges.Add(plane1); } else { int j = 0; while ((ranges[j].isLonger(plane1)) && (j < ranges.Count())) { j++; } ranges.Insert(j, plane1); } Airplane plane2 = new Airplane(time, Convert.ToString(ranges.Count()), "D_Airlines", false, d, -3); int i = 0; while (ranges[i].isLonger(plane2)) { i++; } ranges.Insert(i, plane2); } // теперь имеем отсортированный по убыванию список, содержащий все различные варианты самолетов int free_time = runwaysAmount * 24 * 60; // 24 часа List <Airplane> rand_airplanes = new List <Airplane>(); foreach (Airplane plane in ranges) { int max_frequency = free_time / plane.getRequiredTimeInterval(); int amount = rnd.Next(0, max_frequency + 1); // рандомно решаем, сколько самолетов такого типа будет free_time -= amount * plane.getRequiredTimeInterval(); while (amount > 0) { rand_airplanes.Add(plane); amount--; } } // нашли набор самолетов, с которыми возможно составить рабочее расписание // теперь надо растолкать их по полосам for (int i = 1; i <= runwaysAmount; i++) { runways.Add(i, new Runway()); } for (int i = 0; i < rand_airplanes.Count(); i++) { DateTime begin = new DateTime(2, 1, 1, currentTime.Hour, currentTime.Minute, 0, 0); DateTime next_day = new DateTime(2, 1, 2, currentTime.Hour, currentTime.Minute, 0, 0); DateTime end; // рандомно ищем временной интервал [begin; end) для данного самолета внутри тех 24 часов, на которых моделируем do { int mins = (rnd.Next(0, 60 * 24 / GenerateDelay.integral_step)) * GenerateDelay.integral_step; // домножаем на шаг, который есть минимальная единица измерения времени в программе (5 минут) if (mins != 0) { begin = new DateTime(2, 1, 1, currentTime.Hour, currentTime.Minute, 0, 0); begin = begin.AddMinutes(mins); } end = begin.AddMinutes(rand_airplanes[i].getRequiredTimeInterval()); } while (end >= next_day); // с end до begin находится нужный временной интервал для rand_airplanes for (int j = 1; j <= runwaysAmount; j++) { if (runways[j].countAirplanes() == 0) { Airplane now_plane = new Airplane(begin, "SB " + rand_airplanes[i].flight + i.ToString() + j.ToString(), rand_airplanes[i].companyName, rand_airplanes[i].isArriving, rand_airplanes[i].timeIntervals, j); now_plane.runwayNumber = j; airplanes.Add(now_plane); runways[j].addAirplane(begin, now_plane); break; } if (runways[j].is_free(begin, end)) { Airplane now_plane = new Airplane(begin, "SB " + rand_airplanes[i].flight + i.ToString() + j.ToString(), rand_airplanes[i].companyName, rand_airplanes[i].isArriving, rand_airplanes[i].timeIntervals, j); now_plane.runwayNumber = j; airplanes.Add(now_plane); runways[j].addAirplane(begin, now_plane); break; } if (j == runwaysAmount) { begin = begin.AddMinutes(GenerateDelay.integral_step); begin = new DateTime(2, 1, begin.Day, begin.Hour, begin.Minute, 0, 0); end = end.AddMinutes(GenerateDelay.integral_step); j = 0; if (end >= next_day) { break; } } } } airplanes.Sort(CompareAirplanesForSchedule); using (StreamWriter wr = new StreamWriter(scheduleFilename)) { foreach (Airplane airplane in airplanes) { string status = "DEPARTURE"; if (airplane.isArriving) { status = "ARRIVAL"; } wr.WriteLine("{0};{1};{2};{3};{4}", airplane.applicationTime.ToShortTimeString(), airplane.flight, airplane.companyName, airplane.type, status); } } Airplane.generateDelays(); }