changing the objective function

Hi,

I have a GA source code in C and I want to change its objective function. My objective function has a lot of details and I have opened that file in my project too, but I want to call it in this part of the program by the format of the following objective function and remove this one. Is there anyone who lets me know how I can change it?

objfunc(critter)
/* objective function used in Goldberg's book */
/* fitness function is f(x) = x**n,
normalized to range between 0 and 1,
where x is the chromosome interpreted as an
integer, and n = 10 */

struct individual *critter;
{
unsigned mask=1; /* mask for current bit */
unsigned bitpos; /* current bit position */
unsigned tp;
double pow(), bitpow, coef;
int j, k, stop;
int n = 10;

critter->fitness = 0.0;
coef = pow(2.0,(double) lchrom) - 1.0;
coef = pow(coef, (double) n);

/* loop thru number of bytes holding chromosome */
for(k = 0; k < chromsize; k++)
{
if(k == (chromsize-1))
stop = lchrom-(k*UINTSIZE);
else
stop = UINTSIZE;

/* loop thru bits in current byte */
tp = critter->chrom[k];
for(j = 0; j < stop; j++)
{
bitpos = j + UINTSIZE*k;
/* test for current bit 0 or 1 */
if((tp&mask) == 1)
{
bitpow = pow(2.0,(double) bitpos);
critter->fitness = critter->fitness + bitpow;
}
tp = tp>>1;
}
}

/* At this point, fitness = x */
/* Now we must raise x to the n */
critter->fitness = pow(critter->fitness,(double) n);

/* normalize the fitness */
critter->fitness = critter->fitness/coef;
}

LiLi wrote:
I want to call it in this part of the program by the format of the following objective function and remove this one.

I don't understand what you are asking.
I have this objective function:
for( i = 0; i < Candidate; i++) {
total_fixed_cost += Y_fix[i] * FixedCost[i];
}

int Z_fix[int linknum] = Z_fix;
int OriginalLinkCost[linknum] = OriginalLinkCost;
int linknum;
int total_fixed_cost;

for( linknum = 0; linknum < LinkNumber; linknum++ )
{
Capacity[linknum] = Capacity[linknum] + Z_fix[linknum] * Capacity[linknum];
CapExpCost += w * int LinkLength[linknum] * ( Z_fix[linknum] * Capacity[linknum] );
OriginalLinkCost += Alpha * TravelTime[linknum] * ( 1 + a[linknum] * pow ( ( BackgrdVolume[linknum] / Capacity[linknum] ) , b[linknum] ) );
}
I wanted to use it in the form of the objective function I have introduced before.
Is it OK that I just write this?

objfunc(critter)

struct individual *critter;
{
unsigned mask=1; /* mask for current bit */
unsigned bitpos; /* current bit position */
unsigned tp;
double pow();
int i, j, k, stop;

critter->fitness = 0.0;

/* loop thru number of bytes holding chromosome */
for(k = 0; k < chromsize; k++)
{
if(k == (chromsize-1))
stop = lchrom-(k*UINTSIZE);
else
stop = UINTSIZE;

/* loop thru bits in current byte */
int Y_fix[Candidate] = Y_fix;
tp = critter->chrom[k];
for(j = 0; j < stop; j++)
{
bitpos = j + UINTSIZE*k;
/* test for current bit 0 or 1 */
if((tp&mask) == 1)
{
for( i = 0; i < Candidate; i++) {
total_fixed_cost += Y_fix[i] * FixedCost[i];
}

int Z_fix[int linknum] = Z_fix;
int OriginalLinkCost[linknum] = OriginalLinkCost;
int linknum;
int total_fixed_cost;

for( linknum = 0; linknum < LinkNumber; linknum++ )
{
Capacity[linknum] = Capacity[linknum] + Z_fix[linknum] * Capacity[linknum];
CapExpCost += w * int LinkLength[linknum] * ( Z_fix[linknum] * Capacity[linknum] );
OriginalLinkCost += Alpha * TravelTime[linknum] * ( 1 + a[linknum] * pow ( ( BackgrdVolume[linknum] / Capacity[linknum] ) , b[linknum] ) );
}
critter->fitness = critter->fitness + (total_fixed_cost + CapExpCost + OriginalLinkCost);
}
tp = tp>>1;
}
}

/* inverse the fitness */
critter->fitness = critter->fitness;
}
Code tags, please. There's no way that can be readable, otherwise.

[code]
1
2
3
4
5
#include <iostream>

int main(){
    std::cout <<"Hello, World!\n";
}
[/code]
Topic archived. No new replies allowed.