版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;class
2、 Best (public int generations; /最佳适应值public String str;/最佳染色体public double fitness; /最佳适应值public class SGAFrame extends JFrame (private JTextArea textArea;private String str =;private Best best = null;/最佳染色体private String ipop = new String10;/染色体private int gernation = 0;染色体public static final int G
3、ENE = 5; /基因数/* Launch the application* param args*/public static void main(String args) (try SGAFrame frame = new SGAFrame(); frame.setVisible(true);catch (Exception e) e.printStackTrace();/*/public SGAFrame() (super();this.ipop = inialPops();getContentPane().setLayout(null);setBounds(100, 100, 461
4、, 277);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JLabel label = new JLabel();label.setText(X 的区间:);label.setBounds(23, 10, 88, 15);getContentPane().add(label);final JLabel label_1 = new JLabel();label_1.setText(0,1);label_1.setBounds(92, 10, 84, 15);getContentPane().add(label_1);final JBu
5、tton button = new JButton();button.addActionListener(new ActionListener() (public void actionPerformed(final ActionEvent e) ( SGAFrame s = new SGAFrame();str = str + cess() + n;textArea.setText(str););button.setText(求最大值);button.setBounds(323, 27, 99, 23);getContentPane().add(button);final JLab
6、el label_2 = new JLabel();label_2.setText(利用标准遗传算法求解函数f(x)=xA2的最大值:);label_2.setBounds(23, 31, 318, 15);getContentPane().add(label_2);final JPanel panel = new JPanel(); panel.setLayout(new BorderLayout();panel.setBounds(23, 65, 399, 164); getContentPane().add(panel);final JScrollPane scrollPane = ne
7、w JScrollPane(); panel.add(scrollPane, BorderLayout.CENTER);textArea = new JTextArea();scrollPane.setViewportView(textArea);/*初始化一条染色体(用二进制字符串表示)* return 一条染色体*/private String inialPop() (String res =;for (int i = 0; i 0.5) res += 0; else res += 1;return res;/*初始化一组染色体* return染色体组*/private String in
8、ialPops() String ipop = new String10;for (int i = 0; i 10; i+) ipopi = inialPop();return ipop;/*将染色体转换成x的值* param str 染色体* return染色体的适应值*/private double calculatefitnessvalue(String str) (int b = Integer.parseInt(str, 2);double x =b;double fitness =x*x;return fitness;/*计算群体上每个个体的适应度值;*按由个体适应度值所决定的某个
9、规则选择将进入下一代的个体;*/private void select() (double evals = new double10; / 所有染色体适应值double p = new double10;/ 各染色体选择概率double q = new double10;/ 累计概率double F = 0;/累计适应值总和for (int i = 0; i best.fitness)( / 记录最优个体best.fitness = evalsi;best.generations = gernation;best.str = ipopi;F = F + evalsi; /所有染色体适应值总和f
10、or (int i = 0; i 10; i+) (pi = evalsi / F;if (i = 0)qi = pi;else (qi = qi - 1 + pi;for (int i = 0; i 10; i+) (double r = Math.random();if (r = q0) (ipopi = ipop0;else (for (int j = 1; j 10; j+) (if (r qj)(ipopi = ipopj;break;/*交叉操作*交叉率为25%,平均为25%的染色体进行交叉*/private void cross() (String temp1, temp2;fo
11、r (int i = 0; i 10; i+) (if (Math.random() 0.25) (double r = Math.random();int pos = (int) (Math.round(r * 1000) % GENE;if (pos = 0) (pos = 1;temp1 = ipopi.substring(0, pos)+ ipop(i + 1) % 10.substring(pos);temp2 = ipop(i + 1) % 10.substring(0, pos)+ ipopi.substring(pos); ipopi = temp1;ipop(i + 1) /
12、 10 = temp2;/*基因变异操作* */private void mutation() (for (int i = 0; i = 10) chromosomeNum = 9;String temp;if (ipopchromosomeNum.charAt(mutationNum - 1) = 0) (if (mutationNum = 1) (temp = 1 + ipopchromosomeNum.substring(mutationNum);else (if (mutationNum != GENE) (temp = ipopchromosomeNum.substring(0, m
13、utationNum -1) + 1+ ipopchromosomeNum.substring(mutationNum);else (temp = ipopchromosomeNum.substring(0, mutationNum -1) + 1;else (if (mutationNum = 1) (temp = 0 + ipopchromosomeNum.substring(mutationNum);else (if (mutationNum != GENE) (temp = ipopchromosomeNum.substring(0, mutationNum -1) + 0+ ipopchromosomeNum.substring(mutationNum);else (temp = ipopchromosomeNum.substring(0, mutation
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消防安全义务履行指南
- 2025-2026学年湖南省郴州市临武县九年级(上)期中化学试卷(含答案)
- 先进制造技术相关
- 企划部培训计划
- 肺癌手术护理题库及答案
- 2024北京初二(下)期末物理汇编:机械能(北师大版)
- 【初中 物理】2025-2026学年人教版物理八年级上学期期末模拟练习卷
- 小学五年级语文上册第八单元单元阅读进度监控课件
- 2025年“才聚齐鲁成就未来”山东土地东方发展集团有限公司招聘备考题库及答案详解(易错题)
- 深度解析(2026)《GBT 34096-2017手机支付 基于2.45GHz RCC(限域通信)技术的非接触式读写器终端测试方法》
- 【MOOC】《大学足球》(西南交通大学)章节期末慕课答案
- 《设计创新人才职业能力要求》
- 钢筋混凝土结构课程设计计算书
- 内蒙古中考数学三年(2023-2025)真题分类汇编:专题02 几何初步、相交线与平行线、概率与统计(解析版)
- 中学八年级英语重点词汇与语法解析
- 生产车间节能知识培训课件
- 寄售管理制度及流程
- 公共文化服务质量评价指标体系研究-洞察及研究
- 2025年上海市高考英语试卷及参考答案(完整版)
- 眼科进修结业汇报
- 管桩(方桩)静压施工风险辨识和分析及应对措施
评论
0/150
提交评论