java - 添加蚁群优化作为单个数据中心中虚拟机之间的负载平衡策略

标签 java cloudsim

running-and-using-cloud-analyst

这里是如何应用循环调度的代码

public class RoundRobinVmLoadBalancer extends VmLoadBalancer 

{

    private Map<Integer, VirtualMachineState> vmStatesList;

    private int currVm = -1;

    public RoundRobinVmLoadBalancer(Map<Integer, VirtualMachineState> vmStatesList)

    {

        super();
        this.vmStatesList = vmStatesList;
    }

    /* (non-Javadoc)
     * @see cloudsim.ext.VMLoadBalancer#getVM()
     */
    public int getNextAvailableVm(){
        currVm++;

        if (currVm >= vmStatesList.size()){
            currVm = 0;
        }

        allocatedVm(currVm);

        return currVm;

    }
}

VmLoadBalancer

import java.util.Map;

import java.util.HashMap;

/**
 * This is the base class defining the behaviour of a Virtual Machine load 

balancer

 * used by a {@link DatacenterController}. The main method all load balancers 

should implement


 * is <c
ode>public int getNextAvailableVm()</code>.
 * 
 * This class provides a basic load balancing statistic collection that can be used by 
 * implementing classes. The implementing classes should call the  <code>void allocatedVM(int currVm)</code>
 *  method to use the statisitics collection feature.
 * 
 */
abstract public class VmLoadBalancer 
{

/** Holds the count of allocations for each VM */

protected Map<Integer, Integer> vmAllocationCounts;

    /** No args contructor */

    public VmLoadBalancer(){

    vmAllocationCounts = new HashMap<Integer, Integer>();

    }

    /**
     * The main contract of {@link VmLoadBalancer}. All load balancers should implement
     * this method according to their specific load balancing policy.
     * 
     * @return id of the next available Virtual Machine to which the next task should be
     *          allocated 
     */

    abstract public int getNextAvailableVm();

    /**
     * Used internally to update VM allocation statistics. Should be called by all impelementing
     * classes to notify when a new VM is allocated.
     * 
     * @param currVm
     */

    protected void allocatedVm(int currVm){


    Integer currCount = vmAllocationCounts.get(currVm);

    if (currCount == null){

        currCount = 0;

    }

        vmAllocationCounts.put(currVm, currCount + 1);      

}

    /**
     * Returns a {@link Map} indexed by VM id and having the number of allocations for each VM.
     * @return
     */

public Map<Integer, Integer> getVmAllocationCounts()

{

    return vmAllocationCounts;

}

}

像循环法一样,我想应用蚁群优化作为负载平衡策略。但不清楚如何实现循环,在cloud_analyst cloudsim项目中没有看到循环的代码,那么我如何应用ACO,请分享在cloudsim中应用的ACO代码片段

最佳答案

根据我在 CloudSim 方面的经验,我认为您应该尝试创建一个新的 VmAllocationPolicy 而不是 VmLoadBalancer。因此,如果您尝试使用蚁群优化,那么您的类应如下所示。

public final class MyPolicyNew extends VmAllocationPolicy {

    private Map<String, Host> vmTable;//vm table
    private Map<String, Integer> usedPes;//used pes
    private List<Integer> freePes;//free pes

    public MyPolicyNew(List<? extends Host> list) {
        ....
    }

    @Override
    public boolean allocateHostForVm(Vm vm) {

        **Your code for ACO goes here**
        **You do all the processing here and at the end allocate the vm to a host**
        **using host.vmCreate(vm) which returns a boolean on success of allocation, which you return from the function**
    }

    ....

}

您必须尝试查看VmAllocationPolicy.java类以及cloudsim中包含的Cloudsim模拟示例,这会对您有所帮助。

附注我知道你问这个问题已经一个多月了,但如果它能帮助你,我会很高兴!

关于java - 添加蚁群优化作为单个数据中心中虚拟机之间的负载平衡策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360695/

相关文章:

Java 线程 wait() => 阻塞?

java - 错误: The type of the expression must be an array type but it resolved to int

java - 该程序无法使用子类而不是父类(super class)来运行

java - 从restTemplate 映射到Map<String, Integer>

java - double 失败 - 无法使用大十进制

java - javac 和 Eclipse IDE 编译器之间有趣的泛型相关差异

java - 如何使用 Spring Security 修复对资源的访问?

java - 负载平衡 CloudSim

hadoop - 如何在 cloudsim 中实现 Hadoop?

cloudsim - 如何改变 cloudsim 中的任务优先级?