下面的自己编辑的适应度函数模块的代码,求找出下面代码的错误
void func(population *pop_ptr);
#include<stdio.h>
#include <stdlib.h>
#define SIZE 13
double r[SIZE],
c[SIZE];
float *fp1,*fp2;
void swap(float *p1,float *p2)
{
float temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
void func(population *pop_ptr)
{
/*File ptr to the file to store the value of the g for last iteration
g is the parameter required for a particular problem
Every problem is not required*/
float *realx_ptr, /*Pointer to the array of x values*/
*binx_ptr, /* Pointer to the binary variables */
*fitn_ptr, /*Pointer to the array of fitness function*/
x[2*maxvar], /* problem variables */
f[maxfun], /*array of fitness values*/
*err_ptr, /*Pointer to the error */
cstr[maxcons];
int i,j,p,l,t,n,k,m;
float error, cc;
FILE
*fp1,
*fp2;
fp1=fopen("risk.txt","r");
fp2=fopen("cost.txt","r");
pop_ptr->ind_ptr= &(pop_ptr->ind[0]);
/*Initializing the max rank to zero*/
pop_ptr->maxrank = 0;
for(i = 0;i < popsize;i++)
{
pop_ptr->ind_ptr = &(pop_ptr->ind[i]);
realx_ptr = &(pop_ptr->ind_ptr->xreal[0]);
binx_ptr = &(pop_ptr->ind_ptr->xbin[0]);
for(j = 0; j < nvar; j++)
{ // Real-coded variables
x[j] = *realx_ptr++;
}
for(j = 0; j < nchrom; j++)
{ // Binary-codced variables
x[nvar+j] = *binx_ptr++;
}
fitn_ptr = &(pop_ptr->ind_ptr->fitness[0]);
err_ptr = &(pop_ptr->ind_ptr->error);
/* DO NOT CHANGE ANYTHING ABOVE */
/*----------------------CODE YOUR OBJECTIVE FUNCTIONS HERE------------*/
/*All functions must be of minimization type, negate maximization
functions */
for(p=1;p<SIZE;p++)
{
x[p]=p;
}
for(n=1;n<SIZE/2;n++)
{
p=rand()%SIZE+1;
l=rand()%SIZE+1;
x[p]=p;
x[l]=l;
if(l!=p)
swap(&x[p],&x[l]);
}
/*============Start Coding Your Function From This Point=============*/
for(m=1;m<SIZE;m++)
{
if(fp1!=NULL)
{
fscanf(fp1,"%f",&r[m]);
fclose(fp1);
}
else
printf("Er\n");
if(fp2!=NULL)
{
fscanf(fp2,"%f",&c[m]);
fclose(fp2);
}
else
printf("Er\n");
}
f[1]=0.00;
f[2]=0.00;
for(t=1;t<SIZE;t++)
{
// First fitness function
f[1] = r[t]*x[t];
f[1]++;
// Second Fitness Function
f[2] = c[t]*x[t];
f[2]++;
/*=========End Your Coding Upto This Point===============*/
/******************************************************************/
/* Put The Constraints Here */
/******************************************************************/
// g(x) >= 0 type (normalize g(x) as in the cstr[1] below)
/*===========Start Coding Here=============*/
cstr[1]=x[0]-x[t];
cstr[2]=x[t]-x[t];
cstr[3]=x[SIZE]-x[t];
if((cstr[1]=1)&&(cstr[2]=0)&&(cstr[3]=-1))
continue;
}
/*===========Constraints Are Coded Upto Here=============*/
/* DO NOT CHANGE ANYTHING BELOW */
for(k = 0 ; k < nfunc ;k++)
{
*fitn_ptr++ = f[k];
}
for (k = 0;k < ncons; k++)
{
pop_ptr->ind_ptr->constr[k] = cstr[k];
}
error = 0.0;
for (k = 0;k < ncons;k++)
{
cc = cstr[k];
if(cc < 0.0)
error = error - cc;
}
*err_ptr = error;
}
/*---------------------------* RANKING *------------------------------*/
if(ncons == 0)
ranking(pop_ptr);
else
rankcon(pop_ptr);
return;
}
附:下面是模型的目标函数和约束。
(1)目标函数
设有向网络G=(N,A),其中 N 和 A 分别为节点集合和有向边集,A={(i,j);i,j∈N},路段(i,j)的上的决策变量为x_ij,若路段连接节点i和j,则为1,否则为0。
其中,R_((i,j) )和F_((i,j) )为计算出的已知量,N为某条可行路径中的路段数
(3)约束
其中,式(1)为起点约束,(2)为中间点约束,(3.)为终点约束;式(4)为限定决策变量。