Ejemplo n.º 1
0
  // ainda nao acabou
  public Server chooseServer() {
    int maximun = -1;
    Vector<Server> bestServers = new Vector<Server>();

    // escolher os servidores com maior capacidade
    for (int i = 0; i < p.getServers().size(); i++) {
      if (p.getServers().get(i).getCapacity() >= maximun) {
        if (p.getServers().get(i).getCapacity() > maximun) {
          bestServers = new Vector<Server>();
          maximun = p.getServers().get(i).getCapacity();
        }
        bestServers.add(p.getServers().get(i));
      }
    }

    if (maximun == -1) {
      return null;
    }

    // escolher os servidores com menor tamanho
    Server bestServer = bestServers.get(0);
    for (int i = 1; i < bestServers.size(); i++) {
      if (bestServers.get(i).getSlots() < bestServer.getSlots()) {
        bestServer = bestServers.get(i);
      }
    }

    return null;
  }
Ejemplo n.º 2
0
  public int chooseRow() {
    // escolher menores buracos
    int minimun = Integer.MAX_VALUE;
    Vector<Integer> indexHoleMinimun = new Vector<Integer>();

    for (int i = 0; i < p.getFreeSpace().size(); i++) {
      for (int j = 0; i < p.getFreeSpace().get(i).size(); j++) {
        int elem = p.getFreeSpace().get(i).get(j);
        if (elem > 0 && elem <= minimun) {
          if (elem == minimun) {
            if (indexHoleMinimun.indexOf(i) == -1) {
              indexHoleMinimun.add(i);
            }
          } else {
            minimun = elem;
            indexHoleMinimun = new Vector<Integer>();
            indexHoleMinimun.add(i);
          }
        }
      }
    }

    if (minimun == Integer.MAX_VALUE) {
      return NAO_FOI_ENCONTRADO_BURACO;
    }

    if (indexHoleMinimun.size() == 1) {
      return indexHoleMinimun.get(0);
    }

    // escolher menores capacidades
    minimun = Integer.MAX_VALUE;
    Vector<Integer> indexCapacityMinimun = new Vector<Integer>();

    for (int i = 0; i < indexHoleMinimun.size(); i++) {
      int tempCapacity = 0;
      for (int j = 0; j < p.getSlots(); j++) {
        if (p.getMap()[indexHoleMinimun.get(i)][j] != -2
            && p.getMap()[indexHoleMinimun.get(i)][j] != -1) {
          tempCapacity += p.getServers().get(p.getMap()[indexHoleMinimun.get(i)][j]).getCapacity();
        }
      }

      if (tempCapacity <= minimun) {
        if (tempCapacity == minimun) {
          if (indexCapacityMinimun.indexOf(i) == -1) {
            indexCapacityMinimun.add(i);
          }
        } else {
          minimun = tempCapacity;
          indexCapacityMinimun = new Vector<Integer>();
          indexCapacityMinimun.add(i);
        }
      }
    }

    if (indexCapacityMinimun.size() == 1) {
      return indexCapacityMinimun.get(0);
    }

    // numero de servers diferentes menor
    minimun = Integer.MAX_VALUE;
    int indexServersMinimun = -1;

    int nrServers = 0;
    int server = -666;

    for (int i = 0; i < indexCapacityMinimun.size(); i++) {
      nrServers = 0;
      server = -666;
      for (int j = 0; j < p.getSlots(); j++) {
        if (p.getMap()[indexCapacityMinimun.get(i)][j] != -2
            && p.getMap()[indexCapacityMinimun.get(i)][j] != -1) {
          if (server != p.getMap()[indexCapacityMinimun.get(i)][j]) {
            server = p.getMap()[indexCapacityMinimun.get(i)][j];
            nrServers++;
          }
        }
      }

      if (nrServers < minimun) {
        minimun = nrServers;
        indexServersMinimun = i;
      }
    }

    return indexServersMinimun;
  }