public ALessonNode(ACourse myCourse, int fixedPosition, int numOfPossTS) : this(myCourse, numOfPossTS) { _currPosition = fixedPosition; _positionInGlobalBestSolution = fixedPosition; _posInBestLocalSolution = fixedPosition; }
private bool checkCanGoTogether_AC_HT_ALN(ACourse acht, ALessonNode afixedln) { bool canGoTogether = true; if (afixedln.MyACourse.MyAEduProgram == acht.MyAEduProgram) { if (acht.GroupName == null || acht.GroupName == "")//aln is not group { canGoTogether = false; } else//aln is group { if (afixedln.MyACourse.GroupName == null || afixedln.MyACourse.GroupName == "")//afixedln is not group { canGoTogether = false; } else//afixedln is group { if (afixedln.MyACourse.GroupName == acht.GroupName) { canGoTogether = false; } } } } if (canGoTogether) { if (afixedln.MyACourse.ACoursesHT.Count > 0) { foreach (ACourse ac_afixedln_ht in afixedln.MyACourse.ACoursesHT) { if (ac_afixedln_ht.MyAEduProgram == acht.MyAEduProgram) // the same edu program { if (acht.GroupName == null || acht.GroupName == "") //ac is not group { canGoTogether = false; } else//ac is group { if (ac_afixedln_ht.GroupName == null || ac_afixedln_ht.GroupName == "")//afixedln is not group { canGoTogether = false; } else//afixedln is group { if (ac_afixedln_ht.GroupName == acht.GroupName) { canGoTogether = false; } } } } } } } return(canGoTogether); }
private void resetLSCourseNodesPositions(ACourse ac) { int kk = -1; foreach (ALessonNode alnor in ac.MyAllLessonNodesForAllocation) { alnor.MyLSPos = kk; kk++; } }
private void filterMeRegardingCurrentPartialSolution(ACourse ac, ArrayList partialSolution, bool[] acMyBoolPossTSTemp) { bool[] myConflictTable = ((ALessonNode)ac.MyAllLessonNodesForAllocation[0]).MyConflictTable; foreach (ALessonNode aln in partialSolution) { int posInCT = aln.MyPosInConflictTable; if (myConflictTable[posInCT]) { acMyBoolPossTSTemp[aln.CurrPosition - 1] = false; } else { this.removeUnpossibleWholeTSFromACourse(aln, acMyBoolPossTSTemp); } } }
public ALessonNode(ACourse myACourse, int numOfPossTS) { _myACourse = myACourse; _currPosition = 0; _positionInGlobalBestSolution = 0; _posInBestLocalSolution = 0; //_pheromoneTable = new double[numOfPossTS, numOfPossTS]; _pheromoneTable = new double[numOfPossTS]; for (int m = 0; m < numOfPossTS; m++) { /*for (int n = 0; n < numOfPossTS; n++) * { * _pheromoneTable[m, n] = THETA_START; * }*/ _pheromoneTable[m] = THETA_START; } }
public override double evaluateSC() { ACourse ac = (ACourse)_theObj; int[] mySC_LessonBlocks = (int[])_myArgs[0]; double myWeight = (double)_myArgs[1]; int minBlockSize = (int)mySC_LessonBlocks[0]; int minNumOfBlocks = (int)mySC_LessonBlocks[1]; int maxNumOfBlocks = (int)mySC_LessonBlocks[2]; /// int numOfIncludedDaysPerWeek = AGlobal.NUM_OF_INCLUDED_DAYS_PER_WEEK; int numOfTermsPerDay = AGlobal.NUM_OF_INCLUDED_TERMS_PER_DAY; int numOfSlotsPerRoom = numOfIncludedDaysPerWeek * numOfTermsPerDay; int totalNumOfMyLessons = 0; bool[,] occupTSBool = new bool[numOfTermsPerDay, numOfIncludedDaysPerWeek]; int[,] occupRooms = new int[numOfTermsPerDay, numOfIncludedDaysPerWeek]; if (ac.MyFixedLessonNodes != null) { foreach (ALessonNode aln in ac.MyFixedLessonNodes) { totalNumOfMyLessons++; int tsIndex = aln.CurrPosition; if (tsIndex > 0) { int indexRow = (tsIndex - 1) % numOfTermsPerDay; int indexCol = (int)Math.Floor((decimal)((tsIndex - 1) % numOfSlotsPerRoom) / numOfTermsPerDay); occupTSBool[indexRow, indexCol] = true; int roomIndex = (int)Math.Floor((decimal)(tsIndex - 1) / numOfSlotsPerRoom); occupRooms[indexRow, indexCol] = roomIndex; } } } if (ac.MyAllLessonNodesForAllocation != null) { foreach (ALessonNode aln in ac.MyAllLessonNodesForAllocation) { totalNumOfMyLessons++; int tsIndex = aln.CurrPosition; if (tsIndex > 0) { int indexRow = (tsIndex - 1) % numOfTermsPerDay; int indexCol = (int)Math.Floor((decimal)((tsIndex - 1) % numOfSlotsPerRoom) / numOfTermsPerDay); occupTSBool[indexRow, indexCol] = true; int roomIndex = (int)Math.Floor((decimal)(tsIndex - 1) / numOfSlotsPerRoom); occupRooms[indexRow, indexCol] = roomIndex; } } } int currNumOfBlocks = 0; for (int j = 0; j < numOfIncludedDaysPerWeek; j++) { bool columnFinished = false; bool blockCheckStarted = false; int lastRowIndex = 0; int blockSize = 0; int lastRoomIndex = 0; for (int k = 0; k < numOfTermsPerDay; k++) { if (occupTSBool[k, j] == true) { if (!blockCheckStarted) { blockCheckStarted = true; blockSize++; lastRowIndex = k; lastRoomIndex = occupRooms[k, j]; } else { if ((k - lastRowIndex) != 1) { return(myWeight); } else { blockSize++; lastRowIndex = k; if (occupRooms[k, j] != lastRoomIndex) { return(myWeight); } } } if (k == numOfTermsPerDay - 1) { if (blockSize >= minBlockSize) { currNumOfBlocks++; blockSize = 0; } else { return(myWeight); } } } else { if (blockCheckStarted && !columnFinished) { if (blockSize >= minBlockSize) { currNumOfBlocks++; columnFinished = true; blockSize = 0; } else { return(myWeight); } } } } } if (currNumOfBlocks <= maxNumOfBlocks && currNumOfBlocks >= minNumOfBlocks) { return(0); } else { return(myWeight); } }
private double doPartialOptmisation(double bestLocalValue, ArrayList theSolution, int theSolutionCount) { try { ArrayList theSolutionCopy = new ArrayList(); foreach (ALessonNode aln in theSolution) { aln.CurrPosition = aln.PositionInLocalBestSolution; theSolutionCopy.Add(aln); } ArrayList coursesForLSOptimisationList = new ArrayList(); //int randNumOfCourses = (int)_randomObj.Next(1, 3); int randNumOfCourses = 1; //temp //Console.WriteLine("BROJ PREDMETA: "+randNumOfCourses); int cNumTotal = randNumOfCourses; //!? int cNum = 0; //double rand = _randomObj.Next(0, 1000); //rand = rand / 1000; //if (rand < 0.5) { while (true) { int m = theSolutionCount - _fixedTSCount; int randValue = (int)_randomObj.Next(0, m); int alnIndexForMove = _fixedTSCount + randValue; ALessonNode alnInMove = (ALessonNode)theSolutionCopy[alnIndexForMove]; ACourse aCourse = (ACourse)alnInMove.MyACourse; if (!coursesForLSOptimisationList.Contains(aCourse)) { coursesForLSOptimisationList.Add(aCourse); cNum++; if (cNum == cNumTotal) { break; } } } } /*else * { * ArrayList testCList = new ArrayList(); * double maxLSVal = 0; * ACourse maxCourse = null; * foreach (ACourse testCourse in _allACourses) * { * testCList.Clear(); * testCList.Add(testCourse); * double testLSSolutionValue = this.calculateLSLocalSolutionValue(testCList); * if (testLSSolutionValue >= maxLSVal) * { * maxLSVal = testLSSolutionValue; * maxCourse = testCourse; * } * * } * * coursesForLSOptimisationList.Add(maxCourse); * }*/ double startLSLocalSolutionValue = this.calculateLSLocalSolutionValue(coursesForLSOptimisationList); double bestLSLocalSolutionValue = startLSLocalSolutionValue; if (bestLSLocalSolutionValue > 0) { int coursesInOptCount = coursesForLSOptimisationList.Count; object[] boolCoursePossTSAtStart = new object[coursesInOptCount]; foreach (ACourse aCourseInLS in coursesForLSOptimisationList) { foreach (ALessonNode alnor in aCourseInLS.MyAllLessonNodesForAllocation) { theSolutionCopy.Remove(alnor); } } int currCourseIndex = 0; bool[] acMyBoolPossTSAtStart = null; foreach (ACourse aCourseInLS in coursesForLSOptimisationList) { acMyBoolPossTSAtStart = (bool[])aCourseInLS.MyBoolPosTS.Clone(); this.filterMeRegardingCurrentPartialSolution(aCourseInLS, theSolutionCopy, acMyBoolPossTSAtStart); boolCoursePossTSAtStart[currCourseIndex] = acMyBoolPossTSAtStart; currCourseIndex++; } currCourseIndex = 0; ArrayList smallPartSolution = new ArrayList(); ArrayList freeTSListCourseFirstVisit = new ArrayList(); ArrayList[] courseFreeTSList = new ArrayList[coursesInOptCount]; bool isCourseBacktracking = false; while (true)//START of BIG loop { ACourse aCourseInLS = (ACourse)coursesForLSOptimisationList[currCourseIndex]; ArrayList nodesForOptimisationOfOneCourse = new ArrayList(); foreach (ALessonNode alno in aCourseInLS.MyAllLessonNodesForAllocation) { nodesForOptimisationOfOneCourse.Add(alno); } ArrayList freeTSListForCourse; int nodesForOptimisationOfOneCourseCount = nodesForOptimisationOfOneCourse.Count; int freeTSListForCourseCount; int workingNodeForCombination; if (isCourseBacktracking) { workingNodeForCombination = nodesForOptimisationOfOneCourseCount - 1; freeTSListForCourse = courseFreeTSList[currCourseIndex]; isCourseBacktracking = false; } else { //CLONE()??? bool[] acMyBoolPossTSTempTT2 = (bool[])boolCoursePossTSAtStart[currCourseIndex];//???? bool[] acMyBoolPossTSTemp = new bool[acMyBoolPossTSTempTT2.GetLength(0)]; int q = 0; foreach (bool bl in acMyBoolPossTSTempTT2) { acMyBoolPossTSTemp[q] = bl; q++; } if (currCourseIndex > 0) { this.filterMeRegardingCurrentPartialSolution(aCourseInLS, smallPartSolution, acMyBoolPossTSTemp); } freeTSListForCourse = new ArrayList(); int ts = 1; foreach (bool possTS in acMyBoolPossTSTemp) { if (possTS) { freeTSListForCourse.Add(ts); } ts++; } this.resetLSCourseNodesPositions(aCourseInLS); courseFreeTSList[currCourseIndex] = freeTSListForCourse; workingNodeForCombination = 0; } freeTSListForCourseCount = freeTSListForCourse.Count; while (true)//start of SMALL lop (inside nodes of one course) { ALessonNode alno = (ALessonNode)nodesForOptimisationOfOneCourse[workingNodeForCombination]; alno.MyLSPos++; if (alno.MyLSPos > (freeTSListForCourseCount - 1)) //exit from list of possible TS { if (workingNodeForCombination > 0) //backtracking { workingNodeForCombination--; } else//prvi node od predmeta { if (currCourseIndex == 0) { //calculateCurrentFULLSolutionValue //return from COMPLETE method foreach (ACourse theC in coursesForLSOptimisationList) { foreach (ALessonNode aln in theC.MyAllLessonNodesForAllocation) { aln.CurrPosition = aln.PositionInLocalBestSolution; //aln.PositionInLocalBestSolution = aln.CurrPosition; theSolutionCopy.Add(aln); } } double currSolutionValue = this.calculateCurrentSolutionValue(theSolutionCopy); if (currSolutionValue < bestLocalValue) { bestLocalValue = currSolutionValue; } return(bestLocalValue); } else { currCourseIndex--; isCourseBacktracking = true; break; } } } else//ok { if (isLocalSearchNodeStateOK(nodesForOptimisationOfOneCourse, workingNodeForCombination, alno.MyLSPos, freeTSListForCourse)) { if (workingNodeForCombination == nodesForOptimisationOfOneCourseCount - 1)//ZADNJI NODE { foreach (ALessonNode aln5 in nodesForOptimisationOfOneCourse) { smallPartSolution.Add(aln5); } if (currCourseIndex == (coursesInOptCount - 1))//last course { //have new combination //check ls solution value double lsSolutionValue = this.calculateLSLocalSolutionValue(coursesForLSOptimisationList); if (lsSolutionValue < bestLSLocalSolutionValue) { bestLSLocalSolutionValue = lsSolutionValue; foreach (ACourse theC in coursesForLSOptimisationList) { foreach (ALessonNode aln in theC.MyAllLessonNodesForAllocation) { aln.PositionInLocalBestSolution = aln.CurrPosition; } } if (lsSolutionValue == 0) { foreach (ALessonNode aln1 in smallPartSolution) { theSolutionCopy.Add(aln1); } foreach (ALessonNode aln2 in smallPartSolution) { theSolutionCopy.Remove(aln2); } smallPartSolution.Clear(); return(bestLocalValue - (startLSLocalSolutionValue - bestLSLocalSolutionValue)); } } else { smallPartSolution.Clear(); } } else { currCourseIndex++; break; } } else//not last node { ALessonNode alnoNext = (ALessonNode)nodesForOptimisationOfOneCourse[workingNodeForCombination + 1]; alnoNext.MyLSPos = alno.MyLSPos; workingNodeForCombination++; } } } } //end of small loop } //END of BIG loop } else { //Console.WriteLine("It's already zero"); } }catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace); } return(bestLocalValue); }
public int selectNewTimeSlot(ArrayList partialSolution, bool[] acMyBoolPossTSTemp, Random randObj) { int totalLen = _pheromoneTable.GetLength(0); int myPredecessor; int count = partialSolution.Count; if (count > 0) { myPredecessor = ((ALessonNode)partialSolution[count - 1]).CurrPosition - 1; } else { myPredecessor = 0; } //_myCurrentPredecessor = myPredecessor; double[] totalProbabilityTable = new double[totalLen]; double totalSumPheromoneT = 0.0; double totalSumTotalSquareProbT = 0.0; ACourse ac = this.MyACourse; ATeacher at = ac.MyATeacher; for (int n = 0; n < totalLen; n++) { if (acMyBoolPossTSTemp[n]) { //totalSumPheromoneT += _pheromoneTable[myPredecessor, n]; totalSumPheromoneT += _pheromoneTable[n]; } } for (int n = 0; n < totalLen; n++) { if (acMyBoolPossTSTemp[n]) { //totalProbabilityTable[n] = Math.Pow(((double)_pheromoneTable[myPredecessor, n] / (double)totalSumPheromoneT), 2.0); totalProbabilityTable[n] = Math.Pow(((double)_pheromoneTable[n] / (double)totalSumPheromoneT), 2.0); totalSumTotalSquareProbT += totalProbabilityTable[n]; } } double runningtotal = 0.0; double randValue = randObj.Next(0, 100000); randValue = randValue / 100000.0; int winningIndex = 0; for (int n = 0; n < totalLen; n++) { if (acMyBoolPossTSTemp[n]) { runningtotal += totalProbabilityTable[n] / totalSumTotalSquareProbT; if (runningtotal > randValue) { winningIndex = n + 1; return(winningIndex); } } } return(winningIndex); }