By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

modify a Round Robin algorithm to a Best-Fit

kirara
P: 19
Hi all,
I am trying to develop a mechanism to know which machine can handle a user job (by providing his Requirements) for now I wrote this method but it is a Round robin, I am trying to modify it to a best-fit algorithm can anybody give me tips about how should I begin?

Expand|Select|Wrap|Line Numbers
  1.   public static String canDo(Resources userReq,Hashtable hash){
  2.                 //getResourceInfo() is a method that store the machine name 
  3.                //and thier resources(cpu,memory,disk) in a hashtable
  4.         Hashtable resourceInfo = connectData.getResourcesInfo();
  5.         Enumeration en = resourceInfo.keys();
  6.         String status ="";
  7.         String machineName;
  8.         Resources machineRec;
  9.         while(en.hasMoreElements()) {
  10.                  machineName = (String)en.nextElement();
  11.                  status = "the following machine has been allocated to you: "+machineName;
  12.                  //resourc
  13.                  machineRec = (Resources)resourceInfo.get(machineName);
  14.                  if(machineRec.getCpu().getCpuIdle()>=userReq.getCpu().getCpuIdle())
  15.                  if(machineRec.getMemory().getFreeMemory()>=userReq.getMemory().getFreeMemory())
  16.                  if(machineRec.getDisk().getFreeSpace()>=userReq.getDisk().getFreeSpace()){
  17.                      break;
  18.  
  19.          }
  20.     }
  21.  
  22.         return status;
  23.     }
Dec 27 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Hi all,
I am trying to develop a mechanism to know which machine can handle a user job (by providing his Requirements) for now I wrote this method but it is a Round robin, I am trying to modify it to a best-fit algorithm can anybody give me tips about how should I begin?

Expand|Select|Wrap|Line Numbers
  1.   public static String canDo(Resources userReq,Hashtable hash){
  2.                 //getResourceInfo() is a method that store the machine name 
  3.                //and thier resources(cpu,memory,disk) in a hashtable
  4.         Hashtable resourceInfo = connectData.getResourcesInfo();
  5.         Enumeration en = resourceInfo.keys();
  6.         String status ="";
  7.         String machineName;
  8.         Resources machineRec;
  9.         while(en.hasMoreElements()) {
  10.                  machineName = (String)en.nextElement();
  11.                  status = "the following machine has been allocated to you: "+machineName;
  12.                  //resourc
  13.                  machineRec = (Resources)resourceInfo.get(machineName);
  14.                  if(machineRec.getCpu().getCpuIdle()>=userReq.getCpu().getCpuIdle())
  15.                  if(machineRec.getMemory().getFreeMemory()>=userReq.getMemory().getFreeMemory())
  16.                  if(machineRec.getDisk().getFreeSpace()>=userReq.getDisk().getFreeSpace()){
  17.                      break;
  18.  
  19.          }
  20.     }
  21.  
  22.         return status;
  23.     }
Maybe if you can point me to a best-fit algorithm tutorial or better still to just give it here I think your problem should be quite easy to solve. Just from intuition though:

You are simply getting the next value from enumeration.
To get the best fit you would need to go through the values first, selecting the best fit and removing it from the collection. I would therefore suggest that you load the data items into an ArrayList first(for easier traversal) then you can apply the algorithm to the arraylist.
Dec 28 '06 #2

kirara
P: 19
You are simply getting the next value from enumeration.
To get the best fit you would need to go through the values first, selecting the best fit and removing it from the collection...
that's exactly my question how to select the best fit and remove it from the collection(shorten the orginal list), can you please be more specific I don't need codes just how the algorithm can be implemented
thanks in advance

note: when you said point me to a tutorial you meant I should read a tutorial or I should give you one?? (I didn't understand) :)
Dec 28 '06 #3

10K+
P: 13,264
that's exactly my question how to select the best fit and remove it from the collection(shorten the orginal list), can you please be more specific I don't need codes just how the algorithm can be implemented
thanks in advance

note: when you said point me to a tutorial you meant I should read a tutorial or I should give you one?? (I didn't understand) :)
I meant me. I don't know the algorithm very well.


Your elements obviously have a property(eg, size, length ) that you are comparing with a value you want to fit the elements at. eg Say your list contains objects with memory values that are integers and you want to select the best fit say for size 10.

The brute force approach would be to go through the list as follows

best = firstElement;
for(each object ob in the list) {
if(ob is a better fit than best) {
best = ob;
}
}
then call list.remove(best);

And use best
Dec 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.