Browse Source

Tweaked algorithm

David Weber 7 years ago
parent
commit
616ef7b785
3 changed files with 224 additions and 20 deletions
  1. 114
    9
      BestFitAlgorithm.java
  2. 108
    9
      WorstFitAlgorithm.java
  3. 2
    2
      baseAlgorithm.java

+ 114
- 9
BestFitAlgorithm.java View File

@@ -1,15 +1,120 @@
1 1
 /*
2
- * To change this template, choose Tools | Templates
3
- * and open the template in the editor.
2
+ *  Best Fit Algorithm by David Weber
3
+ *  ITCS 3146
4
+ *  11/9/2012
4 5
  */
5 6
 
6
-/**
7
- *
8
- * @author djw612
9
- */
10
-public class BestFitAlgorithm {
11
-    public BestFitAlgorithm()
7
+import java.util.ArrayList;
8
+
9
+public class BestFitAlgorithm implements baseAlgorithm{
10
+    
11
+    int memoryBlock[];
12
+   
13
+    
14
+    public BestFitAlgorithm(int memorySize)
15
+    {
16
+        //Initialize memory block to whatever the size is
17
+        memoryBlock = new int[memorySize];
18
+    }
19
+    
20
+    public int getBestSizeIndex(int jobSize)
21
+    {
22
+        int bestSize = 0;
23
+        int bestSizeIndex = 0;
24
+        
25
+        ArrayList<Integer> candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size)
26
+
27
+            int counter = 0; //Counter for measuring unallocated memory
28
+            //Scan through memory block and get free blocks
29
+            for(int i = 0; i < memoryBlock.length; i++)
30
+            {
31
+                //If position in memory block here is 0, iterate from that index and count up sequential 0's
32
+                if(memoryBlock[i] == 0)
33
+                {
34
+                    for(int j = i; j < memoryBlock.length - i; j++)
35
+                    {
36
+                        if(memoryBlock[j] == 0)
37
+                        {
38
+                            counter++;
39
+                        }
40
+                    }
41
+                    if(counter >= jobSize)
42
+                    {
43
+                        candidates.add(i); //Store index
44
+                        candidates.add(counter); //Store size of free memory chunk
45
+                    }
46
+                    counter = 0;
47
+                }
48
+            }
49
+            
50
+            //Iterate through candidate sizes
51
+            bestSizeIndex = 0; //Initialize best index to first spot in array list
52
+            
53
+            bestSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate 
54
+            
55
+            //Iterate through sizes and find the best fit
56
+            for(int i = 1; i < candidates.size(); i=i+2)
57
+            {
58
+                //Best possible case: job size = free block size (you're done)
59
+                if(candidates.get(i).intValue() == jobSize)
60
+                {
61
+                    bestSizeIndex = i - 1;
62
+                    return bestSizeIndex; //You're done. Return the value.
63
+                }
64
+                //If the current size is less than the previous best size, make this the new best size
65
+                else if(candidates.get(i).intValue() < bestSize)
66
+                {
67
+                    bestSize = candidates.get(i+1).intValue();
68
+                    bestSizeIndex = i - 1;
69
+                }
70
+            }
71
+            
72
+        //If the best size is less than the job size, run this again
73
+        if(candidates.isEmpty())
74
+        {
75
+            return -1;
76
+        }
77
+        
78
+        return bestSizeIndex;  
79
+    }
80
+
81
+    @Override
82
+    public void allocate(int jobID, int jobSize, int jobTime)
83
+    {
84
+        int bestSizeIndex = getBestSizeIndex(jobSize);
85
+        
86
+        if(bestSizeIndex == -1) //No candidates found
87
+        {
88
+            //Try compacting, then attempt to get an index again
89
+            compact(); 
90
+            bestSizeIndex = getBestSizeIndex(jobSize);
91
+            
92
+            //Compacting still didn't produce an appropriate block
93
+            if(bestSizeIndex == -1)
94
+            {
95
+                //TODO .....
96
+            }
97
+        }
98
+        
99
+        //Allocate the memory
100
+        for(int i = bestSizeIndex; i < jobSize; i++)
101
+        {
102
+            memoryBlock[i] = jobID;
103
+        }
104
+    }
105
+    
106
+    public void compact()
107
+    {
108
+        //TODO: Compact memory if no suitable allocation candidates are found on the first pass
109
+    }
110
+    
111
+    @Override
112
+    public void deallocate(int jobSize, int beginningLocation)
12 113
     {
13
-        //Constructor goes here
114
+        for(int i = beginningLocation; i < jobSize; i++)
115
+        {
116
+            memoryBlock[i] = 0;
117
+        }
14 118
     }
119
+    
15 120
 }

