Java运行时CPU只占用33%左右

芝麻创意 2017-09-16 08:52:22
代码如下
package noah;

import java.io.*;
import java.util.Random;
public class Tabu {

private int MAX_GEN;// 迭代次数
private int N;// 每次搜索邻居个数
private int ll;// 禁忌长度
private int cityNum; // 城市数量,编码长度

private int[][] distance; // 距离矩阵
private int bestT;// 最佳出现代数

private int[] Ghh;// 初始路径编码
private int[] bestGh;// 最好的路径编码
private int bestEvaluation;
private int[] LocalGhh;// 当代最好编码
private int localEvaluation;
private int[] tempGhh;// 存放临时编码
private int tempEvaluation;

private int[][] jinji;// 禁忌表

private int t;// 当前代数
private long timestamp;// 时间

private Random random;

public Tabu() {

}

/**
* constructor of GA
*
* @param n
* 城市数量
* @param g
* 运行代数
* @param c
* 每次搜索邻居个数
* @param m
* 禁忌长度
*
**/
public Tabu(int n, int g, int c, int m) {
cityNum = n;
MAX_GEN = g;
N = c;
ll = m;
}

// 给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默
@SuppressWarnings("resource")
/**
* 初始化Tabu算法类
* @param filename 数据文件名,该文件存储所有城市节点坐标数据
* @throws IOException
*/
private void init(String filename) throws IOException {
// 读取数据
float[] x;
float[] y;
String[] name;
String strbuff;
distance = new int[cityNum][cityNum];
x = new float[cityNum];
y = new float[cityNum];
name = new String[cityNum];

BufferedReader brname;
try {
brname = new BufferedReader(new FileReader(filename));// 读取NAMEID对应值
String sname = null;
int k = 0;
while ((sname = brname.readLine()) != null) {
String[] strcol = sname.split(" ");
name[k] = String.valueOf(strcol[0]);// 名字
x[k] = Float.valueOf(strcol[1]);// 维度
y[k] = Float.valueOf(strcol[2]);//经度
k++;
}
brname.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// 计算距离矩阵
// ,针对具体问题,距离计算方法也不一样,此处用的是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 EARTH_RADIUS = 6370.996; // 地球半径系数单位/公里
double PI = 3.1415926;
double radLat1 = x[i] * PI / 180.0;
double radLat2 = x[j] * PI / 180.0;
double radLng1 = y[i] * PI / 180.0;
double radLng2 = y[j] * PI /180.0;
double a = radLat1 - radLat2;
double b = radLng1 - radLng2;
double distanceFloat = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)));
distanceFloat = distanceFloat * EARTH_RADIUS;
int intdistance = (int) Math.round(distanceFloat);
if (intdistance < distanceFloat) {
distance[i][j] = intdistance + 1;
distance[j][i] = distance[i][j];
// System.out.println("最佳路径:"+distance[i][j]);
// System.out.println("最佳路径:"+x[i]);
// System.out.println("最佳路径:"+y[i]);
// System.out.println("最佳路径:"+x[j]);
// System.out.println("最佳路径:"+y[j]);
// System.exit(0);
} else {
distance[i][j] = intdistance;
distance[j][i] = distance[i][j];
}
}
}
distance[cityNum - 1][cityNum - 1] = 0;

Ghh = new int[cityNum];
bestGh = new int[cityNum];
bestEvaluation = Integer.MAX_VALUE;
LocalGhh = new int[cityNum];
localEvaluation = Integer.MAX_VALUE;
tempGhh = new int[cityNum];
tempEvaluation = Integer.MAX_VALUE;

jinji = new int[ll][cityNum];
bestT = 0;
t = 0;

random = new Random(System.currentTimeMillis());
/*
* for(int i=0;i<cityNum;i++) { for(int j=0;j<cityNum;j++) {
* System.out.print(distance[i][j]+","); } System.out.println(); }
*/

}

