mirror of
				https://github.com/bspeice/itcs3146-project
				synced 2025-10-31 09:30:27 -04:00 
			
		
		
		
	-Added more testing code for best fit
This commit is contained in:
		| @ -4,31 +4,36 @@ | |||||||
|  *  11/9/2012 |  *  11/9/2012 | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  |  | ||||||
| public class BestFitAlgorithm implements baseAlgorithm{ | public class BestFitAlgorithm implements baseAlgorithm{ | ||||||
|      |      | ||||||
|     int memoryBlock[]; |     int memoryBlock[]; | ||||||
|     |     private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; | ||||||
|  |     ArrayList<Integer> candidates; | ||||||
|      |      | ||||||
|     public BestFitAlgorithm(int memorySize) |     public BestFitAlgorithm(int memorySize) | ||||||
|     { |     { | ||||||
|         //Initialize memory block to whatever the size is |         //Initialize memory block to whatever the size is | ||||||
|         memoryBlock = new int[memorySize]; |         memoryBlock = new int[memorySize]; | ||||||
|  |         System.out.println("The size of the memory is: " + memoryBlock.length); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public int getBestSizeIndex(int jobSize) |     public int getBestSizeIndex(int jobSize) | ||||||
|     { |     { | ||||||
|         int bestSize; |         int bestSize; //The most suitable block size for the job | ||||||
|         int bestSizeIndex; |         int bestSizeIndex; //The most suitable block size starting index for the job | ||||||
|          |          | ||||||
|         ArrayList<Integer> candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size) |         System.out.println("The size of the job is: " + jobSize); | ||||||
|  |          | ||||||
|  |         candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size) | ||||||
|  |  | ||||||
|             int counter = 0; //Counter for measuring unallocated memory |             int counter = 0; //Counter for measuring unallocated memory | ||||||
|             //Scan through memory block and get free blocks |             //Scan through memory block and get free blocks. Add candidates for allocation to array list | ||||||
|             for(int i = 0; i < memoryBlock.length; i++) |             for(int i = 0; i < memoryBlock.length; i++) | ||||||
|             { |             { | ||||||
|                 //If position in memory block here is 0, iterate from that index and count up sequential 0's |                 //If position in memory block here is 0, iterate from that index and count up sequential 0's (free space) | ||||||
|                 if(memoryBlock[i] == 0) |                 if(memoryBlock[i] == 0) | ||||||
|                 { |                 { | ||||||
|                     for(int j = i; j < memoryBlock.length - i; j++) |                     for(int j = i; j < memoryBlock.length - i; j++) | ||||||
| @ -38,17 +43,28 @@ public class BestFitAlgorithm implements baseAlgorithm{ | |||||||
|                             counter++; |                             counter++; | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                     if(counter >= jobSize) |                     if(counter == jobSize) | ||||||
|                     { |                     { | ||||||
|                         candidates.add(i); //Store index |                         candidates.add(i); //Store index | ||||||
|                         candidates.add(counter); //Store size of free memory chunk |                         candidates.add(counter); //Store size of free memory chunk | ||||||
|                     } |                     } | ||||||
|  |                     else if(counter >= jobSize) | ||||||
|  |                     { | ||||||
|  |                         candidates.add(i); //Store index | ||||||
|  |                         candidates.add(counter); //Store size of free memory chunk | ||||||
|  |                     } | ||||||
|  |                     //System.out.println("The size of the counter is: " + counter); | ||||||
|                     counter = 0; |                     counter = 0; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             for(int i = 0; i < candidates.size(); i++) | ||||||
|  |             { | ||||||
|  |                 System.out.println("Candidate index: " + candidates.get(i)); | ||||||
|  |                 System.out.println("Candidate size: " + candidates.get(i+1)); | ||||||
|  |             } | ||||||
|              |              | ||||||
|             //Iterate through candidate sizes |             //Iterate through candidate sizes | ||||||
|             bestSizeIndex = 0; //Initialize best index to first spot in array list |             bestSizeIndex = candidates.get(0).intValue(); //Initialize best index to first spot in array list | ||||||
|              |              | ||||||
|             bestSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate  |             bestSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate  | ||||||
|              |              | ||||||
| @ -59,6 +75,7 @@ public class BestFitAlgorithm implements baseAlgorithm{ | |||||||
|                 if(candidates.get(i).intValue() == jobSize) |                 if(candidates.get(i).intValue() == jobSize) | ||||||
|                 { |                 { | ||||||
|                     bestSizeIndex = i - 1; |                     bestSizeIndex = i - 1; | ||||||
|  |                     System.out.println("The best size index is: " +  bestSizeIndex); | ||||||
|                     return bestSizeIndex; //You're done. Return the value. |                     return bestSizeIndex; //You're done. Return the value. | ||||||
|                 } |                 } | ||||||
|                 //If the current size is less than the previous best size, make this the new best size |                 //If the current size is less than the previous best size, make this the new best size | ||||||
| @ -66,12 +83,15 @@ public class BestFitAlgorithm implements baseAlgorithm{ | |||||||
|                 { |                 { | ||||||
|                     bestSize = candidates.get(i+1).intValue(); |                     bestSize = candidates.get(i+1).intValue(); | ||||||
|                     bestSizeIndex = i - 1; |                     bestSizeIndex = i - 1; | ||||||
|  |                     System.out.println("The best size index is: " +  bestSizeIndex); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             System.out.println("The best size is: " +  bestSize); | ||||||
|              |              | ||||||
|         //If the best size is less than the job size, run this again |         //No candidates | ||||||
|         if(candidates.isEmpty()) |         if(candidates.isEmpty()) | ||||||
|         { |         { | ||||||
|  |             System.out.println("No best size index"); | ||||||
|             return -1; |             return -1; | ||||||
|         } |         } | ||||||
|          |          | ||||||
| @ -81,28 +101,55 @@ public class BestFitAlgorithm implements baseAlgorithm{ | |||||||
|     @Override |     @Override | ||||||
|     public void allocate(int jobID, int jobSize, int jobTime) |     public void allocate(int jobID, int jobSize, int jobTime) | ||||||
|     { |     { | ||||||
|         int bestSizeIndex = getBestSizeIndex(jobSize); |         try | ||||||
|  |         { | ||||||
|  |             Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); | ||||||
|  |  | ||||||
|  |             //checks to see if the job will fit in memory | ||||||
|  |             if(jobSize>memoryBlock.length) | ||||||
|  |             { | ||||||
|  |                     System.out.println("This job is too large"); | ||||||
|  |                     System.exit(0); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             int bestSizeIndex = getBestSizeIndex(jobSize); | ||||||
|  |  | ||||||
|  |             //No candidates found | ||||||
|  |             if(bestSizeIndex == -1)  | ||||||
|  |             { | ||||||
|  |                 System.out.println("No candidates found...attempting to compact"); | ||||||
|  |                 //Try compacting, then attempt to get an index again | ||||||
|  |                 compact(); | ||||||
|  |  | ||||||
|  |                 bestSizeIndex = getBestSizeIndex(jobSize); | ||||||
|  |  | ||||||
|  |                 //Compacting still didn't produce an appropriate block | ||||||
|  |                 if(bestSizeIndex == -1) | ||||||
|  |                 { | ||||||
|  |                     //TODO ..... | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 //Allocate the memory | ||||||
|  |                 for(int i = bestSizeIndex; i < jobSize; i++) | ||||||
|  |                 { | ||||||
|  |                     memoryBlock[i] = jobID; | ||||||
|  |                 } | ||||||
|  |                  | ||||||
|  |                 System.out.println("Successfully allocated!"); | ||||||
|  |                  | ||||||
|  |                 for(int i = 0; i < memoryBlock.length; i++) | ||||||
|  |                 { | ||||||
|  |                     System.out.println("Job at position " + i + "in memoryblock: " + memoryBlock[i]); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |         }    | ||||||
|          |          | ||||||
|         //No candidates found |         catch (Exception e) | ||||||
|         if(bestSizeIndex == -1)  |  | ||||||
|         { |         { | ||||||
|             //Try compacting, then attempt to get an index again |             System.out.println("Could not allocate job with ID " + jobID); | ||||||
|             compact();  |  | ||||||
|             bestSizeIndex = getBestSizeIndex(jobSize); |  | ||||||
|              |  | ||||||
|             //Compacting still didn't produce an appropriate block |  | ||||||
|             if(bestSizeIndex == -1) |  | ||||||
|             { |  | ||||||
|                 //TODO ..... |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         else |  | ||||||
|         { |  | ||||||
|             //Allocate the memory |  | ||||||
|             for(int i = bestSizeIndex; i < jobSize; i++) |  | ||||||
|             { |  | ||||||
|                 memoryBlock[i] = jobID; |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  | |||||||
| @ -60,7 +60,7 @@ class FirstFit implements baseAlgorithm | |||||||
| 		if(jobSize>memSize) | 		if(jobSize>memSize) | ||||||
| 		{ | 		{ | ||||||
| 			System.out.println("\n\n*********************************************************"+ | 			System.out.println("\n\n*********************************************************"+ | ||||||
| 										"         THIS JOB IS TO LARGE TO FIT INTO MEMORY"+ | 										"         THIS JOB IS TOO LARGE TO FIT INTO MEMORY"+ | ||||||
| 										"*********************************************************"); | 										"*********************************************************"); | ||||||
| 			System.exit(0); | 			System.exit(0); | ||||||
| 		} | 		} | ||||||
|  | |||||||
| @ -70,6 +70,9 @@ public class memoryManagement{ | |||||||
| 			System.out.println("complete"); | 			System.out.println("complete"); | ||||||
| 			System.out.println(jobLength+" jobs found on file"); | 			System.out.println(jobLength+" jobs found on file"); | ||||||
| 		} | 		} | ||||||
|  |                  | ||||||
|  |                  | ||||||
|  |                  | ||||||
| 		 | 		 | ||||||
| 		//Send jobs to algorithm, time is calculated and printed out after completion | 		//Send jobs to algorithm, time is calculated and printed out after completion | ||||||
| 		System.out.print("Sending jobs to threaded allocation algorithm..."); | 		System.out.print("Sending jobs to threaded allocation algorithm..."); | ||||||
| @ -82,17 +85,25 @@ public class memoryManagement{ | |||||||
| 		} | 		} | ||||||
| 		timeEnd = System.currentTimeMillis() - timeStart; | 		timeEnd = System.currentTimeMillis() - timeStart; | ||||||
| 		System.out.println("complete"); | 		System.out.println("complete"); | ||||||
| 		System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength + | 		System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength +                                            | ||||||
| 							" jobs is " + timeEnd + " milliseconds"); |               " jobs is " + timeEnd + " milliseconds"); | ||||||
|  |                  | ||||||
|  |                  | ||||||
|  |                  | ||||||
|  |                  | ||||||
|  |                  | ||||||
| 		//***Best Fit (David Weber)*** | 		//***Best Fit (David Weber)*** | ||||||
|                 timeStart = System.currentTimeMillis(); |                 timeStart = System.currentTimeMillis(); | ||||||
|                 for(int i = 0; i < jobLength - 1; i++){ |                 for(int i = 0; i < jobLength - 1; i++){ | ||||||
| 			//David_Weber_BestFit.allocate(id[i], size[i], time[i]); | 			David_Weber_BestFit.allocate(id[i], size[i], time[i]); | ||||||
| 		} | 		} | ||||||
|                 timeEnd = System.currentTimeMillis() - timeStart; |                 timeEnd = System.currentTimeMillis() - timeStart; | ||||||
|                 System.out.println("complete"); |                 System.out.println("complete"); | ||||||
| 		System.out.println("Elapsed time for threaded best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds"); | 		System.out.println("Elapsed time for threaded best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds"); | ||||||
|                  |                  | ||||||
|  |                  | ||||||
|  |                  | ||||||
|  |                 /* | ||||||
|                 //***Worst Fit (David Weber)*** |                 //***Worst Fit (David Weber)*** | ||||||
|                 timeStart = System.currentTimeMillis(); |                 timeStart = System.currentTimeMillis(); | ||||||
|                 for(int i = 0; i < jobLength - 1; i++){ |                 for(int i = 0; i < jobLength - 1; i++){ | ||||||
| @ -101,6 +112,7 @@ public class memoryManagement{ | |||||||
|                 timeEnd = System.currentTimeMillis() - timeStart; |                 timeEnd = System.currentTimeMillis() - timeStart; | ||||||
|                 System.out.println("complete"); |                 System.out.println("complete"); | ||||||
| 		System.out.println("Elapsed time for threaded worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds"); | 		System.out.println("Elapsed time for threaded worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds"); | ||||||
|  |                 */ | ||||||
|                  |                  | ||||||
|                 //Put other algorithms here. |                 //Put other algorithms here. | ||||||
|           |           | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 David Weber
					David Weber