//! Calculate number of Conflicts in the schedule /*! * \param ContactWindowVector schedule to calculate fairness * \return int number of overall conflicting contact windows */ public static int getNrOfConflicts(ContactWindowsVector contacts) { int nrOfConflicts = 0; HashSet <Guid> hashConflict = new HashSet <Guid>(); for (int i = 0; i < contacts.Count(); i++) { for (int k = 0; k < contacts.Count(); k++) { if (i != k && contacts.getAt(i).getSheduledInfo() && contacts.getAt(k).getSheduledInfo() && contacts.getAt(i).checkConflikt(contacts.getAt(k))) { if (contacts.getAt(k).getSatName() == contacts.getAt(i).getSatName() || contacts.getAt(k).getStationName() == contacts.getAt(i).getStationName()) { if (!hashConflict.Contains(contacts.getAt(k).getID())) { nrOfConflicts++; hashConflict.Add(contacts.getAt(i).getID()); } } } } } hashConflict.Clear(); return(nrOfConflicts); }
//! Fill Gaps in finisched Schedule /*! * this funcions will run through the final solution and * adds contact windows to the schedule were there is still * space and time. Thus filling in empty spaces were contacts * can be made */ private void fillGaps() { for (int i = 0; i < result.Count(); i++) { if (!result.getAt(i).getSheduledInfo()) { bool found = false; for (int k = 0; k < result.Count(); k++) { if (set.getAt(i).checkConflikt(set.getAt(k)) && result.getAt(k).getSheduledInfo() && i != k) { if (set.getAt(i).getSatName() == set.getAt(k).getSatName() || set.getAt(i).getStationName() == set.getAt(k).getStationName()) { found = true; break; } } } if (!found) { result.getAt(i).setSheduled(); } } } }
//! Solve Conflicts by Priority /*! * this function will go throuh all conflicting elements that have * ben scheduled by the scheduler and will remove conflicting elements * with lower priority (None - Critical -> 4 - 0) */ public void solveConflictsByPriority() { if (schedule != null) { for (int i = 0; i < schedule.Count(); i++) { for (int k = 0; k < schedule.Count(); k++) { int maxPriority = (int)schedule.getAt(i).getPriority(); if (schedule.getAt(i).getSheduledInfo() && k != i && schedule.getAt(k).getSheduledInfo() && schedule.getAt(k).getStationName() == schedule.getAt(i).getStationName() && schedule.getAt(i).checkConflikt(schedule.getAt(k))) { if ((int)schedule.getAt(k).getPriority() < maxPriority) { schedule.getAt(i).unShedule(); break; } else { schedule.getAt(k).unShedule(); break; } } } } } }
private int calcualteMaxPrioValue(ContactWindowsVector contacts, int[] population = null) { int[] priorityCounts = new int[5] { 0, 0, 0, 0, 0, }; int res = 0; for (int i = 0; i < contacts.Count(); i++) { int p = 0; if (population != null) { if (population[i] == 1) { p = (int)contacts.getAt(i).getPriority(); } } else { p = (int)contacts.getAt(i).getPriority(); } priorityCounts[p]++; } for (int i = 0; i < priorityCounts.Count(); i++) { res += priorityCounts[i] * (5 - i); } return(res); }
public static double getDurationOfScheduledContacts(ContactWindowsVector contacts) { double duration = 0.0; for (int i = 0; i < contacts.Count(); i++) { if (contacts.getAt(i).getSheduledInfo()) { duration += contacts.getAt(i).getDuration(); } } return(duration); }
//! Set the finall schedule /*! * \param int[] current populus * \param ContactWindowsVector starting contactwindows * Each contact windows will be scheduled or unscheduled defined by the found solution */ private void setSchedule(int[] pop, ContactWindowsVector contacts) { for (int i = 0; i < nrOfContacts; i++) { if (pop[i] == 1) { contacts.getAt(i).setSheduled(); } else { contacts.getAt(i).unShedule(); } } }
//Test Function public static string getNrOfUweContacts(ContactWindowsVector contacts) { int count = 0; for (int i = 0; i < contacts.Count(); i++) { if (contacts.getAt(i).getSheduledInfo() && contacts.getAt(i).getSatName() == "UWE-3") { count++; } } return("UWE-3: " + count); }
private void fillContacts(ContactWindowsVector contacts) { for (int i = 0; i < contacts.Count(); i++) { bool confilcts = false; if (!contacts.getAt(i).getSheduledInfo()) { for (int j = 0; j < contacts.Count(); j++) { if (contacts.getAt(j).getSheduledInfo() && i != j && contacts.getAt(i).checkConflikt(contacts.getAt(j))) { if (contacts.getAt(i).getStationName() == contacts.getAt(j).getStationName() || contacts.getAt(i).getSatName() == contacts.getAt(j).getSatName()) { confilcts = true; break; } } } } if (!confilcts) { contacts.getAt(i).setSheduled(); } } }
//! Calculates a schedule from the defined problem /*! * \pram ScheduleProblemInterface defined problem with contactwindows * This Function will calculate the solution to the problem defined in * Schedule Problem Interface */ public void CalculateSchedule(ScheduleProblemInterface problem) { set = problem.getContactWindows(); schedule = new ContactWindowsVector(); set.sort(Structs.sortByField.TIME); while (!isComplete()) { for (int i = 0; i < set.Count(); i++) { bool collisionFound = false; List <int> collisionSet = new List <int>(); collisionSet.Add(i); for (int j = 1; j < set.Count() - 1; j++) { collisionFound = checkCollision(set.getAt(i), set.getAt(j)); collisionSet.Add(j); } if (collisionFound) { set.getAt(i).setSheduled(); for (int k = 0; k < collisionSet.Count - 1; k++) { set.getAt(collisionSet[k]).unShedule(); } } else { schedule.add(set.getAt(i)); set.deleteAt(i); i--; } } } //retrive all the contactwindows that need to be scheduled //ContactWindowsVector set = problem.getContactWindows(); //Scheduler Magic until is Complete returns true //No Element of the ContactWindowsVector set should be deleted //To Schedule a item call set.getAt(index).setSheduled() //To Unschedule a item call set.getAt(index).unShedule() }
//! Calculates a schedule from the defined problem /*! * \pram ScheduleProblemInterface defined problem with contactwindows * Greedy approach earliest job first. */ public void CalculateSchedule(ScheduleProblemInterface problem) { set = problem.getContactWindows(); ContactWindowsVector set1 = new ContactWindowsVector(); ContactWindowsVector set2 = new ContactWindowsVector(); List <string> staName = set.getStationNames(); //while set is not empty do while (!isComplete()) { //loop through all Stations for (int i = 0; i < staName.Count(); i++) { int pos = -1; double earliest = 9999.99; int priority = 4; //loop through all Contacts and find the item that finisches //first and has the highest priority. for (int k = 0; k < set.Count(); k++) { if (set.getAt(k).getStationName() == staName[i]) { if (set.getAt(k).getStopTime().getEpoch() < earliest && (int)set.getAt(k).getPriority() <= priority) { pos = k; earliest = set.getAt(k).getStopTime().getEpoch(); priority = (int)set.getAt(k).getPriority(); } } } //update Progress Bar on Main Form if (Properties.Settings.Default.global_MaxPerf == false) { System.Windows.Forms.Application.DoEvents(); } if (f != null) { f.incrementProgressBar(); } //the found earliest job is added to set1 if its empty //or no other contact in set1 is conflicting with it //if there is a conflict add this element to set2 //Then the element is deleted from set1 if (pos > -1) { if (set1.isEmpty()) { set1.add(set.getAt(pos)); set.deleteAt(pos); } else { bool found = false; for (int k = 0; k < set1.Count(); k++) { if (set.getAt(pos).checkConflikt(set1.getAt(k))) { if (set.getAt(pos).getSatName() == set1.getAt(k).getSatName()) { set2.add(set.getAt(pos)); set.deleteAt(pos); found = true; break; } } } if (!found) { set1.add(set.getAt(pos)); set.deleteAt(pos); } } } } } for (int i = 0; i < set1.Count(); i++) { set1.getAt(i).setSheduled(); } for (int i = 0; i < set2.Count(); i++) { set2.getAt(i).unShedule(); } set.add(set1); set.add(set2); schedule = set; solveConflictsByPriority(); if (f != null) { f.resetProgressBar(); } }
//! Solve Conflict and all collisions by random /*! * this funcions checks all contacts that are included in the * current population and removes conflicting ones, either by * priority or by random chance */ private void surviveConflicts(int[] pop, Random ran, bool piroity = false) { for (int i = 0; i < nrOfContacts; i++) { if (pop[i] == 1) { List <int> conflicting = new List <int>(); conflicting.Add(i); int startItem = i - (nrOfSatellites * nrOfStation) / 2; if (startItem < 0) { startItem = 0; } int stopItem = i + (nrOfSatellites * nrOfStation) / 2; if (stopItem > nrOfContacts) { stopItem = nrOfContacts; } for (int k = startItem; k < stopItem; k++) { if (pop[k] == 1 && k != i && set.getAt(i).checkConflikt(set.getAt(k))) { if (set.getAt(i).getSatName() == set.getAt(k).getSatName() || set.getAt(i).getStationName() == set.getAt(k).getStationName()) { conflicting.Add(k); } } } if (conflicting.Count > 1) { if (piroity) { int maxPrio = 4; int posMax = 0; for (int l = 0; l < conflicting.Count; l++) { if ((int)set.getAt(conflicting[l]).getPriority() <= maxPrio) { maxPrio = (int)set.getAt(conflicting[l]).getPriority(); posMax = conflicting[l]; pop[conflicting[l]] = 0; } } pop[posMax] = 1; } else { for (int l = 0; l < conflicting.Count; l++) { pop[conflicting[l]] = 0; } pop[conflicting[ran.Next(0, conflicting.Count - 1)]] = 1; } } } } }
//! returns the number of contacts for each priority /*! * \param ContactWindowVector schedule to calculate fairness * \return string containing number of contacts schedule for each priorty */ public static string getNrOfPrioritysScheduled(ContactWindowsVector contacts) { int p0 = 0; int p1 = 0; int p2 = 0; int p3 = 0; int p4 = 0; int sp0 = 0; int sp1 = 0; int sp2 = 0; int sp3 = 0; int sp4 = 0; for (int i = 0; i < contacts.Count(); i++) { int p = (int)contacts.getAt(i).getPriority(); switch (p) { case 0: p0++; break; case 1: p1++; break; case 2: p2++; break; case 3: p3++; break; case 4: p4++; break; } if (contacts.getAt(i).getSheduledInfo()) { switch (p) { case 0: sp0++; break; case 1: sp1++; break; case 2: sp2++; break; case 3: sp3++; break; case 4: sp4++; break; } } } return(sp0 + "/" + p0 + " - " + sp1 + "/" + p1 + " - " + sp2 + "/" + p2 + " - " + sp3 + "/" + p3 + " - " + sp4 + "/" + p4); }
//! Calculate scheduling solution from certain startpoint /*! * calculate a schedule from a certain starting point * if iterating trhough every contact window can be used to brute force * the scheduling solution * \param ScheduleProblemInterface to solve scheduling problem * \param int point from which to start from */ public void BruteForceSchedule(ScheduleProblemInterface problem, int step) { objective = problem.getObjectiveFunction(); set = problem.getContactWindows(); int nrOfAllContacts = set.Count(); ContactWindowsVector set1 = new ContactWindowsVector(); ContactWindowsVector set2 = new ContactWindowsVector(); //double maxFitness = 0.0; int count = 0; set1.add(set.getAt(step)); set.deleteAt(step); while (!isComplete()) { int pos = -1; double maxFitness = 0.0; for (int i = 0; i < set.Count(); i++) { objective.calculateValues(set1, set, nrOfAllContacts, set.getAt(i)); double fitness = objective.getObjectiveResults(); if (fitness > maxFitness) { maxFitness = fitness; pos = i; } if (Properties.Settings.Default.global_MaxPerf == false) { System.Windows.Forms.Application.DoEvents(); } } bool found = false; if (pos >= 0) { for (int i = 0; i < set1.Count(); i++) { if (set.getAt(pos).checkConflikt(set1.getAt(i))) { if (set.getAt(pos).getSatName() == set1.getAt(i).getSatName() || set.getAt(pos).getStationName() == set1.getAt(i).getStationName()) { set2.add(set.getAt(pos)); set2.getLast().unShedule(); set.deleteAt(pos); found = true; break; } } if (Properties.Settings.Default.global_MaxPerf == false) { System.Windows.Forms.Application.DoEvents(); } } if (!found) { set1.add(set.getAt(pos)); set1.getLast().setSheduled(); set.deleteAt(pos); } } else { count++; } } set.add(set1); set.add(set2); schedule = set; }
//! Remove unwanted contact windows /*! * \param int Min. Duration window * Deletes all contact windows whitch contact times are lower then min duration */ public void removeUnwantedContacts(int minDuration) { for (int i = 0; i < schedulerContacts.Count(); i++) { if (schedulerContacts.getAt(i).getDuration() < minDuration) { schedulerContacts.deleteAt(i); i--; } } }
//! Calculates a schedule from the defined problem /*! * \pram ScheduleProblemInterface defined problem with contactwindows * This Function will calculate the solution to the problem defined in * Schedule Problem Interface */ public void CalculateSchedule(ScheduleProblemInterface problem) { objective = problem.getObjectiveFunction(); result = problem.getContactWindows(); if (adaptiveMaxIterations) { maxNumberOfIteration = result.Count() * 4; } if (randomStart) { result.randomize(); fillContacts(result); } if (mainform != null) { mainform.setProgressBar(maxNumberOfIteration); } currentFitness = 0.0; result.getAt(0).setSheduled(); while (!isComplete()) { for (int i = 0; i < result.Count(); i++) { iterations++; for (int j = 0; j < result.Count(); j++) { if (i != j && result.getAt(i).checkConflikt(result.getAt(j))) { if (result.getAt(i).getStationName() == result.getAt(j).getStationName() || result.getAt(i).getSatName() == result.getAt(j).getSatName()) { //collision detected result.getAt(i).unShedule(); result.getAt(j).setSheduled(); double newFitness = getFitness(result); if (newFitness < currentFitness) { result.getAt(j).unShedule(); result.getAt(i).setSheduled(); } else { currentFitness = newFitness; break; } } } } } if (Properties.Settings.Default.global_MaxPerf == false) { System.Windows.Forms.Application.DoEvents(); } if (mainform != null) { mainform.updateProgressBar(iterations); } currentFitness = getFitness(result); fillContacts(result); } }
//! Draw Conatact Windows. /*! * \param ContactWindowsVector Contacts * \param bool Draw All if false only scheduled contacts will be drawn * \return Image bmp-Image * Creates a bmp Image and returns it */ public static Image drawContacts(ContactWindowsVector contacts, bool drawAll) { if (contacts != null) { //sort by Groundstations contacts.sort(Structs.sortByField.GROUNDSTATION); List <string> satNameList = contacts.getSatelliteNames(); List <string> staNameList = contacts.getStationNames(); int x_offset = 100; int y_offset = 20; int satBoxheight = 20; //calculate Size of Image double timeLength = contacts.getStopTime().getEpoch() - contacts.getStartTime().getEpoch(); int imWidth = Convert.ToInt32(timeLength = 8640 * timeLength); int imHeight = satNameList.Count() * staNameList.Count() * 20; Image dest = new Bitmap(imWidth + x_offset, imHeight + y_offset); //Generate Front and Brusch for drawing System.Drawing.Graphics g; System.Drawing.Pen pen = new System.Drawing.Pen(Color.Black, 1F); Font drawFont = new Font("Arial", 10); SolidBrush brush = new SolidBrush(Color.Black); g = Graphics.FromImage(dest); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; //Start Drawing Contact Windows for (int i = 0; i < contacts.Count(); i++) { //Draw only if scheduled unless drawAll = true if (contacts.getAt(i).getSheduledInfo() || drawAll) { int satPosY = satNameList.IndexOf(contacts.getAt(i).getSatName()); int staPosY = staNameList.IndexOf(contacts.getAt(i).getStationName()); int y = 0; int x = 0; int satBoxhWidth = 0; y = satPosY * 20 + staPosY * satNameList.Count() * 20 + y_offset; double satx = 8640 * (contacts.getAt(i).getStartTime().getEpoch() - contacts.getStartTime().getEpoch()); x = Convert.ToInt32(satx) + x_offset; satBoxhWidth = Convert.ToInt32(contacts.getAt(i).getDuration() / 10.0); //brush = new SolidBrush(Color.FromArgb(contacts.getAt(i).getHash())); if (drawAll && !contacts.getAt(i).getSheduledInfo()) { brush = new SolidBrush(Color.FromArgb(215, 215, 215)); g.FillRectangle(brush, new Rectangle(x, y, satBoxhWidth, satBoxheight)); g.DrawString(contacts.getAt(i).getSatName(), drawFont, new SolidBrush(Color.DarkGray), x, y); } else { brush = new SolidBrush(Color.FromArgb(contacts.getAt(i).getHash())); g.FillRectangle(brush, new Rectangle(x, y, satBoxhWidth, satBoxheight)); g.DrawString(contacts.getAt(i).getSatName(), drawFont, new SolidBrush(Color.Black), x, y); } } } System.Drawing.Pen stationPen; stationPen = new System.Drawing.Pen(Color.DarkGray, 1); g.DrawLine(stationPen, x_offset, 0, x_offset, imHeight); //Start Drawing Stations Names for (int i = 0; i < staNameList.Count(); i++) { int x1 = 0; int y1 = (i + 1) * satNameList.Count() * 20; int x2 = imWidth; int y2 = (i + 1) * satNameList.Count() * 20; g.DrawString(staNameList[i], drawFont, new SolidBrush(Color.Black), 5, y1 - ((satNameList.Count() / 2) * 20)); g.DrawLine(stationPen, x1, y1 + y_offset, x2, y2 + y_offset); } One_Sgp4.EpochTime time = new One_Sgp4.EpochTime(contacts.getStartTime()); g.DrawString(contacts.getStartTime().ToString(), drawFont, new SolidBrush(Color.Black), x_offset, 5); for (int i = x_offset; i < imWidth; i += 180) { g.DrawString(time.ToString(), drawFont, new SolidBrush(Color.Black), i, 5); g.DrawLine(stationPen, i, 0, i, imHeight); time.addTick(10 * 180); } //g.Dispose(); drawFont.Dispose(); brush.Dispose(); pen.Dispose(); dest.Save("Contacts.bmp"); return(dest); } else { int imWidth = 8640; int imHeight = 20; Image dest = new Bitmap(imWidth, imHeight); return(dest); } }
//! calculate the fitness value of ALL defined objetive values /*! * \param ContactWindowsVector contact windows to check * \param int max number of Contacts of the Scheduling problem * \param int[] population representation of the Genetic scheduler default NULL */ private void calculate(ContactWindowsVector contactWindows, int priorityValue, int nrOfAllContacts = 0, int[] population = null, ContactWindowsVector allcontactWindows = null) { //list of stations and Satellites List <string> stationList = contactWindows.getStationNames(); List <string> satelliteList = contactWindows.getSatelliteNames(); //number of times sation and satellite is scheduled int nrOfStation = contactWindows.getNumberOfStation(); int nrOfSatellites = contactWindows.getNumberOfSatellites(); // int[] nrOfContactsPerSatellite = new int[nrOfSatellites]; int[] nrOfContactsPerStation = new int[nrOfStation]; //number of Contacts Scheduled int nrOfScheduledContacts = 0; //Overall Scheduled Time double scheduledDuration = 0.0; //Complete Time of ALL contacts double allDuaration = 0.0; //Priority counts int priorityMax = priorityValue; int prio = calcualteMaxPrioValue(contactWindows); for (int i = 0; i < contactWindows.Count(); i++) { int stapo = -1; int satpo = -1; if (population != null) { if (population[i] == 1) { stapo = stationList.IndexOf(contactWindows.getAt(i).getStationName()); satpo = satelliteList.IndexOf(contactWindows.getAt(i).getSatName()); nrOfScheduledContacts++; scheduledDuration += contactWindows.getAt(i).getDuration(); } } else { if (nrOfAllContacts == 0) { if (contactWindows.getAt(i).getSheduledInfo()) { stapo = stationList.IndexOf(contactWindows.getAt(i).getStationName()); satpo = satelliteList.IndexOf(contactWindows.getAt(i).getSatName()); scheduledDuration += contactWindows.getAt(i).getDuration(); nrOfScheduledContacts++; } } else { stapo = stationList.IndexOf(contactWindows.getAt(i).getStationName()); satpo = satelliteList.IndexOf(contactWindows.getAt(i).getSatName()); scheduledDuration += contactWindows.getAt(i).getDuration(); nrOfScheduledContacts++; } } if (allcontactWindows == null) { allDuaration += contactWindows.getAt(i).getDuration(); } if (stapo > -1) { nrOfContactsPerStation[stapo]++; } if (satpo > -1) { nrOfContactsPerSatellite[satpo]++; } } //calculate Fairness for Stations double a_station = 0.0; double b_station = 0.0; for (int i = 0; i < nrOfStation; i++) { a_station = a_station + nrOfContactsPerStation[i]; b_station = b_station + Math.Pow(nrOfContactsPerStation[i], 2); } val_FairStations = Math.Pow(a_station, 2) / (nrOfStation * b_station); //calculate Fairnes for Satellites double a_satellites = 0.0; double b_satellites = 0.0; for (int i = 0; i < nrOfSatellites; i++) { a_satellites = a_satellites + nrOfContactsPerSatellite[i]; b_satellites = b_satellites + Math.Pow(nrOfContactsPerSatellite[i], 2); } val_FairSatellites = Math.Pow(a_satellites, 2) / (nrOfSatellites * b_satellites); if (nrOfAllContacts == 0) { nrOfAllContacts = contactWindows.Count(); } val_Scheduled = nrOfScheduledContacts / (double)nrOfAllContacts; val_Duration = scheduledDuration / allDuaration; val_Priority = (double)prio / (double)priorityMax; }