grasshopper-galapagos遗传算法

grasshopper-galapagos遗传算法
grasshopper-galapagos遗传算法

Evolutionary Principles applied to

Problem Solving

遗传算法

There is nothing particularly new about Evolutionary Solvers or Genetic Algorithms. The first references to this field of computation stem from the early 60's when Lawrence J. Fogel published the landmark paper "On the Organization of Intellect" which sparked the first endeavours into evolutionary computing. The early 70's witnessed further forays with seminal work produced by -among others- Ingo Rechenberg and John Henry Holland. Evolutionary Computation didn't gain popularity beyond the programmer world until Richard Dawkins' book "The Blind Watchmaker" in 1986, which came with a small program that generated a seemingly endless stream of body-plans called "Bio-morphs" based on human selection. Since the 80's the advent of the personal computer has made it possible for individuals without government funding to apply evolutionary principles to personal projects and they have since made it into the common parlance.

其实在遗传算法和基因算法里并什么特别新的理论出现,该领域的第一篇文献出现在六十年代由Lawrence J. Fogel 出版的具有里程碑意义的论文“智能组织”,这篇论文使人们开始致力于研究遗传算法。七十年代又由ngo Rechenberg and John Henry Holland的工作进一步带动了基因算法的发展,遗传算法直到1986年才因为Richard Dawkins的“The Blind Watchmake”而让人广

为人知,里面有个小的例子,基于人类的选择仍然会产生无尽的被称为“生态形变”的动作计划。80年代由于个人电脑的出现使得每个人都可以将进化算法用于个人项目而不用政府提供资金支持,从此,进化算法开始像日常话题一样进入公众视野。

The term "Evolutionary Computing" may very well be widely known at this point in time, but they are still very much a programmers tool. 'By programmers for programmers' if you will. The applications out there that apply evolutionary logic are either aimed at solving specific problems, or they are generic libraries that allow other programmers to

piggyback along. It is my hope that Galapagos will provide a generic platform for the application of Evolutionary Algorithms to be used on a wide variety of problems by non-programmers.

虽然现在进化算法在现在已经广为人知,但他大部分还作为编程工具使用,只是程序员和程序员之间交流,遗传算法即使应用也是处理一些特别的问题,大部分时间还是呆在通用库里等着程序员来用它,我希望galapagos可以提供一个遗传算法的广泛平台来让那些非编程人员来处理一些问题。

Pros and Cons

赞成和反对

Before we dive into the subject matter too deeply though I feel it is important to highlight some of the (dis)advantages of this particular type of solver, just so you know what to expect. Since we are not living in the best of all possible worlds there is often no such thing as the perfect solution. Every approach has drawbacks and limitations. In the case of Evolutionary Algorithms these are luckily well known and easily understood drawbacks, even though they are not trivial. Indeed, they may well be prohibitive for many a particular problem.

在我们深入探讨这个问题之前,我认为有必要向大家介绍这类特殊算法处理方式的优缺点,这样可能更利于你了解你到底想通过这个获得什么。事实上我们生活在一个不完美的世界,没有什么事情会有一个完美地解决方案,每个解决方案都会有他的缺陷和限制。而在遗传算法领域可喜的是我们明确地知道他的一些缺陷,而且这些缺陷也不小,而且确实在一些具体问题的处理上明确禁止用遗传算法来解决。

Firstly; Evolutionary Algorithms are slow. Dead slow. It is not unheard of that a single process may run for days or even weeks. Especially complicated set-ups that require a long time in order to solve a single iteration will quickly run out of hand. A light/shadow or acoustic computation for example may easily take a minute per iteration. If we assume we'll need at least 50 generations of 50 individuals each (which is almost certainly an underestimate unless the problem has a very obvious solution.) we're already looking at a two-day runtime.

首先,遗传算法在运行起来的时候很慢,奇慢,非常慢,慢的要死。一个小程序运行几天甚至几个星期一点也不见怪,尤其是需要长时间简单迭代的复杂数据结构,运行起来会马上超出你的掌控,就算简单的光影分析或者是声学分析每次迭代就要几分钟,假设我们设置50代,每代50个个体(这都是按照最少最理想的状态来设定的),那这个程序需要两天才能做完。

Secondly, Evolutionary Algorithms do not guarantee a solution. Unless a predefined

'good-enough' value is specified, the process will tend to run on indefinitely, never reaching The Answer, or, having reached it, not recognizing it for what it is.

其次,遗传算法并不能百分之百的保证求出正确的解,除非一个预先你已经想好的差不多的解已经确定下来,遗传算法会尽量的朝着你想要的那个解努力,但是他永远不会找到“真理”,当然,也可能找到了只不过他是电脑,根本意识不到那个是最优解,即“真理”。

All is not bleak and dismal however, Evolutionary Algorithms have strong benefits as well, some of them rather unique amongst the plethora of computational methods. They are remarkably flexible for example, able to tackle a wide variety of problems. There are classes of problems which are by definition beyond the reach of even the best solver implementation and other classes that are very difficult to solve, but these are typically rare in the province of the human meso-world. By and large the problems we encounter on a daily basis fall into the 'evolutionary solvable' category.

当然,遗传算法也并不是上面我说的那么单调无力,一直被吐槽。进化算法在一些方面也有着强大的性能体现,在茫茫多的计算算法里他确实一枝独秀,进化算法有很强的灵活性,能同时处理各种各样的问题,有些问题最好的解算器也解不出来,而且很多的我们日常遇到的问题都可以放到进化算法的范畴内进行解决。

Evolutionary Algorithms are also quite forgiving. They will happily chew on problems that have been under- or over-constrained or otherwise poorly formulated. Furthermore, because the run-time process is progressive, intermediate answers can be harvested at practically any time. Unlike many dedicated algorithms, Evolutionary Solvers spew forth a never ending stream of answers, where newer answers are generally of a higher quality than older answers. So even a pre-maturely aborted run will yield something which could be called a result. It might not be a very good result, but it will be a result of sorts.

进化算法的容忍性也很好,他很有能力去处理一些你低估或者高估或者很傻X的操作,进一步讲,因为它运行的时候是动态的,所以你在任何时候都可以收获一大堆的解而不像其他解算法(解完了才能得到答案),进化算法射出来一大堆永远没有尽头的解,而且这一代的解总体上来说会比上一代的质量更高,所以,尽管一些早产的结果产生我们也可以姑且把他认为是结果,虽然他不是最优秀的结果,但是他确是得到最优结果必不可少的一部分。

Finally, Evolutionary Solvers allow -in principle- for a high degree of interaction with the user. This too is a fairly unique feature, especially given the broad range of possible applications. The run-time process is highly transparent and browsable, and there exists a lot of opportunity for a dialogue between algorithm and human. The solver can be coached across barriers with the aid of human intelligence, or it can be goaded into exploring sub-optimal branches and superficially dead-ends.

最后讲一点,进化算法和用户有很强的交互性,这是非常特别独特的属性,特别是当你给了他一大堆可能的条件是。当他在运行起来的时候,求解过程非常透明也便于浏览,因此存在很多机会创建人机交互的对话窗口,引导解算跨过障碍,或者探索一些并不是很理想的答案,甚至一条死胡同。

The Process

运行特点

In this section I shall briefly outline the process of an Evolutionary Solver run. It is a highly simplified version of the remainder of the blog post, and I'll skip over many interesting and even important details. I'll show the process as a series of image frames, where each frame shows the state of the 'population' at a given moment in time. Before I can start however, I need to explain what the image below means.

在这段里我将简单的介绍一下进化算法是如何运行的,这些内容都是以前一些博客简化了的,所以忽略了一些有趣甚至重要的细节。下面我会用一系列的图片来展示他是如何运行的,每一张图都记录了在特定时刻的“代”的关系,在开始之前,我先解释一下下图是啥意思。