+ 108
- 9
WorstFitAlgorithm.java View File

@@ -1,15 +1,114 @@
1 1
 /*
2
- * To change this template, choose Tools | Templates
3
- * and open the template in the editor.
2
+ *  Worst Fit Algorithm by David Weber
3
+ *  ITCS 3146
4
+ *  11/9/2012
4 5
  */
5 6
 
6
-/**
7
- *
8
- * @author djw612
9
- */
10
-public class WorstFitAlgorithm {
11
-    public WorstFitAlgorithm()
7
+import java.util.ArrayList;
8
+
9
+public class WorstFitAlgorithm implements baseAlgorithm{
10
+    
11
+    int memoryBlock[];
12
+   
13
+    
14
+    public WorstFitAlgorithm(int memorySize)
15
+    {
16
+        //Initialize memory block to whatever the size is
17
+        memoryBlock = new int[memorySize];
18
+    }
19
+    
20
+    public int getWorstSizeIndex(int jobSize)
21
+    {
22
+        int worstSize = 0;
23
+        int worstSizeIndex = 0;
24
+        
25
+        ArrayList<Integer> candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size)
26
+
27
+            int counter = 0; //Counter for measuring unallocated memory
28
+            //Scan through memory block and get free blocks
29
+            for(int i = 0; i < memoryBlock.length; i++)
30
+            {
31
+                //If position in memory block here is 0, iterate from that index and count up sequential 0's
32
+                if(memoryBlock[i] == 0)
33
+                {
34
+                    for(int j = i; j < memoryBlock.length - i; j++)
35
+                    {
36
+                        if(memoryBlock[j] == 0)
37
+                        {
38
+                            counter++;
39
+                        }
40
+                    }
41
+                    if(counter >= jobSize)
42
+                    {
43
+                        candidates.add(i); //Store index
44
+                        candidates.add(counter); //Store size of free memory chunk
45
+                    }
46
+                    counter = 0;
47
+                }
48
+            }
49
+            
50
+            //Iterate through candidate sizes
51
+            worstSizeIndex = 0; //Initialize best index to first spot in array list
52
+            
53
+            worstSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate 
54
+            
55
+            //Iterate through sizes and find the best fit
56
+            for(int i = 1; i < candidates.size(); i=i+2)
57
+            {
58
+                //If the current size is greater than the previous best size, make this the new best size
59
+                if(candidates.get(i).intValue() > worstSize)
60
+                {
61
+                    worstSize = candidates.get(i+1).intValue();
62
+                    worstSizeIndex = i - 1;
63
+                }
64
+            }
65
+            
66
+        //If the best size is less than the job size, run this again
67
+        if(candidates.isEmpty())
68
+        {
69
+            return -1;
70
+        }
71
+        
72
+        return worstSizeIndex;  
73
+    }
74
+
75
+    @Override
76
+    public void allocate(int jobID, int jobSize, int jobTime)
77
+    {
78
+        int worstSizeIndex = getWorstSizeIndex(jobSize);
79
+        
80
+        if(worstSizeIndex == -1) //No candidates found
81
+        {
82
+            //Try compacting, then attempt to get an index again
83
+            compact(); 
84
+            worstSizeIndex = getWorstSizeIndex(jobSize);
85
+            
86
+            //Compacting still didn't produce an appropriate block
87
+            if(worstSizeIndex == -1)
88
+            {
89
+                //TODO .....
90
+            }
91
+        }
92
+        
93
+        //Allocate the memory
94
+        for(int i = worstSizeIndex; i < jobSize; i++)
95
+        {
96
+            memoryBlock[i] = jobID;
97
+        }
98
+    }
99
+    
100
+    public void compact()
101
+    {
102
+        //TODO: Compact memory if no suitable allocation candidates are found on the first pass
103
+    }
104
+    
105
+    @Override
106
+    public void deallocate(int jobSize, int beginningLocation)
12 107
     {
13
-        //Constructor goes here
108
+        for(int i = beginningLocation; i < jobSize; i++)
109
+        {
110
+            memoryBlock[i] = 0;
111
+        }
14 112
     }
113
+    
15 114
 }

+ 2
- 2
baseAlgorithm.java View File

@@ -1,5 +1,5 @@
1 1
 interface baseAlgorithm{
2 2
 	
3
-	void allocate(int jobID, int jobSize, int jobTime);
4
-	void deallocate(int jobSize, int beginningLocation);
3
+	public void allocate(int jobID, int jobSize, int jobTime);
4
+	public void deallocate(int jobSize, int beginningLocation);
5 5
 }

Loading…
Cancel
Save