Hi,
Do you think the following fitness function is correct :
Code:
typedef struct
{
uint32_t fitness; // Minimize number of wrong cells, was uint8_t
cell_t cell[9][9];
/*const*/ uint8_t padding[3]; // TODO add const, use function copy for this struct
} Chrom_struct;
bool bounded(cell_t cell)
{
bool r;
if(cell==0 || cell==ICV)
r=false;
else if(cell>=1 && cell<=9)
r=true;
else
fatal_err("Wrong cell value, cell==%u",cell);
return r;
}
bool depend(int row,int col,int row2,int col2)
{
bool res=false;
if(row2==row || col2==col || region(row,col,row2,col2) )
res=true;
return res;
}
bool region(int row, int col, int row2, int col2)
{
bool r=false;
if( in_between(row,0,2) && in_between(col,0,2) && in_between(row2,0,2) && in_between(col2,0,2) )
r=true;
else if( in_between(row,0,2) && in_between(col,3,5) && in_between(row2,0,2) && in_between(col2,3,5) )
r=true;
else if( in_between(row,0,2) && in_between(col,6,8) && in_between(row2,0,2) && in_between(col2,6,8) )
r=true;
else if( in_between(row,3,5) && in_between(col,0,2) && in_between(row2,3,5) && in_between(col2,0,2) )
r=true;
else if( in_between(row,3,5) && in_between(col,3,5) && in_between(row2,3,5) && in_between(col2,3,5) )
r=true;
else if( in_between(row,3,5) && in_between(col,6,8) && in_between(row2,3,5) && in_between(col2,6,8) )
r=true;
else if( in_between(row,6,8) && in_between(col,0,2) && in_between(row2,6,8) && in_between(col2,0,2) )
r=true;
else if( in_between(row,6,8) && in_between(col,3,5) && in_between(row2,6,8) && in_between(col2,3,5) )
r=true;
else if( in_between(row,6,8) && in_between(col,6,8) && in_between(row2,6,8) && in_between(col2,6,8) )
r=true;
return r;
}
void compute_fitness(Chrom_struct *const cs_ptr)
{
uint8_t fitness=0;
for(int row=0; row<9; row++)
for(int col=0; col<9; col++)
for(int row2=0; row2<9; row2++)
for(int col2=0; col2<9; col2++)
{
if(row==row2 && col==col2) // Same cell
continue;
cell_t cell=cs_ptr->cell[row][col];
cell_t cell2=cs_ptr->cell[row2][col2];
if(depend(row,col,row2,col2) && cell==cell2 && bounded(cell) && bounded(cell2))
fitness++;
}
if(fitness%2 != 0)
fatal_err("Wrong fitness==%d",fitness);
if(fitness==0 )
{
bool all_bounded=true;
for(int row=0; row<9; row++)
for(int col=0; col<9; col++)
{
cell_t cell=cs_ptr->cell[row][col];
if(!bounded(cell))
all_bounded=false;
}
if(all_bounded)
{
printf("# solved: #\n");
print_chrom(*cs_ptr);
exit (EXIT_SUCCESS);
}
}
cs_ptr->fitness=(fitness/2);
return;
}
Code:Chrom cs[i]:
3 6 9 5 5 6 3 7 2
3 4 3 2 6 6 4 8 5
4 7 1 7 7 4 9 2 1
9 7 4 8 1 6 8 5 8
1 2 7 5 5 3 4 9 6
1 5 2 9 2 1 7 3 3
1 3 5 1 8 7 2 5 3
7 1 9 8 4 2 8 6 4
9 8 8 4 9 9 2 6 6
Fitness is 36
--- i==0
Code:Chrom cs[i]:
3 6 9 5 5 1 3 7 2
3 4 3 2 6 6 4 8 5
4 7 1 7 7 4 9 2 1
9 7 4 8 1 6 8 5 8
1 2 7 5 5 3 4 9 6
1 5 2 9 2 1 7 3 3
6 3 5 1 8 7 2 5 3
7 1 9 8 4 2 8 6 4
9 8 8 4 9 9 2 6 6
Fitness is 53
--- i==2