完整的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. IFC是什么

    IFC是用EXPRESS语言来描述的一种数据格式 IFC的物理文件 为了数据交换的目的,STEP标准Prat 21规定了正文文件的结构,认为一个STEP文件或一个Part 21文件包括两端:头段和数据 ...

  2. iOS10推送通知适配

    iOS10推送新增了UserNotifications Framework,使用起来其实很简单. 只是在iOS10以上系统上点击通知栏,回调方法不再走原来的这两个方法 - (void)applicat ...

  3. java多线程通信 例子

    package com.cl.www.thread; public class NumberHolder { private Integer number = 0; // 增加number publi ...

  4. android-配置文件AndroidManifest.xml

    AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实 ...

  5. git的基本介绍和使用

    前言:从事iOS开发一年多以来,一直使用svn管理源代码.对svn的特点和弊端已经深有体会.前些天双十二前后,项目工期紧张到爆,起早贪黑的加班,可谓披星戴月,这还不止,回到家中还要疯狂的敲代码.那么问 ...

  6. Servlet学习:实现分页效果的方法

    分页的算法:需要定义四个变量,它们有各自的用处int pageSize:每页显示多少条记录int pageNow:希望显示第几页int pageCount:一共有多少页int rowCount:一共有 ...

  7. VR、AR、MR的区别

    VR.AR.MR定义: 什么是虚拟现实? 虚拟现实(Virtual Reality,简称VR,又译作灵境.幻真)是近年来出现的高新技术,也称灵境技术或人工环境.虚拟现实是利用电脑模拟产生一个三维空间的 ...

  8. Brackets

    按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...

  9. javascript操作字符串的方法

    string.indexOf()//返回字符串中第一个与给定子串匹配的子串序号字符串的IndexOf()方法搜索在该字符串上是否出现了作为参数传递的字符串,如果找到字符串,则返回字符的起始位置 (0表 ...

  10. linux下配置yun源

    备份原yum源   /etc/yum.repos.d/centos一base.repo 下载yum源       wagt 源网址/源名称/etc/yum.repos.d/原yum名