What you see here is the Fitness Landscape of a particular model. The model contains two variables, meaning two values which are allowed to change. In Evolutionary Computing we refer to variables as genes. As we change Gene A, the state of the model changes and it either becomes better or worse (depending on what we're looking for). So as Gene A changes, the fitness of the entire model goes up or down. But for every value of A, we can also vary Gene B, resulting in better or worse combinations of A and B. Every combination of A and B results in a particular fitness, and this fitness is expressed as the height of the Fitness Landscape. It is the job of the solver to find the highest peak in this landscape.

你可以把上面这张图当成一个特别的景观模型(类似于山脉),这个模型包涵了两个变量,他们可以同时变化。在进化算法中我们习惯把变量称为基因。当我们改变基因A的时候结果会变好或者变坏(取决于我们希望他变好还是变坏)来匹配适合模型的状态。所以,当基因A改变时,整个模型都在变化。同理,在基因A发生变化时,基因B也可以同时参与变化,这时候出来的结果就是A和B同时起的作用。每一个A和B所产生出来的结果都被反映在这个模型的Z轴高度上,因为我们的任务就是找

出来这个山峰的最高点。

Of course a lot of problems are defined by not just two but many genes, in which case we can no longer speak of a 'landscape' in the strict sense. A model with 12 genes would be a 12-dimensional fitness volume deformed in 13 dimensions instead of a

two-dimensional fitness plane deformed in 3 dimensions. As this is impossible to visualize I shall only use one and two-dimensional models, but note that when we speak of a "landscape", it might mean something terribly more complex than the above image shows.

诚然,大量的问题并非简单的通过两个基因就能确定,而是需要很多基因,所以严格意义上来讲的话也就不能用景观(山脉)这个概念来解释,你想想看,一个用12个变量确定的将会是一个12维体扭曲在13维空间里,而不再是2维平面扭曲在三维空间里。所以我用一个2维的模型是不可能展示出来12维物体的,不过我提到这个只是想说有时候我们研究的要比上面这个2维世界的图复杂得多。

As the solver starts it has no idea about the actual shape of the fitness landscape. Indeed, if we knew the shape we wouldn't need to bother with all this messy evolutionary stuff in the first place. So the initial step of the solver is to populate the landscape (or "model-space") with a random collection of individuals (or "genomes"). A genome is nothing more than a specific value for each and every gene. In the above case, a genome could for example be {A=0.2 B=0.5}. The solver will then evaluate the fitness for each and every one of these random genomes, giving us the following distribution:

当这个进化算法开始的时候,其实电脑是不知道这个山脉景观的,如果电脑会像我们一样一眼看出来这个山脉的高低一开始就不用这么麻烦的去弄一大堆的数据。所以,在一开始,电脑就要用一大堆随机的点(基因组)来给他塑形。基因组就是一组数据,没别的,在这个例子里,一个基因组就相当于A=0.2 B=0.5的一个坐标。电脑计算过每一个随机的基因组,并且适应化之后,我们就可以看到下图这样的分布。

Once we know how fit every genome is (i.e., the elevation of the red dots), we can make a hierarchy from fittest to lamest. We are looking for high-ground in the landscape and it is a reasonable assumption that the higher genomes are closer to potential

high-ground than the low ones. Therefore we can kill off the worst performing ones and focus on the remainder:

一旦我们知道了这些基因组(下图这些抬高的红点点)匹配这个山脉的程度,我们就可以通过匹配的程度,非常匹配或者差一些的,来给他们分级。我们是在寻找这个山脉上位置较高的点,所以有充足的理由来假设这些位置较高的基因组要比位置靠下的基因组更加接近顶端,所以,我们可以把那些表现的差一些的拖出去斩了而只关注那些剩下来的优秀的。

It is not good enough to simply pick the best performing genome from the initial population and call it quits. Since all the genomes in Generation 0 were picked at random, it is actually quite unlikely that any of them will have hit the jack-pot. What we need to do is breed the best performing genomes in Generation 0 to create Generation 1. When we breed two genomes, their offspring will end up somewhere in the intermediate model-space, thus exploring fresh ground:

如果我们只是把位置较好的基因从第一代提取出来并且停止工作这样并不是很好,因为0代基因是随机生成的点,不一定这些随机的点就有一个恰好在这个山脉的顶端。所以我们要做的是把第一代表现优良的基因组取出来并且让他们培育第二代,他们的后代会在这个原来父代模型空间的中间区域,所以也相当于扩展到了新的空间。

We now have a new population, which is no longer completely random and which is already starting to cluster around the three fitness 'peaks'. All we have to do is repeat the above steps (kill off the worst performing genomes, breed the best-performing genomes) until we have reached the highest peak.

现在我们有了新的一代人,并且这一代人并不是完全的随机了,而是更加的成组团的靠近三个山峰。后面我们要做的就是重复上一步(干掉那些掉链子的,养育那些表现好的,PS题外话:这就是赤裸裸的落后就要挨打啊,还不光挨打,直接干掉。。。。)直到我们找到到达最顶峰的点。

In order to perform this process, an Evolutionary Solver requires five interlocking parts, which I'll discuss in something resembling detail. We could call this the anatomy of the Solver.

这种算法需要五个具有连锁反应的部分,稍后我们会讨论下类似的细节,下面我们开始解剖他吧。

1Fitness Function 适应度函数

2Selection Mechanism 选择机制

3Coupling Algorithm 耦合算法

4Coalescence Algorithm 合并算法

5Mutation Factory 突变因素

Fitness Functions

适应度函数

In biological evolution, the quality known as "Fitness" is actually something of a stumbling block. Usually it is very difficult to say exactly what it means to be fit. It certainly has little or nothing to do with being the strongest, or the fastest, or the most vicious. The reason there are no flying dogs isn't that evolution hasn't gotten around to making any yet, it is that the dog lifestyle is supremely incompatible with flying and the sacrifices required to equip a dog with flight would certainly detract more from the overall fitness than flight would add to it. Fitness is the result of a million conflicting forces. Evolutionary Fitness is the ultimate compromise.

在生物进化论里,适应度这种概念在一定程度上就相当于阻碍。有时候我们很难准确定义什么是适应。适应这种概念这个年代看起来和更强壮的,更快的,或者是更邪恶的没有什么关系。没有进化出来飞狗并不是进化还没到一个很高的程度,而是狗的生活习惯不适于飞行,如果非要让一个狗进化出来能飞的翅膀,这个翅膀反而会大大削减其他方面的机能,所以他没有进化出来。适应的进化是百万年外界压力的磨合,进化的形态是最终向大自然妥协的结果。

A fit individual is on average able to produce more offspring than an unfit one, so we could say that fitness equals the number of genetic children. A better measure yet would be to count the number of grand-children. And a better measure yet would be to count the allele frequency in the gene-pool of the genes that made up the individual in question. But these are all rather ad-hoc definitions that cannot be measured on the spot.

一个适应性更强的父代平均来说要比适应性差的父代子孙更多,所以我们可以简单地说适应性就相当于后代数量。一个很好的测算方法就是看他的后代数量,或者更好的方法就是看后代基因组频率和父代的相似度。但是这些专门定义方法在一些情况下不适用。

At least in Evolutionary Computation, fitness is a very easy concept. Fitness is whatever we want it to be. We are trying to solve a specific problem, and therefore we know what it means to be fit. If for example we are seeking to position a shape so that it may be milled with minimum material waste, there is a very strict fitness function that leaves no room for argument.

在进化算法里,适应性是个很简单的概念,适应性就是我们想要的结果,当我们尝试解决一些特别的我们已经知道以何种方式匹配的问题。举个例子,我们试图寻找一个图形的位置他可以尽量的减少材料的浪费,这就是一个非常严格的没有什么讨论余地的问题。

Let's have a look at the fitness landscape again and let's imagine it represents a model that seeks to encase an object in a minimum volume bounding-box. A minimum bounding-box is the smallest orthogonal box that completely contains any given shape. In the image below, the green shape is encased by two bounding boxes. B has a smaller area than A and is therefore fitter.

我们再看一个地形,让我们把它想象成一个包在最小长方形体里的模型,最小包裹体就是包含任何形状最小的正交方体,在下面的图例,绿色的形状被两个盒子包含,B就比A的面积更小,更合适。

When we need to mill or 3D-print a shape, it is often a good idea to rotate it until it requires the least amount of material to be used during manufacturing. For a real minimum bounding-box we need at least three rotation axes, but since that will not allow me to display the real fitness landscape, we will restrict ourselves to rotation around the world X and Y axes. So, Gene A will represent the rotation around the X axis and Gene B will represent rotation around the Y axis. There is no need to allow for rotation higher than 360 degrees, so both genes have a limited working domain. (In fact, since we are talking about orthogonal boxes, even a 0-90 degree domain would suffice). Behold rotation around a single axis:

当我们需要做一个三维的模型时,如果能在加工制造时最大化的减少材料的浪费是个很好的想法,一个三维的最小的包涵盒子最少要3个旋转轴,但是因为也显示不了真正的结果,我们在这里就只围绕X,Y轴旋转。所以基因A就代表X轴旋转,基因B就代表Y轴旋转。没必要旋转360度因为每个基因都有一个限制工作的范围(事实上,因为我们讨论的是正交的盒子,所以90度就已经足够了)看他围绕单轴旋转:

When we pick two rotational angles at random, we end up somewhere on the fitness landscape. If we allow for 4 decimal places in the rotation angles it means we can actually generate almost 810,000,000,000 (or 810 billion) unique rotations. It is therefore exceptionally unlikely that we manage to pick a random rotation that yields the best possible answer. But let's say we don't even manage to get close. Let's say we manage to pick a random genome that is at the bad end of the fitness scale, i.e. at the bottom of the fitness landscape. What can we say about the blood-line of this genome? When we track the descendants of a particular genome there is always a large amount of randomness involved due to the workings of the Solver, but there is a strong general tendency that can be distinguished. Just like water will always flow downhill along the steepest slope, so genetic descendants will generally climb uphill along the steepest slope:

当我们选择两个旋转轴的时候,随机停在一个时候,如果我们的旋转轴设定4位小数,那就意味着我们会产生810,000,000,000 个角度的解,看来我们随机选一个角度来产生优秀的解这个方法是行不通了,甚至根本无法接近那个最优解,我们随机选的这些基因最后都不是最优解,像是在刚才那个

山丘的最底层的那些点。我们怎么来定义这些基因的血缘呢?当我们去追溯一个基因的后代,通常因为计算会产生出大量的后代,但是他们都有一个很强的易区别的某种倾向性。就像水流只顺着斜率最大的陡坡流下,这些后代会朝着最小阻力的顶部攀登。

Every individual tries to maximize its own fitness, as high fitness is rewarded by the solver. And the steepest uphill climb is the fastest way towards high fitness. So if the black sphere represents the location of the ancestral genome, the orange track represents the pathway of its most successful offspring. We can repeat this exercise for a large amount of sample points which will tell us something about how the Solver and the Fitness Landscape interact:

每一个个体都尽力最大化自己的适应性,因为适应性高才能存活,而且斜率最高的攀爬路径是最快的,所以大家看上面那张图,黑色的球体代表最初的基因,而橘黄色的路径就是那些表现最优良的基因所攀爬的方式,我们可以通过大量的点来测试一下,看看基因算法和适应性机制是如何相互作用的。

Since every genome is pulled uphill, every peak in the fitness landscape has a basin of attraction around it. This basin represents all the points in model-space that will converge upon that specific peak. It is important to notice that the area of the basin is in no way representative of the quality of the peak. Indeed, a very poor solution may have a large basin of attraction while a good peak might have a small catchment area. Problems like this are typically very difficult to solve, as the solution tends to get stuck in local optima. But we'll have a look at problematic fitness functions later on.

因为每个基因都会被拉到山顶,所以开始之前就有点池在山顶的周围环绕,这个池子里面就是将会聚集到山顶的点,我们要特别注意一点就是点池并不意味着山顶的质量,因为存在一个问题,有可能一个比较矮的山顶周围围绕了大量的点,而较高的山顶只围绕了少量的,像出现这样的问题一般很难处理,这个解算在局部最优以后就会停止,所以随后我们会看见一个有些问题的解。

First, let's have a closer look at the actual fitness landscape for our minimum bounding-box model. I'm afraid it's not quite as simple as the image we've been using so far. I was actually quite surprised how organic and un-box-like the actual fitness landscape for this problem is. Remember, the x-axis rotation is mapped along the Gene A direction and the y-axis rotation along the Gene B direction. So every point on the AB plane represents a unique rotation composed of two angles. The elevation of this point is a direct mapping of the volume of the bounding-box at those two rotation angles:

首先,看一下上图那个平面景观的最小的包含体,实际上远没有那么简单。如果是有机形态或者是非长方体那复杂程度真的吓我一跳。记住,基因A代表X轴旋转角度,基因B代表Y轴旋转角度,,所以以AB为平面的每一个点都代表了相应的XY轴旋转的角度,而那些提高的点(z轴坐标)意味着xy旋转轴所对应的最小的包含体的体积。

The first thing to notice is that the landscape is periodic. I.e., it repeats itself every 90 degrees in both directions. Also, this landscape is in fact inverted as we're looking for a minimum volume, not a maximum one. Thus, the orange peaks in fact represent the worst solutions to this problem. Note that there are 16 of these peaks in the entire range and that they are rounded. When we look at the bottom of this fitness landscape, we get a rather different view:

首先我们观察到的是由那些数据绘制出来的图是这样的周期曲面,说明每90度他们就会重复一次,这张图展示的和我们预想的正好相反(因为最顶端代表体积越大),你看图中橘黄色的就代表最差的结果,而且在我们搜寻的范围内,他们能够出现16次,当我们看看这个图的底部的时候,我们会发现一个有趣的现象:

It would appear that the lowest points in this landscape (the minimum bounding-boxes) are both fewer in number and of a different kind. We only get 8 optimal solutions and they are all very sharp, indicating a somewhat more fragile state.

图中展示的最低点(也就是我们要找的最小盒子)在数量上和差异性上都要少一些,我们只得到8个最优解,而且他们所处的地方都很陡峭,说明他们所处的状态很脆弱(也就是他们的相似值范围很小,我们搜寻是很容易错过)。

Still, on the whole we have nothing to complain about. All the solutions are of equal quality and there are no local optima at all. We can generalize this landscape to a

2-dimensional graph:

直到现在,我们对这些结果也没什么抱怨的,所有的解品质都一样而且也没有局部最优的现象,我们可以把那张图做成这样的二维图表:

No matter where you end up as an ancestral genome, your blood-line will always find its way to a minimum bounding box. There's nowhere for it to get 'stuck'. So it's really just a question about who gets there first. If we look at a slightly more complex fitness graph, it becomes apparent that this need not be the case:

无论你在什么时候暂停,并把当前状态作为最初的父代,根据原来的血缘我们还是能够找到那个最小的盒子,而且运行时不会被卡主。唯一的问题就是谁更快的到达那个地方。但是如果我们做个稍微复杂一点的图形,你就可以很明显的看出来问题所在。

This fitness landscape has two kinds of solutions. The high quality sharp ones near the bottom of the graph and the low quality flat ones near the top. The basin of attraction is given for both solutions (yellow for high quality, pink for low quality) and you can see that about half of the model space is attracted to the low quality solutions.

根据上面这张图这时候有两类解,高质量的解在这张图表的最下面锋利的地方,二低质量的解在靠近顶部的地方,开始的那些父代给了我们两种解(黄色代表高质量解,粉色代表低质量解)你可以看出将近有一半的父代导致我们求出了低质量的解。

An even worse example (flipped upright again this time, so high values indicate good solutions) would be the following fitness landscape:

现在我们把刚才那个最优和次优的图标简化一下翻转过来,现在高的代表高质量解,低的代表低质量解:

The basins for these peaks are very small indeed and therefore easy to miss by a random sampling of the landscape. As soon as a lucky genome finds the peak on the left, its offspring will rapidly populate the low peak causing the rest of the population to go extinct. It is now even less likely that the better peak on the right will be found. The smaller the basins for solution, the harder it is to solve a problem with an evolutionary algorithm.

如果点池很少那么很可能错过一些最优解。一旦一个基因幸运的找到左侧的那个山峰,他就会跑过去并进行大量的繁衍,而下面的就会渐渐消失,这样右侧那个更高的山峰可能就找不到了,所以说刚开始的点池越少(父代越少),进化算法越难算出来你想要的结果。

Another example of a cumbersome problem to solve would be a discontinuous fitness landscape:

另一类更难处理的就是这种不连续适应性的问题:

Even though there are strictly speaking no local optima, there is also no 'improvement' on the plateaus. A genome which finds itself in the middle of one of these horizontal patches doesn't know where to go. If it takes a step to the left, nothing changes. If it takes a step to the right, nothing changes. There's no 'pressure' in this fitness landscape, so all the genomes will wander about aimlessly, until one of them has the good fortune to suddenly step onto a higher plateau. At this point it will quickly dominate the

gene-pool and the wandering starts again until the next plateau is accidentally found.

尽管在严格意义上说这里不存在什么局部最优解,但是他们在不同的层级之间却没有提高,当一个基因发现自己在一个水平的平面时,他就不知道去哪儿了,往左一步没什么变化,往右一步也没什么变化,因为在这个系统里没有压力,所以每一个基因也是这样漫无目的的游牧,直到有一个幸运的基因突然找到了那个最高的平面,这时候这个店会迅速占领整个基因池,并开始漫无目的的游牧直到下一个新大陆被发现。

Even worse than this though is a landscape that has a high degree of noise or chaos. A landscape may be continuous and yet feature so much detail that it becomes impossible to make any intelligible pronunciations regarding the fitness of a local patch:

更糟糕的情况就是下图这种类似多级噪音或混沌状态的图。这个图虽然是连续的,但是大量的细节让他们很难的展示出来甚至是局部最优:

In a landscape like this, mommy and daddy may both be very similar and both be very fit, but when they mate the offspring might end up in one of the fissures. A landscape like this defies navigation.

在这样的图表中,父代和母代都非常相似适应性也都非常高,然而当他们孕育出下一代的时候,那一代很可能在另一个图中的裂缝里,像这样的图表,你很难给他做一个方向性的引导。

Selection Mechanisms

选择机制

Biological Evolution proceeds by Natural Selection. The ruthless force identified by Darwin as the arbiter of progress. Put simply, Natural Selection affects the direction of the gene-pool over time by regulating who gets to mate. In extreme cases mating is prevented because a specific genome is so unfit that the bearer cannot survive until reproductive age. Another rather extreme case would be sterility. However, there's a

myriad ways in which Natural Selection can make it difficult or impossible for certain individuals to pass on their genetic footprint.

生物进化的进程取决于自然选择,这种无情的定义源于达尔文。简单地讲,自然选择通过调整交配来影响基因池发展的方向。在一些极端的例子里,那些适应性差的基因很难活到繁殖期。在另一些极端情况下甚至是不孕不育。然而,并非只有上述的哪两种,自然选择有无数的方法来让那些适应性差的物种很难,基本不可能来传递他们的精子或卵子。

However, Natural Selection isn't the only game in town. For a long time now humans have been using Artificial Selection in order to breed specific characteristics into a (sub)species. When we try to solve problems using an Evolutionary Solver, we always use some form of artificial selection. There's no such thing as sex or gender in the computer. The process of selection is also much simpler than in nature, as there is basically only one question that needs to be answered: Who gets to mate?

其实自然选择并不是自然界中唯一的游戏规则,其实人类很早就开始利用人工选择来培育亚物种成为我们想要的。当我们用进化算法来解决问题的时候,其实我们也是利用了人工选择,因为电脑里可没有性或者是性器官一类的东西,所以这种进程要比在自然界简单得多,所以我们唯一要关心的基本的问题就是:我们给谁权利去交配?

Allow me to enumerate the mechanisms for parent selection that are available in Galapagos. This is only a small subset of the selection algorithms that are possible, but they seem to cover the basics rather well.

下面我介绍一下galapagos里面应用的选择机制,其实他只是选择算法里的一小部分,不过他基本上涵盖了大部分应用的机制。

First off, we have Isotropic Selection, which is the simplest kind of algorithm you can imagine. In fact, it is the absence of a selection algorithm. In Isotropic Selection everyone gets to mate:

首先,我们有等方性选择机制,这个算法很好理解,事实上,他在选择算法里是不存在的,在等方性选择机制里,每个个体都有权利去交配:

No matter where you find yourself on this fitness graph, your chances of ending up in a mating couple are constant. You might think that this is a particularly pointless selection strategy as it does nothing to further the evolution of the gene-pool. But it is not without precedent in nature. Take for example wind-pollination or coral spawning. If you're a sexually functioning member of such a species, you get to play ball come mating season. Another example would be females in a walrus colony. Every female in a colony gets to breed with the dominant male, no matter how fit or unfit she is. Isotropic Selection is

certainly not without function either. For one, it dampens the speed with which a population runs uphill. It therefore acts as a safe-guard against a premature colonization of a local -and possibly inferior- optimum.

无论你在这个图标里找什么位置,这种交配的机会都是均等的,你可能会向这种无意义的选择策略对基因池根本没什么影响。但是,其实自然界并不是没有这种先例,例如风媒传播和珊瑚繁殖。如果你是这样的一类性功能物种,在交配的季节你就只能玩个球了。另一个例子就是海象殖民地的雌性,每一个雌性海象都会和占领这个地方的雄性海象一一发生性关系(听起来好happy),而不管这个雌海象是不是适应性很高的。等方性选择也并不是没有一定的作用。其中有一条就是他会抑制往上进化的速度,这种选择策略其实起到了一种安全保护的作用,他可以在一个不成熟的繁殖环境进行局部次级优化。

Another mechanism available in Galapagos is Exclusive Selection, where only the top N% of the population get to mate:

另一种galapagos里的选择机制是独有选择机制,也就是只有顶端的N%才有权利进行交配,

If you're lucky enough to be in the top N%, you'll likely have multiple offspring. A good analogy in nature for Exclusive Selection would be Walrus males. There's only a few harems to go around and far too many males to assign them all (a harem of one female after all is not really a harem). The flunkies get to sit on the side-line without a single chance to father a walrus baby, doing whatever it is walruses do when they can't get any action.

如果你很幸运的就是那N%,那你会有很多的后代,一个好的独有选择机制就是海象里面的雄海象,很少的妻妾群要被很多雄海象分配(一个妻妾群里只有一个雌海象肯定不能算是一个妻妾群),那些除了雄海象王剩下的随从只能在在一边,根本没有交配的机会,除了交配他们倒是可以做一些自己想做的事。

Another common pattern in nature is Biased Selection, where the chance of mating increases as the fitness increases. This is something we typically see with species that form stable couples. Everyone is basically capable of finding a mate, but the really attractive individuals manage to get a lot of hanky-panky on the side, thus increasing their chances of becomes genetic founders for future generations. Biased Selection can be amplified by using power functions, which have the effect of flattening or exaggerating the curve.

另一类常用的类型就是偏向选择,交配的频率和适应程度是相一致的,这个图形我们在形态稳定的物种中经常见到,没一个个体都有能力去找一个伴侣,但是那些更有吸引力的个体会使用各种花招来增

加他们成为更多后代父亲的机会,偏向选择可以通过外力来扩大或者缩小幅度,在图里就意味着更陡峭的曲线或者更扁平的曲线。

Coupling Algorithms

耦合算法

Coupling is the process of finding mates. Once a genome has been elected to mate by the active Selection Algorithm, it has to pick a mate from the population to complete the act. There are of course many ways in which mate selection could occur, but Galapagos at the moment only allows one; selection by genomic distance. In order to explain this in detail, I should first tell you how a Genome Map works. This

耦合就是寻找配对对象的过程,一旦有基因通过选择算法被选择出来并赋予交配的权利,那么他就要在他这一代中找一个对象来完成耦合。在其他场合中虽然有很多的配对耦合的方法,但是在galapagos里只有一种方法,通过一进组的距离来选择,为了解释这个细节,我会先讲一下下面这个基因图是怎么工作的:

is a Genome Map. It displays all the genomes (individuals) in a certain population as dots on a grid. The distance between two genomes on the grid is roughly analogous with the distance between the genomes in gene-space. I say roughly because it is in fact impossible to draw a map with exact distances. A single genome is defined by a number of genes. We assume that all the genomes in a species have the same number of genes (this is not technically a limitation of Evolutionary Algorithms, even though it is currently a limitation of Galapagos). Therefore the distance between two genomes is an

N-Dimensional value, where N equals the number of genes. It is not possible to accurately display an N-Dimensional point cloud on a 2-Dimensional screen so the Genome Map is only a coarse approximation. It also follows that the axes of this graph have no meaning whatsoever, the only information a Genome Map conveys is which genomes are more or less similar (close together) and which genomes are more or less different (far apart).

这是一个基因地图,每一代的个体在这个网格中就是一个点。在基因地图上两个点之间的距离和真实的基因空间里两个基因的距离对比起来图中的明显很粗糙,我之所以说它粗糙是引文事实上不可能在这个网格上画出准确的距离。一个基因组是有许多基因组成的。我们假设所有的基因组都有相同的基因数(其实在进化算法里没这样的限制,只是在galapagos里才有这样的限制),因此,存在于两个基因组之间的距离就是N维域,这个N等同于组成基因组的基因数量,所以不可能在一个2维的平面上面来准确的展示N维空间的点云,所以基因地图只是一个很粗糙的接近值。所以随着轴的推移所对应的数据没有什么意义,基因地图唯一能够传达给我们的信息就是哪些基因组离得近一些差异小一些,哪些基因组离得远一些差异大一些。

Imagine you are an individual that has been selected for mating (yay). The population is well distributed and you are somewhere near the average (I'm sure you are a wildly original and delightful person in real life, but for the time being try to imagine you are in fact sort of average):

现在把你想象成被选中有交配权利的个体(我知道你在暗爽),这一代分布的很均匀,而你也恰好在平均值附近(我知道你在现实生活中非常有才而且受人喜爱,但是这个时候你就委屈一下把自己当成一个在平均水平的普通人):

使用遗传算法求解函数最大值

使用遗传算法求解函数最大值 题目 使用遗传算法求解函数 在及y的最大值。 解答 算法 使用遗传算法进行求解,篇末所附源代码中带有算法的详细注释。算法中涉及不同的参数,参数的取值需要根据实际情况进行设定,下面运行时将给出不同参数的结果对比。 定义整体算法的结束条件为,当种群进化次数达到maxGeneration时停止,此时种群中的最优解即作为算法的最终输出。 设种群规模为N,首先是随机产生N个个体,实验中定义了类型Chromosome表示一个个体,并且在默认构造函数中即进行了随机的操作。 然后程序进行若干次的迭代,在每次迭代过程中,进行选择、交叉及变异三个操作。 一选择操作 首先计算当前每个个体的适应度函数值,这里的适应度函数即为所要求的优化函数,然后归一化求得每个个体选中的概率,然后用轮盘赌的方法以允许重复的方式选择选择N个个体,即为选择之后的群体。

但实验时发现结果不好,经过仔细研究之后发现,这里在x、y取某些值的时候,目标函数计算出来的适应值可能会出现负值,这时如果按照把每个个体的适应值除以适应值的总和的进行归一化的话会出现问题,因为个体可能出现负值,总和也可能出现负值,如果归一化的时候除以了一个负值,选择时就会选择一些不良的个体,对实验结果造成影响。对于这个问题,我把适应度函数定为目标函数的函数值加一个正数,保证得到的适应值为正数,然后再进行一般的归一化和选择的操作。实验结果表明,之前的实验结果很不稳定,修正后的结果比较稳定,趋于最大值。 二交叉操作 首先是根据交叉概率probCross选择要交叉的个体进行交叉。

这里根据交叉参数crossnum进行多点交叉,首先随机生成交叉点位置,允许交叉点重合,两个重合的交叉点效果互相抵消,相当于没有交叉点,然后根据交叉点进行交叉操作,得到新的个体。 三变异操作 首先是根据变异概率probMutation选择要变异的个体。 变异时先随机生成变异的位置,然后把改位的01值翻转。

用遗传算法求解Rosenbrock函数最优解实验报告

姓名学号 实验 成绩 华中师范大学计算机科学系 实验报告书 实验题目:用遗传算法求解Rosenbrock函数的最大值问题课程名称:智能计算 主讲教师:沈显君 辅导教师: 课程编号: 班级:2011级 实验时间:2011.11

用遗传算法求解Rosenbrock函数最大值问题 摘要: 本文利用遗传算法研究了求解Rosenbrock函数的最大值问题.在较多的计算机模拟实验结果中表明,用遗传算法可以有效地解决这一问题.文中分析了一种基于遗传算法对Rosenbrock函数最大值问题的求解,得到了适于解决此问题的合理的遗传操作,从而为有效地解决最速下降法所不能实现的某一类函数代化问题提供了一种新的途径.通过对基于遗传算法对Rosenbrock函数最大值问题的求解,进一步理解遗传算法对解决此类问题的思想。 关键词:遗传算法,Rosenbrock函数,函数优化,最速下降法。 Abstract: This paper deals with the maximum of Rosenbrock s function based ongenetic algorithms. The simulated results show that the problem can be solved effectivelyusing genetic algorithms. The influence of some rnodified genetic algorithms on searchspeed is also examined. Some genetic operations suitable to the optimization technique areobtained, therefore, a novel way of solving a class of optimizations of functions that cannot be realized using the method of steepest descent is proposed.Through dealing with the maximum of Rosenbrock s function based ongenetic algorithms,a better understanding of the genetic algorithm to solve such problems thinking. Keyword:ongenetic algorithms,Rosenbrock function,function optimization,Steepest descent method

MATLAB实验报告-遗传算法解最短路径以及函数最小值问题

硕士生考查课程考试试卷 考试科目:MATLAB教程 考生姓名:考生学号: 学院:专业: 考生成绩: 任课老师(签名) 考试日期:20 年月日午时至时

《MATLAB教程》试题: A、利用MATLAB设计遗传算法程序,寻找下图11个端点的最短路径,其中没有连接的端点表示没有路径。要求设计遗传算法对该问题求解。 a c d e f h i k 1 2 1 6 8 3 1 7 9 4 6 7 2 9 4 2 1 1 B、设计遗传算法求解f(x)极小值,具体表达式如下: 要求必须使用m函数方式设计程序。 C、利用MATLAB编程实现:三名商人各带一个随从乘船渡河,一只小船只能容纳二人,由他们自己划行,随从们密约,在河的任一岸,一旦随从的人数比商人多,就杀人越货,但是如何乘船渡河的大权掌握在商人手中,商人们怎样才能安全渡河? D、结合自己的研究方向选择合适的问题,利用MATLAB进行实验。 以上四题任选一题进行实验,并写出实验报告。

选择题目: A 一、问题分析(10分) 1 2 3 4 5 6 8 9 10 11 1 2 1 6 8 3 1 7 9 4 6 7 2 9 4 2 1 1 如图如示,将节点编号,依次为 1.2.3.4.5.6.7.8.9.10.11,由图论知识,则可写出其带权邻接矩阵为: 0 2 8 1 500 500 500 500 500 500 500 2 0 6 500 1 500 500 500 500 500 500 8 6 0 7 500 1 500 500 500 500 500 1 500 7 0 500 500 9 500 500 500 500 500 1 500 500 0 3 500 2 500 500 500 500 500 1 500 3 0 4 500 6 500 500 500 500 500 9 500 4 0 500 500 1 500 500 500 500 500 2 500 500 0 7 500 9 500 500 500 500 500 6 500 7 0 1 2 500 500 500 500 500 500 1 500 1 0 4 500 500 500 500 500 500 500 9 2 4 0 注:为避免计算时无穷大数吃掉小数,此处为令inf=500。 问题要求求出任意两点间的最短路径,Floyd算法采用的是在两点间尝试插入顶点,比较距离长短的方法。我思考后认为,用遗传算法很难找到一个可以统一表示最短路径的函数,但是可以对每一对点分别计算,然后加入for循环,可将相互之间的所有情况解出。观察本题可发现,所有节点都是可双向行走,则可只计算i到j的路径与距离,然后将矩阵按主对角线翻折即可得到全部数据。二、实验原理与数学模型(20分) 实现原理为遗传算法原理: 按所选择的适应度函数并通过遗传中的复制、交叉及变异对个体进行筛选,使得适应度高的个体被保留下来,组成新的群体,新的群体既继承了上一代的信息,又优于上一代。这样周而复始,群体中个体适应度不断提高,直到满足一定的条件。 数学模型如下: 设图由非空点集合和边集合组成,其中 又设的值为,故可表示为一个三元组 则求最短路径的数学模型可以描述为:

遗传算法用于函数优化

遗传算法用于函数优化求解 一、实验目的 本实验要求在掌握遗传算法的基本思想、原理和算法流程的基础上,能够针对指定的单变量优化目标函数,设计相应的遗传算法优化程序,并求得全局最优解。 二、实验要求 针对目标函数 2 1(1),[0,2]y x x =--∈,设计利用遗传算法进行优化求解的程序,绘制迭代过程中最优解的变化情况,并分别改变算法中的编码位数、种群规模、交叉和变异概率,分析这些变量对算法精度及收敛性的影响。 三、实验步骤 1、初始化种群,确定种群规模M=20,编码位数n=5 和编码机制(二进制编码); 初始化种群:E = round(rand(M,n)); 每个编码对应的二进制数值: (1) 2i i i y y -=?∑ i y 为第i 位二进制代码; 二进制数y 转换为十进制数x : max min min *21n x x x y x -= +-; 2、根据给定的目标函数,计算各个种群的适应度值; 3、采用轮盘选择法对种群进行选择复制; 4、设定交叉概率为0.9,进行遗传操作(交叉); 5、设定变异概率0.05,进行遗传操作(变异); 6、产生下一代种群,与终止条件比较,不满足返回到步骤2直到满足条件退出。 算法的流程如图7.1所示。

N Y 结束 输出结果 迭代次数达上限? 开始 初始化种群(编码) 计算适应度函数 交叉、变异 选择、复制 达到系统指标? 图7.1 算法流程图 四、实验结果及分析 我们采用遗传算法来寻求目标函数的最大值。初始化样本个数为20个,编码位数为5位,采用二进制编码,交叉概率为0.9,变异概率为0.05,最大迭代次数为1000次,初始样本随机选择,当父代与子代间适应度变化小于0.001时,达到系统指标。MATLAB 模拟运行输出迭代种群的平均适应度变化、种群的最优解与最差解,绘出图像(见图1),计算运行时间的平均值(见表1),由表可知,平均运行时间约为0.65秒左右,速度较快。由图可知,前期平均适应度是不断上升的,到达一定程度后即平均适应度在0.9以上后,就基本处于波动平衡状态。

MATLAB实验遗传算法和优化设计

实验六 遗传算法与优化设计 一、实验目的 1. 了解遗传算法的基本原理和基本操作(选择、交叉、变异); 2. 学习使用Matlab 中的遗传算法工具箱(gatool)来解决优化设计问题; 二、实验原理及遗传算法工具箱介绍 1. 一个优化设计例子 图1所示是用于传输微波信号的微带线(电极)的横截面结构示意图,上下两根黑条分别代表上电极和下电极,一般下电极接地,上电极接输入信号,电极之间是介质(如空气,陶瓷等)。微带电极的结构参数如图所示,W 、t 分别是上电极的宽度和厚度,D 是上下电极间距。当微波信号在微带线中传输时,由于趋肤效应,微带线中的电流集中在电极的表面,会产生较大的欧姆损耗。根据微带传输线理论,高频工作状态下(假定信号频率1GHz ),电极的欧姆损耗可以写成(简单起见,不考虑电极厚度造成电极宽度的增加): 图1 微带线横截面结构以及场分布示意图 {} 28.6821ln 5020.942ln 20.942S W R W D D D t D W D D W W t D W W D e D D παπππ=+++-+++?????? ? ??? ??????????? ??????? (1) 其中πρμ0=S R 为金属的表面电阻率, ρ为电阻率。可见电极的结构参数影响着电极损耗,通过合理设计这些参数可以使电极的欧姆损耗做到最小,这就是所谓的最优化问题或者称为规划设计问题。此处设计变量有3个:W 、D 、t ,它们组成决策向量[W, D ,t ] T ,待优化函数(,,)W D t α称为目标函数。 上述优化设计问题可以抽象为数学描述: ()()min .. 0,1,2,...,j f X s t g X j p ????≤=? (2)

遗传算法求解函数最大值

人工智能 遗传算法函数优化

目录 1引言 (3) 1.1 摘要 (3) 1.2 背景 (3) 2 实验过程 (4) 2.1 程序目标 (4) 2.2 实验原理及步骤 (4) 2.3程序 (5) 2.3.1程序理解: (5) 2.3.3调试程序: (5) 2.4 实验总结 (18)

1引言 1.1 摘要 函数优化是遗传算法的经典应用领域,也是对遗传算法进行性能评价的常用算例。本文将用一个详细的例子来说明用遗传算法解一个简单参数优化问题的过程。这里求解的是一个函数的最大值的问题。 1.2 背景 遗传算法采纳自然进化模型。通过保持一个潜在解的群体执行了多方向的搜索并支持这些方向上的信息构成和交换。群体经过一个模拟进化的过程:在每一代,相对“好”的解产生,相对“差”的解死亡。为区别不同解,我们使用了一个目标(评价)函数,它起着一个环境的作用。 选择是用来确定管理费用或交叉个体,以及被选个体将产生多少个代个体。 杂交组合了两个亲代染色体的特征,并通过交换父代相应的片断形成了两个相似的后代。杂交算子的意图是在不同潜在解之间进行信息交换。 变异是通过用一个等于变异率的概率随机地改变被选择染色体上的一个或多个基因。变异算子的意图是向群体引入一些额外的变化性。 运用遗传算法解题必经的五个步骤: 1.对问题潜在解的遗传表达。 2.产生潜在解初始群体的方法。 3.起环境作用的用“适应值”评价解的适应程度的评价函数。 4.改变后代组成的各种遗传算子。 5.遗传算法所使用的各种参数:群体规模、应用遗传算子的概率等。

2 实验过程 2.1 程序目标 在实验过程中,我们应用遗传算法来模拟一个函数优化的问题。程序所要解决的问题是求f(x1,x2)=21.5+x1*sin(4pi*x1)+x2*sin(20pi*x2)的最大值,其中-3.0≤x1≤12.1及4.1≤x2≤5.8。 2.2 实验原理及步骤 1 )首先确立变量x1的定义域长度为15.1;所要求的小数点以后第四位精度意味着区间[-3.0, 12.1]应该至少被分成15.1*10000个等距区间,即染色体的第一部分需要18位;自变量x2域长度为 1.7,精度要求区间[4.1, 5.8]应该至少被分成1.7*10000个等距区间,即染色体的第二部分需要15位。所以染色体总长度为33位。用遗传算法的优化函数f,产生了一个有pop_size = 20个染色体的群体。所有染色体的33位都是随机初始化。对每个染色体进行解码并计算解码后的(x1,x2)的适应函数值,eval(vi) (i=1,..,pop_size) = f(x1,x2)。 2)为选择过程建立一个轮盘。计算群体的总适应值F,并计算每个染色体vi (i=1,..,pop_size)的选择概率pi:pi = eval(vi) / F 和累积概率qi: qi = p1 + .. + pi. 3)转动轮盘20次,每次为新群体选择一单个染色体。生成一个区间[0,1]里的20个数的一个随机序列。如果一个随机数位于qi于q(i+1)之间,则q(i+1)被选择。4)对新群体中的个体应用杂交算子。杂交概率pc = 0.25,所以预计染色体中平均有25%将经历杂交。杂交按照下面的方法进行:对新群体中的每个染色体,产生在区间[0,1]里的随机数r,并从随机序列中选出r<0.25的染色体进行杂交。 5)对被选择的染色体随机进行配对。并从区间[1,32]里产生一个随机整数pos。数字pos表示杂交点的位置。 6)算子变异。在一位一位基础上执行。变异概率pm = 0.01,所以我们预计平均将有1%的位经历变异。整个群体共有m*pop_size = 660位,可以预计平均每代有6.6次变异。因为每一位都有均等的机会被变异,所以对群体中的每一位可以产生区间

4遗传算法与函数优化

第四章遗传算法与函数优化 4.1 研究函数优化的必要性: 首先,对很多实际问题进行数学建模后,可将其抽象为一个数值函数的优化问题。由于问题种类的繁多,影响因素的复杂,这些数学函数会呈现出不同的数学特征。除了在函数是连续、可求导、低阶的简单情况下可解析地求出其最优解外,大部分情况下需要通过数值计算的方法来进行近似优化计算。 其次,如何评价一个遗传算法的性能优劣程度一直是一个比较难的问题。这主要是因为现实问题种类繁多,影响因素复杂,若对各种情况都加以考虑进行试算,其计算工作量势必太大。由于纯数值函数优化问题不包含有某一具体应用领域中的专门知识,它们便于不同应用领域中的研究人员能够进行相互理解和相互交流,并且能够较好地反映算法本身所具有的本质特征和实际应用能力。所以人们专门设计了一些具有复杂数学特征的纯数学函数,通过遗传算法对这些函数的优化计算情况来测试各种遗传算法的性能。 4.2 评价遗传算法性能的常用测试函数 在设计用于评价遗传算法性能的测试函数时,必须考虑实际应用问题的数学模型中所可能呈现出的各种数学特性,以及可能遇到的各种情况和影响因素。这里所说的数学特性主要包括: ●连续函数或离散函数; ●凹函数或凸函数; ●二次函数或非二次函数; ●低维函数或高维函数; ●确定性函数或随机性函数; ●单峰值函数或多峰值函数,等等。 下面是一些在评价遗传算法性能时经常用到的测试函数: (1)De Jong函数F1: 这是一个简单的平方和函数,只有一个极小点f1(0, 0, 0)=0。

