📄 genetic algorithm tool.h
字号:
class Genetic_algorithm_tool
{
private:
public:
//------------------------------------------
void ethlon(short *total_number,short **contour_x_coordinate,short **contour_y_coordinate,short *population, int *length_of_chromosome,short *break_point_coordinate,double *cost_function,short *break_point_x_coordinate_2,short *break_point_y_coordinate_2)
{
short *a= new short ();
short *b= new short ();
short *c= new short ();
float *d= new float ();
short *node = new short ();
short *count_node= new short();//accumulate the numbers between two nodes
for(short i=0;i<*length_of_chromosome -1;i++)
{
*node = *(break_point_coordinate+population[i+1])- *(break_point_coordinate+population[i]);
*a = contour_y_coordinate[0][*(break_point_coordinate+population[i+1])] - contour_y_coordinate[0][*(break_point_coordinate+population[i])];
*b = contour_x_coordinate[0][*(break_point_coordinate+population[i+1])] - contour_x_coordinate[0][*(break_point_coordinate+population[i])];
*c = (contour_y_coordinate[0][*(break_point_coordinate+population[i])] * contour_x_coordinate[0][*(break_point_coordinate+population[i+1])])-(contour_x_coordinate[0][*(break_point_coordinate+population[i])] * contour_y_coordinate[0][*(break_point_coordinate+population[i+1])]);
*d = (float)sqrt(pow(*b,2)+pow(*a,2));
*count_node=*(break_point_coordinate+population[i]);
do
{
(*count_node)++;
*cost_function += fabs((*a * contour_x_coordinate[0][*count_node]- *b * contour_y_coordinate[0][*count_node]+ *c)/ *d);
(*node)--;
}while( *node > 1);
}
*node = *(break_point_coordinate+population[0])- *(break_point_coordinate+population[*length_of_chromosome-1]);
*a = contour_y_coordinate[0][*(break_point_coordinate+population[0])] - contour_y_coordinate[0][*(break_point_coordinate+population[*length_of_chromosome-1])];
*b = contour_x_coordinate[0][*(break_point_coordinate+population[0])] - contour_x_coordinate[0][*(break_point_coordinate+population[*length_of_chromosome-1])];
*c = (contour_y_coordinate[0][*(break_point_coordinate+population[*length_of_chromosome-1])] * contour_x_coordinate[0][*(break_point_coordinate+population[0])])-(contour_x_coordinate[0][*(break_point_coordinate+population[*length_of_chromosome-1])] * contour_y_coordinate[0][*(break_point_coordinate+population[0])]);
*d = (float)sqrt(pow(*b,2)+pow(*a,2));
*count_node=*(break_point_coordinate+population[*length_of_chromosome-1]);
if(*node < 0)
*node = *total_number - *(break_point_coordinate+population[*length_of_chromosome-1]) + *(break_point_coordinate+population[0])-1;
do
{
(*count_node)++;
if(*count_node == *total_number)
*count_node = 0;
*cost_function += fabs((*a * contour_x_coordinate[0][*count_node]- *b * contour_y_coordinate[0][*count_node]+ *c)/ *d);
(*node)--;
}while( *node > 1);
}
//------------------------------------------
void bubble_sort_population(short **population,short *random_p_string,int *segment)
{
short *node_instead = new short();
for(short i=0;i<*random_p_string;i++)
for(short j=0;j<*segment -1;j++)
for(short k=0;k<*segment -1;k++)
if(population[i][k] > population[i][k+1])
{
*node_instead = population[i][k+1];
population[i][k+1] = population[i][k];
population[i][k] = *node_instead;
}
}
//------------------------------------------
void determination_of_delimiters(short *optimal_string,short *randomly_string_for_learning,int *segment,short *number_of_delimiters,short *position_of_delimiters)
{
for(short i=0;i<*segment;i++)
if(optimal_string[i] == randomly_string_for_learning[i])
{
position_of_delimiters[*number_of_delimiters]=i;
(*number_of_delimiters)++;
}
}
//------------------------------------------
void learning_strategy(short *optimal_string,short *randomly_string_for_learning,short *be_learning_string,short *start_position,short *end_position,int *segment)
{
for(short i=0;i<*segment;i++)
if(i>= *start_position && i<= *end_position)
be_learning_string[i] = randomly_string_for_learning[i];
else
be_learning_string[i] = optimal_string[i];
}
//------------------------------------------
void cross_over_in_segment_bound(short *string_1,short *string_2,short *next_string_1,short *next_string_2,int *segment)
{
srand(time(NULL));
bool *repeating_string_in_next_string_1 = new bool(false);
bool *repeating_string_in_next_string_2 = new bool(false);
short *random_position = new short();
*random_position = random((*segment-1));
for(short i=0;i< *segment;i++)
{
if( i< *random_position)
{
next_string_1[i]= string_1[i];
next_string_2[i]= string_2[i];
}
else
{
next_string_1[i]= string_2[i];
next_string_2[i]= string_1[i];
}
}
if(*random_position !=0)
{
do
{
*repeating_string_in_next_string_1 = false;
for(short i=0;i< *segment -1;i++)
{
for(short j=i+1;j< *segment;j++)
if(next_string_1[i] == next_string_1[j])
{
next_string_1[i] = next_string_2[j];
*repeating_string_in_next_string_1 = true;
break;
}
if(*repeating_string_in_next_string_1 == true)
break;
}
}while(*repeating_string_in_next_string_1 == true);
do
{
*repeating_string_in_next_string_2 = false;
for(short i=0;i< *segment -1;i++)
{
for(short j=i+1;j< *segment;j++)
if(next_string_2[i] == next_string_2[j])
{
next_string_2[i] = next_string_1[j];
*repeating_string_in_next_string_2 = true;
break;
}
if(*repeating_string_in_next_string_2 == true)
break;
}
}while(*repeating_string_in_next_string_2 == true);
}
}
//------------------------------------------
void mutation_in_segment(short *string_1,int *segment,short *unmber_of_break_point_2)
{
srand(time(NULL));
short *position_of_mutation = new short();
short *value_of_mutation = new short();
bool *stop_mutation = new bool();
*position_of_mutation = random(*segment);
do
{
*stop_mutation = true;
*value_of_mutation = random(*unmber_of_break_point_2);
for(short i=0;i<*segment;i++)
if(string_1[i] == *value_of_mutation)
if(i != *position_of_mutation)
{
*stop_mutation = false;
break;
}
}while(*stop_mutation == false);
}
//------------------------------------------
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -