- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章java实现遗传算法实例分享(打印城市信息)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
代码如下
import java.util.*; public class Tsp { private String cityName[]={"北京","上海","天津","重庆","哈尔滨","长春","沈阳","呼和浩特","石家庄","太原","济南","郑州","西安","兰州","银川","西宁","乌鲁木齐","合肥","南京","杭州","长沙","南昌","武汉","成都","贵州","福建","台北","广州","海口","南宁","昆明","拉萨","香港","澳门"}; //private String cityEnd[]=new String[34]; private int cityNum=cityName.length; //城市个数 private int popSize = 50; //种群数量 private int maxgens = 20000; //迭代次数 private double pxover = 0.8; //交叉概率 private double pmultation = 0.05; //变异概率 private long[][] distance = new long[cityNum][cityNum]; private int range = 2000; //用于判断何时停止的数组区间 private class genotype { int city[] = new int[cityNum]; //单个基因的城市序列 long fitness; //该基因的适应度 double selectP; //选择概率 double exceptp; //期望概率 int isSelected; //是否被选择 } private genotype[] citys = new genotype[popSize]; /** * 构造函数,初始化种群 */ public Tsp() { for (int i = 0; i < popSize; i++) { citys[i] = new genotype(); int[] num = new int[cityNum]; for (int j = 0; j < cityNum; j++) num[j] = j; int temp = cityNum; for (int j = 0; j < cityNum; j++) { int r = (int) (Math.random() * temp); citys[i].city[j] = num[r]; num[r] = num[temp - 1]; temp--; } citys[i].fitness = 0; citys[i].selectP = 0; citys[i].exceptp = 0; citys[i].isSelected = 0; } initDistance(); } /** * 计算每个种群每个基因个体的适应度,选择概率,期望概率,和是否被选择。 */ public void CalAll(){ for( int i = 0; i< popSize; i++){ citys[i].fitness = 0; citys[i].selectP = 0; citys[i].exceptp = 0; citys[i].isSelected = 0; } CalFitness(); CalSelectP(); CalExceptP(); CalIsSelected(); } /** * 填充,将多选的填充到未选的个体当中 */ public void pad(){ int best = 0; int bad = 0; while(true){ while(citys[best].isSelected <= 1 && best<popSize-1) best ++; while(citys[bad].isSelected != 0 && bad<popSize-1) bad ++; for(int i = 0; i< cityNum; i++) citys[bad].city[i] = citys[best].city[i]; citys[best].isSelected --; citys[bad].isSelected ++; bad ++; if(best == popSize ||bad == popSize) break; } } /** * 交叉主体函数 */ public void crossover() { int x; int y; int pop = (int)(popSize* pxover /2); while(pop>0){ x = (int)(Math.random()*popSize); y = (int)(Math.random()*popSize); executeCrossover(x,y);//x y 两个体执行交叉 pop--; } } /** * 执行交叉函数 * @param 个体x * @param 个体y * 对个体x和个体y执行佳点集的交叉,从而产生下一代城市序列 */ private void executeCrossover(int x,int y){ int dimension = 0; for( int i = 0 ;i < cityNum; i++) if(citys[x].city[i] != citys[y].city[i]){ dimension ++; } int diffItem = 0; double[] diff = new double[dimension]; for( int i = 0 ;i < cityNum; i++){ if(citys[x].city[i] != citys[y].city[i]){ diff[diffItem] = citys[x].city[i]; citys[x].city[i] = -1; citys[y].city[i] = -1; diffItem ++; } } Arrays.sort(diff); double[] temp = new double[dimension]; temp = gp(x, dimension); for( int k = 0; k< dimension;k++) for( int j = 0; j< dimension; j++) if(temp[j] == k){ double item = temp[k]; temp[k] = temp[j]; temp[j] = item; item = diff[k]; diff[k] = diff[j]; diff[j] = item; } int tempDimension = dimension; int tempi = 0; while(tempDimension> 0 ){ if(citys[x].city[tempi] == -1){ citys[x].city[tempi] = (int)diff[dimension - tempDimension]; tempDimension --; } tempi ++; } Arrays.sort(diff); temp = gp(y, dimension); for( int k = 0; k< dimension;k++) for( int j = 0; j< dimension; j++) if(temp[j] == k){ double item = temp[k]; temp[k] = temp[j]; temp[j] = item; item = diff[k]; diff[k] = diff[j]; diff[j] = item; } tempDimension = dimension; tempi = 0; while(tempDimension> 0 ){ if(citys[y].city[tempi] == -1){ citys[y].city[tempi] = (int)diff[dimension - tempDimension]; tempDimension --; } tempi ++; } } /** * @param individual 个体 * @param dimension 维数 * @return 佳点集 (用于交叉函数的交叉点) 在executeCrossover()函数中使用 */ private double[] gp(int individual, int dimension){ double[] temp = new double[dimension]; double[] temp1 = new double[dimension]; int p = 2 * dimension + 3; while(!isSushu(p)) p++; for( int i = 0; i< dimension; i++){ temp[i] = 2*Math.cos(2*Math.PI*(i+1)/p) * (individual+1); temp[i] = temp[i] - (int)temp[i]; if( temp [i]< 0) temp[i] = 1+temp[i]; } for( int i = 0; i< dimension; i++) temp1[i] = temp[i]; Arrays.sort(temp1); //排序 for( int i = 0; i< dimension; i++) for( int j = 0; j< dimension; j++) if(temp[j]==temp1[i]) temp[j] = i; return temp; } /** * 变异 */ public void mutate(){ double random; int temp; int temp1; int temp2; for( int i = 0 ; i< popSize; i++){ random = Math.random(); if(random<=pmultation){ temp1 = (int)(Math.random() * (cityNum)); temp2 = (int)(Math.random() * (cityNum)); temp = citys[i].city[temp1]; citys[i].city[temp1] = citys[i].city[temp2]; citys[i].city[temp2] = temp; } } } /** * 打印当前代数的所有城市序列,以及其相关的参数 */ public void print(){ /** * 初始化各城市之间的距离 */ private void initDistance(){ for (int i = 0; i < cityNum; i++) { for (int j = 0; j < cityNum; j++){ distance[i][j] = Math.abs(i-j); } } } /** * 计算所有城市序列的适应度 */ private void CalFitness() { for (int i = 0; i < popSize; i++) { for (int j = 0; j < cityNum - 1; j++) citys[i].fitness += distance[citys[i].city[j]][citys[i].city[j + 1]]; citys[i].fitness += distance[citys[i].city[0]][citys[i].city[cityNum - 1]]; } } /** * 计算选择概率 */ private void CalSelectP(){ long sum = 0; for( int i = 0; i< popSize; i++) sum += citys[i].fitness; for( int i = 0; i< popSize; i++) citys[i].selectP = (double)citys[i].fitness/sum; } /** * 计算期望概率 */ private void CalExceptP(){ for( int i = 0; i< popSize; i++) citys[i].exceptp = (double)citys[i].selectP * popSize; } /** * 计算该城市序列是否较优,较优则被选择,进入下一代 */ private void CalIsSelected(){ int needSelecte = popSize; for( int i = 0; i< popSize; i++) if( citys[i].exceptp<1){ citys[i].isSelected++; needSelecte --; } double[] temp = new double[popSize]; for (int i = 0; i < popSize; i++) { // temp[i] = citys[i].exceptp - (int) citys[i].exceptp; // temp[i] *= 10; temp[i] = citys[i].exceptp*10; } int j = 0; while (needSelecte != 0) { for (int i = 0; i < popSize; i++) { if ((int) temp[i] == j) { citys[i].isSelected++; needSelecte--; if (needSelecte == 0) break; } } j++; } } /** * @param x * @return 判断一个数是否是素数的函数 */ private boolean isSushu( int x){ if(x<2) return false; for(int i=2;i<=x/2;i++) if(x%i==0&&x!=2) return false; return true; } /** * @param x 数组 * @return x数组的值是否全部相等,相等则表示x.length代的最优结果相同,则算法结束 */ private boolean isSame(long[] x){ for( int i = 0; i< x.length -1; i++) if(x[i] !=x[i+1]) return false; return true; } /** * 打印任意代最优的路径序列 */ private void printBestRoute(){ CalAll(); long temp = citys[0].fitness; int index = 0; for (int i = 1; i < popSize; i++) { if(citys[i].fitness<temp){ temp = citys[i].fitness; index = i; } } System.out.println(); System.out.println("最佳路径的序列:"); for (int j = 0; j < cityNum; j++) { String cityEnd[]={cityName[citys[index].city[j]]}; for(int m=0;m<cityEnd.length;m++) { System.out.print(cityEnd[m] + " "); } } //System.out.print(citys[index].city[j] + cityName[citys[index].city[j]] + " "); //System.out.print(cityName[citys[index].city[j]]); System.out.println(); } /** * 算法执行 */ public void run(){ long[] result = new long[range]; //result初始化为所有的数字都不相等 for( int i = 0; i< range; i++) result[i] = i; int index = 0; //数组中的位置 int num = 1; //第num代 while(maxgens>0){ System.out.println("----------------- 第 "+num+" 代 -------------------------"); CalAll(); print(); pad(); crossover(); mutate(); maxgens --; long temp = citys[0].fitness; for ( int i = 1; i< popSize; i++) if(citys[i].fitness<temp){ temp = citys[i].fitness; } System.out.println("最优的解:"+temp); result[index] = temp; if(isSame(result)) break; index++; if(index==range) index = 0; num++; } printBestRoute(); } /** * @param a 开始时间 * @param b 结束时间 */ public void CalTime(Calendar a,Calendar b){ long x = b.getTimeInMillis() - a.getTimeInMillis(); long y = x/1000; x = x - 1000*y; System.out.println("算法执行时间:"+y+"."+x+" 秒"); } /** * 程序入口 */ public static void main(String[] args) { Calendar a = Calendar.getInstance(); //开始时间 Tsp tsp = new Tsp(); tsp.run(); Calendar b = Calendar.getInstance(); //结束时间 tsp.CalTime(a, b); } } 。
最后此篇关于java实现遗传算法实例分享(打印城市信息)的文章就讲到这里了,如果你想了解更多关于java实现遗传算法实例分享(打印城市信息)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
滑动窗口限流 滑动窗口限流是一种常用的限流算法,通过维护一个固定大小的窗口,在单位时间内允许通过的请求次数不超过设定的阈值。具体来说,滑动窗口限流算法通常包括以下几个步骤: 初始化:设置窗口
表达式求值:一个只有+,-,*,/的表达式,没有括号 一种神奇的做法:使用数组存储数字和运算符,先把优先级别高的乘法和除法计算出来,再计算加法和减法 int GetVal(string s){
【算法】前缀和 题目 先来看一道题目:(前缀和模板题) 已知一个数组A[],现在想要求出其中一些数字的和。 输入格式: 先是整数N,M,表示一共有N个数字,有M组询问 接下来有N个数,表示A[1]..
1.前序遍历 根-左-右的顺序遍历,可以使用递归 void preOrder(Node *u){ if(u==NULL)return; printf("%d ",u->val);
先看题目 物品不能分隔,必须全部取走或者留下,因此称为01背包 (只有不取和取两种状态) 看第一个样例 我们需要把4个物品装入一个容量为10的背包 我们可以简化问题,从小到大入手分析 weightva
我最近在一次采访中遇到了这个问题: 给出以下矩阵: [[ R R R R R R], [ R B B B R R], [ B R R R B B], [ R B R R R R]] 找出是否有任
我正在尝试通过 C++ 算法从我的 outlook 帐户发送一封电子邮件,该帐户已经打开并记录,但真的不知道从哪里开始(对于 outlook-c++ 集成),谷歌也没有帮我这么多。任何提示将不胜感激。
我发现自己像这样编写了一个手工制作的 while 循环: std::list foo; // In my case, map, but list is simpler auto currentPoin
我有用于检测正方形的 opencv 代码。现在我想在检测正方形后,代码运行另一个命令。 代码如下: #include "cv.h" #include "cxcore.h" #include "high
我正在尝试模拟一个 matlab 函数“imfill”来填充二进制图像(1 和 0 的二维矩阵)。 我想在矩阵中指定一个起点,并像 imfill 的 4 连接版本那样进行洪水填充。 这是否已经存在于
我正在阅读 Robert Sedgewick 的《C++ 算法》。 Basic recurrences section it was mentioned as 这种循环出现在循环输入以消除一个项目的递
我正在思考如何在我的日历中生成代表任务的数据结构(仅供我个人使用)。我有来自 DBMS 的按日期排序的任务记录,如下所示: 买牛奶(18.1.2013) 任务日期 (2013-01-15) 任务标签(
输入一个未排序的整数数组A[1..n]只有 O(d) :(d int) 计算每个元素在单次迭代中出现在列表中的次数。 map 是balanced Binary Search Tree基于确保 O(nl
我遇到了一个问题,但我仍然不知道如何解决。我想出了如何用蛮力的方式来做到这一点,但是当有成千上万的元素时它就不起作用了。 Problem: Say you are given the followin
我有一个列表列表。 L1= [[...][...][.......].......]如果我在展平列表后获取所有元素并从中提取唯一值,那么我会得到一个列表 L2。我有另一个列表 L3,它是 L2 的某个
我们得到二维矩阵数组(假设长度为 i 和宽度为 j)和整数 k我们必须找到包含这个或更大总和的最小矩形的大小F.e k=7 4 1 1 1 1 1 4 4 Anwser是2,因为4+4=8 >= 7,
我实行 3 类倒制,每周换类。顺序为早类 (m)、晚类 (n) 和下午类 (a)。我固定的订单,即它永远不会改变,即使那个星期不工作也是如此。 我创建了一个函数来获取 ISO 周数。当我给它一个日期时
假设我们有一个输入,它是一个元素列表: {a, b, c, d, e, f} 还有不同的集合,可能包含这些元素的任意组合,也可能包含不在输入列表中的其他元素: A:{e,f} B:{d,f,a} C:
我有一个子集算法,可以找到给定集合的所有子集。原始集合的问题在于它是一个不断增长的集合,如果向其中添加元素,我需要再次重新计算它的子集。 有没有一种方法可以优化子集算法,该算法可以从最后一个计算点重新
我有一个包含 100 万个符号及其预期频率的表格。 我想通过为每个符号分配一个唯一(且前缀唯一)的可变长度位串来压缩这些符号的序列,然后将它们连接在一起以表示序列。 我想分配这些位串,以使编码序列的预
我是一名优秀的程序员,十分优秀!