完整的GA算法的工程实现,包括与轮询(RR)算法效果对比:

完整项目代码地址(导入到eclipse中即可运行): GA-cloudsim.zip

遗传算法GA的核心代码实现:

最核心:

private static ArrayList<int[]> GA(ArrayList<int[]> pop,int gmax,double crossoverProb,double mutationRate)
{
HashMap<Integer,double[]> segmentForEach=calcSelectionProbs(pop);
ArrayList<int[]> children=new ArrayList<int[]>();
ArrayList<int[]> tempParents=new ArrayList<int[]>();
while(children.size()<pop.size())
{
//selection phase:select two parents each time.
for(int i=0;i<2;i++)
{
double prob = new Random().nextDouble();
for (int j = 0; j < pop.size(); j++)
{
if (isBetween(prob, segmentForEach.get(j)))
{
tempParents.add(pop.get(j));
break;
}
}
}
//cross-over phase.
int[] p1,p2,p1temp,p2temp;
p1= tempParents.get(tempParents.size() - 2).clone();
p1temp= tempParents.get(tempParents.size() - 2).clone();
p2 = tempParents.get(tempParents.size() -1).clone();
p2temp = tempParents.get(tempParents.size() -1).clone();
if(new Random().nextDouble()<crossoverProb)
{
int crossPosition = new Random().nextInt(cloudletList.size() - 1);
//cross-over operation
for (int i = crossPosition + 1; i < cloudletList.size(); i++)
{
int temp = p1temp[i];
p1temp[i] = p2temp[i];
p2temp[i] = temp;
}
}
//choose the children if they are better,else keep parents in next iteration.
children.add(getFitness(p1temp) < getFitness(p1) ? p1temp : p1);
children.add(getFitness(p2temp) < getFitness(p2) ? p2temp : p2);
// mutation phase.
if (new Random().nextDouble() < mutationRate)
{
// mutation operations bellow.
int maxIndex = children.size() - 1; for (int i = maxIndex - 1; i <= maxIndex; i++)
{
operateMutation(children.get(i), mutationRate);
}
}
} gmax--;
return gmax > 0 ? GA(children, gmax, crossoverProb, mutationRate): children;
}

