?? myfunction.c
字號:
// ----------------------------------------------------------------------------- MYFUNCTION
float MyFunction(struct position pos,struct param param, struct model model)
{
float a_1,a_2;
int d,dmax,d2;
float f_model;
int funct;
float hi;
int i,j;
int ix,ix2;
int n1,n2;
int used[Max_DD]={0};
float x1,x2;
struct position post;
float total;
float x;
float y;
// Data for model tuning example (see case 12)
static float data[4][2] =
{
{ 10 , 19 },
{ 20 , 643 },
{ 30 , 8360 },
{ 40 , 20687 }
};
// Data for Master Mind
static int data_M[4] =
{ 1,2,3,4};
/*
// Arbitrary table 1
static int r_w[5][5]=
{
{9 ,7, 5, 3, 1},
{8 ,6, 4, 2, 0},
{7, 5, 3, 0, 0},
{6, 4, 0, 0 ,0},
{5 ,0, 0 ,0, 0}
};
*/
/*
// Arbitrary table 2
static int r_w[5][5]=
{
{5 ,4, 3, 2, 1},
{6 ,5, 4, 3, 0},
{7, 6, 5, 0, 0},
{8, 7, 0, 0 ,0},
{9 ,0, 0 ,0, 0}
};
*/
/*
// Weighted Informational table
static int r_w[5][5]=
{
{94 ,273, 130, 20, 1},
{249, 172, 18, 0, 0},
{224, 52, 2, 0, 0},
{56, 2, 0, 0 ,0},
{3 ,0, 0 ,0, 0}
};
*/
// Specific Informational table for 4 different colors
static int r_w[5][5]=
{
{16 ,192, 120, 20, 1},
{152, 192, 24, 0, 0},
{312, 108, 6, 0, 0},
{136, 8, 0, 0 ,0},
{9 ,0, 0 ,0, 0}
};
switch(param.mine)
{
case 1: // ====================Solving an equation
/* parameters
dimension 1
xmin 0
xmax 10
target 0
eps 0.001
*/
x=pos.x[0];
total=x*(x*(x-5)+1)-1; // x^3 -5x^2 +x -1 = 0
return total;
case 2: // ======================Fermat
/* Suggested parameters:
- dimension 3
- xmin 2
- xmax 100
- target 0
- granul 1
- eps 0
You will then find three integer numbers x,y,z
so that x^2 + y^2 = z^2
*/
total = 0;
dmax=pos.size-1;
for( d=0;d<dmax;d++) //for each dimension
{
total=total+pos.x[d]*pos.x[d];
}
total=(float)fabs(total-pos.x[dmax]*pos.x[dmax]);
return total;
case 3: // ===========================================Knapsack
/* Suggested parameters:
- dimension 10
- xmin 1
- xmax 100
- target 100
- granul 1
- eps 0
*/
strcpy(functions[99],"Knapsack");
total = 0;
for( d=0;d<pos.size;d++)
total = total + pos.x[ d];
return total;
case 4: // ======================================== sin wave
/*
dimension 1
*/
strcpy(functions[99],"Sin wave");
x=pos.x[0];
total=(float)sin(x);
return total;
case 5: // ===================================== 2D Linear system
/* dimension =2, solution (5,2)
*/
x=pos.x[0];
y=pos.x[1];
total=(float)(fabs(3*x+2*y-19)+fabs(2*x-y-8));
return total;
case 6: // ===================================== Non linear system
/* dimension =2
*/
x=pos.x[0];
y=pos.x[1];
total=(float)((x*x+y*y-1)*(x*x+y*y-1)+(sin(10*x)-y)*(sin(10*x)-y));
return total;
//-- Intersection of two circles
total=(float)(fabs(x*x+y*y-1)+fabs((x-2)*(x-2)+y*y-4));
return total;
//--
total=(float) (fabs(sin(x)-y -sqrt(3)/2+1)+fabs(x*x-log(y)-(pi/3)*(pi/3)));
return total;
case 7: // ===================================== D linear system
// When pos.size=3, the solution is (1,1,1)
//printf("\n\n");
total=0;
for (i=0;i<pos.size;i++)
{
hi=0;
for (d=0;d<pos.size;d++)
{
if (i!=d) {y=(float)(i-d);} else {y=(float)pos.size;}
hi=hi+y*pos.x[d];
//printf("%f ",y);
}
//printf("\n");
//total=total+fabs (hi-pos.size*i);
total=total+(hi-pos.size*i)*(hi-pos.size*i);
}
return total;
case 8: // ===================================== 3 linear system. Pb chinois des 100 volailles
// Several integer solutions
//printf("\n\n");
strcpy(functions[99],"100 fowls");
total=(float)(fabs (pos.x[0]+pos.x[1]+pos.x[2]-100));
total=(float)(total+fabs (5*pos.x[0]+3*pos.x[1]+(1/3)*pos.x[2]-100));
return total;
x=pos.x[0]+pos.x[1]+pos.x[2]-100;
y=5*pos.x[0]+3*pos.x[1]+(1/3)*pos.x[2]-100;
total=x*x+y*y;
return total;
case 9: // ===================================== Magic square
strcpy(functions[99],"Magic square");
d2=(int)(sqrt(pos.size));
total=0;
for (i=0;i<d2-1;i++) // Rows
{
x=0;
for (j=0;j<d2;j++)
{
d=j+d2*i;
x=x+pos.x[d];
d=j+d2*(i+1);
x=x-pos.x[d];
}
total=total+x*x;
}
for (j=0;j<d2-1;j++) // Columns
{
x=0;
{
d=j+d2*i;
x=x+pos.x[d]-pos.x[d+1];
}
total=total+x*x;
}
return total;
case 10: //===============================Rosenbrock with homogeneous coordinates
post=homogen_to_carte(pos);
//display_position(pos);
//display_position(post);
funct=param.funct;
param.funct=4;
total=tot(post,param,model);
param.funct=funct;
return total;
case 11: //===============================Rastrigin with homogeneous coordinates
post=homogen_to_carte(pos);
//display_position(pos);
//display_position(post);
funct=param.funct;
param.funct=7;
total=tot(post,param,model);
param.funct=funct;
return total;
case 12: // ======================== Model tuning ================
/* We have some data and a D parameters model
Find the "best" set of parameters
*/
/*
Model lambda*(mu^D)
column 1: D
column 2: function value
*/
d2=4;
total=0;
for (d=0;d<d2;d++)
{
f_model=(float)(pos.x[0]*pow(data[d][0],pos.x[1]));
total=total+(f_model-data[d][1])*(f_model-data[d][1]);
}
return total;
case 13: // =================== 4 positions 6 colors Master Mind
/*
// Test "Distance"
total=0;
for (d=0;d<4;d++) //Right position
{
x1=fabs(pos.x[d]-data_M[d]);
total=total+x1*x1;
}
return total;
*/
// "Normal" estimation
n1=0; // right color, right position
n2=0; // right color, wrong position
for (d=0;d<4;d++) //Right position
{
ix=(int)pos.x[d];
if (data_M[d]!=ix) continue;
n1=n1+1;// Right position
used[ix]=1;
}
for (d=0;d<4;d++) // Wrong position
{
ix=(int)pos.x[d];
if (used[ix]>0) continue;
for (d2=0;d2<4;d2++)
{
if (d2==d) continue;
ix2=data_M[d2];
if (ix2!=ix) continue;
if (used[ix2]>0) continue;
n2=n2+1;
used[ix]=1;
//printf("\n d %i =>ix %i, d2 %i => ix2 %i. n2 %i",d,ix,d2,ix2,n2);
}
}
//total=fabs((float)r_w[n2][n1]-1); // So that the minimum is 0
total=(float)(400-100*n1 - n2);
//display_position (pos);
//printf("\n n1, n2, r_w: %i %i => %i",n1,n2, r_w[n2][n1]);
return total;
case 14: // Dual graph coloring
total=0;
for (i=0;i<pos.size-2;i++)
{
x=pos.x[i]+pos.x[i+1]-pos.x[i+2];
total=total+x*x;
}
return total;
case 15: // Catalan's conjecture x^m - y^n = 1 has just one integer solution (3^2 - 2^3 = 1)
// dimension = 4
total=pow(pos.x[0],pos.x[1])-pow(pos.x[2],pos.x[3])-1;
if (pos.x[0]==pos.x[2]) total=total+pow(pos.x[0],pos.x[1]); // Just to avoid the local minimum x0=x2 and x1=x3
return total;
default:
printf("\n ERROR. MyFunction: unknown param.mine");
exit(1);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -