按權重分配任務


public class TaskAlloc {

    private Map<String, Integer> weight;

    public Map<String, Integer> getWeight() {
        return weight;
    }

    public Map<String, Integer> getCmpltedTask() {
        return cmpltedTask;
    }

    public AtomicInteger getAllcodCount() {
        return allcodCount;
    }

    private Map<String, Integer> cmpltedTask = new ConcurrentHashMap<String, Integer>();
    private AtomicInteger allcodCount = new AtomicInteger(0);

    public TaskAlloc(Map<String, Integer> weight) throws Exception {
        this.weight = weight;
        Integer sum = MapHelper.reduce(weight, 0, (init, current) -> init + current);
        if (sum != 100) {
            throw new Exception("權重之合必須為100");
        }

        for (String key : weight.keySet()) {
            cmpltedTask.put(key, 0);
        }
    }

    public String alloc() {
        int ac = allcodCount.getAndIncrement();
        String key = getMin(cmpltedTask, weight, ac);
        cmpltedTask.put(key, cmpltedTask.get(key) + 1);
        return key;
    }

    private static String getMin(Map<String, Integer> allocedRecordMap, Map<String, Integer> weightMap, Integer allocedCount) {
        double min = 1;
        String result = null;
        for (Map.Entry<String, Integer> entry : weightMap.entrySet()) {
            String key = entry.getKey();
            //計算權重所占最大的比例
            double maxRatio = entry.getValue() * 1.0 / 100;
            //計算已分配的比例
            double cmpltedRatio = allocedCount == 0 ? 0 : allocedRecordMap.get(key) * 1.0 / allocedCount;
            double ratio = cmpltedRatio / maxRatio;
            if (ratio < min) {
                result = key;
                min = ratio;
            }
        }

        return result;
    }
}

 

 

第2種算法

 

public class TaskAlloc {

    /**
     * 任務權重Map,Key為任務標識,Value為任務對應的權重
     */
    private Map<String, Integer> weight;

    public Map<String, Integer> getWeight() {
        return weight;
    }

    /**
     *  ctor
     * @param weight 任務權重Map,Key為任務標識,Value為任務對應的權重
     * @throws Exception
     */
    public TaskAlloc(Map<String, Integer> weight) throws Exception {
        this.weight = weight;
        Integer sum = MapHelper.reduce(weight, 0, (result, current) -> result + current);
        if (sum != 100) {
            throw new Exception("權重之合必須為100");
        }
    }

    /**
     * 分配任務實現.
     * @param taskAllocStatisticsMapFunc 提供所有任務的分配統計Map Key為任務標識,Value為該任務已分配總數
     * @return 需要分配的任務標識
     * @throws Exception 任務的分配統計Map不能為空
     */
    public String alloc(Supplier<Map<String, Integer>> taskAllocStatisticsMapFunc)throws Exception  {
        Map<String, Integer> taskAllocStatisticsMap = taskAllocStatisticsMapFunc.get();
        if (taskAllocStatisticsMap == null) {
            throw new Exception("任務的分配統計Map不能為空");
        }
        int allocedCount = MapHelper.reduce(taskAllocStatisticsMap, 0, (result, currentValue) -> result + currentValue);
        String key = getMin(taskAllocStatisticsMap, weight, allocedCount);
        taskAllocStatisticsMap.put(key, taskAllocStatisticsMap.get(key) + 1);
        return key;
    }

    /**
     * 找到分配比率最小的任務標識(即需要分配的任務標識)
     * @param taskAllocStatisticsMap 任務的分配統計Map(Key為任務標識,Value為該任務已分配總數)
     * @param weightMap 任務的權重Map
     * @param allocedCount 已分配的總次數
     * @return 需要分配的任務標識
     */
    private static String getMin(Map<String, Integer> taskAllocStatisticsMap, Map<String, Integer> weightMap,
            Integer allocedCount) {
        double min = 1;
        String result = null;
        for (Map.Entry<String, Integer> entry : weightMap.entrySet()) {
            String key = entry.getKey();
            //計算權重所占最大的比例
            double maxRatio = entry.getValue() * 1.0 / 100;
            //計算已分配的比例
            double cmpltedRatio = allocedCount == 0 ? 0 : taskAllocStatisticsMap.getOrDefault(key, 0) * 1.0 / allocedCount;
            double ratio = cmpltedRatio / maxRatio;
            if (ratio < min) {
                result = key;
                min = ratio;
            }
        }

        return result;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM