<OJ_Sicily>Order Crossover
来源:互联网 发布:测试风扇转速软件 编辑:程序博客网 时间:2024/05/21 18:31
Description
The procedure of Order Crossover (OX) applied in the Genetic Algorithm is as follows.
1. Select a substring from a parent at random.
2. Produce a proto-child by copying the substring into the corresponding position of it.
3. Delete the cities which are already in the substring from the 2nd parent. The resulted sequence of citires contains the cities that the proto-child needs.
4. Place the cities into the unfixed positions of the proto-child from left to right according to the order of the sequence to produce an offspring.
Input
There may be multiple cases. For each case, the first line is an integer n (1<=n<=100). The second line and the third line give two parents(each line has ninteger). The fourth line gives the starting and ending position (x and y with 0<=x<=y<n) of the substring.
Output
For each case, output two resultant offsprings.
问题解释:使用顺序交叉应用到遗传算法上。这里使用一组数代表一个个体,一个后代个体的产生由两个个体产生(暂且一个称A个体一个称B个体),其中一个个体会遗传一段连续的片段到子代相应的位置,剩下的子代内容则从另外一个个体中按照母个体的顺序获取。
输入:第一行表示每一个个体包含的数的个数(有点类似于DNA长度);第二行则是A个体的基因(一组由n个数组成的数);第三行则是B个体的基因;第四行表示从其中一个个体遗传基因的起止位置
输出:输出两种可能的子个体基因
#include <iostream>#include <vector>using namespace std;static bool flag[100];static int parent1[100],parent2[100];//将输入的一组数变成数组void getParent(int* a,int num){ for(int i = 0; i <num; i ++){ cin >> a[i]; }}//获取子个体void getOffspring(const int *p1,const int *p2,int begin,int end,int len,int* result){ int i,j; //子个体从p1个体中获取从begin到end的一段序列 for(i = begin; i <= end; i ++){ flag[p1[i]] = true; result[i] = p1[i]; } //子个体剩下的内容从p2个体中顺序获得 for(i = 0,j = 0; i < len;i++ ){ if(!flag[p2[i]]){ if(j == begin) j = end +1; result[j] = p2[i]; j ++; } else{ flag[p2[i]] = false; } }}//输出子个体void printVecInt(int *result,int len){ for(int i = 0; i < len-1; i++){ cout << result[i] << " "; } cout << result[len-1] << endl;}int main(int argc, const char * argv[]) { // insert code here... int Num; while(cin >> Num){ if(Num == 0)break; int startPos,endPos; getParent(parent1, Num); //将输入的一组数转为数组 getParent(parent2, Num); int offSpring1[100] ={0}, offSpring2[100]={0}; cin >> startPos >> endPos; getOffspring(parent1, parent2, startPos, endPos,Num,offSpring1); //获取第一种情况的子个体 getOffspring(parent2, parent1, startPos, endPos,Num,offSpring2); //获取第二种情况的子个体 printVecInt(offSpring1,Num); printVecInt(offSpring2,Num); } return 0;}
后记:
因为要获得两种情况的子个体,可以写一个函数,改变输入参数顺序便可以获得两种情况。
代码新手,欢迎各位提出宝贵的建议和意见
- <OJ_Sicily>Order Crossover
- Crossover
- CrossOver
- <OJ_Sicily>Fibonacci
- <OJ_Sicily>Hanoi_Tower_Sequence
- <OJ_Sicily>Maze
- <OJ_Sicily>Bicoloring
- <OJ_Sicily>DAG
- <OJ_Sicily>Forest
- <OJ_Sicily>Maze
- <OJ_Sicily>Rails
- <OJ_Sicily>Pair
- <OJ_Sicily>Polynomial
- <OJ_Sicily>Connection
- CrossOver介绍
- <OJ_Sicily>Single-link Clustering
- <OJ_Sicily>N_Queen Problem
- <OJ_Sicily>1134积木分发
- Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V解决办法
- bzoj 1072: [SCOI2007]排列perm(状压DP)
- jQuery+css3实现的超酷圆角tab选项卡切换效果
- android------sqlite中的 query() 参数分析-----------------------------------
- Centos 清理内存
- <OJ_Sicily>Order Crossover
- 逆序对/归并排序的应用
- java的三大特性--封装,继承,多态
- NFS使用
- 5--指针与数组(下)
- [Java]工厂设计模式
- jQuery文件上传插件Uploadify使用指南
- LVS keepalived 备忘
- 按钮宽度和高度固定,字体大小根据字数自适应用的javascript实现