public List<PopUnit> InitPop()
{
if (CalculateFitnessHanler == null)
throw new Exception("未设置计算表达式的事件CalculateHanler");
if (m_Parameters.PopSize<=10)
throw new Exception("种群数太小");
List<PopUnit> pool = new List<PopUnit>();
for (int i = 0; i <= m_Parameters.PopSize-1; i++)
{
double d = m_Random.NextDouble();
d = m_Parameters.L + (m_Parameters.R - m_Parameters.L) * d;
double val= CalculateFitnessHanler(new double[] { d });
PopUnit unit = new PopUnit(d, val);
pool.Add(unit);
}
return pool;
}
public List<PopUnit> Select(List<PopUnit> data)
{
int dropoutCount = (int)(m_Parameters.DropoutRate * data.Count);
List<PopUnit> newpool = new List<PopUnit>();
for (int i = 0; i <= dropoutCount - 1; i++)
{
newpool.Add(data[i].Clone() as PopUnit);
newpool.Add(data[i].Clone() as PopUnit);
}
int tmpCount = data.Count - newpool.Count;
for (int i = dropoutCount; i <= dropoutCount + tmpCount - 1; i++)
{
newpool.Add(data[i].Clone() as PopUnit);
}
return newpool;
}
public void CrossOver(double a,double b,out double a1,out double b1)
{
if (m_Random.NextDouble() > m_Parameters.CrossOverRate)
{
a1 = a;
b1 = b;
return;
}
double r = 0.1;
double a2 = m_Random.NextDouble() * r;
double b2 = m_Random.NextDouble() * r;
a1 = (1 - a2) * a + b2 * b;
b1 = (1 - b2) * b + a2 * a;
if (a1 < m_Parameters.L)
a1 = m_Parameters.L;
if (b1 < m_Parameters.L)
b1 = m_Parameters.L;
if (a1 > m_Parameters.R)
a1 = m_Parameters.R;
if (b1 > m_Parameters.R)
b1 = m_Parameters.R;
//Console.WriteLine("crossover a1={0},b1={1} -> a2={2},b2={3}", a, b, a1, b1);
}
public double Mutation(double c)
{
double tmpd = m_Random.NextDouble();
if (tmpd > m_Parameters.MutateRate)
{
return c;
}
double k = 0.2;
//k = (m_Parameters.MaxGenerations-T)/ (m_Parameters.MaxGenerations + 2D) * 0.4D;
double res;
if (m_Random.Next(2) == 1)
{
res = c + k * (m_Parameters.R - c) *m_Random.NextDouble();
}
else
{
res = c - k * (c - m_Parameters.L) *m_Random.NextDouble();
}
//Console.WriteLine("mutation: {0} -> {1}", c, res);
return res;
}