(2)De Jong 函数F2: 这是一个二维函数,它具有一个全局极小点f 2(1,1) = 0。该函数虽然是单峰值的函数,但它却是病态的,难以进行全局极小化。 (3)De Jong 函数F3: 这是一个不连续函数,对于]0.5,12.5[--∈i x 区域内的每一个点,它都取全局极小值 30),,,,(543213-=x x x x x f 。

遗传算法与优化问题(重要,有代码)

实验十遗传算法与优化问题 一、问题背景与实验目的 遗传算法(Genetic Algorithm—GA),是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,它是由美国Michigan大学的J.Holland教授于1975年首先提出的.遗传算法作为一种新的全局优化搜索算法,以其简单通用、鲁棒性强、适于并行处理及应用范围广等显著特点,奠定了它作为21世纪关键智能计算之一的地位. 本实验将首先介绍一下遗传算法的基本理论,然后用其解决几个简单的函数最值问题,使读者能够学会利用遗传算法进行初步的优化计算.1.遗传算法的基本原理 遗传算法的基本思想正是基于模仿生物界遗传学的遗传过程.它把问题的参数用基因代表,把问题的解用染色体代表(在计算机里用二进制码表示),从而得到一个由具有不同染色体的个体组成的群体.这个群体在问题特定的环境里生存竞争,适者有最好的机会生存和产生后代.后代随机化地继承了父代的最好特征,并也在生存环境的控制支配下继续这一过程.群体的染色体都将逐渐适应环境,不断进化,最后收敛到一族最适应环境的类似个体,即得到问题最优的解.值得注意的一点是,现在的遗传算法是受生物进化论学说的启发提出的,这种学说对我们用计算机解决复杂问题很有用,而它本身是否完全正确并不重要(目前生物界对此学说尚有争议). (1)遗传算法中的生物遗传学概念 由于遗传算法是由进化论和遗传学机理而产生的直接搜索优化方法;故而在这个算法中要用到各种进化和遗传学的概念. 首先给出遗传学概念、遗传算法概念和相应的数学概念三者之间的对应关系.这些概念如下: 序号遗传学概念遗传算法概念数学概念 1 个体要处理的基本对象、结构也就是可行解 2 群体个体的集合被选定的一组可行解 3 染色体个体的表现形式可行解的编码 4 基因染色体中的元素编码中的元素 5 基因位某一基因在染色体中的位置元素在编码中的位置 6 适应值个体对于环境的适应程度, 或在环境压力下的生存能力可行解所对应的适应函数值 7 种群被选定的一组染色体或个体根据入选概率定出的一组 可行解 8 选择从群体中选择优胜的个体, 淘汰劣质个体的操作保留或复制适应值大的可行解,去掉小的可行解 9 交叉一组染色体上对应基因段的 交换根据交叉原则产生的一组新解 10 交叉概率染色体对应基因段交换的概 率(可能性大小)闭区间[0,1]上的一个值,一般为0.65~0.90 11 变异染色体水平上基因变化编码的某些元素被改变

遗 传 算 法 详 解 ( 含 M A T L A B 代 码 )

GATBX遗传算法工具箱函数及实例讲解 基本原理: 遗传算法是一种典型的启发式算法,属于非数值算法范畴。它是模拟达尔文的自然选择学说和自然界的生物进化过程的一种计算模型。它是采用简单的编码技术来表示各种复杂的结构,并通过对一组编码表示进行简单的遗传操作和优胜劣汰的自然选择来指导学习和确定搜索的方向。遗传算法的操作对象是一群二进制串(称为染色体、个体),即种群,每一个染色体都对应问题的一个解。从初始种群出发,采用基于适应度函数的选择策略在当前种群中选择个体,使用杂交和变异来产生下一代种群。如此模仿生命的进化进行不断演化,直到满足期望的终止条件。 运算流程: Step 1:对遗传算法的运行参数进行赋值。参数包括种群规模、变量个数、交叉概率、变异概 率以及遗传运算的终止进化代数。 Step 2:建立区域描述器。根据轨道交通与常规公交运营协调模型的求解变量的约束条件,设置变量的取值范围。 Step 3:在Step 2的变量取值范围内,随机产生初始群体,代入适应度函数计算其适应度值。 Step 4:执行比例选择算子进行选择操作。 Step 5:按交叉概率对交叉算子执行交叉操作。

Step 6:按变异概率执行离散变异操作。 Step 7:计算Step 6得到局部最优解中每个个体的适应值,并执行最优个体保存策略。 Step 8:判断是否满足遗传运算的终止进化代数,不满足则返回Step 4,满足则输出运算结果。 运用遗传算法工具箱: 运用基于Matlab的遗传算法工具箱非常方便,遗传算法工具箱里包括了我们需要的各种函数库。目前,基于Matlab的遗传算法工具箱也很多,比较流行的有英国设菲尔德大学开发的遗传算法工具箱GATBX、GAOT以及Math Works公司推出的GADS。实际上,GADS就是大家所看到的Matlab中自带的工具箱。我在网上看到有问为什么遗传算法函数不能调用的问题,其实,主要就是因为用的工具箱不同。因为,有些人用的是GATBX带有的函数,但MATLAB自带的遗传算法工具箱是GADS,GADS当然没有GATBX里的函数,因此运行程序时会报错,当你用MATLAB来编写遗传算法代码时,要根据你所安装的工具箱来编写代码。 以GATBX为例,运用GATBX时,要将GATBX解压到Matlab下的toolbox文件夹里,同时,set path将GATBX文件夹加入到路径当中。 这块内容主要包括两方面工作:1、将模型用程序写出来(.M文件),即目标函数,若目标函数非负,即可直接将目标函数作为适应度函数。2、设置遗传算法的运行参数。包括:种群规模、变量个数、区域描述器、交叉概率、变异概率以及遗传运算的终止进化代数等等。

遗传算法求解函数极值C语言代码

#include "stdio.h" #include "stdlib.h" #include "conio.h" #include "math.h" #include "time.h" #define num_C 12 //个体的个数,前6位表示x1,后6位表示x2 #define N 100 //群体规模为100 #define pc 0.9 //交叉概率为0.9 #define pm 0.1 //变异概率为10% #define ps 0.6 //进行选择时保留的比例 #define genmax 2000 //最大代数200 int RandomInteger(int low,int high); void Initial_gen(struct unit group[N]); void Sort(struct unit group[N]); void Copy_unit(struct unit *p1,struct unit *p2); void Cross(struct unit *p3,struct unit *p4); void Varation(struct unit group[N],int i); void Evolution(struct unit group[N]); float Calculate_cost(struct unit *p); void Print_optimum(struct unit group[N],int k); /* 定义个体信息*/ typedef struct unit { int path[num_C]; //每个个体的信息 double cost; //个体代价值 }; struct unit group[N]; //种群变量group int num_gen=0; //记录当前达到第几代 int main() { int i,j; srand((int)time(NULL)); //初始化随机数发生器 Initial_gen(group); //初始化种群 Evolution(group); //进化:选择、交叉、变异 getch(); return 0; } /* 初始化种群*/ void Initial_gen(struct unit group[N]) { int i,j; struct unit *p; for(i=0;i<=N-1;i++) //初始化种群里的100个个体 {

遗传算法求复杂函数极值问题【精品毕业设计】(完整版)

遗传算法求复杂函数极值问题 中文摘要: 本文首先介绍遗传算法的历史背景,基本思想,对遗传算法的常见的编码解码方法进行了深入的阐述,并对算子选择方法进行深入分析和对比,在此基础上把遗传算法应用于求解复杂函数的极值计算。最后在MATLAB语言环境下编写程序,对求解函数的最大值进行了仿真,并对调试的结果进行了分析,得出了部分结论。 关键词:遗传算法最优解算子选择复杂函数 作者:xx xx 指导老师:xxxx xx

Using Genetic Algorithm to Solve Extreme Problem of Complex Function Abstract Firstly,the historical background and basic idea of genetic algorithm are introduced in this paper. The common coding and decoding method of genetic algorithm are discussed too. Secondly, the selection method of genetic operator is analyzed and compared deeply, based on which genetic algorithm is used to solve extreme problem of complex function. Finally, with MA TLAB software, the program is compiled and the maximum is sought out. At the end of the paper, the debugging result is analyzed and the conclusion is given. Keywords: Genetic Algorithm Optimal Solution Operator Selection Complex Function Written by : xx xx Supervised by: xxxx xx

基于遗传算法的库位优化问题

Logistics Sci-Tech 2010.5 收稿日期:2010-02-07 作者简介:周兴建(1979-),男,湖北黄冈人,武汉科技学院经济管理学院,讲师,武汉理工大学交通学院博士研究生,研究方向:物流价值链、物流系统规划;刘元奇(1988-),男,甘肃天水人,武汉科技学院经济管理学院;李泉(1989-),男,湖北 武汉人,武汉科技学院经济管理学院。 文章编号:1002-3100(2010)05-0038-03 物流科技2010年第5期Logistics Sci-Tech No.5,2010 摘 要:应用遗传算法对邯运集团仓库库位进行优化。在充分考虑邯运集团仓库所存放的货物种类、货物数量、出入库频 率等因素的基础上进行库位预分区规划,建立了二次指派问题的数学模型。利用遗传算法对其求解,结合MATLAB 进行编程计算并得出最优划分方案。 关键词:遗传算法;预分区规划;库位优化中图分类号:F253.4 文献标识码:A Abstract:The paper optimize the storage position in warehouse of Hanyun Group based on genetic algorithm.With thinking of the factors such as goods categories,quantities and frequencies of I/O,etc,firstly,the storage district is planned.Then the model of quadratic assignment problems is build,and genetic algorithm is utilized to resolve the problem.The software MATLAB is used to program and figure out the best alternatives. Key words:genetic algorithm;district planning;storage position optimization 1 库位优化的提出 邯郸交通运输集团有限公司(简称“邯运集团”)是一家集多种业务为一体的大型综合性物流企业。邯运集团的主要业务板块有原料采购(天信运业及天昊、天诚、天恒等)、快递服务(飞马快运)、汽贸业务(天诚汽贸)及仓储配送(河北快运)等。其中,邯运集团的仓储配送业务由河北快运经营,现有仓库面积总共40000㎡,主要的业务范围为医药、日用百货、卷烟、陶瓷、化工产品的配送,其中以医药为主。邯运集团库存货物主要涉及两个方面:一个是大宗的供应商货物,如医药,化工产品等;另一方面主要是大规模的小件快递货物,如日用百货等[1]。经分析,邯运集团在仓储运作方面存在如下问题: (1)存储货物繁多而分拣速度低下。仓库每天到货近400箱,有近200多种规格,缺乏一套行之有效的仓储管理系统。(2)货架高度不当而货位分配混乱。现在采用的货架高度在2米以上,而且将整箱货物直接码垛在货架上,不严格按货位摆放。当需要往货架最上层码放货物需要借助梯子,增加操作难度且操作效率较低。货物在拣货区货架摆放是以件为单位的,分拣和搬运速度较慢。 (3)拣货货架设计不当而仓储效率低下。发货前装箱工作主要由人工协同完成,出库效率低,出错率难以控制。 (4)存储能力和分拣能力不能满足需求。根据邯运集团的业务发展现状及趋势,现有的仓库储存和分拣能力远远达不到集团公司对配送业务量的需求。 当前邯运集团的货位分配主要采用物理地址编码的方式,很少考虑货位分配对仓储管理员工作效率的影响。对其进行库位优化设计不仅直接影响到其库存量的大小、出入库的效率,还间接影响到邯运集团的整体经营效益。本文对邯运集团的仓库货位进行优化时,结合考虑仓库所存放的货物种类、货物数量、出入库频率等因素,对仓库货位进行规划,以提高仓储效率。 2库位预分区规划 在进行仓库货位规划时,作如下假设: (1)货物的存放种类已知; (2)货物每种类的单位时间内存放的数量己知; (3) 每一种货物的存取频率已知。 在仓库货位优化中一个重要的环节即预分区。所谓预分区,是指没有存放货物时的分区,分区时只考虑仓储作业人员的速基于遗传算法的库位优化问题 Optimization of Storage Position in Warehouse Based on Genetic Algorithm 周兴建1,2,刘元奇1,李泉1 ZHOU Xing-jian 1,2,LIU Yuan-qi 1,LI Quan 1 (1.武汉科技学院经济管理学院,湖北武汉430073;2.武汉理工大学交通学院,湖北武汉430063) (1.College of Economics &Management,Wuhan University of Science &Engineering,Wuhan 430073,China; 2.School of Transportation,Wuhan University of Technology,Wuhan 430063,China) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 38

(实例)matlab遗传算法工具箱函数及实例讲解

matlab遗传算法工具箱函数及实例讲解 核心函数: (1)function [pop]=initializega(num,bounds,eevalFN,eevalOps,options)--初始种群的生成函数 【输出参数】 pop--生成的初始种群 【输入参数】 num--种群中的个体数目 bounds--代表变量的上下界的矩阵 eevalFN--适应度函数 eevalOps--传递给适应度函数的参数 options--选择编码形式(浮点编码或是二进制编码)[precision F_or_B], 如 precision--变量进行二进制编码时指定的精度 F_or_B--为1时选择浮点编码,否则为二进制编码,由precision指定精度) (2)function [x,endPop,bPop,traceInfo] = ga(bounds,evalFN,evalOps,startPop,opts,... termFN,termOps,selectFN,selectOps,xOverFNs,xOverO ps,mutFNs,mutOps)--遗传算法函数 【输出参数】 x--求得的最优解 endPop--最终得到的种群 bPop--最优种群的一个搜索轨迹 【输入参数】 bounds--代表变量上下界的矩阵 evalFN--适应度函数 evalOps--传递给适应度函数的参数 startPop-初始种群 opts[epsilon prob_ops display]--opts(1:2)等同于initializega 的options参数,第三个参数控制是否输出,一般为0。如[1e-6 1 0] termFN--终止函数的名称,如['maxGenTerm'] termOps--传递个终止函数的参数,如[100] selectFN--选择函数的名称,如['normGeomSelect'] selectOps--传递个选择函数的参数,如[0.08] xOverFNs--交叉函数名称表,以空格分开,如['arithXover heuristicXover simpleXover'] xOverOps--传递给交叉函数的参数表,如[2 0;2 3;2 0] mutFNs--变异函数表,如['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation'] mutOps--传递给交叉函数的参数表,如[4 0 0;6 100 3;4 100 3;4 0 0]

