@contract
def best_fit_decreasing(last_n_vm_cpu, hosts_cpu, hosts_ram,
                        inactive_hosts_cpu, inactive_hosts_ram,
                        vms_cpu, vms_ram):
    if log.isEnabledFor(logging.DEBUG):
        log.debug('last_n_vm_cpu: %s', str(last_n_vm_cpu))
        log.debug('hosts_cpu: %s', str(hosts_cpu))
        log.debug('hosts_ram: %s', str(hosts_ram))
        log.debug('inactive_hosts_cpu: %s', str(inactive_hosts_cpu))
        log.debug('inactive_hosts_ram: %s', str(inactive_hosts_ram))
        log.debug('vms_cpu: %s', str(vms_cpu))
        log.debug('vms_ram: %s', str(vms_ram))
    vms_tmp = []
    for vm, cpu in vms_cpu.items():
        if cpu:
            last_n_cpu = cpu[-last_n_vm_cpu:]
            vms_tmp.append((sum(last_n_cpu) / len(last_n_cpu),
                            vms_ram[vm],
                            vm))
        else:
            log.warning('No CPU data for VM: %s - skipping', vm)

    vms = sorted(vms_tmp, reverse=True)
    hosts = sorted(((v, hosts_ram[k], k)
                    for k, v in hosts_cpu.items()))
    inactive_hosts = sorted(((v, inactive_hosts_ram[k], k)
                             for k, v in inactive_hosts_cpu.items()))
    mapping = {}
    for vm_cpu, vm_ram, vm_uuid in vms:
        mapped = False
        while not mapped:
            for _, _, host in hosts:
                if hosts_cpu[host] >= vm_cpu and \
                    hosts_ram[host] >= vm_ram:
                        mapping[vm_uuid] = host
                        hosts_cpu[host] -= vm_cpu
                        hosts_ram[host] -= vm_ram
                        mapped = True
                        break
            else:
                if inactive_hosts:
                    activated_host = inactive_hosts.pop(0)
                    hosts.append(activated_host)
                    hosts = sorted(hosts)
                    hosts_cpu[activated_host[2]] = activated_host[0]
                    hosts_ram[activated_host[2]] = activated_host[1]
                else:
                    break

    if len(vms) == len(mapping):
        return mapping
    return {}
Message Nack'ed in RabbitMQ Publisher Acknowledgments mode
dataset/ETHPy150Open beloglazov/openstack-neat/neat/globals/vm_placement/bin_packing.py/best_fit_decreasing--crossover with--dataset/ETHPy150Open agoragames/haigha/tests/integration/rabbit_extensions_test.py/_PubackState.handle_nack