📄 gacontrol.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace GA_RPS.GAKernel
{
public class GAControl
{
public const int ROCK = 0;
public const int PAPER = 1;
public const int SCISSOR = 2;
public static int POP_SIZE = 12;
public static int POP_NUM = 8;
public static int SEL_SCALAR = 4;
public static double MUT_RATIO = 1.0/(double)POP_SIZE;
public static int ITER_TIME = 10;
private volatile bool isEvolving = false;
public bool IsEvolve
{
get { return isEvolving; }
set { isEvolving = value; }
}
private Population[] p;
private int curPop;
public GAControl()
{
p = new Population[POP_NUM];
curPop = 0;
}
public bool gaTrain(int gaMove, int playerMove)
{
//autoGA();
//System.Windows.Forms.MessageBox.Show(Convert.ToString(curPop + 1));
if (p[curPop] == null)
{
p[curPop] = new Population();
}
if (!p[curPop].isFull())
{
p[curPop].addMember(gaMove, playerMove);
}
if (p[curPop].isFull())
{
if (curPop < POP_NUM - 1)
{
p[++curPop] = new Population();
return false;
}
else
{
isEvolving = true;
//evolve();
return true;
}
}
else
{
return false;
}
}
private void autoGA()
{
Roulette r = new Roulette();
for (int i = 0; i < POP_NUM; i++)
{
p[i] = new Population();
while (!p[i].isFull()) p[i].addMember(r.ranRPS(), r.ranRPS());
}
evolve();
}
public void evolve()
{
for (int i = 0; i < ITER_TIME; i++)
{
p = new Selection().selectOp(p);
p = new Crossover().crossOp(p);
p = new Mutation().mutateOp(p);
}
isEvolving = false;
}
public int[] newGeneration()
{
int[] gen = new int[POP_NUM * POP_SIZE];
int n = 0;
foreach (Population c in p)
{
for (int i=0;i<POP_SIZE;i++)
{
gen[n++] = c.GaArray[i];
}
}
p = new Population[POP_NUM];
curPop = 0;
return gen;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -