title: Load Balancing CloudSim
body: <p>I have been recently trying to work on Cloud Computing as a part of my College assignment.</p>
  
  <p>I have been trying to implement a new Load Balancing algorithm, (which has been proposed in some research paper) using CloudSim.</p>
  
  <p>Please help me out with this algorithm, I have some major issues implementing it. </p>
  
  <p>This is the code</p>
  
  <pre><code>/*
  * Title: Load Balancing in Cloud Computing
  */
  package org.cloudbus.cloudsim;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  
  public final class MyPolicyNew extends VmAllocationPolicy {
  
      private Map&lt;String, Host&gt; vmTable;
      private Map&lt;String, Integer&gt; usedPes;
      private List&lt;Integer&gt; freePes;
  
      private int status[] = new int[100];
  
      public MyPolicyNew(List&lt;? extends Host&gt; list) {
          super(list);
          setFreePes(new ArrayList&lt;Integer&gt;());
          for (Host host : getHostList()) {
              getFreePes().add(host.getPesNumber());
  
          }
  
          setVmTable(new HashMap&lt;String, Host&gt;());
          setUsedPes(new HashMap&lt;String, Integer&gt;());
      }
  
  @Override
  
  public boolean allocateHostForVm(Vm vm) {
      int idx = -1;
      int requiredPes;
      requiredPes = vm.getPesNumber();
      boolean result = false;
      int tries = 0; 
      List&lt;Integer&gt; freePesTmp = new ArrayList&lt;Integer&gt;();
  
      for (Integer freePes : getFreePes()) {
          freePesTmp.add(freePes);
      }
      int tempstatus[] = new int[100];
      for(int j=0; j&lt;100; j++){
          tempstatus[j]= status[j];
      }
      if (!getVmTable().containsKey(vm.getUid())) {
          do {
              int moreFree = Integer.MIN_VALUE;
              for (int i=0; i &lt; freePesTmp.size(); i++) {
                  if ((freePesTmp.get(i) &gt; moreFree) &amp;&amp; (tempstatus[i]!=1)) {
                      moreFree = freePesTmp.get(i);
                      idx = i;
                  }
                  tempstatus[idx]=1;
                  int flag=0;
                  for(int j=0; j&lt; freePesTmp.size(); j++)
                  { //
                      if(tempstatus[j]==1)
                          flag=1;
                      else
                          flag=0;
                  }
                  if(flag==1){
                      moreFree = Integer.MIN_VALUE;
                      for (int k=0; k &lt; freePesTmp.size(); k++) {
                          if (freePesTmp.get(k) &gt; moreFree) {
                              moreFree = freePesTmp.get(k);
                              idx = k;
                          }
                      }
                  }
              }
              Host host = getHostList().get(idx);
              result = host.vmCreate(vm);
  
              if (result) {
                  getVmTable().put(vm.getUid(), host);
                  getUsedPes().put(vm.getUid(), requiredPes);
  
                  getFreePes().set(idx, getFreePes().get(idx) - requiredPes);
                  status[idx]=1;
                  result = true;
                  break;
              }
              else {
                  freePesTmp.set(idx, Integer.MIN_VALUE);
                  tempstatus[idx]=0;
              }
              tries++;
          }while(!result &amp;&amp; tries &lt; getFreePes().size());
      }
      return result;
  }
  
  @Override
  public void deallocateHostForVm(Vm vm) {
  
      Host host = getVmTable().remove(vm.getUid());
      int idx = getHostList().indexOf(host);
      int pes = getUsedPes().remove(vm.getUid());
      if (host != null) {
          host.vmDestroy(vm);
          status[idx]= 0;
      }
  }
  
  getFreePes().set(idx, getFreePes().get(idx) + pes);
  
  @Override
  public Host getHost(Vm vm) {
      return getVmTable().get(vm.getUid());
  }
  
  @Override
  public Host getHost(int vmId, int userId) {
      return getVmTable().get(Vm.getUid(userId, vmId));
  }
  
  public Map&lt;String, Host&gt; getVmTable() {
      return vmTable;
  }
  
  protected void setVmTable(Map&lt;String, Host&gt; vmTable) {
      this.vmTable = vmTable;
  }
  
  protected Map&lt;String, Integer&gt; getUsedPes() {
      return usedPes;
  }
  
  protected void setUsedPes(Map&lt;String, Integer&gt; usedPes) {
      this.usedPes = usedPes;
  }
  
  protected List&lt;Integer&gt; getFreePes() {
      return freePes;
  }
  
  protected void setFreePes(List&lt;Integer&gt; freePes) {
      this.freePes = freePes;
  }
  
  @Override
  public List&lt;Map&lt;String, Object&gt;&gt; optimizeAllocation(List&lt;? extends Vm&gt; vmList) {
      return null;
  }
  
  @Override
  public boolean allocateHostForVm(Vm vm, Host host) {
      if (host.vmCreate(vm)){
          getVmTable().put(vm.getUid(), host);
          Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock());
          return true;
      }
      return false;
  }
  
  }
  </code></pre>
  
  <p>Where ever I use the function <code>getFreePes()</code> my NetBeans compiler gives an error <code>void cannot be deferenced</code>  and <code>cannot find symbol getFreePes()</code>. </p>
  
  <p>Basic idea behind the algorithm:: The algorithm checks if there are any free hosts in the data center and if there are any then it assigns a process to that host and decreases the number of free processors with that host. If there are no free hosts, but have some free processors then it checks the host with the maximum number of processors and assigns the new incoming process to that host..</p>
  
  <p>Please help me out with this code, I am not very good at Java either, I have been more of a C/C++ coder, so, I am having a bit of problem dealing with things and further this a new library to me so I am not used to much of its features and function, a few of my seniors have helped me come up with this code, but now its not working, please help me guys.</p>
  
  <p>Thanks for any help in advance. :)</p>