进化策略 [英] Evolution strategy
问题描述
你好朋友
我编写了一个程序来查找"Ackley Function"的全局最小值.
我用过ES(mu,Lambda)和Lambda = 7 * Mu
每个染色体具有N个变量和N个Sigma(Sigma的自适应方法).
但这很糟糕,有什么主意吗?正确吗?
主要功能:
Hi friends
I''ve written a program to find global minimum for "Ackley Function"
I''ve used ES(mu ,Lambda) and Lambda = 7 * Mu
Each chromosome has N variable and N Sigma (self-adaption method for Sigma).
But it works very bad, is there any Idea? is it correct?
main function:
EScode es = new EScode(p, m, r, d, l, h);
// create first generation
es.initial();
for (i = 0; i < itereration; i++)
{
//create child
es.generateChilds();
//select next generation
es.selectNextGeneration();
}
EScode类别:
EScode Class:
//Ackly function that should return 0 at best situation
//It also is fitness function that less amount is better
private double Func_Ackley(int chromIndex)
{
double fvalue;
double gvalue;
double hvalue;
double x;
fvalue = 0.0;
gvalue = 0.0;
hvalue = 0.0;
x = 0.0;
for (int index = 0; index < Dimantion; index++)
{
x = ChromosomeMatrix[index, chromIndex];
gvalue = gvalue + Math.Pow(x, 2.0);
hvalue = hvalue + Math.Cos(x * 2.0 * Math.PI);
}
fvalue = -20.0 * Math.Exp(-0.02 * Math.Sqrt(gvalue / Dimantion)) - Math.Exp(hvalue / Dimantion) + 20.0 + Math.E;
return fvalue;
}
//sort Chromosome. the best answer is in first place
private void PopulationSort()
{
bool k = true;
double Temp;
while (k)
{
k = false;
for (int i = 0; i <= Population - 2; i++)
{
if (CostMatrix[i] > CostMatrix[i + 1])
{
Temp = CostMatrix[i];
CostMatrix[i] = CostMatrix[i + 1];
CostMatrix[i + 1] = Temp;
for (int j = 0; j < 2 * Dimantion; j++)
{
Temp = ChromosomeMatrix[j, i];
ChromosomeMatrix[j, i] = ChromosomeMatrix[j, i + 1];
ChromosomeMatrix[j, i + 1] = Temp;
}
k = true;
}
}
}
}
//create first generation
public void initial()
{
// fill SIGMA
for (int i = 0; i < Population; i++)
{
for (int j = Dimantion; j < 2*Dimantion; j++)
{
ChromosomeMatrix[j, i] = SimpleRNG.GetUniform();
}
}
// fill VARIABLES
for (int i = 0; i < Population; i++)
{
for (int j = 0; j < Dimantion; j++)
{
ChromosomeMatrix[j, i] = Min + (Max - Min) * SimpleRNG.GetUniform();
}
}
//evalution
for (int i = 0; i < Population; i++)
{
CostMatrix[i] = Func_Ackley(i);
}
//sort
PopulationSort();
}
//mutation function using Gaussian noise
private void mutation(int ChromIndexChild)
{
double ta;
double SIGMA;
double x;
// for SIGMA
for (int j = Dimantion; j < 2 * Dimantion; j++)
{
SIGMA = ChildChromosomeMatrix[j, ChromIndexChild];
ta = 1 / (Math.Sqrt(2 * Math.Sqrt(Population)));
SIGMA = SIGMA * Math.Exp(ta * SimpleRNG.GetUniform());
ChildChromosomeMatrix[j, ChromIndexChild] = SIGMA;
}
// for VARIABLES
for (int j = 0; j < Dimantion; j++)
{
x = ChildChromosomeMatrix[j, ChromIndexChild];
SIGMA = ChildChromosomeMatrix[j + Dimantion, ChromIndexChild];
x = x + (SIGMA * SimpleRNG.GetUniform());
ChildChromosomeMatrix[j, ChromIndexChild] = x;
}
}
//recombination method
private void recombination(int ChromIndexChild, int ChromIndexP1, int ChromIndexP2)
{
double p;
// for VARIABLES
for (int j = 0; j < Dimantion; j++)
{
p = SimpleRNG.GetUniform();
if (p < 0.5)
{
ChildChromosomeMatrix[j, ChromIndexChild] = ChromosomeMatrix[j, ChromIndexP1];
}
else
{
ChildChromosomeMatrix[j, ChromIndexChild] = ChromosomeMatrix[j, ChromIndexP2];
}
}
// for SIGMA
for (int j = Dimantion; j < 2 * Dimantion; j++)
{
ChildChromosomeMatrix[j, ChromIndexChild] = (ChromosomeMatrix[j, ChromIndexP1]+ChromosomeMatrix[j, ChromIndexP2]) / 2;
}
}
//create child
public void generateChilds()
{
int parent1 ;
int parent2 ;
double pRecom ;
double pMute;
for (int j = 0; j < NextPopulation; j++)
{
parent1 = Int16.Parse(Math.Floor(Population * SimpleRNG.GetUniform()).ToString());
parent2 = Int16.Parse(Math.Floor(Population * SimpleRNG.GetUniform()).ToString());
pRecom = SimpleRNG.GetUniform();
if (pRecom < RecombinationRate)
recombination(j, parent1, parent2);
else
{
for (int k = 0 ; k < 2 * Dimantion; k++)
{
ChildChromosomeMatrix[k, j] = ChromosomeMatrix[k, parent1] ;
}
}
pMute = SimpleRNG.GetUniform();
if (pMute < MutationRate)
mutation(j);
}
for (int j = 0; j < NextPopulation; j++)
{
ChildCostMatrix[j] = ChildFunc_Ackley(j);
}
ChildPopulationSort();
}
//select best member for next generation
public void selectNextGeneration()
{
for (int i = 0; i < Population; i++)
{
for (int j = 0; j < 2 * Dimantion; j++)
{
ChromosomeMatrix[j, i] = ChildChromosomeMatrix[j, i];
}
}
}
}
在Advance中致谢
Thanks in Advance
推荐答案
您可能要考虑实现其他排序方法,而不是像快速排序那样的冒泡排序.
http://en.csharp-online.net/Quick_Sort [ http://en.csharp-online.net/Sort_Routines [
You might want to consider implementing some other sort method instead of bubble sort, like quick sort.
http://en.csharp-online.net/Quick_Sort[^]
... or maybe some other sort you fancy
http://en.csharp-online.net/Sort_Routines[^]
If I''m not mistaken, the multi dimensioned array is an array of x elements with an array of y elements. Meaning that the y elements are aligned next to each other in memory. This means that if you first would process each first element of x, then the second, etc... you would be trashing the cache enormously.
Try to change loops like these:
for (int i = 0; i < Population; i++)
{
for (int j = 0; j < Dimantion; j++)
{
ChromosomeMatrix[j, i] = Min + (Max - Min) * SimpleRNG.GetUniform();
}
}
进入这个(其中j现在是外循环):
Into this (where j is now the outer loop):
for (int j = 0; j < Dimantion; j++)
{
for (int i = 0; i < Population; i++)
{
ChromosomeMatrix[j, i] = Min + (Max - Min) * SimpleRNG.GetUniform();
}
}
祝你好运!
Good luck!
首先,工作非常糟糕"的确切含义是什么?
其次,不要发布整个程序.给我们足够的代码,以便我们为您提供帮助,并列举出您遇到的具体问题.
最后,如何注释您的代码,以便我们知道应该执行WTF.
First, what exactly does "works very bad" mean?
Second, don''t post the entire program. Give us enough code to allow us to help you, and cite thes specific problem you''re having.
Finally, how about commenting your code so we know WTF it''s supposed to do.
朋友们
问题是随机数生成器.它仅生成正数.所以我将其更改为:
Hi friends
The problem was random number generator. It generated only positive numbers. So I changed it into :
if (SimpleRNG.GetUniform() < 0.5)
return SimpleRNG.GetUniform();
else
return SimpleRNG.GetUniform() * -1;
现在的输出是可以接受的,但效果不是很好.
我认为我的初始数字不合适.有什么想法可以改善它们吗?
谢谢
now the output is acceptable but not very well.
I think my initial numbers aren''t suitable. Is there any Idea to improve them?
thanks
这篇关于进化策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!