完整核心代码:

 private static int[] findBestSchedule(ArrayList<int[]> pop)
{
double bestFitness=1000000000;
int bestIndex=0;
for(int i=0;i<pop.size();i++)
{
int []schedule=pop.get(i);
double fitness=getFitness(schedule);
if(bestFitness>fitness)
{
bestFitness=fitness;
bestIndex=i;
}
}
return pop.get(bestIndex);
} private static int[] getScheduleByGA(int popSize,int gmax,double crossoverProb,double mutationRate)
{
ArrayList<int[]> pop=initPopsRandomly(cloudletList.size(),vmList.size(),popSize);
pop=GA(pop,gmax,crossoverProb,mutationRate);
return findBestSchedule(pop);
} private static ArrayList<int[]> initPopsRandomly(int taskNum,int vmNum,int popsize)
{
ArrayList<int[]> schedules=new ArrayList<int[]>();
for(int i=0;i<popsize;i++)
{
//data structure for saving a schedule:array,index of array are cloudlet id,content of array are vm id.
int[] schedule=new int[taskNum];
for(int j=0;j<taskNum;j++)
{
schedule[j]=new Random().nextInt(vmNum);
}
schedules.add(schedule);
}
return schedules;
} private static double getFitness(int[] schedule)
{
double fitness=0; HashMap<Integer,ArrayList<Integer>> vmTasks=new HashMap<Integer,ArrayList<Integer>>();
int size=cloudletList.size(); for(int i=0;i<size;i++)
{
if(!vmTasks.keySet().contains(schedule[i]))
{
ArrayList<Integer> taskList=new ArrayList<Integer>();
taskList.add(i);
vmTasks.put(schedule[i],taskList);
}
else
{
vmTasks.get(schedule[i]).add(i);
}
} for(Entry<Integer, ArrayList<Integer>> vmtask:vmTasks.entrySet())
{
int length=0;
for(Integer taskid:vmtask.getValue())
{
length+=getCloudletById(taskid).getCloudletLength();
} double runtime=length/getVmById(vmtask.getKey()).getMips();
if (fitness<runtime)
{
fitness=runtime;
}
} return fitness;
} private static ArrayList<int[]> GA(ArrayList<int[]> pop,int gmax,double crossoverProb,double mutationRate)
{
HashMap<Integer,double[]> segmentForEach=calcSelectionProbs(pop);
ArrayList<int[]> children=new ArrayList<int[]>();
ArrayList<int[]> tempParents=new ArrayList<int[]>();
while(children.size()<pop.size())
{
//selection phase:select two parents each time.
for(int i=0;i<2;i++)
{
double prob = new Random().nextDouble();
for (int j = 0; j < pop.size(); j++)
{
if (isBetween(prob, segmentForEach.get(j)))
{
tempParents.add(pop.get(j));
break;
}
}
}
//cross-over phase.
int[] p1,p2,p1temp,p2temp;
p1= tempParents.get(tempParents.size() - 2).clone();
p1temp= tempParents.get(tempParents.size() - 2).clone();
p2 = tempParents.get(tempParents.size() -1).clone();
p2temp = tempParents.get(tempParents.size() -1).clone();
if(new Random().nextDouble()<crossoverProb)
{
int crossPosition = new Random().nextInt(cloudletList.size() - 1);
//cross-over operation
for (int i = crossPosition + 1; i < cloudletList.size(); i++)
{
int temp = p1temp[i];
p1temp[i] = p2temp[i];
p2temp[i] = temp;
}
}
//choose the children if they are better,else keep parents in next iteration.
children.add(getFitness(p1temp) < getFitness(p1) ? p1temp : p1);
children.add(getFitness(p2temp) < getFitness(p2) ? p2temp : p2);
// mutation phase.
if (new Random().nextDouble() < mutationRate)
{
// mutation operations bellow.
int maxIndex = children.size() - 1; for (int i = maxIndex - 1; i <= maxIndex; i++)
{
operateMutation(children.get(i), mutationRate);
}
}
} gmax--;
return gmax > 0 ? GA(children, gmax, crossoverProb, mutationRate): children;
} public static void operateMutation(int []child,double mutationRate)
{
if(new Random().nextDouble()<mutationRate)
{
int mutationIndex=new Random().nextInt(cloudletList.size());
int newVmId=new Random().nextInt(vmList.size());
while(child[mutationIndex]==newVmId)
{
newVmId=new Random().nextInt(vmList.size());
} child[mutationIndex]=newVmId;
}
} private static boolean isBetween(double prob,double[]segment)
{
if(segment[0]<=prob&&prob<=segment[1])
return true;
return false;
} private static HashMap<Integer,double[]> calcSelectionProbs(ArrayList<int[]> parents)
{
int size=parents.size();
double totalFitness=0;
ArrayList<Double> fits=new ArrayList<Double>();
HashMap<Integer,Double> probs=new HashMap<Integer,Double>(); for(int i=0;i<size;i++)
{
double fitness=getFitness(parents.get(i));
fits.add(fitness);
totalFitness+=fitness;
}
for(int i=0;i<size;i++)
{
probs.put(i,fits.get(i)/totalFitness );
} return getSegments(probs);
} private static HashMap<Integer,double[]> getSegments(HashMap<Integer,Double> probs)
{
HashMap<Integer,double[]> probSegments=new HashMap<Integer,double[]>();
//probSegments保存每个个体的选择概率的起点、终点,以便选择作为交配元素。
int size=probs.size();
double start=0;
double end=0;
for(int i=0;i<size;i++)
{
end=start+probs.get(i);
double[]segment=new double[2];
segment[0]=start;
segment[1]=end;
probSegments.put(i, segment);
start=end;
} return probSegments;
}

