c语言在线解答不会啊 求好心人帮忙解答 谢谢 急急急

&& 查看话题
求助高手将以下C语言翻译为Matlab语言,急急急!
#include&&
#include&&
#include&&
#include&&
#include&&
#include&&
#include&&
#define& &Fit_name& &&&"fit.ga"
#define& &Indiv_name& &"ind.ga"
#define& &Result_name&&"out.ga"
#define& &Test_name& & "test.ga"
#define& &Cont_name& & "ctr.ga"
#define& &Mv_name& && &"m_v.ga"
#define& &Amv_name& &&&"am_v.ga"
#define& &_in_varl& &&&2
#define& &_out_varl& & 1
#define& &_rule& && &&&4
#define& &_pi& && && & 3.1415926
#define& &_order& && & 1
#define& &_time1& && &&&250
#define& &_popsize& & 50
#define& &_maxgen& &&&500& &// max generation
#define& &_number& &2& && & // for tournament selection
float& &_pmutation = 0.1 ;& &// initial mutation probability
float& &_pcrossover= 0.4 ;
const int _lchrom =&&_rule*(2*_in_varl+ (_in_varl+1)*_out_varl) ;
class&&Data
& &public&&:
& &float& &in,out, ym,
& &float& &scale_in ,scale_
& &Data()&&;
& &void& & input(int) ;
& &void& & output(float *,float *) ;
& &void& & init(void);
& &void& & Save() ;
Data :: Data()
& &for(i=0;i<_i++)
& &{&&yp = 2 ;
& &for(i=0;i<_in_i++)
& &&&scale_in = 4.2 ;
& &for(i=0;i<_out_i++)
& &&&scale_out = 4.2 ;
class Rule
friend void&&Eva_out(float *,Data ,int,Rule *) ;
float in_mu,
void& & In_mem(Data,int,int) ;
class&&Population
& &friend class&&R
& &friend void&&generation(int,int *) ;
& &friend void&&Crossover(int,int,int) ;
& &friend void&&report(float,float,float,char *) ;
& &friend void&&Eva_out(float *,Data ,int,Rule *) ;
& &friend void&&Save(int) ;
& &float& &chrom, sum_fitness, in_mu, x&&;
& &int& &&&
& &public :
& &float& &
& &Population() {sum_fitness = 0 ; }
& &void& & clear() ;
& &void& & initpop(void) ;
& &void& & Save_m_v(FILE *ft) ;
& &void& & objfunc(float) ;& &
inline float phi(float x,float mm,float v)
{ return(&&exp( - (x-mm)*(x-mm)/(v*v) )) ; }
inline float max(float a , float b)
{ return ( ( a>b) ? a :b); }
Type mutation(Type ge,int j)
&&n = 2*_in_varl+_out_
&&m = _rule*
if (flip(_pmutation))
& &if (j<=m)
& && & i = j%
& & & && & if (( i == 0 ) || (i > 2*_in_varl) ) ge&&=& &(rand()%()/1300.0 ;& & // consequent parameter a0
& && & else if ( i%2 == 1 )&&ge =&&(rand()%(200) - 100)/100.0 ;&&// mean value
& && & else&&ge =&&0.05+ (rand()%700)/1000.0 ;& &// width
& &else ge =&&(rand()%()/1300.0 ; // TSK-type consequence a1 -- an
_nmutation = _nmutation + 1 ;
int& &select(int) ;
void&&initialize(void) ;
void&&statistics(float *,float *,float *, float, int) ;
void&&generation(int, int *) ;
void&&report(float,float,float,char *) ;
float Eva_Fitness(int,int) ;
Population& &_oldpop,_newpop&&;
int& &&&_nmutation=0,_nncross=0 ;
float& &_sumfitness, _avg,_max,_min&&;
int main(void)
int& &max&&,
void Save(int) ;
srand((unsigned) time(&tt));
initialize() ;
for(gen=1; gen < _gen++)
&&cout << "\n Generation is --- " <<
&&generation(gen,&max) ;
&&cout << "\n *******&&The&&End ****** " ;
&&Save(max) ;
&&return(0);
void initialize()
&&float fmax_fit=-1E7, fmin_fit=1E7, ave_fit=0 ,
cout << "\n Initial generation& &" <<
&&for(j=1;j<=_j++)
&&{& &_newpop.initpop();
& && &_newpop.clear() ;
&&for(j=1;j<=_j++)
& && &fit =&&Eva_Fitness(j,0) ;
& && &statistics(&fmax_fit,&fmin_fit,&ave_fit,fit,j) ;
& &&&_newpop.objfunc(fit) ;
&&if( (fm=fopen(Mv_name,"w"))==NULL) exit(1) ;
&&for(j=1;j<=_j++)
& & _newpop.Save_m_v(fm) ;
&&fclose(fm) ;
&&report(fmax_fit,fmin_fit,ave_fit,Fit_name) ;
&&for(j=1;j<=_j++)
& &{&&_oldpop = _
float Eva_Fitness(int jj,int gen)
&&float act_out, error, fit=0 ;
for(k=1;k<=_time1;k++)
& & t.input(k) ;
& & Eva_out(act_out,t,jj,rules) ;
& & t.output(&error,act_out) ;
& & fit = fit +
& & return&&1./
void&&Data::input(int k)
ym = sin(2*_pi*k/100);
in = ym/scale_
in = yp/scale_
void&&Data::output(float *err,float *u)
yp&&= yp/(1+yp*yp)+u*u*u;
*err = (yp - ym)*(yp - ym) ;
& &for(i=1;i <=_ i++)
{&&yp = yp ;
void Data::init()
& &for(i=0;i<_i++)
& &&&yp = -2 ;
void Eva_out(float *y,Data t,int jj,Rule *rules)
&&int i,j,k,n,mm,nn,
&&float *den , *
& &mm = 2*_in_varl+_out_
& &nn = _rule*
& &kk = nn + _rule*_
&&if ( !( num = new float ) ||
& && & !( den = new float ) )
&&{&&cout << "\n Insufficient memory for num " ;
& &&&exit(1) ;
&&for(k=1;k<=_k++)
& &&&rules.In_mem(t,jj,k) ;&&// firing strength
&&for(j=0;j< _out_j++)
& && &for(n=1;n<=_n++)
& &&&{& & & & rules.f = _newpop.
& & & && &for(i=1;i<=_in_i++)
& & & && & rules.f += _newpop.chrom*t. //consequence value
& && &den = 0 ;
& && &num = 0 ;
& & for(n=1;n<= _n++)
& &&&{&&den = den +&&rules.in_
& & & & num = num +& &rules.in_mu * rules.
& & if ( fabs(den) < 1E-10 )
& && &*(y+j) = 0 ;
& && &*(y+j) = num/&&//defuzzify&&
&&delete&&
&&delete&&
void Population::clear()
& &fitness = 0 ;
& &checked = 0 ;
void Rule::In_mem(Data x,int jj,int k)
in_mu =&&1. ;
n = 2*_in_varl+_out_
for(i=1;i<= _in_i++)
&&in_mu = in_mu *
&&phi(x.in,_newpop.chrom,
& & & & & & & & _newpop.chrom) ;
void Population :: initpop()
int i,j1,n,m,
n = 2*_in_varl+_out_
m = _rule*
for(i=0;i<_i++)
{&&for(j1=1;j1<=_in_j1++)
& &{&&chrom = (rand()%200 -100 )/100.;& &// center
& && &chrom =&&0.05 + (rand()%700)/1000.0 ;&&//width
& &for(j=1;j<=_out_j++)
& & chrom = (rand()%()/1200. ;& & // consequence
& &for(j1=1;j1<=_j1++)
& &&&for(i=1;i<=_in_i++)
& & & & chrom =&&(rand()%()/2600.&&;&&// TSK linear parameter
void Population :: objfunc(float fit)
&&fitness =
void& &statistics(float *max_fit,float *min_fit,float *ave_fit,float fit,int j)
if ( fit > *max_fit )&&{ *max_fit =&&}
if ( fit < *min_fit ) { *min_fit = }
*ave_fit = *ave_fit + (1.0/(j+1))*(fit - *ave_fit)&&;
int flip(float prob)
i = rand()%(10001) ;
if ( i&&< 10000*prob )
void generation(int _gen,int *max)
int& &i,mate1,mate2;
float&&max_f=-1E8;
float&&fmax_fit=-1E8,fmin_fit=1E8&&;
float&&fit,ave_fit=0 ;
&&for(i=1;i<=_i++)
&&{ if ( _oldpop.fitness >&&max_f )
& & & & {& & max_f =&&_oldpop.fitness& &;
& & & && && &*max =
& & & && &}&&// end if
& &} // end for i
for(i=1;i<= _popsize/2;i++)
mate1 = select(_popsize) ;
mate2 = select(_popsize) ;
Crossover(i,mate1,mate2) ;
& &&&_newpop = _
&&for(i=1;i<=_i++)
& &_newpop.clear() ;
&&for(i=1;i<=_i++)
& && &fit =&&Eva_Fitness(i,_gen) ;
& && &statistics(&fmax_fit,&fmin_fit,&ave_fit,fit,i) ;
& &&&_newpop.objfunc(fit) ;
&&report(fmax_fit,fmin_fit,ave_fit,Fit_name) ;
&&for(i=1;i<=_i++)
& &&&_oldpop = _newpop ;
void&&Crossover(int i,int mate1,int mate2)
if (flip(_pcrossover))
{&&jcross = rand()%(_lchrom-1) +1 ;
for(j=1;j<=j++)
_newpop.chrom
& &&&= mutation( _oldpop.chrom,j);
_newpop.chrom
& &&&= mutation( _oldpop.chrom,j);
for (j=jcross+1;j<=_j++)
_newpop.chrom
& &&&= mutation( _oldpop.chrom,j);
_newpop.chrom
& && &= mutation( _oldpop.chrom,j);
for(j=1;j<=_j++)
_newpop.chrom
& &&&=&&_oldpop.
_newpop.chrom
& &&&=&&_oldpop.
int select(int range)
float max_
int max,j=0 ;
for(j=1;j<=_j++)
& &ran = rand()%(range) + 1&&;
&&max_value = _oldpop].
&&for(j=2;j<=_j++)
& &&&if ( _oldpop].fitness >&&max_value )
& && & { max =
& & & &&&max_value =&&_oldpop].
void& &report(float max_fit,float min_fit,float ave_fit,char *p)
static int i= 0 ;
cout << "\n max --- " << max_fit <<
& & & & " , min --- " << min_fit << " , avg -- " << ave_
cout << "&&Nmutation " << _nmutation&&;
if (i==2) { _nmutation = 0 ; i = 0 ; }
& &if( (fm=fopen(p,"a"))==NULL) exit(1) ;
& && & fprintf(fm,"%f&&%f&&%f" ,max_fit,min_fit,ave_fit) ;
&&fprintf(fm,"\n") ;
fclose(fm);
void Save(int max)
float act_out,
void Save_Con(float *);
&&if( (fm=fopen(Amv_name,"w"))==NULL) exit(1) ;
& &&&_newpop.Save_m_v(fm) ;
&&fclose(fm) ;
for(k=1;k<=_time1;k++)
& & t.input(k) ;
& & Eva_out(act_out,t,max,rules) ;
& & t.output(&error,act_out) ;
& & Save_Con(act_out);
& & t.Save();
void Save_Con(float *u)
& &&&FILE *
if( (fout=fopen(Cont_name,"a"))==NULL) exit(1) ;
&&fprintf(fout,"%f \n", u ) ;
& &fclose(fout) ;
void Data::Save()
if( (fout=fopen(Result_name,"a"))==NULL) exit(1) ;
&&fprintf(fout,"%f %f \n", yp,ym ) ;
& &fclose(fout) ;
void Population::Save_m_v(FILE&&*ft)
&&for(k=1;k<=_k++)
& && & fprintf(ft," %f", chrom ) ;
&&fprintf(ft,"\n") ;
不好译;但可以在matlab中调用就行了,(具体调用去网上找,一大堆)因为单独从程序上来说,简单转化是可以的,但是你的程序里面又调用了不少库函数,在matlab中对应起来相当困难,如果没有头文件什么的,转换倒不是问题,。。。 这是遗传算法吧,matlab有现成的
>> help ga
GA& & Constrained optimization using genetic algorithm.
& & GA attempts to solve problems of the form:
& && &&&min F(X)&&subject to:&&A*X&&<= B, Aeq*X&&= Beq (linear constraints)
& && && &X& && && && && && && &C(X) <= 0, Ceq(X) = 0 (nonlinear constraints)
& && && && && && && && && && & LB <= X <= ub
& & X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
& & FITNESSFCN using GA. NVARS is the dimension (number of design
& & variables) of the FITNESSFCN. FITNESSFCN accepts a vector X of size
& & 1-by-NVARS, and returns a scalar evaluated at X.
& & X = GA(FITNESSFCN,NVARS,A,b) finds a local minimum X to the function
& & FITNESSFCN, subject to the linear inequalities A*X <= B. Linear
& & constraints are not satisfied when the PopulationType option is set to
& & 'bitString' or 'custom'. See the documentation for details.
& & X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq) finds a local minimum X to the
& & function FITNESSFCN, subject to the linear equalities Aeq*X = beq as
& & well as A*X <= B. (Set A= and B= if no inequalities exist.) Linear
& & constraints are not satisfied when the PopulationType option is set to
& & 'bitString' or 'custom'. See the documentation for details.
& & X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub) defines a set of lower and
& & upper bounds on the design variables, X, so that a solution is found in
& & the range lb <= X <= ub. Use empty matrices for lb and ub if no bounds
& & exist. Set lb(i) = -Inf if X(i)&&set ub(i) = Inf if
& & X(i) is unbounded above. Linear constraints are not satisfied when the
& & PopulationType option is set to 'bitString' or 'custom'. See the
& & documentation for details.
& & X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON) subjects the
& & minimization to the constraints defined in NONLCON. The function
& & NONLCON accepts X and returns the vectors C and Ceq, representing the
& & nonlinear inequalities and equalities respectively. GA minimizes
& & FITNESSFCN such that C(X)<=0 and Ceq(X)=0. (Set lb= and/or ub= if
& & no bounds exist.) Nonlinear constraints are not satisfied when the
& & PopulationType option is set to 'bitString' or 'custom'. See the
& & documentation for details.
& & X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes
& & with the default optimization parameters replaced by values in the
& & structure OPTIONS. OPTIONS can be created with the GAOPTIMSET function.
& & See GAOPTIMSET for details.
& & X = GA(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure
& & that has the following fields:
& && &&&fitnessfcn:
& && && && & nvars:
& && && && & Aineq:
: Originally posted by libralibra at
这是遗传算法吧,matlab有现成的
&& help ga
GA& & Constrained optimization using genetic algorithm.
& & GA attempts to solve problems of the form:
& && &&&min F(X)&&subject to:&&A*X&&&= B ... 是用遗传算法来确定模糊TSK模型的,光遗传算法的比较好弄,但是和TSK模型结合的我就不会弄了~~:cry: : Originally posted by wu at
不好译;但可以在matlab中调用就行了,(具体调用去网上找,一大堆)因为单独从程序上来说,简单转化是可以的,但是你的程序里面又调用了不少库函数,在matlab中对应起来相当困难,如果没有头文件什么的,转换倒不是 ... 调用的我也看了,说是要用什么MEX,我还是没搞明白~~:cry: : Originally posted by 亚star at
调用的我也看了,说是要用什么MEX,我还是没搞明白~~:cry:... 这个你知道原理,自己可以写吧...没什么难的!c语言能写出来,matlab也能写出来:hand: : Originally posted by lijie169 at
这个你知道原理,自己可以写吧...没什么难的!c语言能写出来,matlab也能写出来:hand:... C语言的是我从网上下的~~~:cry: : Originally posted by 亚star at
C语言的是我从网上下的~~~:cry:... 你自己不懂原理就直接用么?这样不好吧。像2楼说的,有可能还不用翻译,直接用matlab的代码呢 : Originally posted by lijie169 at
你自己不懂原理就直接用么?这样不好吧。像2楼说的,有可能还不用翻译,直接用matlab的代码呢... 原理我懂啊~~但这个不是单纯的遗传算法啊,要用模糊系统,最后还要实现迭代学习控制啊~~~这好多结合起来我就不知道该怎么弄了。 三个过程叠加...应该还可以吧 : Originally posted by lijie169 at
三个过程叠加...应该还可以吧 好吧……我慢慢往懂了看吧~~~有什么不懂的请教你啊~ :sweat:,我懂编程,但是你的那些算法不是怎么懂! : Originally posted by lijie169 at
:sweat:,我懂编程,但是你的那些算法不是怎么懂! 能不能把你的QQ给我,我有什么不懂的地方请教你。C语言 求解答 急急急_百度知道
C语言 求解答 急急急
求小于m的素数#include &conio.h&#include &stdio.h&void fun( int m, int *k, int xx[] ){#include &conio.h&#include &stdio.h&void fun( int m, int *k, int xx[] ){int i,j,k=0;for(i=2;i&m;i++){
for(j=2;j&i;j++)
if(i%j==0) }
if(j==i-1)
xx[k++]=i;}*k=k;}main(){
int m, n, zz[100]; FILE * printf( &&#92;nPlease enter an integer number between 10 and 100: & ); scanf( &%d&, &n ); fun( n, &m, zz ); printf( &&#92;n&#92;nThere are %d non-prime numbers less than %d: &, m, n ); for( n = 0; n & n++ )
printf( &&#92;n
%4d&, zz[n] ); out=fopen(&out.dat&, &w&); fun( 28, &m, zz ); fprintf(out, &%d&#92;n&, m); for( n = 0; n & n++ )
fprintf(out, &%d&#92;n&, zz[n] ); fclose(out);}}main(){
int m, n, zz[100]; FILE * printf( &&#92;nPlease enter an integer number between 10 and 100: & ); scanf( &%d&, &n ); fun( n, &m, zz ); printf( &&#92;n&#92;nThere are %d non-prime numbers less than %d: &, m, n ); for( n = 0; n & n++ )
printf( &&#92;n
%4d&, zz[n] ); out=fopen(&out.dat&, &w&); fun( 28, &m, zz ); fprintf(out, &%d&#92;n&, m); for( n = 0; n & n++ )
fprintf(out, &%d&#92;n&, zz[n] ); fclose(out);}
提问者采纳
void fun( int m, int *k, int xx[] ){int i,j,k=0;k重复了吧
其他类似问题
为您推荐:
c语言的相关知识
其他1条回答
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁急急急!请求帮助!用C语言编写程序,求1+3!+5!+...+15!的和。请写出详细步骤,谢谢。是阶乘,用C语言编写程序计算。_百度作业帮
急急急!请求帮助!用C语言编写程序,求1+3!+5!+...+15!的和。请写出详细步骤,谢谢。是阶乘,用C语言编写程序计算。
急急急!请求帮助!用C语言编写程序,求1+3!+5!+...+15!的和。请写出详细步骤,谢谢。是阶乘,用C语言编写程序计算。
1+3+5+7+9+11+13+15=64这题怎么做?明天就要交了,求好心人帮帮忙,急急急,谢谢!步骤清楚,一步一步计算,么么哒,急急急_百度知道
提问者采纳
提问者评价
太给力了,你的回答完美地解决了我的问题,非常感谢!
其他类似问题
为您推荐:
其他1条回答
m小于二真分数,大于二假分数
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 c语言入门 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信