资源简介
爬山算法和禁忌搜索对比代码+数据,可运行
![](http://www.nz998.com/pic/36691.jpg)
代码片段和文件信息
package tabu;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class HillClimbing {
private int MAX_GEN;// 迭代次数
private int cityNum; // 城市数量编码长度
private int[][] distance; // 距离矩阵
private int bestT;// 最佳出现代数
private int[] bestGh;// 最好的路径编码
private int bestEvaluation;
private Random random;
public HillClimbing() {
}
/**
* constructor of GA
*
* @param n
* 城市数量
* @param g
* 运行代数
*
**/
public HillClimbing(int n int g) {
cityNum = n;
MAX_GEN = g;
}
// 给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默
@SuppressWarnings(“resource“)
/**
* 初始化HillClimbing算法类
* @param filename 数据文件名,该文件存储所有城市节点坐标数据
* @throws IOException
*/
private void init(String filename) throws IOException {
// 读取数据
int[] x;
int[] y;
String strbuff;
BufferedReader data = new BufferedReader(new InputStreamReader(
new FileInputStream(filename)));
distance = new int[cityNum][cityNum];
x = new int[cityNum];
y = new int[cityNum];
for (int i = 0; i < cityNum; i++) {
// 读取一行数据,数据格式1 6734 1453
strbuff = data.readLine();
// 字符分割
String[] strcol = strbuff.split(“ “);
x[i] = Integer.valueOf(strcol[1]);// x坐标
y[i] = Integer.valueOf(strcol[2]);// y坐标
}
// 计算距离矩阵
// 针对具体问题,距离计算方法也不一样,
// 此处用的是att48作为案例,它有48个城市,距离计算方法为伪欧氏距离,最优值为10628
for (int i = 0; i < cityNum - 1; i++) {
distance[i][i] = 0; // 对角线为0
for (int j = i + 1; j < cityNum; j++) {
double rij = Math
.sqrt(((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j])
* (y[i] - y[j])) / 10.0);
// 四舍五入,取整
int tij = (int) Math.round(rij);
if (tij < rij) {
distance[i][j] = tij + 1;
distance[j][i] = distance[i][j];
} else {
distance[i][j] = tij;
distance[j][i] = distance[i][j];
}
}
}
distance[cityNum - 1][cityNum - 1] = 0;
bestGh = new int[cityNum];
bestEvaluation = Integer.MAX_VALUE;
bestT = 0;
random = new Random(System.currentTimeMillis());
}
// 初始化编码Ghh
void initGroup() {
int i j;
bestGh[0] = random.nextInt(65535) % cityNum;
for (i = 1; i < cityNum;)// 编码长度
{
bestGh[i] = random.nextInt(65535) % cityNum;
for (j = 0; j < i; j++) { //排除第i个和前面有重合
if (bestGh[i] == bestGh[j]) {
break;
}
}
if (j == i) {
i++;
}
}
}
public int evaluate(int[] chr) {
int len = 0;
// 染色体,起始城市城市1城市2...城市n
for (int i = 1; i < cityNum; i++) {
len += distance[chr[i - 1]][chr[i]];
}
// 城市n起始城市
len += distance[chr[cityNum - 1]][chr[0]];
return len;
}
// 爬山算法
public void pashan(int[] Gh int T) {
int i temp tt = 0;
int ran1 ran2;
int e;// 评价新值
int[] tempGh = new int[cityNum];
bestEvaluation = evaluate(Gh);
// 爬山代数T
for (tt = 0; tt < T; tt++) {
for (i = 0; i < cityNum; i++)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 602 2017-07-11 17:19 att48.txt
文件 9173 2017-07-12 17:35 Tabu.java
文件 4461 2017-07-11 20:27 HillClimbing.java
- 上一篇:dnw_ubuntu
- 下一篇:listview 实现多按钮操作
相关资源
- SVR算法程序可运行
- 计算机图形学 边填充算法实现代码
- 福建师范大学历年算法考卷
- 栈的实现及应用,六种基本算法
- Bresenham算法绘制线段并利用“橡皮筋
- 介绍几种压缩算法及《笨笨数据压缩
- 改进的BP神经网络算法
- A星算法_原理讲解_例子
- 云模型的相关算法cloud
- 旋转矩阵求欧拉角的简单算法
- 栅栏填充算法源码(VC)
- RSA算法源码
- 关联分析Apriori算法实现
- [免费]relax算法成像
- 操作系统 LRU算法 实验报告 及 程序代
- 分治法快速排序算法QuickSort C
- 现代谱估计算法 music ESPRIT 谐波分解
- MUSIC算法c 实现
- 007出纳管理系统 v7[1].5.94 算法注册机
- 克鲁斯卡尔算法C和C 实现代码
- capon波束形成算法-VC实现
- QGA 量子遗传算法
- 利用OpenGL写毛笔字算法
- 带头结点的单链表的c算法实现
- 自适应隐写算法wow
- 协同过滤算法源码
- RSA AES DES ECC加密算法源码
- 密码学课程设计:DES加密解密算法的
- 北航人工智能原理课大作业源代码,
- A*算法的2D演示(带源码)
评论
共有 条评论