// 初始化编码Ghh
void initGroup() {
int i, j;
Ghh[0] = random.nextInt(65535) % cityNum;
for (i = 1; i < cityNum;)// 编码长度
{
Ghh[i] = random.nextInt(65535) % cityNum;
for (j = 0; j < i; j++) {
if (Ghh[i] == Ghh[j]) {
break;
}
}
if (j == i) {
i++;
}
}
}

// 复制编码体,复制编码Gha到Ghb
public void copyGh(int[] Gha, int[] Ghb) {
for (int i = 0; i < cityNum; i++) {
Ghb[i] = Gha[i];
}
}

public int evaluate(int[] chr) {
// 0123
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 Linju(int[] Gh, int[] tempGh) {
int i, temp;
int ran1, ran2;

for (i = 0; i < cityNum; i++) {
tempGh[i] = Gh[i];
}
ran1 = random.nextInt(65535) % cityNum;
ran2 = random.nextInt(65535) % cityNum;
while (ran1 == ran2) {
ran2 = random.nextInt(65535) % cityNum;
}
temp = tempGh[ran1];
tempGh[ran1] = tempGh[ran2];
tempGh[ran2] = temp;
}

// 判断编码是否在禁忌表中
public int panduan(int[] tempGh) {
int i, j;
int flag = 0;
for (i = 0; i < ll; i++) {
flag = 0;
for (j = 0; j < cityNum; j++) {
if (tempGh[j] != jinji[i][j]) {
flag = 1;// 不相同
break;
}
}
if (flag == 0)// 相同,返回存在相同
{
// return 1;
break;
}
}
if (i == ll)// 不等
{
return 0;// 不存在
} else {
return 1;// 存在
}
}

// 解禁忌与加入禁忌
public void jiejinji(int[] tempGh) {
int i, j, k;
// 删除禁忌表第一个编码,后面编码往前挪动
for (i = 0; i < ll - 1; i++) {
for (j = 0; j < cityNum; j++) {
jinji[i][j] = jinji[i + 1][j];
}
}

// 新的编码加入禁忌表
for (k = 0; k < cityNum; k++) {
jinji[ll - 1][k] = tempGh[k];
}

}

public void solve() {
int nn;
timestamp = System.currentTimeMillis()/1000000;
// 初始化编码Ghh
initGroup();
copyGh(Ghh, bestGh);// 复制当前编码Ghh到最好编码bestGh
bestEvaluation = evaluate(Ghh);

while (t < MAX_GEN) {
nn = 0;
localEvaluation = Integer.MAX_VALUE;
while (nn < N) {
Linju(Ghh, tempGhh);// 得到当前编码Ghh的邻域编码tempGhh
if (panduan(tempGhh) == 0)// 判断编码是否在禁忌表中
{
// 不在
tempEvaluation = evaluate(tempGhh);
if (tempEvaluation < localEvaluation) {
copyGh(tempGhh, LocalGhh);
localEvaluation = tempEvaluation;
}
nn++;
}
}
if (localEvaluation < bestEvaluation) {
bestT = t;
copyGh(LocalGhh, bestGh);
bestEvaluation = localEvaluation;
}
copyGh(LocalGhh, Ghh);

// 解禁忌表,LocalGhh加入禁忌表
jiejinji(LocalGhh);
t++;
}

timestamp = System.currentTimeMillis()/1000000 - timestamp;

System.out.println("最佳长度出现代数:");
System.out.println(bestT);
System.out.println("最佳长度");
System.out.println(bestEvaluation);
System.out.println("最佳路径:");
for (int i = 0; i < cityNum; i++) {
String args = "->";
if(i + 1 == cityNum)
{
args = "";
}
System.out.print(bestGh[i] + args);
}
System.out.println("\n任务耗时:"+timestamp);
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("Start....");
Tabu tabu = new Tabu(34, 10, 200, 10);
tabu.init("data/citydata.txt");
tabu.solve();
}
}
...全文
281 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
可乐罐 2017-09-19
  • 打赏
  • 举报
回复
楼主不是来提问的,是来炫耀代码
小伙真帅 2017-09-16
  • 打赏
  • 举报
回复
不懂得提问,就永远没有人会回复你的问题。 贴一堆 代码上来,还没有人知道你想问什么。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