谁有关于进化算法的程序用C语言写的!急用!

谁有关于进化算法的程序用C语言写的!急用!,第1张

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

文件名:Classifier.h

#ifndef CLASSIFIER_H

#define CLASSIFIER_H

#include <iostream>

#include <stdio.h>

#define SELF0

#define NONSELF 1

#define MASKVALUE 2

// detector class

class Detector

{

public:

Detector(const unsigned int length)

Detector::~Detector(void)

unsigned int length

unsigned int *value

double threshold

unsigned int type

void save(FILE *outputStream)

void show(void) { save(stdout)}

}

#endif

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

//文件名:Classifier.cpp

#include "Classifier.h"

// detector class public methods

Detector::Detector(const unsigned int length)

{

this->length = length

threshold = 0.0

value = new unsigned int [length]

type = 0

}

Detector::~Detector(void)

{

delete [] value

}

void Detector::save(FILE *outputStream)

{

register unsigned int i

fprintf(outputStream, \

"%-3d %-.10f %-1d\n", \

length, \

threshold, \

type \

)

for(i = 0i <lengthi++)

fprintf(outputStream, "%-1d ", value[i])

fprintf(outputStream, "\n")

fflush(outputStream)

}

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

//文件名:EvolutionaryAlgorithm.h

#ifndef EVOLUTIONARYALGORITHM_H

#define EVOLUTIONARYALGORITHM_H

#include "Classifier.h"

#include <stdio.h>

// genome class

class Genome

{

public:

Genome(const unsigned int length)

~Genome(void)

unsigned int size

unsigned int *locus

unsigned int type

double mutationProbability

double crossoverProbability

double fitness, scaledFitness

unsigned int thresholdLength, patternLength

double generalityBias

double typeBias

void copyGenome(Genome *genome)

void uniformCrossover(Genome *genome1, Genome *genome2)

void mutateBinary(void)

void randomiseBinary(void)

void setDetector(Detector *detector)

void save(FILE *outputStream)

void show(void) { save(stdout)}

}

// species class

class Species

{

public:

Species(const unsigned int speciesSize, const unsigned int genomeLength)

~Species(void)

unsigned int speciesSize

Genome **genome

unsigned int fittestIndividual

double speciesScaledFitnessSum

double meanSpeciesFitness

Genome *FPSelection(void)

void randomise(void)

void copySpecies(Species *species)

void save(FILE *outputStream)

void show(void) { save(stdout)}

}

#endif

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

//文件名:EvolutionaryAlgorithm.cpp

#include "EvolutionaryAlgorithm.h"

#include <stdlib.h>

// genome class public methods

Genome::Genome(const unsigned int length)

{

thresholdLength = 8

patternLength = length

size = thresholdLength + 2 * patternLength

locus = new unsigned int [size]

mutationProbability = 2.0 / double(size)

crossoverProbability = 0.6

generalityBias = typeBias = 0.5

fitness = 0.0

type = 0

}

Genome::~Genome(void)

{

delete [] locus

}

void Genome::copyGenome(Genome *genome)

{

register unsigned int i = size

register unsigned int *from = genome->locus

register unsigned int *to = locus

while(i--)

to[i] = from[i]

mutationProbability = genome->mutationProbability

crossoverProbability = genome->crossoverProbability

fitness = genome->fitness

scaledFitness = genome->scaledFitness

generalityBias = genome->generalityBias

size = genome->size

patternLength = genome->patternLength

thresholdLength = genome->thresholdLength

type = genome->type

}

void Genome::uniformCrossover(Genome *genome1, Genome *genome2)

{

register unsigned int i = size

register unsigned int *from1 = genome1->locus

register unsigned int *from2 = genome2->locus

register unsigned int *to = locus

register double cp = crossoverProbability

while(i--)

{

if(drand48() <cp)

to[i] = from1[i]

else

to[i] = from2[i]

}

if(drand48() <cp)

type = genome1->type

else

type = genome2->type

}

void Genome::mutateBinary(void)

{

register unsigned int i = size

register unsigned int *loci = locus

register double mp = mutationProbability

while(i--)

if(drand48() <mp)

loci[i] = 1 - loci[i]

if(drand48() <mp)

type = 1 - type

}

void Genome::randomiseBinary(void)

{

register unsigned int index, i

index = 0

i = thresholdLength

while(i--)

locus[index++] = int((double(rand()) * 2.0) / double(RAND_MAX + 1.0))

i = patternLength

while(i--)

locus[index++] = int((double(rand()) * 2.0) / double(RAND_MAX + 1.0))

i = patternLength

while(i--)

if(drand48() <generalityBias)

locus[index++] = 0

else

locus[index++] = 1

if(drand48() <typeBias)

type = SELF

else

type = NONSELF

}

void Genome::save(FILE *outputStream)

{

register unsigned int i

Detector *detector = new Detector(patternLength)

fprintf(outputStream, \

"%-3d %-3d %-3d %-1d %-10f %-10f %-10f %-10f %-10f %-10f\n", \

size, \

thresholdLength, \

patternLength, \

type, \

fitness, \

scaledFitness, \

mutationProbability, \

crossoverProbability, \

generalityBias, \

typeBias \

)

for(i = 0i <sizei++)

fprintf(outputStream, "%-2d ", locus[i])

fprintf(outputStream, "\n")

setDetector(detector)

detector->save(outputStream)

delete detector

fflush(outputStream)

}

void Genome::setDetector(Detector *detector)

