当前位置:首页 » 编程博文
开发技术指南» 文章正文
    引言: 遗传算法的一个例子(C/C++)ix
 

 

 ·howto: cvs with vs.net    »显示摘要«
    摘要: introduction i have looked for several months how to use some kind of source control on my projects. up to now , i had very little success, or the integration and usage of the client software were......
 ·volume rendering     »显示摘要«
    摘要:volume rendering 我们知道空气中存在着各种的微粒,光线在空气中传播的时候会和空气中的各种微粒相互作用。这些相互作用会改变光线的强度和方向,这样我们计算真实的光照的话,我们就必须把这些相互作用纳入考虑范围。 我们首先要来看看光线和微粒的相互作用,这些作用共有三种情况: 1 吸收 当光线和微粒相互作用的时候,有些光能被粒子吸收变成热能和其他形式的能量。通过吸收光线离开微粒后亮度减......


遗传算法的一个例子(C/C++)
遗传算法的一个例子(c/c++)

摘要: m个工件分配给m架机床的效益最优化问题,使用一种遗传算法解决. 【程序编程相关:[全程建模]Rose与IDE工具如何配合

作者:newsuppy 【推荐阅读:XML应用与XGen实战

一,已知效益矩阵e 【扩展信息:sql server中格式化表中的数据

 

eij

m1

m2

m3

m4

m5

j1

5

6

4

8

3

j2

6

4

9

8

5

j3

4

3

2

5

4

j4

7

2

4

5

3

j5

3

6

4

5

5

 

jn为工件,mn为机床,矩阵对应的一格即为,某工件分配给某机床的效益.效益指花费的时间,金钱等,数值越大越差.产生一个分配序列c=(5,2,3,4,1)指将5号工件分配给1号机床,2号工件分配给2号机床,以此类推求得当前分配对应的效益为3+4+2+5+3=17.最佳化问题就是要求该效益值最小时对应的分配序列.下面采用一种遗传算法,具体算法这里不再复述,可以参考[1]或其他相关书籍.要注意由于工件与机床是一一对应的,算法与一些常见的遗传算法在某些细节上有所不同,如染色体编码,到位,变异.

下面是算法实现,由于某些原因,不得不采用c/c++混合编码,c++代码主要在文件输入输出部分.另外要让程序正常运行需要包括几个支持文件(与程序在同一路径):

benefit.txt输入效益矩阵

num_of_gen.txt最后产生的代序

numofcolony.txt为群体中个体总数

numoftm.txt为工件数与机床数

内容可以如下:

benefit.txt

5 6 4 8 3

6 4 9 8 5

4 3 2 5 4

7 2 4 5 3

3 6 4 5 5

num_of_gen.txt

50

numofcolony.txt

10

numoftm.txt

5

5

注意:由于程序写得比较仓促,未考虑错误处理,请保证相关文件的正确性.

#include <iostream>

#include <fstream>

#include <algorithm>

#include <numeric>

#include <stdlib.h>

#include <time.h>

#include <math.h>

using namespace std;

 

void generate_colony(int **colony, int num_of_chromosomes, int num_of_job); //初始群体产生

int compute_benefit(int **benefit_matrix, int *chromosome, int num_of_machine); // 对单个染色体的效益评估

void copy_chromosomes(int **colony, int num_of_chromosomes, int num_of_job, int *benefit_array);//复制

int execute_probably(float probability); //按一定概率返回1或0

//上面的函数实现中使用rand产生随机数,这是不严格的,应该采用一个均匀(uniform)分布的随机数生成器

 

void exchange_gene(int *chromosome_first, int *chromosome_second, int num_of_job); //对两个染色体实行交换

void reverse_gene(int *chromosome, int num_of_job); //对单个染色体实行到位

void gene_mutate(int *chromosome, int num_of_job); //对单个染色体实行变异

 

void erv_colony(int **colony, float pe, float pr, float pv, int num_of_job, int num_of_chromosomes);

//对群体按概率实行交换,到位与变异

 

int main()

{

    int **benefit_matrix = null;        // 效益矩阵

    int num_of_job;             // 工件数

    int num_of_machine;       // 机床数

 

    ifstream inputtm("numoftm.txt");

    inputtm >> num_of_job >> num_of_machine;

    inputtm.close();

 

    benefit_matrix = (int**)malloc(sizeof(int*)*num_of_job);

    for (int i=0; i<num_of_job; ++i)

    {

        benefit_matrix[i] = (int*)malloc(sizeof(int)*num_of_machine);

    }

 

    ifstream inputbenefit("benefit.txt");

    for (int i=0; i<num_of_job; ++i)

    {

        for (int j=0; j<num_of_machine; ++j)

        {

            inputbenefit >> benefit_matrix[i][j];

        }

    }

    inputbenefit.close();

 

    /*********************************************

    // test for inital data´s input

    cout <<num_of_job << ´\t´ << num_of_machine << endl;

    for (int i=0; i<num_of_job; ++i)

    {

        for (int j=0; j<num_of_machine; ++j)

        {

            cout <<  benefit_matrix[i][j] << ´\t´;

        }

        cout << ´\n´;

    }

    **********************************************/

 

    int num_of_chromosomes;   //群体数

    ifstream inputcln("numofcolony.txt");

    inputcln >> num_of_chromosomes;

    inputcln.close();

 

    int **colony = null;   //群体   

    colony = (int**)malloc(sizeof(int*)*num_of_chromosomes);

    for (int i=0; i<num_of_chromosomes; ++i)

    {

        colony[i] =(int*)malloc(sizeof(int)*num_of_job);

    }

 

    srand(time(0));


...   下一页
 ·jflash源代码分析(一)    »显示摘要«
    摘要:常常是板子出了问题就手足无措,常常要给板子上面信号的时候要用ads写长长的程序(我用arm)常常看到jflash的程序出错就只知道重起板子,于是我就常常想阅读一下jflash的源代码今天,我终于祭起长久不用的source insight,建立工程,开始阅读jflash所谓打蛇打七寸,读程序先读main我就从main开始对jflash进行解剖我读的代码是windows版本的,用vc进行编译,我想li......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE