没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
河南理工大学
计算机科学与技术学院
课程设计报告
2015— 2016 学年第一学期
课程名称 Java
语言程序设计
设计题目 利用粒子群算法优化函数
姓 名 任晓兵
学 号 321509010205
专业班级 计算机(本) 1502
指导教师 刘 志 中
2015 年 1 月 6 日
1
目录
一.课程设计内容.............................................................................................................................12
(一)课程设计题目......................................................................................................................12
(二)课程设计目的.....................................................................................................................12
(三)课程设计要求......................................................................................................................12
二.算法相关知识.............................................................................................................................12
(一) 粒子群算法简介.................................................................................................................12
PSO 算法............................................................................................................................12
(二) 粒子群算法的运算流程....................................................................................................12
三.算法的 JAVA 实现.........................................................................................................................13
比较编辑:........................................................................................................................................13
演化计算技术.....................................................................................................................................13
(二)主要程序代码......................................................................................................................15
1.主函数..............................................................................................................................15
3.代码实现.........................................................................................................................15
//package noah;...........................................................................................................................15
.....................................................................................................................................................15
import java.io.BufferedReader;..................................................................................................15
import java.io.FileInputStream;..................................................................................................15
import java.io.IOException;........................................................................................................15
import java.io.InputStreamReader;.............................................................................................15
import java.util.ArrayList;..........................................................................................................15
import java.util.Random;............................................................................................................15
.....................................................................................................................................................15
public class PSO {.......................................................................................................................15
.....................................................................................................................................................15
private int bestNum;...................................................................................................................15
private float w;...........................................................................................................................15
private int MAX_GEN;// 迭代次数..........................................................................................15
private int scale;// 种群规模......................................................................................................16
.....................................................................................................................................................16
private int cityNum; // 城市数量,编码长度...........................................................................16
private int t;// 当前代数.............................................................................................................16
.....................................................................................................................................................16
private int[][] distance; // 距离矩阵..........................................................................................16
.....................................................................................................................................................16
private int[][] oPopulation;// 粒子群.........................................................................................16
private ArrayList<ArrayList<SO>> listV;// 每科粒子的初始交换序列.................................16
.....................................................................................................................................................16
private int[][] Pd;// 一颗粒子历代中出现最好的解,............................................................16
2
private int[] vPd;// 解的评价值.................................................................................................16
.....................................................................................................................................................16
private int[] Pgd;// 整个粒子群经历过的的最好的解,每个粒子都能记住自己搜索到的最
好解.............................................................................................................................................16
private int vPgd;// 最好的解的评价值.....................................................................................16
private int bestT;// 最佳出现代数.............................................................................................16
.....................................................................................................................................................16
private int[] fitness;// 种群适应度,表示种群中各个个体的适应度....................................16
.....................................................................................................................................................16
private Random random;............................................................................................................16
.....................................................................................................................................................16
public PSO() {............................................................................................................................16
.....................................................................................................................................................16
}..................................................................................................................................................16
.....................................................................................................................................................16
/**...............................................................................................................................................16
* constructor of GA....................................................................................................................16
*..................................................................................................................................................16
* @param n................................................................................................................................16
* 城市数量.................................................................................................................................16
* @param g................................................................................................................................16
* 运行代数.................................................................................................................................16
* @param w...............................................................................................................................16
* 权重.........................................................................................................................................16
**/...............................................................................................................................................16
public PSO(int n, int g, int s, float w) {.....................................................................................16
this.cityNum = n;........................................................................................................................16
this.MAX_GEN = g;..................................................................................................................16
this.scale = s;..............................................................................................................................16
this.w = w;..................................................................................................................................16
}..................................................................................................................................................16
.....................................................................................................................................................16
// 给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默...............16
@SuppressWarnings("resource")...............................................................................................17
/**...............................................................................................................................................17
* 初始化 PSO 算法类................................................................................................................17
* @param filename 数据文件名,该文件存储所有城市节点坐标数据..............................17
* @throws IOException.............................................................................................................17
*/.................................................................................................................................................17
private void init(String filename) throws IOException {..........................................................17
// 读取数据.................................................................................................................................17
int[] x;.........................................................................................................................................17
int[] y;.........................................................................................................................................17
String strbuff;.............................................................................................................................17
3
BufferedReader data = new BufferedReader(new InputStreamReader(...................................17
new FileInputStream(filename)));.............................................................................................17
distance = new int[cityNum][cityNum];....................................................................................17
x = new int[cityNum];................................................................................................................17
y = new int[cityNum];................................................................................................................17
for (int i = 0; i < cityNum; i++) {..............................................................................................17
// 读取一行数据,数据格式 1 6734 1453...............................................................................17
strbuff = data.readLine();...........................................................................................................17
// 字符分割.................................................................................................................................17
String[] strcol = strbuff.split(" ");..............................................................................................17
x[i] = Integer.valueOf(strcol[1]);// x 坐标.................................................................................17
y[i] = Integer.valueOf(strcol[2]);// y 坐标.................................................................................17
}..................................................................................................................................................17
// 计算距离矩阵........................................................................................................................17
// ,针对具体问题,距离计算方法也不一样,此处用的是 att48 作为案例,它有 48 个城
市,距离计算方法为伪欧氏距离,最优值为 10628.............................................................17
for (int i = 0; i < cityNum - 1; i++) {.........................................................................................17
distance[i][i] = 0; // 对角线为 0................................................................................................17
for (int j = i + 1; j < cityNum; j++) {.........................................................................................17
double rij = Math........................................................................................................................17
.sqrt(((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]).............................................................................17
* (y[i] - y[j])) / 10.0);.................................................................................................................17
// 四舍五入,取整....................................................................................................................17
int tij = (int) Math.round(rij);.....................................................................................................17
if (tij < rij) {................................................................................................................................17
distance[i][j] = tij + 1;................................................................................................................17
distance[j][i] = distance[i][j];.....................................................................................................17
} else {........................................................................................................................................17
distance[i][j] = tij;......................................................................................................................17
distance[j][i] = distance[i][j];.....................................................................................................17
}..................................................................................................................................................17
}..................................................................................................................................................17
}..................................................................................................................................................17
distance[cityNum - 1][cityNum - 1] = 0;...................................................................................18
.....................................................................................................................................................18
oPopulation = new int[scale][cityNum];...................................................................................18
fitness = new int[scale];.............................................................................................................18
.....................................................................................................................................................18
Pd = new int[scale][cityNum];...................................................................................................18
vPd = new int[scale];.................................................................................................................18
.....................................................................................................................................................18
/*.................................................................................................................................................18
* for(int i=0;i<scale;i++) { vPd[i]=Integer.MAX_VALUE; }..................................................18
*/.................................................................................................................................................18
4
.....................................................................................................................................................18
Pgd = new int[cityNum];...........................................................................................................18
vPgd = Integer.MAX_VALUE;.................................................................................................18
.....................................................................................................................................................18
// nPopulation = new int[scale][cityNum];................................................................................18
.....................................................................................................................................................18
bestT = 0;....................................................................................................................................18
t = 0;...........................................................................................................................................18
.....................................................................................................................................................18
random = new Random(System.currentTimeMillis());.............................................................18
/*.................................................................................................................................................18
* for(int i=0;i<cityNum;i++) { for(int j=0;j<cityNum;j++) {...................................................18
* System.out.print(distance[i][j]+","); } System.out.println(); }..............................................18
*/.................................................................................................................................................18
.....................................................................................................................................................18
}..................................................................................................................................................18
.....................................................................................................................................................18
// 初始化种群,多种随机生成办法........................................................................................18
void initGroup() {......................................................................................................................18
int i, j, k;.....................................................................................................................................18
for (k = 0; k < scale; k++)// 种群数...........................................................................................18
{..................................................................................................................................................18
oPopulation[k][0] = random.nextInt(65535) % cityNum;........................................................18
for (i = 1; i < cityNum;)// 粒子个数..........................................................................................18
{..................................................................................................................................................18
oPopulation[k][i] = random.nextInt(65535) % cityNum;.........................................................18
for (j = 0; j < i; j++) {.................................................................................................................18
if (oPopulation[k][i] == oPopulation[k][j]) {............................................................................18
break;..........................................................................................................................................18
}..................................................................................................................................................18
}..................................................................................................................................................18
if (j == i) {..................................................................................................................................18
i++;.............................................................................................................................................18
}..................................................................................................................................................19
}..................................................................................................................................................19
}..................................................................................................................................................19
.....................................................................................................................................................19
/*.................................................................................................................................................19
* for(i=0;i<scale;i++) { for(j=0;j<cityNum;j++) {...................................................................19
* System.out.print(oldPopulation[i][j]+","); } System.out.println(); }.....................................19
*/.................................................................................................................................................19
}..................................................................................................................................................19
.....................................................................................................................................................19
void initListV() {........................................................................................................................19
剩余26页未读,继续阅读
资源评论
qq_33703504
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python_免费开源加密交易机器人.zip
- Python_免费是指《帝国时代2》引擎的自由开源克隆版.zip
- Python_面向科学家和工程师的深度学习和强化学习库.zip
- Python_免费在线教科书的Jupyter笔记本为快速计算线性代数课程.zip
- 移动机器人路径规划(人工势场法),本次路径规划的代码是基于matlab语言的,该方法的基本思想是机器人受到来自目的地的引力以及受到来自障碍物的斥力 这 些力形成合力后驱使机器人避开障碍物后移向目的地
- 机械设计堆垛机sw18可编辑全套设计资料100%好用.zip.zip
- 多编组列车仿真,车体加载fluent里导出的气动力进行仿真 利用脚本建立fluent里的导出的气动力数据和simpack力元的接口进行快速的数据更新
- 编译原理词法分析的实现
- 电压电流双闭环NPC三电平逆变器的仿真,采用载波层叠调制
- Wincc报表模板 1、数据库存储全局脚本 存储时间自由设置 2、报表查询VBS脚本,带下拉框,组合框,查询内容自由选择,时间自由选择 3、导出到本地EXECL 并打印 4、各类控件,语音报警
- 数据快速拷贝软件,拷贝速度为WINDOWS的2-7倍 具有不间断、挑选拷贝、快速删除、剪切等功能
- MATLAB基础应用精讲-【智能优化算法】黏菌算法(SMA)(附MATLAB和python代码实现)
- CS1.5★ALAN装逼脚本Ver.2025.03.弹道版【ALT开压枪】
- 有源电力滤波器APF MATLAB仿真 选阶补偿,matlab版本V2014,基于LCL滤波器的I型三电平拓扑仿真模型,三相四线制,软件锁相环,C语言编程提取谐波指令,直流电压和中点电位控制稳定
- 修复版早起打卡+完美细教程+修复支付接口问题已完美运营
- 光储一体机仿真模型,光伏与储能在直流侧耦合,采用boost电路加NPC电路,基于MATLAB Simulink建模仿真 闭环控制仿真模型,可以进行功率调度和充放电控制 仿真模型使用MATLAB 2
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功