{

register unsigned int i, loci = 0, sum, lastLoci

// set activation threshold

// gray coding for threshold gene

sum = lastLoci = locus[loci++]

while(loci <thresholdLength)

{

sum = (sum <<1) | (lastLoci ^ locus[loci])

lastLoci = locus[loci++]

}

detector->threshold = double(sum) / 255.0// !!!!!!!!!!!!!!!!!!!!

for(i = 0i <patternLengthi++)

detector->value[i] = locus[loci++]

for(i = 0i <patternLengthi++)

if(!locus[loci++])

detector->value[i] = MASKVALUE

detector->type = type

detector->length = patternLength

}

// species class public methods

Species::Species(const unsigned int speciesSize, \

const unsigned int genomeLength)

{

register unsigned int i = speciesSize

this->speciesSize = speciesSize

fittestIndividual = 0

speciesScaledFitnessSum = meanSpeciesFitness = 0.0

genome = new Genome * [speciesSize]

while(i--)

genome[i] = new Genome(genomeLength)

}

Species::~Species(void)

{

register unsigned int i = speciesSize

while(i--)

delete genome[i]

delete genome

}

Genome *Species::FPSelection(void)

{

register unsigned int i = 0

register double dtmp1, dtmp2

dtmp1 = drand48() * speciesScaledFitnessSum

dtmp2 = 0.0

while((i <speciesSize) &&((dtmp2 = dtmp2 + genome[i]->scaledFitness) \

<dtmp1))

i++

return((i <speciesSize) ? genome[i] : genome[i - 1])

}

void Species::randomise(void)

{

register unsigned int i = speciesSize

while(i--)

genome[i]->randomiseBinary()

}

void Species::save(FILE *outputStream)

{

fprintf(outputStream, \

"%-4d %-4d %-5.10f %-.10f\n", \

speciesSize, \

fittestIndividual, \

speciesScaledFitnessSum, \

meanSpeciesFitness \

)

genome[fittestIndividual]->save(outputStream)

fflush(outputStream)

}

void Species::copySpecies(Species *species)

{

register unsigned int i = species->speciesSize

speciesSize = i

while(i--)

genome[i]->copyGenome(species->genome[i])

fittestIndividual = species->fittestIndividual

speciesScaledFitnessSum = species->speciesScaledFitnessSum

meanSpeciesFitness = species->meanSpeciesFitness

}

//补充了下,刚才少了个Classifier.cpp。

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

我想学下编程,但是想这些东西经常听老师们提起,他们有关联吗?如果有的话,请告诉我学习的顺序,先学哪个,在学哪个,谢谢各位大侠拉~~!!小弟在这谢谢拉~~!

解析:

basic就不要提了,几十年前就被淘汰了

先说说VB吧:

建议不学VB,因为VB垃圾、简单至极、功能不全、开发出来的软件对系统资源依赖性强(俗话-运行的这么卡呢?)、以上原因导致VB已经被社会淘汰了,已经没有客户要买用VB开发出来的软件了。

VB的开发实际上就是在一个窗口里“画”按钮之类的东西,然后再在按钮里加代码,这个所谓“按钮”实际上是VB众多被称为“控件”的东西的其中之一,是别人为你编写好的,但这些“控件”是用什么编写的呢?,是用C++编写的(VB想要实现什么功能,必须先用C++做控件),所以有句话说的好“真正的VB高手其实是C++高手”,不学VB还有一个原因,就是他破坏你学习其他软件的思路,他的语法与其他流行软件的语法不一样,学完以后会有少许误入歧途的感觉。

说说C语言:

我学过C语言,它只能编写DOS程序,功能不是很强大,但却是基础,因为大部分流行的编程软件都与C语言的语法结构类似,一定要先学。

C++:

C语言进化而来的,有强大的功能,举例吧:

软件:

微软的Office系列

Macromedia网页三剑客 - flash,dreamweaver,fireworks

Adobe出的 超有名的作图软件 - PhotoShop

3D动画软件 - 3DMax

.....

游戏:

PC平台几乎所有的游戏

星际争霸、魔兽争霸、CS、帝国时代、跑跑卡丁车、传奇、魔兽世界....

那数不胜数了,自己数吧:)

以上均出自C++之手,原因是C++靠近计算机底层,编写出来的程序对系统资源依赖较小,功能强大,运行速度快,比如你的两个朋友与你分别玩 用 VB、Java、与C++编写的“跑跑卡丁车”,你玩C++那款游戏已经跑玩结束了,发现你的两个朋友还没开始跑呢,那是相当的卡啊,所以这一系列的软件、游戏都会采用C++编写。

虽然C++很有优点,但是对于学习者来说应该是比较困难的一门语言。(再说明一点,C或C++属于语言,而VC - “Visual C++”是用来编写C或C++的工具)

Java:

不可小视的编程语言,我举不出来用它编写的应用软件和游戏,因为它是商业编程语言,我之前举的C++开发的软件都是面向普通人的,而Java开发的软件大部分是面向事业单位、公司及企业的,它的功能同样强大,结构比C++清晰,学习起来比C++简单多了,并且是跨平台运行的程序,用Java开发出来的软件可以在世界几乎所有的系统上运行(包括Linux、掌上电脑、手机等),但正因为如此,使之运行时会比C++开发的软件要慢。

没接触过编程的人恐怕很少有人知道,我不知道楼主你年龄多大了,你学编程是因为兴趣还是因为职业?如果是因为职业,建议选择学习Java。

VF:

Visual FoxPro - 微软出的数据库编程软件,和以上都不一样,没必要考虑这个,并且近几年已经被微软淘汰,因为这个软件在世界上早已被 Sql-Server 和 Oracle 等数据库编程软件所取代。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12049495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存