(完整版)遗传算法求解函数最大值(matlab)

遗传算法求解函数F(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2; 的最大值(MATLAB) %Generic Algorithm for function f(x1,x2) optimum (最大值) clear all; close all; %Parameters Size=80; G=100; CodeL=10; umax=2.048; umin=-2.048; E=round(rand(Size,2*CodeL)); %Initial Code %Main Program for k=1:1:G time(k)=k; for s=1:1:Size m=E(s,:); y1=0;y2=0; %Uncoding m1=m(1:1:CodeL); for i=1:1:CodeL y1=y1+m1(i)*2^(i-1); end x1=(umax-umin)*y1/1023+umin; m2=m(CodeL+1:1:2*CodeL); for i=1:1:CodeL y2=y2+m2(i)*2^(i-1); end x2=(umax-umin)*y2/1023+umin; F(s)=100*(x1^2-x2)^2+(1-x1)^2; end Ji=1./(F+1);

%****** Step 1 : Evaluate BestJ ****** BestJ(k)=min(Ji); fi=F; %Fitness Function [Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger Bestfi=Oderfi(Size); %Let Bestfi=max(fi) BestS=E(Indexfi(Size),:); %Let BestS=E(m), m is the Indexfi belong to max(fi) bfi(k)=Bestfi; %****** Step 2 : Select and Reproduct Operation****** fi_sum=sum(fi); fi_Size=(Oderfi/fi_sum)*Size; fi_S=floor(fi_Size); %Selecting Bigger fi value (取整) kk=1; for i=1:1:Size for j=1:1:fi_S(i) %Select and Reproduce TempE(kk,:)=E(Indexfi(i),:); kk=kk+1; %kk is used to reproduce end end %************ Step 3 : Crossover Operation ************ pc=0.60; n=ceil(20*rand); for i=1:2:(Size-1) temp=rand; if pc>temp %Crossover Condition for j=n:1:20 TempE(i,j)=E(i+1,j); TempE(i+1,j)=E(i,j); end end end TempE(Size,:)=BestS; E=TempE; %************ Step 4: Mutation Operation ************** %pm=0.001; %pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi, smaller Pm %pm=0.0; %No mutation pm=0.1; %Big mutation

各种优化算法求解函数优化问题

各种优化算法求解函数优化问题 1.遗传算法的简单介绍及流程 1.1遗传算法的基本原理 遗传算法 ( Genetic Algorithm ,简称 GA) 是近年来迅速发展起来的一种全新的随机搜索优化算法。与传统搜索算法不同 ,遗传算法从一组随机产生的初始解 (称为群体 )开始搜索。群体中的每个个体是问题的一个解 ,称为染色体。这些染色体在后续迭代中不断进化 , 称为遗传。遗传算法主要通过交叉、变异、选择运算实现。交叉或变异运算生成下一代染色体,称为后 代。染色体的好坏用适应度来衡量。根据适应度的大小从上一代和后代中选择一定数量的个 体 ,作为下一代群体 ,再继续进化 ,这样经过若干代之后 ,算法收敛于最好的染色体 ,它很可能就是问题的最优解或次优解。遗传算法中使用适应度这个概念来度量群体中的各个个体在优化计算中有可能达到最优解的优良程度。度量个体适应度的函数称为适应度函数。适应度函数的定义一般与具体求解问题有关。 1.2遗传算法的流程 第一步:确定决策变量及各种约束条件,即确定出个体的表现型X和问题的解空间; 第二步:确定出目标函数的类型,即求目标函数的最大值还是最小值,以及其数学描述形式或量化方法,建立其优化模型; 第三步:确定表示可行解的染色体编码方法,即确定出个体的基因型X和遗传算法的搜 索空间。 第四步:确定解码方法,即确定出个体的基因型 X和个体的表现型 X的对应关系或转换方法; 第五步:确定个体时候适应度的量化评价方法,即确定出由目标函数 f(X) 值到个体适应度F(X) 的转换规则; 第六步:设计遗传算子,即确定出选择运算、交叉运算、变异运算等遗传算子的具体操作方法; 第七步:确定出遗传算法的运行参数,即确定出遗传算法的M、 T、 Pc、 Pm等参数。1.3 遗传算法求解函数优化问题中的参数分析 目前,函数优化是遗传算法的经典应用领域,也是对遗传算法进行性能评价的常用范 例。对于函数优化中求解实数型变量的问题,一般采用动态编码和实数编码的方法来提高其搜

遗传算法代码

%求下列函数的最大值% %f(x)=10*sin(5x)+7*cos(4x)x∈[0,10]% %将x的值用一个10位的二值形式表示为二值问题,一个10位的二值数提供的分辨率是每为(10-0)/(2^10-1)≈0.01。% %将变量域[0,10]离散化为二值域[0,1023],x=0+10*b/1023,其中b是[0,1023]中的一个二值数。 %2.1初始化(编码) %initpop.m函数的功能是实现群体的初始化,popsize表示群体的大小,chromlength表示染色体的长度(二值数的长度), %长度大小取决于变量的二进制编码的长度(在本例中取10位)。 %遗传算法子程序 %Name:initpop.m %初始化 function pop=initpop(popsize,chromlength) pop=round(rand(popsize,chromlength));%rand随机产生每个单元为{0,1}行数为popsize,列数为chromlength的矩阵, %roud对矩阵的每个单元进行圆整。这样产生的初始种群。 %2.2计算目标函数值 %2.2.1将二进制数转化为十进制数(1) %遗传算法子程序 %Name:decodebinary.m %产生[2^n2^(n-1)...1]的行向量,然后求和,将二进制转化为十进制function pop2=decodebinary(pop) [px,py]=size(pop);%求pop行和列数 for i=1:py pop1(:,i)=2.^(py-i).*pop(:,i); end pop2=sum(pop1,2);%求pop1的每行之和 %2.2.2将二进制编码转化为十进制数(2) %decodechrom.m函数的功能是将染色体(或二进制编码)转换为十进制,参数spoint表示待解码的二进制串的起始位置 %(对于多个变量而言,如有两个变量,采用20为表示,每个变量10为,则第一个变量从1开始,另一个变量从11开始。本例为1), %参数1ength表示所截取的长度(本例为10)。 %遗传算法子程序 %Name:decodechrom.m

相关文档
最新文档