#include<iostream>
using namespace std;
struct person
{
int value[8][8];//静态评估表
int sum;//静态评估表的所有数据之和
int num;//赢的盘数
};
int n = 20;
struct person p[20];
void calcu();
void competition()
void selbest(best[])
void crossvariation(int best[])
void copyps(struct person q[20])
int main()
{
//readdata();
//init();
calcu();
//output();
return 0;
}
void calcu()
{
int best[20];
int i;
for(i = 0;i < 500;i++)
{
//1.竞争
comptition();
//2.选优
selbest(best);
//3.交叉 变异(先跟据的结果复制样本)
crossvariation(best);
}
}
void competition()
{
}
void selbest(int best[])
{
int q[20];
int i,j;
int sum = 0;
int randnum;
for(i = 0;i < 20;i++)
{
sum = sum + p[i].num;
q[i] = sum;
}
for(i = 0;i < n;i++)
{
randnum = rand() % 1720;
j = 0;
while(randnum >= q[j])
{
j++;
}
best[i] = j;
}
}
void crossvariation(int best[])
{//先复制,交叉后还放在q[20]内,最后变异
int pos;//交叉的位置
int temp;
int sum;
struct person q[20];
int r ,c;
int i, j, k;
//1.复制
copyps(q);
//2.交叉
pos = rand() % 50 +5;
for(i = 0;i <n;i += 2)
{//交换i和i+1的尾部数据 (pos位置以后的所有元素)
for(j = pos;j < 64;j ++)
{
r = j / 8;
c = j % 8;
temp = q[i].value[r][c];
q[i].value[r][c] = q[i + 1].value[r][c];
q[i + 1].value[r][c] = temp;
}
}
//3.填回到样本集并变异
for(i = 0;i < n;i ++)
{
sum = 0;
for(j = 0;j < 8;j ++)
{
for(k = 0;k < 8;k ++ )
{
if(rand() < 0.002)
{
p[i].value[j][k] = q[i].value[j][k] + (rand() % 5 - 2);
}
else
{
p[i].value[j][k] = q[i].value[j][k]
}
sum = sum + p[i].value[j][k];
}
}
p[i].sum = sum;
}
}
void copyps(struct person q[20])
{
int i ,j, k;
for(i = 0;i < n;i ++)
{
for(j = 0;j < 8;j ++)
{
for(k = 0;k < 8;k ++)
{
q[i].value[j][k] = p[i].value[j][k];
}
}
}
}