用遗传算法GA改进CloudSim自带的资源调度策略(2)的更多相关文章

  1. 用遗传算法GA改进CloudSim自带的资源调度策略

    首先理解云计算里,资源调度的含义: 看了很多云计算资源调度和任务调度方面的论文,发现很多情况下这两者的意义是相同的,不知道这两者是同一件事的不同表述还是我没分清吧,任务调度或者资源调度大概就是讲这样一 ...

  2. 遗传算法GA

    遗传算法(Genetic Algorithms,GA)是一种全局优化方法,它借用了生物遗传学的观点,通过自然选择.遗传.变异等作用机制,实现种群中个体适应性的提高,体现了自然界中“物竞天择.适者生存” ...

  3. 机器学习笔记之遗传算法(GA)

    遗传算法是一种大致基于模拟进化的学习方法,假设常被描述为二进制串.在遗传算法中,每一步都根据给定的适应度评估准则去评估当前的假设,然后用概率的方法选择适应度最高的假设作为产生下一代的种子.产生下一代的 ...

  4. 【比较】遗传算法GA和遗传编程GP有什么不同?

    遗传算法GA 本质上有一个固定的长度,这意味着所产生的功能有限的复杂性 通常会产生无效状态,因此需要以非破坏性方式处理这些状态 通常依赖于运算符优先级(例如,在我们的例子中,乘法发生在减法之前),这可 ...

  5. 【比较】粒子群算法PSO 和 遗传算法GA 的相同点和不同点

    目录 PSO和GA的相同点 PSO和GA不同点 粒子群算法(PSO)和遗传算法(GA)都是优化算法,都力图在自然特性的基础上模拟个体种群的适应性,它们都采用一定的变换规则通过搜索空间求解. PSO和G ...

  6. 【Unity】4.2 提升开发效率的捷径--导入 Unity 5.3.4 自带的资源包

    分类:Unity.C#.VS2015 创建日期:2016-04-06 一.简介 Unity自带的资源包也称为标准资源包.换言之,Unity自带的所有标准资源包导入到Unity项目中以后,都会放在Pro ...

  7. 使用Android自带的资源

    Android自带的资源文件有 :https://developer.android.google.cn/reference/android/R.html 代码中使用如下: 1.查看源代码的资源文件 ...

  8. 【优化算法】遗传算法GA求解混合流水车间调度问题(附C++代码)

    00 前言 各位读者大家好,好久没有介绍算法的推文了,感觉愧对了读者们热爱学习的心灵.于是,今天我们带来了一个神奇的优化算法--遗传算法! 它的优点包括但不限于: 遗传算法对所求解的优化问题没有太多的 ...

  9. 用gulp打包带参数资源做法与asp.net/java项目结合的自动构建方案探讨

    先探讨方案,后续再实现. gulp打包前端教程配置:http://www.cnblogs.com/EasonJim/p/6209951.html 可能存在以下场景: 1.整个服务端采用接口的形式暴露给 ...

随机推荐

  1. Spring + SpringMVC + Druid + MyBatis 给你一个灵活的后端解决方案

    生命不息,折腾不止. 折腾能遇到很多坑,填坑我理解为成长. 两个月前自己倒腾了一套用开源框架构建的 JavaWeb 后端解决方案. Spring + SpringMVC + Druid + JPA(H ...

  2. 星云opencv总结

  3. LTE中的各种ID含义

    原文链接:http://www.hropt.com/ask/?q-7128.html ECI (28 Bits) = eNB ID(20 Bits) + Cell ID(8 Bits) 换成16进制就 ...

  4. Windows Live Writer代码插件整理

    以下code插件命名按照 Windows Live Writer 中显示的插件名 1.Source code plug-in(cnblogs官方推荐) 界面: 效果: /** * Returns th ...

  5. 初步学习border-radius

    1.属性解析 border-radius是css3属性,他可以使div的角进行一定程度的弯曲. 比如说下面这个width和height的正方形div 经过设置border-radius之后四个角会出现 ...

  6. LoadRunner安装+汉化+破解

    因为工作需要要用到LoadRunner,找个好几个版本,换了两台电脑(公司的win7折腾了好久装不上去),耗时两天终于搞定了,分享给需要的小伙伴们,避免大家踩更多的坑~ 一.安装前的准备 Win10系 ...

  7. bzoj1500

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 12544  Solved: 3970[Submit][Statu ...

  8. 一.Jmeter+Ant+Jenkins搭建持续集成接口性能自动化测试

    微创新作品信息 1)微创新作品描述 A.为什么诞生: 1. 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换, ...

  9. QQ,微信第三方登陆

    感觉越是大公司的SDK越不好用,其实我也是一直在想为什么他们拿那么高的工资却干着不相应的事儿. 下面说下QQ和微信第三方登陆的一点坑 首先 (QQ互联)自带的sdk中  一个文件工程没有调用产生关联错 ...

  10. crodova打包apk个人总结

    1.安装nodejs 2.安装 cordova npm install -g cordova 3.安装Java JDK,官网下载地址 系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安 ...