javajava 一元二次方程问题

本帖子已过去太久远了,不再提供回复功能。问题:求解四元一次方程,例如
&&&&&& x+y-z=9;
&&&&&& x-y+2z=8;
&&&&&& 2x+y-z=7;
&&&&&& x+y+z=m;
这个解是 x=-2.0 y=32.0 z=21.0 m=51.0
&如果你想利用迭代暴力计算的话,不用试了,我替你试过了,太慢,慢的一天你也解不出来,比如x,y,z,m的范围是从-2.0到51.0
那么解决不了了吗?这时脑海中依稀想起来了大学学过线性代数,于是乎找美丽的度娘亲热了大半个下午,终于把线性代数部分知识拾起来了点 o(╯□╰)o
顺便也帮大家温习下,先来个入门简单的
PS:或许好多客官认为这个温习没必要,傻逼才看呢,可是我想说,等你想拓展这个程序,想要解5元一次方程,n元一次方程的时候,你绝对用得着。但是如果你精通高数,线性代数的话,算我没说。。。
然后再看下克拉默法则:
然后再看看代数余子式:
汗,最后步入正题,代码就简单了,其实就是套公式,看代码吧。。。
(注意要解的方程组把变量都放到左边,常亮放到右边,然后再写strss这个二维数组)
&* 解四元一次方程
&* @author liuyj
public class ArithmeticOfCalcFour {
public static void main(String[] args) {
// x+y-z=9;
// x-y+2z=8;
// 2x+y-z=7;
// x+y+z=m;
// 解x=-2,y=32,z=21,m=51
double[][] strss={
{1,1,-1,0,9},
& & {1,-1,2,0,8},
& & {2,1,-1,0,7},
& & {1,1,1,-1,0},
calculate(strss);
public static void calculate(double[][] strss) {
double[][] dss={
{strss[0][0],strss[0][1],strss[0][2],strss[0][3]},
{strss[1][0],strss[1][1],strss[1][2],strss[1][3]},
{strss[2][0],strss[2][1],strss[2][2],strss[2][3]},
{strss[3][0],strss[3][1],strss[3][2],strss[3][3]}
double[][] d1ss={
{strss[0][4],strss[0][1],strss[0][2],strss[0][3]},
{strss[1][4],strss[1][1],strss[1][2],strss[1][3]},
{strss[2][4],strss[2][1],strss[2][2],strss[2][3]},
{strss[3][4],strss[3][1],strss[3][2],strss[3][3]}
double[][] d2ss={
{strss[0][0],strss[0][4],strss[0][2],strss[0][3]},
{strss[1][0],strss[1][4],strss[1][2],strss[1][3]},
{strss[2][0],strss[2][4],strss[2][2],strss[2][3]},
{strss[3][0],strss[3][4],strss[3][2],strss[3][3]}
double[][] d3ss={
{strss[0][0],strss[0][1],strss[0][4],strss[0][3]},
{strss[1][0],strss[1][1],strss[1][4],strss[1][3]},
{strss[2][0],strss[2][1],strss[2][4],strss[2][3]},
{strss[3][0],strss[3][1],strss[3][4],strss[3][3]}
double[][] d4ss={
{strss[0][0],strss[0][1],strss[0][2],strss[0][4]},
{strss[1][0],strss[1][1],strss[1][2],strss[1][4]},
{strss[2][0],strss[2][1],strss[2][2],strss[2][4]},
{strss[3][0],strss[3][1],strss[3][2],strss[3][4]}
double d=calculateFour(dss);
System.out.println(&无解或多个解&);
double d1=calculateFour(d1ss);
double d2=calculateFour(d2ss);
double d3=calculateFour(d3ss);
double d4=calculateFour(d4ss);
double x=d1/d;
double y=d2/d;
double z=d3/d;
double m=d4/d;
System.out.println(&结果:x=&+x+& y=&+y+& z=&+z+& m=&+m);
System.out.println(&计算完毕!&);
* 计算4阶线性方程组
* @param strss
private static double calculateFour(double[][] strss) {
// {strss[0][0],strss[0][1],strss[0][2],strss[0][3]},
// {strss[1][0],strss[1][1],strss[1][2],strss[1][3]},
// {strss[2][0],strss[2][1],strss[2][2],strss[2][3]},
// {strss[3][0],strss[3][1],strss[3][2],strss[3][3]}
double[][] t00={
{strss[1][1],strss[1][2],strss[1][3]},
{strss[2][1],strss[2][2],strss[2][3]},
{strss[3][1],strss[3][2],strss[3][3]}
double[][] t01={
{strss[1][0],strss[1][2],strss[1][3]},
{strss[2][0],strss[2][2],strss[2][3]},
{strss[3][0],strss[3][2],strss[3][3]}
double[][] t02={
{strss[1][0],strss[1][1],strss[1][3]},
{strss[2][0],strss[2][1],strss[2][3]},
{strss[3][0],strss[3][1],strss[3][3]}
double[][] t03={
{strss[1][0],strss[1][1],strss[1][2]},
{strss[2][0],strss[2][1],strss[2][2]},
{strss[3][0],strss[3][1],strss[3][2]}
double result=Math.pow(-1, 0+0)*strss[0][0]*calculateThree(t00) + Math.pow(-1, 0+1)*strss[0][1]*calculateThree(t01) +&
Math.pow(-1, 0+2)*strss[0][2]*calculateThree(t02) + Math.pow(-1, 0+3)*strss[0][3]*calculateThree(t03) ;
* 计算3阶行列式
* @param strss
private static double calculateThree(double[][] strss) {
// {strss[0][0],strss[0][1],strss[0][2]
// {strss[1][0],strss[1][1],strss[1][2]
// {strss[2][0],strss[2][1],strss[2][2]
double result=strss[0][0]*strss[1][1]*strss[2][2] + &strss[0][1]*strss[1][2]*strss[2][0] + &strss[0][2]*strss[1][0]*strss[2][1] + &
-strss[0][2]*strss[1][1]*strss[2][0] - strss[0][1]*strss[1][0]*strss[2][2] - strss[0][0]*strss[2][1]*strss[1][2] ;
本文已收录于以下专栏:
相关文章推荐
import java.text.DecimalF
import java.util.zip.ZipE
public class CalculationEquations {
iOS7的解锁界面还是比较绚丽的,所以mo
上篇博客给出了关于单变量方程近似解的解法的四个方法,这里
给出Java的具体实现和两个方程
的根的实例
最后给出这几个方法逼近速度的比较
java解四元一次方程
在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题。将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好。下面介绍一下数据保存的方式:
本文匹配的 SDK版本:r2.1.5 以后。
查看最近更新了解最新的SDK更新情况。
使用Xcode 6及以上版本可以使用新版Push SDK,Xcode 5环境...
在iOS开发过程中常用的本地化存储有五种方式:
1.plist (XML属性列表归档 NSArray\NSDictionary)
2.preference (偏好设置\NSUserDefaults) ...
1.plist文件存储
每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒
应用沙盒的文件系统目录,如下图所...
项目需要做趋势预测,采用线性拟合、2阶曲线拟合和指数拟合的算法,各种线性拟合算法写成矩阵大概是这么个形式:
其中x是横坐标采样值,y是纵坐标采样值,i是采样点序列号,a是系数,N是采样点个数,n是阶...
他的最新文章
讲师:刘文志
讲师:陈伟
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)怎么用Java写高斯回归方程? - 知乎9被浏览729分享邀请回答7添加评论分享收藏感谢收起写回答&>&Java版高斯算法解析多元多次方程
Java版高斯算法解析多元多次方程
上传大小:4KB
Java版高斯算法解析多元多次方程
Java版高斯算法解析多元多次方程
Java版高斯算法解析多元多次方程
Java版高斯算法解析多元多次方程
综合评分:4.1(18位用户评分)
所需积分/C币:
下载个数:81
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var q = $("#form1").serializeArray();
console.log(q);
var res_area_r = $.trim($(".res_area_r").val());
if (res_area_r == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
var mess = $(".res_area_r").val();
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, _username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click",'.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
$(".res_area_r").val($.trim($(".res_area").val()));
评论共有5条
写的很不错,可惜我没用上
感谢分享,我看了一下,还是很有帮助的
感谢分享,对我有帮助!
这不是多元一次么
可用,但最终发现没用上...
审核通过送C币
计算机算法数学基础
创建者:lijunhu
计算机算法
创建者:lijunhu
创建者:lijunhu
上传者其他资源上传者专辑
解析极限编程:拥抱变化(原书第2版)[高清版]
cocos2d-x_v2.0.3离线文档
Mina real life
架构风格与基于网络的软件架构设计
QuickSshD 2.2
开发技术热门标签
VIP会员动态
下载频道积分规则调整V1710.18
CSDN下载频道积分调整公告V1710.17
开通VIP,海量IT资源任性下载
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
CSDN&VIP年卡&4000万程序员的必选
为了良好体验,不建议使用迅雷下载
Java版高斯算法解析多元多次方程
会员到期时间:
剩余下载个数:
剩余C币:593
剩余积分:786
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
(仅够下载10个资源)
全站1200个资源免积分下载
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
全站1200个资源免积分下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
全站1200个资源免积分下载
为了良好体验,不建议使用迅雷下载
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
Java版高斯算法解析多元多次方程遗传算法求解给定方程最值
一、题目要求
将一段用C语言实现的求解下列方程:
f(x1,x2,x3)=x1^2-x1*x2+x3
在给定区间的最大值的遗传算法程序改成C#程序。要求:
   (1)、数据从文件读入,结果输出到另一文件;
   (2) 实现多线程。
二、原C语言代码
以下是李老师提供的原C语言代码,我根据自己的理解添加了一些自己人为较为重要的注释。
/********************************************************************
/* This is a simple genetic algorithm implementation where the */
/* evaluation function takes positive values only and the
/* fitness of an inpidual is the same as the value of the
/* objective function
/*******************************************************************/
/* Change any of these parameters to match your needs */
#define POPSIZE 50
/* population size */
#define MAXGENS 1000
/* max. number of generations */
#define NVARS 3
/* no. of problem variables */
#define PXOVER 0.8
/* probability of crossover */
#define PMUTATION 0.15
/* probability of mutation */
#define TRUE 1
#define FALSE 0
/* current generation no. */
/* best inpidual */
/* an output file */
struct genotype /* genotype (GT), a member of the population */
double gene[NVARS];
/* a string of variables */
/* GT's fitness */
double upper[NVARS];
/* GT's variables upper bound */
double lower[NVARS];
/* GT's variables lower bound */
/* relative fitness */
/* cumulative fitness */
struct genotype population[POPSIZE+1];
/* population */
struct genotype newpopulation[POPSIZE+1]; /* */
/* replaces the */
/* old generation */
/* Declaration of procedures used by this genetic algorithm */
void initialize(void);
double randval(double, double);
void evaluate(void);
void keep_the_best(void);
void elitist(void);//优秀人才,精英主义
void select(void);
void crossover(void);
void Xover(int,int);
void swap(double *, double *);
void mutate(void);
void report(void);
/***************************************************************/
/* Initialization function: Initializes the values of genes
/* within the variables bounds. It also initializes (to zero)
/* all fitness values for each member of the population. It
/* reads upper and lower bounds of each variable from the
/* input file `gadata.txt'. It randomly generates values
/* between these bounds for each gene of each genotype in the
///////////////////////////////////////////////////////////////////////////////////
/* population. The format of the input file gadata.txt' is
/* var1_lower_bound var1_upper bound
/* var2_lower_bound var2_upper bound ...
/***************************************************************/
void initialize(void)
double lbound,
if ((infile = fopen("gadata.txt","r"))==NULL)
fprintf(galog,"/nCannot open input file!/n");
/* initialize variables within the bounds */
for (i = 0; i < NVARS; i++)
fscanf(infile, "%lf",&lbound);
fscanf(infile, "%lf",&ubound);
for (j = 0; j < POPSIZE; j++)
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] =
population[j].upper[i]=
population[j].gene[i] = randval(population[j].lower[i],
population[j].upper[i]);
fclose(infile);
/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/
double randval(double low, double high)
val = ((double)(rand()%.0)*(high - low) +
// val = ((double)(rand()%(1000+1))/1000.0)*(high - low) +
return(val);
/*************************************************************/
/* Evaluation function: This takes a user defined function.
/* Each time this is changed, the code has to be recompiled. */
/////////////////////////////////////////////////////////////////////////////
/* The current function is:
x[1]^2-x[1]*x[2]+x[3]
/*************************************************************/
void evaluate(void)
double x[NVARS+1];
for (mem = 0; mem < POPSIZE; mem++)
for (i = 0; i < NVARS; i++)
x[i+1] = population[mem].gene[i];
population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];
/***************************************************************/
/* Keep_the_best function: This function keeps track of the
/* best member of the population. Note that the last entry in
/* the array Population holds a copy of the best inpidual
/***************************************************************/
void keep_the_best()
cur_best = 0; /* stores the index of the best inpidual */
for (mem = 0; mem
population[POPSIZE].fitness)
cur_best =
population[POPSIZE].fitness = population[mem].
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[cur_best].gene[i];
/****************************************************************/
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of
/* the current generation is worse then the best member of the
/* previous generation, the latter one would replace the worst
/* member of the current population
/****************************************************************/
void elitist()
double best,
/* best and worst fitness values */
int best_mem, worst_ /* indexes of the best and worst member */
best = population[0].
worst = population[0].
for (i = 0; i
population[i+1].fitness)
if (population[i].fitness >= best)
best = population[i].
best_mem =
if (population[i+1].fitness <= worst)
worst = population[i+1].
worst_mem = i + 1;
if (population[i].fitness = best)
best = population[i+1].
best_mem = i + 1;
/* if best inpidual from the new population is better than */
/* the best inpidual from the previous population, then
/* copy the best fro else replace the
/* worst inpidual from the current population with the
/* best one from the previous generation
if (best >= population[POPSIZE].fitness)
for (i = 0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[best_mem].gene[i];
population[POPSIZE].fitness = population[best_mem].
for (i = 0; i < NVARS; i++)
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
population[worst_mem].fitness = population[POPSIZE].
/**************************************************************/
/* Selection function: Standard proportional selection for
/* maximization problems incorporating elitist model - makes
/* sure that the best member survives
/**************************************************************/
void select(void)
int mem, i, j,
double sum = 0;
/* find total fitness of the population */
for (mem = 0; mem < POPSIZE; mem++)
sum += population[mem].
/* calculate relative fitness */
for (mem = 0; mem < POPSIZE; mem++)
population[mem].rfitness =
population[mem].fitness/
population[0].cfitness = population[0].
/* calculate cumulative fitness */
for (mem = 1; mem < POPSIZE; mem++)
population[mem].cfitness =
population[mem-1].cfitness +
population[mem].
/* finally select survivors using cumulative fitness. */
for (i = 0; i < POPSIZE; i++)
p = rand()%;
if (p < population[0].cfitness)
newpopulation[i] = population[0];
for (j = 0; j = population[j].cfitness &&
p<population[j+1].cfitness)
newpopulation[i] = population[j+1];
/* once a new population is created, copy it back */
for (i = 0; i < POPSIZE; i++)
population[i] = newpopulation[i];
/***************************************************************/
/* Crossover selection: selects two parents that take part in
/* the crossover. Implements a single point crossover
/***************************************************************/
void crossover(void)
int i, mem,
0; /* count of the number of members chosen */
for (mem = 0; mem < POPSIZE; ++mem)
x = rand()%;
if(NVARS == 2)
point = 1;
point = (rand() % (NVARS - 1)) + 1;
for (i = 0; i < i++)
swap(&population[one].gene[i], &population[two].gene[i]);
/*************************************************************/
/* Swap: A swap procedure that helps in swapping 2 variables */
/*************************************************************/
void swap(double *x, double *y)
temp = *x;
/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and
/* upper bounds of this variable
/**************************************************************/
void mutate(void)
double lbound,
for (i = 0; i < POPSIZE; i++)
for (j = 0; j < NVARS; j++)
x = rand()%;
if (x < PMUTATION)
/* find the bounds on the variable to be mutated */
lbound = population[i].lower[j];
hbound = population[i].upper[j];
population[i].gene[j] = randval(lbound, hbound);
/***************************************************************/
/* Report function: Reports progress of the simulation. Data
/* dumped into the
output file are separated by commas
/***************************************************************/
void report(void)
double best_
/* best population fitness */
/* avg population fitness */
/* std. deviation of population fitness */
double sum_
/* sum of square for std. calc */
double square_
/* square of sum for std. calc */
/* total population fitness */
sum = 0.0;
sum_square = 0.0;
for (i = 0; i < POPSIZE; i++)
sum += population[i].
sum_square += population[i].fitness * population[i].
avg = sum/(double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));
best_val = population[POPSIZE].
fprintf(galog, "\n%5d,
%6.3f, %6.3f, %6.3f %6.3f, %6.3f\n\n", generation,best_val, avg,
square_sum,sum_square,stddev);
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then
/* evaluating the resulting population, until the terminating */
/* condition is satisfied
/**************************************************************/
void main(void)
if ((galog = fopen("galog.txt","w"))==NULL)
generation = 0;
fprintf(galog, "\n generation
standard \n");
fprintf(galog, " number
value fitness
deviation \n");
initialize();
evaluate();
keep_the_best();
while(generation<MAXGENS)
generation++;
crossover();
evaluate();
elitist();
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");
for (i = 0; i < NVARS; i++)
fprintf (galog,"\n var(%d) = %3.3f",i,population[POPSIZE].gene[i]);
fprintf(galog,"\n\n Best fitness = %3.3f",population[POPSIZE].fitness);
fclose(galog);
printf("Success\n");
三、我的C#代码
//Chrosome.cs文件
using System.Collections.G
using System.L
using System.T
using System.Threading.T
namespace myCWindowsProgram
class Chrosome
private const int NVARS = 3;
private double PMUTATION;
private double[] gene = new double[NVARS];
private double[] upper = new double[NVARS];
private double[] lower = new double[NVARS];
private double rF
private double cF
private Random myR
public Chrosome(double tempPMUTATION)
PMUTATION = tempPMUTATION;
myRandom = new Random(unchecked((int)DateTime.Now.Ticks));
public double Fitness
set { fitness = }
public double RFitness
get { return rF }
set { rFitness = }
public double CFitness
get { return cF }
set { cFitness = }
public double GetIOneGen(int id)
return this.gene[id];
public void SetIOneGen(int id,double tempGen)
this.gene[id] = tempG
public double GetIOneLower(int id)
return this.lower[id];
public void SetIOneLower(int id, double tempLower)
this.lower[id] = tempL
public double GetIOneUpper(int id)
return this.upper[id];
public void SetIOneUpper(int id, double tempUpper)
this.upper[id] = tempU
/*********************************************************
* 参数:分别表示上下界
* 返回值:doule
* 功能:产生介于[low,high)的随机数
* *********************************************************/
public double RandValue(double low, double high)
val = (double)myRandom.Next(0, 1000);
val = val / 1000.0 * (high - low) +
/*********************************************************
* 参数:无
* 返回值:无
* 功能:单个染色体变异
* *********************************************************/
public void ChrosomeMution()
double lbound, hbound,x;
for (int i = 0; i < NVARS; i++)
x = myRandom.Next(0, 1000) / 1000.0;
if (x < PMUTATION)
lbound = this.lower[i];
hbound = this.upper[i];
this.SetIOneGen(i, RandValue(lbound, hbound));
/*********************************************************
* 参数:无
* 返回值:无
* 功能:单个染色体评价适应度
* *********************************************************/
public void ChrosomeEvaluate()
double[] x = new double[NVARS + 1];
for (int i = 0; i < NVARS; i++)
x[i + 1] = this.GetIOneGen(i);
this.fitness = (x[1] * x[1]) - (x[1] * x[2]) + x[3];
/*********************************************************
* 参数:模板基因染色体
* 返回值:无
* 功能:复制染色体基因
* *********************************************************/
public void CopyAllGene(Chrosome tempChrosome)
for (int i = 0; i < NVARS; i++)
this.gene[i] = tempChrosome.GetIOneGen(i);
/*********************************************************
* 参数:模板基因染色体
* 返回值:无
* 功能:复制整个染色体
* *********************************************************/
public void CopyChrosome(Chrosome tempChrosome)
for (int i = 0; i < NVARS; i++)
this.gene[i] = tempChrosome.GetIOneGen(i);
for (int i = 0; i < NVARS; i++)
this.upper[i] = tempChrosome.GetIOneUpper(i);
for (int i = 0; i < NVARS; i++)
this.lower[i] = tempChrosome.GetIOneLower(i);
this.fitness = tempChrosome.F
this.rFitness = tempChrosome.RF
this.cFitness = tempChrosome.CF
//Population.cs文件
using System.Collections.G
using System.IO;
using System.L
using System.T
using System.Threading.T
namespace myCWindowsProgram
class Population
private int POPSIZE;
private int MAXGENS;
private double PXOVER;
private double PMUTATION;
private const int NVARS = 3;
private int curB
private Chrosome[] myC
private Chrosome[] newC
private Random myR
private bool isFirstW
private string readerP
private const string writePath =
@"C:\Users\Chrome\Desktop\myCWindowsProgram\myCWindowsProgram\Write.txt";
private const string resultPath =
@"C:\Users\Chrome\Desktop\myCWindowsProgram\myCWindowsProgram\Result.txt";
private MyWindows myW
private double[]
private double[]
/*********************************************************
* 参数:无
* 返回值:无
* 功能:读入待处理数据集
* *********************************************************/
public void ReadDataSet()
int i = 0;
lbound=new double[NVARS];
ubound = new double[NVARS];
using (StreamReader sr = new StreamReader(readerPath))
while ((line = sr.ReadLine()) != null)
string[] strcol = line.Split(new char[] { &#39; &#39; });
lbound[i] = Convert.ToDouble(strcol[0]);
ubound[i] = Convert.ToDouble(strcol[1]);
sr.Close();
/*********************************************************
* 参数:分别别表示种群规模、迭代次数、种群交叉率、基因变异率、窗口句柄
* 返回值:无
* 功能:初始化种群
* *********************************************************/
public Population(int tempPOPSIZE, int tempMAXGENS, double tempPXOVER,
double tempPMUTATION, string tempReaderPath, MyWindows tempMyWindows)
POPSIZE=tempPOPSIZE;
MAXGENS=tempMAXGENS;
PXOVER=tempPXOVER;
PMUTATION = tempPMUTATION;
readerPath=tempReaderP
myWindows = tempMyW
isFirstWrite =
myRandom = new Random(unchecked((int)DateTime.Now.Ticks));
myChrosome=new Chrosome[POPSIZE+1];
newChrosome = new Chrosome[POPSIZE + 1];
ReadDataSet();
for (int i = 0; i <= POPSIZE; i++)
myChrosome[i] = new Chrosome(PMUTATION);
newChrosome[i] = new Chrosome(PMUTATION);
for (int i = 0; i < NVARS; i++)
for (int j = 0; j < POPSIZE; j++)
myChrosome[j].Fitness = 0;
myChrosome[j].RFitness = 0;
myChrosome[j].CFitness = 0;
myChrosome[j].SetIOneLower(i,lbound[i]);
myChrosome[j].SetIOneUpper(i, ubound[i]);
myChrosome[j].SetIOneGen(i,myChrosome[j].RandValue(
myChrosome[j].GetIOneLower(i),myChrosome[j].GetIOneUpper(i)));
/*********************************************************
* 参数:无
* 返回值:无
* 功能:评价当前种群每个个体的适应度
* *********************************************************/
public void PopulationEvaluate()
for (int i = 0; i < POPSIZE; i++)
myChrosome[i].ChrosomeEvaluate();
/*********************************************************
* 参数:无
* 返回值:无
* 功能:精英主义(选择当前种群适应度最好个体存活下来)
* *********************************************************/
public void PopulationKeepTheBest()
curBest = 0;
for (int i = 0; i
myChrosome[POPSIZE].Fitness)
myChrosome[POPSIZE].Fitness = myChrosome[i].F
myChrosome[POPSIZE].CopyAllGene(myChrosome[curBest]);
/*********************************************************
* 参数:无
* 返回值:无
* 功能:遗传算子-精英主义操作(保留最好个体,淘汰最坏个体)
* *********************************************************/
public void PopulationElitist()
double best,
int bestMem=0, worstMem=0;
best = myChrosome[0].F
worst = myChrosome[0].F
for (int i = 0; i
myChrosome[i + 1].Fitness)
if (myChrosome[i].Fitness >= best)
best = myChrosome[i].F
if (myChrosome[i + 1].Fitness <= worst)
worst = myChrosome[i + 1].F
worstMem = i + 1;
if (myChrosome[i].Fitness = best)
best = myChrosome[i + 1].F
bestMem= i + 1;
if (best >= myChrosome[POPSIZE].Fitness)
myChrosome[POPSIZE].CopyAllGene(myChrosome[bestMem]);
myChrosome[POPSIZE].Fitness = myChrosome[bestMem].F
myChrosome[worstMem].CopyAllGene(myChrosome[POPSIZE]);
myChrosome[worstMem].Fitness = myChrosome[POPSIZE].F
/*********************************************************
* 参数:无
* 返回值:无
* 功能:遗传算子-选择操作(采用轮盘赌方法选择适应度较高个体)
* *********************************************************/
public void PopulationSelect()
double sum = 0;
for (int i = 0; i < POPSIZE;i++)
sum += myChrosome[i].F
for (int i = 0; i < POPSIZE; i++)
myChrosome[i].RFitness = myChrosome[i].Fitness/
myChrosome[0].CFitness= myChrosome[0].RF
for (int i = 1; i < POPSIZE; i++)
myChrosome[i].CFitness = myChrosome[i - 1].CFitness + myChrosome[i].RF
for (int i = 0; i < POPSIZE; i++)
int val = (int)myRandom.Next(0, 1000);
double p = val % 1000 / 1000.0;
if (p < myChrosome[0].CFitness)
newChrosome[i].CopyChrosome(myChrosome[0]);
for (int j = 0; j = myChrosome[j].CFitness && p < myChrosome[j + 1].CFitness)
newChrosome[i].CopyChrosome(myChrosome[j + 1]);
for (int i = 0; i
if (NVARS == 2)
point = 1;
point = ((int)myRandom.Next(0, NVARS - 1)) + 1;
for (int i = 0; i < i++)
double temp = myChrosome[one].GetIOneGen(i);
myChrosome[one].SetIOneGen(i, myChrosome[two].GetIOneGen(i));
myChrosome[two].SetIOneGen(i, temp);
/*********************************************************
* 参数:无
* 返回值:无
* 功能:遗传算子-交叉操作(依概率选择染色体杂交)
* *********************************************************/
public void PopulationCrossover()
int i, mem, one=-1;
for (mem = 0; mem < POPSIZE; ++mem)
x =myRandom.Next(0, .0;
if (x < PXOVER)
if (first % 2 == 0)
PopulationXover(one, mem);
/*********************************************************
* 参数:无
* 返回值:无
* 功能:遗传算子-变异操作(基因变异)
* *********************************************************/
public void PopulationMutate()
for (int i = 0; i < POPSIZE; i++)
myChrosome[i].ChrosomeMution();
/*********************************************************
* 参数:无
* 返回值:无
* 功能:打印当前种群适应度信息
* *********************************************************/
public void PopulationReport()
double bestVal, avg, stddev, sumSquare,squareSum,
sum = 0.0;
sumSquare = 0.0;
for (int i = 0; i < POPSIZE; i++)
sum += myChrosome[i].F
sumSquare += myChrosome[i].Fitness * myChrosome[i].F
avg = sum/(double)POPSIZE;
squareSum = avg * avg * POPSIZE;
stddev = Math.Sqrt((sumSquare - squareSum) /(double) (POPSIZE - 1));
bestVal = myChrosome[POPSIZE].F
//保证在第一次读写时覆盖上个数据集的处理结果,同时保证当前结果累加输到文件末尾
if (isFirstWrite)
//保证当前结果累加输到文件末尾
using (StreamWriter br = new StreamWriter(writePath, false))
br.WriteLine("generation
standard");
br.WriteLine("number
deviation");
br.WriteLine("{0},
{3}", generation, bestVal, avg, stddev);
br.Close();
//保证在第一次读写时覆盖上个数据集的处理结果
using (StreamWriter br = new StreamWriter(writePath, true))
br.WriteLine("{0},
{3}", generation, bestVal, avg, stddev);
br.Close();
this.myWindows.setList(bestVal, 2);
this.myWindows.setList(avg, 3);
this.myWindows.setList(sumSquare, 4);
this.myWindows.setList(squareSum, 5);
this.myWindows.setList(stddev, 6);
this.myWindows.setList(myChrosome[POPSIZE].GetIOneGen(0), 7);
this.myWindows.setList(myChrosome[POPSIZE].GetIOneGen(1), 8);
this.myWindows.setList(myChrosome[POPSIZE].GetIOneGen(2), 9);
if (isFirstWrite)
isFirstWrite =
catch (EndOfStreamException e)
Console.WriteLine(e.Message);
Console.WriteLine("已经读到末尾");
/*********************************************************
* 参数:无
* 返回值:无
* 功能:遗传算法主框架程序
* *********************************************************/
public void GeneticAlgorithm()
DateTime starttime = DateTime.N
this.generation = 0;
this.myWindows.setProgressBar(0, MAXGENS);
this.myWindows.setList(1.0*this.generation,1);
this.PopulationEvaluate();
this.PopulationKeepTheBest();
while (this.generation < MAXGENS)
this.generation++;
this.PopulationSelect();
this.PopulationCrossover();
this.PopulationMutate();
this.PopulationReport();
this.PopulationEvaluate();
this.PopulationElitist();
this.myWindows.setProgressBar(this.generation, MAXGENS);
this.myWindows.setList(1.0*this.generation, 1);
//显示最优解
using (StreamWriter br = new StreamWriter(writePath, true))
br.WriteLine(); br.WriteLine(); br.WriteLine();
br.WriteLine("Simulation completed");
br.WriteLine("Best member: ");
for (int i = 0; i < NVARS; i++)
br.WriteLine("var({0}) ={1} ", i, myChrosome[POPSIZE].GetIOneGen(i));
br.WriteLine(" Best fitness = {0}", myChrosome[POPSIZE].Fitness);
timespan = DateTime.Now.Subtract(starttime);//获取就是开始时间很结束时间差
string diff = timespan.TotalSeconds.ToString();
br.WriteLine("the total time of program :{0} Seconds", diff);
br.Close();
catch (EndOfStreamException e)
Console.WriteLine(e.Message);
Console.WriteLine("已经读到末尾");
//显示计算时间
using (StreamWriter br = new StreamWriter(resultPath, true))
br.WriteLine("geration={0}
Best fitness = {1}", MAXGENS,myChrosome[POPSIZE].Fitness);
timespan = DateTime.Now.Subtract(starttime);//获取就是开始时间很结束时间差
string diff = timespan.TotalSeconds.ToString();
br.WriteLine("the total time of program :{0} Seconds", diff);
br.WriteLine(); br.WriteLine(); br.WriteLine();
br.Close();
catch (EndOfStreamException e)
Console.WriteLine(e.Message);
Console.WriteLine("已经读到末尾");
//MyWindows.Designer.cs文件
namespace myCWindowsProgram
partial class MyWindows
/// Required designer variable.
ponentModel.IContainer components =
/// Clean up any resources being used.
/// true if managed resource otherwise, false.
protected override void Dispose(bool disposing)
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.textBox7 = new System.Windows.Forms.TextBox();
this.textBox8 = new System.Windows.Forms.TextBox();
this.textBox9 = new System.Windows.Forms.TextBox();
this.textBox10 = new System.Windows.Forms.TextBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label12 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.label13 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox13 = new System.Windows.Forms.TextBox();
this.textBox12 = new System.Windows.Forms.TextBox();
this.textBox11 = new System.Windows.Forms.TextBox();
this.label16 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
boBox1 = new System.boBox();
this.label17 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
this.label1.AutoSize =
this.label1.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.label1.Location = new System.Drawing.Point(12, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 20);
this.label1.TabIndex = 0;
this.label1.Text = "当前代数";
// textBox1
this.textBox1.Location = new System.Drawing.Point(109, 25);
this.textBox1.Multiline =
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
this.label2.AutoSize =
this.label2.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.label2.Location = new System.Drawing.Point(241, 28);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(79, 20);
this.label2.TabIndex = 2;
this.label2.Text = "当前最优解";
// textBox2
this.textBox2.Location = new System.Drawing.Point(357, 20);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 26);
this.textBox2.TabIndex = 3;
this.label3.AutoSize =
this.label3.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.label3.Location = new System.Drawing.Point(479, 26);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(107, 20);
this.label3.TabIndex = 4;
this.label3.Text = "当前平均适应度";
// textBox3
this.textBox3.Location = new System.Drawing.Point(620, 25);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(113, 26);
this.textBox3.TabIndex = 5;
this.label4.AutoSize =
this.label4.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.label4.Location = new System.Drawing.Point(12, 81);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(79, 20);
this.label4.TabIndex = 6;
this.label4.Text = "当前平方和";
this.label5.AutoSize =
this.label5.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.label5.Location = new System.Drawing.Point(241, 81);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(93, 20);
this.label5.TabIndex = 7;
this.label5.Text = "当前和的平方";
this.label6.AutoSize =
this.label6.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label6.Location = new System.Drawing.Point(479, 78);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(65, 20);
this.label6.TabIndex = 8;
this.label6.Text = "当前方差";
// textBox4
this.textBox4.Location = new System.Drawing.Point(109, 78);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 26);
this.textBox4.TabIndex = 9;
// textBox5
this.textBox5.Location = new System.Drawing.Point(357, 78);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(100, 26);
this.textBox5.TabIndex = 10;
// textBox6
this.textBox6.Location = new System.Drawing.Point(620, 75);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(113, 26);
this.textBox6.TabIndex = 11;
// button1
this.button1.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(16, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(251, 34);
this.button1.TabIndex = 12;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor =
this.button1.Click += new System.EventHandler(this.button1_Click);
// button2
this.button2.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(16, 187);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(251, 34);
this.button2.TabIndex = 13;
this.button2.Text = "暂停";
this.button2.UseVisualStyleBackColor =
this.button2.Click += new System.EventHandler(this.button2_Click);
// button3
this.button3.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button3.Location = new System.Drawing.Point(465, 168);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(268, 34);
this.button3.TabIndex = 14;
this.button3.Text = "退出";
this.button3.UseVisualStyleBackColor =
this.button3.Click += new System.EventHandler(this.button3_Click);
this.label7.AutoSize =
this.label7.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label7.Location = new System.Drawing.Point(12, 38);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(79, 20);
this.label7.TabIndex = 15;
this.label7.Text = "种群个体数";
this.label8.AutoSize =
this.label8.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label8.Location = new System.Drawing.Point(451, 38);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(93, 20);
this.label8.TabIndex = 16;
this.label8.Text = "种群迭代次数";
this.label9.AutoSize =
this.label9.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label9.Location = new System.Drawing.Point(391, 300);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(0, 20);
this.label9.TabIndex = 17;
// label10
this.label10.AutoSize =
this.label10.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label10.Location = new System.Drawing.Point(12, 82);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(79, 20);
this.label10.TabIndex = 18;
this.label10.Text = "种群变异率";
// label11
this.label11.AutoSize =
this.label11.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label11.Location = new System.Drawing.Point(451, 73);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(79, 20);
this.label11.TabIndex = 19;
this.label11.Text = "种群交叉率";
// textBox7
this.textBox7.Location = new System.Drawing.Point(97, 32);
this.textBox7.Name = "textBox7";
this.textBox7.Size = new System.Drawing.Size(135, 26);
this.textBox7.TabIndex = 20;
// textBox8
this.textBox8.Location = new System.Drawing.Point(591, 35);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(142, 26);
this.textBox8.TabIndex = 21;
// textBox9
this.textBox9.Location = new System.Drawing.Point(97, 76);
this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(135, 26);
this.textBox9.TabIndex = 22;
// textBox10
this.textBox10.Location = new System.Drawing.Point(591, 73);
this.textBox10.Name = "textBox10";
this.textBox10.Size = new System.Drawing.Size(142, 26);
this.textBox10.TabIndex = 23;
// progressBar1
this.progressBar1.Location = new System.Drawing.Point(109, 144);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(533, 23);
this.progressBar1.TabIndex = 24;
// label12
this.label12.AutoSize =
this.label12.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label12.Location = new System.Drawing.Point(12, 147);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(65, 20);
this.label12.TabIndex = 25;
this.label12.Text = "当前进度";
// button4
this.button4.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button4.Location = new System.Drawing.Point(465, 187);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(268, 34);
this.button4.TabIndex = 26;
this.button4.Text = "继续";
this.button4.UseVisualStyleBackColor =
this.button4.Click += new System.EventHandler(this.button4_Click);
// label13
this.label13.AutoSize =
this.label13.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label13.Location = new System.Drawing.Point(677, 144);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(56, 20);
this.label13.TabIndex = 27;
this.label13.Text = "0.000%";
// groupBox1
this.groupBox1.Controls.Add(this.textBox13);
this.groupBox1.Controls.Add(this.textBox12);
this.groupBox1.Controls.Add(this.textBox11);
this.groupBox1.Controls.Add(this.label16);
this.groupBox1.Controls.Add(this.label15);
this.groupBox1.Controls.Add(this.label14);
this.groupBox1.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(42, 510);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(761, 107);
this.groupBox1.TabIndex = 28;
this.groupBox1.TabStop =
this.groupBox1.Text = "当前迭代次数下最优解对应各变量取值区";
// textBox13
this.textBox13.Location = new System.Drawing.Point(620, 49);
this.textBox13.Name = "textBox13";
this.textBox13.Size = new System.Drawing.Size(113, 26);
this.textBox13.TabIndex = 34;
// textBox12
this.textBox12.Location = new System.Drawing.Point(353, 49);
this.textBox12.Name = "textBox12";
this.textBox12.Size = new System.Drawing.Size(100, 26);
this.textBox12.TabIndex = 33;
// textBox11
this.textBox11.Location = new System.Drawing.Point(109, 49);
this.textBox11.Name = "textBox11";
this.textBox11.Size = new System.Drawing.Size(100, 26);
this.textBox11.TabIndex = 32;
// label16
this.label16.AutoSize =
this.label16.Location = new System.Drawing.Point(492, 55);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(52, 20);
this.label16.TabIndex = 31;
this.label16.Text = "x3取值";
// label15
this.label15.AutoSize =
this.label15.Location = new System.Drawing.Point(241, 55);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(52, 20);
this.label15.TabIndex = 30;
this.label15.Text = "x2取值";
// label14
this.label14.AutoSize =
this.label14.Location = new System.Drawing.Point(12, 52);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(52, 20);
this.label14.TabIndex = 29;
this.label14.Text = "x1取值";
// groupBox2
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Controls.Add(this.textBox1);
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Controls.Add(this.label13);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Controls.Add(this.label12);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.progressBar1);
this.groupBox2.Controls.Add(this.textBox3);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Controls.Add(this.textBox4);
this.groupBox2.Controls.Add(this.label5);
this.groupBox2.Controls.Add(this.textBox5);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.label6);
this.groupBox2.Controls.Add(this.textBox6);
this.groupBox2.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox2.Location = new System.Drawing.Point(42, 254);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(761, 237);
this.groupBox2.TabIndex = 29;
this.groupBox2.TabStop =
this.groupBox2.Text = "当前迭代次数下种群信息区";
// groupBox3
this.groupBox3.Controls.boBox1);
this.groupBox3.Controls.Add(this.label17);
this.groupBox3.Controls.Add(this.label7);
this.groupBox3.Controls.Add(this.textBox7);
this.groupBox3.Controls.Add(this.label8);
this.groupBox3.Controls.Add(this.label10);
this.groupBox3.Controls.Add(this.textBox9);
this.groupBox3.Controls.Add(this.textBox10);
this.groupBox3.Controls.Add(this.textBox8);
this.groupBox3.Controls.Add(this.button1);
this.groupBox3.Controls.Add(this.label11);
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Font = new System.Drawing.Font("Microsoft YaHei",
10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox3.Location = new System.Drawing.Point(42, 12);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(761, 224);
this.groupBox3.TabIndex = 30;
this.groupBox3.TabStop =
this.groupBox3.Text = "设置种群初始信息区";
// comboBox1
boBox1.FormattingEnabled =
boBox1.Items.AddRange(new object[] {
"C:\\Users\\Chrome\\Desktop\\myCWindowsProgram\\myCWindowsProgram\\DataSet_One.txt",
"C:\\Users\\Chrome\\Desktop\\myCWindowsProgram\\myCWindowsProgram\\DataSet_Two.txt",
"C:\\Users\\Chrome\\Desktop\\myCWindowsProgram\\myCWindowsProgram\\DataSet_Three.txt"});
boBox1.Location = new System.Drawing.Point(97, 121);
boBox1.Name = "comboBox1";
boBox1.Size = new System.Drawing.Size(636, 28);
boBox1.TabIndex = 25;
// label17
this.label17.AutoSize =
this.label17.Location = new System.Drawing.Point(12, 129);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(79, 20);
this.label17.TabIndex = 24;
this.label17.Text = "导入数据集";
// MyWindows
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.F
this.ClientSize = new System.Drawing.Size(834, 639);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label9);
this.ForeColor = System.Drawing.SystemColors.InactiveCaptionT
this.Name = "MyWindows";
this.Text = "遗传算法求解函数最大值C#应用程序";
this.Load += new System.EventHandler(this.MyWindows_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.TextBox textBox10;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox13;
private System.Windows.Forms.TextBox textBox12;
private System.Windows.Forms.TextBox textBox11;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.boBox comboBox1;
private System.Windows.Forms.Label label17;
//MyWindows.cs文件
using System.Collections.G
using System.D
using System.D
using System.L
using System.T
using System.Threading.T
using System.Windows.F
using System.T
namespace myCWindowsProgram
public partial class MyWindows : Form
private Thread myT//遗传算法对应线程
public MyWindows()
InitializeComponent();
/************************************************
* 参数:分别表示当前进度与总进度
* 返回值:无
* 功能:完成进程框的设定
* ***********************************************/
public delegate void setProgressInvoke(int num,int MAXGENS);
public void setProgressBar(int num,int MAXGENS)
if (this.InvokeRequired)
setProgressInvoke _setInvoke = new setProgressInvoke(setProgressBar);
this.Invoke(_setInvoke, new object[] { num ,MAXGENS});
label13.Text =Math.Round((100.0 * num / MAXGENS), 3).ToString("f1")+"%";
progressBar1.Value =(int)((int)((1.0*num/ MAXGENS)*100)%(100+1));
/************************************************
* 参数:分别表示文本框待显示的值与对应文本框归属
* 返回值:无
* 功能:完成6个文本框的设定
* ***********************************************/
public delegate void setListInvoke(double val, int flag);
public void setList(double val, int flag)
if (this.InvokeRequired)
setListInvoke _setInvoke = new setListInvoke(setList);
this.Invoke(_setInvoke, new object[] { val, flag });
switch (flag)
textBox1.Text = val.ToString();
textBox2.Text = Math.Round(val, 8).ToString("f8");
textBox3.Text = Math.Round(val, 8).ToString("f8");
textBox4.Text = Math.Round(val, 8).ToString("f8");
textBox5.Text = Math.Round(val, 8).ToString("f8");
textBox6.Text = Math.Round(val, 8).ToString("f8");
textBox11.Text = Math.Round(val, 8).ToString("f8");
textBox12.Text = Math.Round(val, 8).ToString("f8");
textBox13.Text = Math.Round(val, 8).ToString("f8");
/************************************************
*“开始”按钮
* ***********************************************/
private void button1_Click(object sender, EventArgs e)
//组合框为空,未选择数据集,重新选择
if (comboBox1.Text == "")
MessageBox.Show("请选择数据集!");
//当前线程未结束,直接结束该线程
if (null != myThread)
myThread.Abort();
Population myPopulation = new Population(Convert.ToInt32(textBox7.Text),
Convert.ToInt32(textBox8.Text), Convert.ToDouble(textBox10.Text),
Convert.ToDouble(textBox9.Text), comboBox1.Text, this);
myThread = new Thread(myPopulation.GeneticAlgorithm);
myThread.Start();
/************************************************
*“暂停”按钮
* ***********************************************/
private void button2_Click(object sender, EventArgs e)
if (null == myThread)
MessageBox.Show("请确认程序正在运行!");
myThread.Suspend();
/************************************************
*“退出”按钮
* ***********************************************/
private void button3_Click(object sender, EventArgs e)
MessageBox.Show("你按了退出键");
//当前线程未结束,直接结束该线程
if (null != myThread)
myThread.Abort();
Application.Exit();
/************************************************
*“继续”按钮
* ***********************************************/
private void button4_Click(object sender, EventArgs e)
//保证当前线程处在挂起状态
if (ThreadState.Suspended == myThread.ThreadState)
myThread.Resume();
MessageBox.Show("请确认当前程序处于暂停状态!");
/************************************************
*初始化窗口
* ***********************************************/
private void MyWindows_Load(object sender, EventArgs e)
this.textBox7.Text = "100";
this.textBox8.Text = "10000";
this.textBox9.Text = "0.15";
this.textBox10.Text = "0.80";
//Program.cs文件
using System.Collections.G
using System.L
using System.Threading.T
using System.Windows.F
namespace myCWindowsProgram
static class Program
/// The main entry point for the application.
[STAThread]
static void Main()
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyWindows());
四、运行结果分析
上面是程序最终跑出的结果,当然文件Result.txt中也有,截图
五、我的特色
根据题目特点,我的C#程序总体架构为有用户界面的运用遗传算法求解函数f(x1,x2,x3)=x1^2-x1*x2&#43;x3最大&#20540;的应用程序。作为一名即将步入职场的软件开发者,准确说是奋战在一线的程序员—码农。由于之前做ACM,程序要求简单高效,所以总是在用C语言或者说是放弃对象的C&#43;&#43;进行面向过程编程,近来我一改以往编程风&#26684;,转而尝试使用面向对象技术,当然这也是C#语言的特点之一。
1.特色一:C#特色语言知识的运用
1.1属性的使用:
属性是C#不同于C/C&#43;&#43;、的特色功能,属性的&#20540;是对象相应的状态&#20540;。属性体现了类的封装性:不直接访问类的数据成员而是通过访问器来访问。属性通常两个函数:获取属性&#20540;的get,和设置属性&#20540;的set。我的理解是属性的引入可以完成对私有字段的访问,同时在访问的过程中检测类型或完成其他一些工作。下面是我尝试使用的属性:
public double Fitness
set { fitness = }
public double RFitness
get { return rF }
set { rFitness = }
public double CFitness
get { return cF }
set { cFitness = }
其中fitness、rFitness、cFitness本是private型的,为了安全的对其进行访问,引入属性。
在C/C&#43;&#43;中回调函数其实是通过引入指向函数的指针来实现的,这种方法直接操作内存,但不是安全的。在C#中, 引入了委托的概念。委托是一种特殊的数据类型,它引用一个或多个方法,这些方法对应C/C&#43;&#43;中的回调函数,委托是类型安全的,可以将方法作为参数传递,可用于定义回调方法,可以连接在一起。委托通常与触发它的事件联系在一起。事件是指当类或对象状态发生变化时,通过其他的类或对象所引发的事情。事件并不是C#中的新名词,在此不展开。如果要使用事件,就要使用委托,事件是个特殊的签名的委托,可以从发行类中调用。我的程序做了可视化界面,往往要监听控件的变化,于是我很自然地尝试委托和事件。我的程序有进程条,我设置了委托完成类&#20284;函调函数的功能:
/************************************************
* 参数:分别表示当前进度与总进度
* 返回值:无
* 功能:完成进程框的设定
* ***********************************************/
public delegate void setProgressInvoke(int num,int MAXGENS);
public void setProgressBar(int num,int MAXGENS)
if (this.InvokeRequired)
setProgressInvoke _setInvoke = new setProgressInvoke(setProgressBar);
this.Invoke(_setInvoke, new object[] { num ,MAXGENS});
label13.Text =Math.Round((100.0 * num / MAXGENS), 3).ToString("f1")+"%";
progressBar1.Value =(int)((int)((1.0*num/ MAXGENS)*100)%(100+1));
/************************************************
* 参数:分别表示文本框待显示的值与对应文本框归属
* 返回值:无
* 功能:完成6个文本框的设定
* ***********************************************/
public delegate void setListInvoke(double val, int flag);
public void setList(double val, int flag)
if (this.InvokeRequired)
setListInvoke _setInvoke = new setListInvoke(setList);
this.Invoke(_setInvoke, new object[] { val, flag });
switch (flag)
textBox1.Text = val.ToString();
textBox2.Text = Math.Round(val, 8).ToString("f8");
textBox3.Text = Math.Round(val, 8).ToString("f8");
textBox4.Text = Math.Round(val, 8).ToString("f8");
textBox5.Text = Math.Round(val, 8).ToString("f8");
textBox6.Text = Math.Round(val, 8).ToString("f8");
textBox11.Text = Math.Round(val, 8).ToString("f8");
textBox12.Text = Math.Round(val, 8).ToString("f8");
textBox13.Text = Math.Round(val, 8).ToString("f8");
2.特色二:模块化
C#是面向对象的语言,在进行本次C#程序的编写时我将自己的程序分成了3个相对独立的功能模块,并将他们加以抽象封装成3各类,分别是染色体类、种群类、窗口类,从而很好的保证的程序模块的低耦合、高内聚的特点,从而提高程序的可读性、可扩展性、扩维护性。
2.1染色体类
完成单个个体即染色体的字段、属性、接口的设定,如单个染色体变异、单个染色体适应度评价等;
聚合染色体模拟自然界的演化规律。完成种群的相关遗传算子:选择、交叉、变异等。总的来说是算法的核心与框架。
具体与算法设计无关,只是为了直观的呈现给用户一个可视化的界面,这样也是我第一次尝试在C#下做界面。完成当前代数、当前最好适应度、当前平方和、当前方差等的实时显示。
3.特色三:多线程的引入
首先我谈谈对多线程的理解。提起线程,就不得不说进程。程序关于某个数据集在处理机上的一次执行过程。而线程是进程的某个执行路径。一个进程可能有多个执行路径,每个线程仅是其中的一条。
  关于多线程,如果是多台计算机分布式计算,则可通过多线程完全实现真正意义上的并行计算;如果是单台计算机,但是有多个CPU,通过多线程也可实现真正意义上的并行计算;如果是我们普通的计算机-单机单CPU单核,即使在计算的时候使用多线程我认为也是不能够实现真正意义上的并行的。
在本题中,演化计算中的遗传算法在计算部分几乎全部资源就是CPU计算,但就计算而言,单机单CPU单核引入多线程我认为是没有多大意义的。因为都只用CPU计算,即使线程挂起也是在等待空闲的CPU时间,引入多线程相互穿插的执行,或者单线程直接顺序执行,是没有改善的。
如上图,在遗传计算中都要用CPU,且都只用CPU,当然内存读取时间很短在此忽略。线程越多,分的越细(在实际中一个线程并不是以下执行完的,上图只是示意图),同等CPU时间内完成的计算任务不可能提高,而且反而会大幅降低。因为CPU在不同线程之中切换需要CPU时间、需要资源。单核CPU上运行的多线程程序, 同一时间只能一个线程在跑, 系统帮你切换线程而已, 系统给每个线程分配时间片来执行, 每个时间片大概10ms左右,
看起来像是同时跑, 但实际上是每个线程跑一点点就换到其它线程继续跑效率不会有提高的切换线程反倒会增加开销。
3.1、分组&#43;多线程=失败
我首先写了个将当前代数下种群分成更小的子种群,子种群分别独立完成交叉、变异、选择等操作,然后从中提取更好的。看&#20284;可以多线程实现并行计算,实则效率会大幅降低,原因如上分析。
3.2、磁盘读写与演化计算并行&#43;多线程=困难&#43;低效
在分组失败后,我尝试结合操作系统中的知识,将同一进程下IO操作(在此程序中为最开始从文件gadata.txt读入初始边界,以及后面将当前迭代次数下最好结果写入文件galog.txt,为典型的磁盘读写)与CPU计算分别写成一个线程,然后分别执行。
这样理论上是可行的。但是问题很大。
(1)、在读入文件gadata.txt中全部内容之前,种群的初始信息不全面,不能执行CPU计算,此处多线程会导致程序出错。
(2)、在写入文件galog.txt时执行CPU计算,这是理论可行的。但得不偿失。因为李老师的给的C语言程序只要求将当前最好个体信息输出(还有其他的),
generation、best、average、standard number
value fitness 、deviation
总的来说写入到文件的内容少。对应时间几乎可以忽略。试想一下在如此短暂地时间内CPU能做什么事?况且引入多线程后系统开销增加,可能效率不增反降。如果每次输出很多,尝试多线程是必要的,因为长时间等待IO操作而挂起CPU计算是不划算的,此时可以让其他线程来执行CPU计算,本进程继续负责IO操作,二者所占计算机资源不同,完全可以并行执行。
   当用多线程时,运用多线程各自独立分管计算是不会改善效率的而会降低效率。当既要CPU计算,又有IO操作时,可以尝试多线程。但要注意数据冲突,即保证正确的读写次序。当涉及既要读某段数据又来写时,如果控制不合理,会导致错误。例如,当前IO操作要输出最好的个体myChrosome[POPSIZE],但我们知道IO操作都比较慢而CPU计算是很快的,此时可能经过CPU计算后myChrosome[POPSIZE]被赋予了新&#20540;,从而导致输出出错。这就是操作系统中的读者-写者问题,涉及到互斥与同步,这在本题是很复杂的。本题输出较少,对应时间几乎可以忽略,从而完全可以不用多线程。
3.3、遗传算子&#43;盲目多线程=错误
在本题的遗传操作 GeneticAlgorithm()中,主要有以下算子:
this.PopulationSelect();
this.PopulationCrossover();
this.PopulationMutate();
this.PopulationReport();
this.PopulationEvaluate();
this.PopulationElitist();  如果直接将上面的函数分别置于不同的线程中,肯定是错误的。上面的函数本应该由时序关系的,即是顺序执行的。直接多线程会导致穿插执行,完全打乱执行流程,使得当前myChrosome[j]中染色体的所处的迭代次数、当前迭代次数下计算状态完全不一致,导致灾难后果。这样的做法错误的根本原因是根本没有考虑互斥与同步,盲目引入多线程,对多线程的适用场合和本质完全不清楚。
3.4、主线程&#43;有关窗口的线程=正确
我的程序做了可视化界面,关于遗传算法的实现没有多线程,但窗口的刷新涉及多线程,程序中至少存在主线程与有关窗口的线程,如在主线程中有:
   Application.Run(new MyWindows())  
4.特色四:窗口界面的设计
学习一门语言,最主要的是灵活运用。要用所学的语言多编程、做些实用软件,而编写实用软件很重要的是完成人机交互,良好的用户体验至关重要。所以,我认为可视化界面是很关键的。而且,可视化界面掌握或尝试使用一门语言的体现。我的程序就做了个可视化界面,将输出到文件的数据同时也显示到窗口对应的文本中,并提供进程条、原始数据选择目录等。下面贴几张图:
上面button提供出错处理,保证程序的健壮性。如保证点击“继续”按钮唤醒当前线程之前必须保证当前线程处于挂起状态,即点击过“暂停”按钮,否则直接返回等。
在添加控件的过程中,发现C和VS比VC方便多了,以后的多用VS,多用更加现代的IDE。
六、编程中遇到的困难及解决方案
由于本次课程报告在非编程言语方面几乎没有难度(只是改改),所以编程中遇到的困难并不多。在做本次报告之前,也就是课下写了几个C#小程序加之2次实习,在写报告时遇到了一些语言方面的小问题、小bug,但经过debug,都能很快解决。下面我就总结一下遇到的问题以及解决方案。
1.数组元素出现空引用
在初始化种群时,我用了
myChrosome=new Chrosome[POPSIZE&#43;1];
上面声明,可是运行程序时出错了。经过查看出错提示,我明白了Chrosome是自定义对象,上述只是声明并创建了数组,并没有给具体的每个元素myChrosome[i]分配内存空间,从而导致myChrosome=null,进而引发错误。
在C#中是很少用指针的,引用很大程度上替代了指针。在C#中定义数组、某种类库中的对象、自定义对象等都是声明该类型的引用,并未分配实际存储空间,此时必须通过 new操作符来完成相关初始化工作。
2.深拷贝与浅拷贝
在遗传操作-选择算子时我直接复制稍加修改用了李老师的代码
myChrosome[]=newChrosome[] ;
结果发现myChrosome[]中改变的数据会影响newChrosome[]中的数据,在C/C&#43;&#43;中是不允许这样的(不能对数组赋&#20540;,可以声明指针来完成相关的操作)。经过断点调试,我发现myChrosome、newChrosome是共用同一内存区,两个都只是数组引用。上述语句是浅拷贝,即不另外开辟存储空间存放副本,二者共用同一内存区;而我要的是深拷贝,即另外开辟存储空间存放副本,二者不共用内存区,于是我编写函数完成了相同功能。代码如下:
i = 0; i < POPSIZE ; i++)
myChrosome[i].CopyChrosome(newChrosome[i]);
在编程工程中还遇到了其它困难,但都是具体语言知识使用方面的,如怎样按给定&#26684;式从文件中数据等(在网上完全可以找到),不具有总结价&#20540;,在此就不再赘言。
以上是我C#作业,由于初学,可能问题很多,欢迎斧正!点击此处C#源码。

我要回帖

更多关于 java一元二次方程求根 的文章

 

随机推荐