用遗传算法GA改进CloudSim自带的资源调度策略(2)
完整的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)的更多相关文章
- 用遗传算法GA改进CloudSim自带的资源调度策略
首先理解云计算里,资源调度的含义: 看了很多云计算资源调度和任务调度方面的论文,发现很多情况下这两者的意义是相同的,不知道这两者是同一件事的不同表述还是我没分清吧,任务调度或者资源调度大概就是讲这样一 ...
- 遗传算法GA
遗传算法(Genetic Algorithms,GA)是一种全局优化方法,它借用了生物遗传学的观点,通过自然选择.遗传.变异等作用机制,实现种群中个体适应性的提高,体现了自然界中“物竞天择.适者生存” ...
- 机器学习笔记之遗传算法(GA)
遗传算法是一种大致基于模拟进化的学习方法,假设常被描述为二进制串.在遗传算法中,每一步都根据给定的适应度评估准则去评估当前的假设,然后用概率的方法选择适应度最高的假设作为产生下一代的种子.产生下一代的 ...
- 【比较】遗传算法GA和遗传编程GP有什么不同?
遗传算法GA 本质上有一个固定的长度,这意味着所产生的功能有限的复杂性 通常会产生无效状态,因此需要以非破坏性方式处理这些状态 通常依赖于运算符优先级(例如,在我们的例子中,乘法发生在减法之前),这可 ...
- 【比较】粒子群算法PSO 和 遗传算法GA 的相同点和不同点
目录 PSO和GA的相同点 PSO和GA不同点 粒子群算法(PSO)和遗传算法(GA)都是优化算法,都力图在自然特性的基础上模拟个体种群的适应性,它们都采用一定的变换规则通过搜索空间求解. PSO和G ...
- 【Unity】4.2 提升开发效率的捷径--导入 Unity 5.3.4 自带的资源包
分类:Unity.C#.VS2015 创建日期:2016-04-06 一.简介 Unity自带的资源包也称为标准资源包.换言之,Unity自带的所有标准资源包导入到Unity项目中以后,都会放在Pro ...
- 使用Android自带的资源
Android自带的资源文件有 :https://developer.android.google.cn/reference/android/R.html 代码中使用如下: 1.查看源代码的资源文件 ...
- 【优化算法】遗传算法GA求解混合流水车间调度问题(附C++代码)
00 前言 各位读者大家好,好久没有介绍算法的推文了,感觉愧对了读者们热爱学习的心灵.于是,今天我们带来了一个神奇的优化算法--遗传算法! 它的优点包括但不限于: 遗传算法对所求解的优化问题没有太多的 ...
- 用gulp打包带参数资源做法与asp.net/java项目结合的自动构建方案探讨
先探讨方案,后续再实现. gulp打包前端教程配置:http://www.cnblogs.com/EasonJim/p/6209951.html 可能存在以下场景: 1.整个服务端采用接口的形式暴露给 ...
随机推荐
- JavaScript模板引擎artTemplate.js——是否编码输出html字符
template.config(name, value)方法用于更改引擎的默认配置. 其中字段escape,类型为boolean,默认为true. 首先,我们不修改配置信息输出一段带有html标签的字 ...
- maven配置和下载
下载链接:http://maven.apache.org/docs/ 以maven3.0.4为例,eclipse以kepler为例 环境变量的配置 1.系统变量-新建-变量名:MAVEN_HOME-变 ...
- CentOS安装Maven
现有的一个项目使用了Maven来管理,源代码放到了Subversion中.虽然Maven管理项目很方便,但是部署起来还是很麻烦的.先要在本地生成项目jar包,上传到服务器,然后再重启服务.如果在服务器 ...
- jquery和zepto的扩展方法extend
jquery和zepto的扩展方法extend 总结下jQuery(3.1.1)和zepto(1.1.6)到底是如何来开放接口,使之可以进行扩展,两者都会有类型判断,本文使用简单的类型判断,暂不考虑兼 ...
- Kinect2.0 for Mac开箱
前段时间从米国带回来一个Kinect,坑爹地发现需要适配器才能连接电脑.于是又从微软官网下单了适配器.今天终于在Mac上把Kinect装起来跑了,与大家分享一点图片. Kinect驱动安装 Kinec ...
- phylogeny analysis
Multiple Alignment: MUSCLE ProbCons T-Coffee ClustalW Alignment curation: Gblocks Remove positions w ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- mybatis-generator 1.3.5支持流式 fluent 方法
在以往的无数此写model的过程中,大家都会烦恼model的set方法写一堆.比如 Person p = new Person(); p.setName("name"); p.se ...
- django数据库操作和中间件
数据库配置 django的数据库相关表配置在models.py文件中,数据库的连接相关信息配置在settings.py中 models.py相关相关参数配置 from django.db import ...
- 数据结构作业——Fresh Meat(优先队列)
Fresh Meat Description 我们故事的主角是屠夫扒鸡,起初屠夫扒鸡只是一个佣兵,他先去拜了太上老君为师,学了一技能肉钩,凭着一技肉钩驰骋决斗场,达到一段以后到阿尔伯特那里偷学了二技能 ...