390 likes | 523 Vues
Sisteme de programe pentru timp real. Universitatea “Politehnica” din Bucuresti 2004-2005 Adina Magda Florea http://turing.cs.pub.ro/sptr_05. Curs Nr. 10. Genetic Algorithms applications GAIA. 2. 1 Generating new images using Genetic Algorithms.
E N D
Sisteme de programepentru timp real Universitatea “Politehnica” din Bucuresti 2004-2005 Adina Magda Florea http://turing.cs.pub.ro/sptr_05
Curs Nr. 10 Genetic Algorithms applications • GAIA 2
1 Generating new images using Genetic Algorithms • The Gaia program has been developed as a tool to generate new kind of images. • Based on the paper Artificial Evolution for Computer Graphics from Karl Sims (Computer Graphics, Volume 25, Number 4, July 1991), it uses the ideas of genetic algorithms and evolution to assist the user in the creation of new images. • Every image is generated evaluating a mathematic formula in the real domain. • The problem is to find formulas which upon evaluated give us interesting images. We use genetic algorithms to find this formulas. 3
Generating new images using Genetic Algorithms • Starting from a random and simply formula, the program generates multiple variations of the current image modifing slightly the formula. • The new formulas are evaluated and the results presented to the user, which will choose and select the most interesting based on his artistic criterion. • The selected formula becomes the new generator and the process is repeated again, closing this way the cycle. • The concept of evolution is used to find new formulas from the current ones, in a way the new formulas become not so simply, and the asociated images quite good looking. 4
Generating new images using Genetic Algorithms • (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) • (color_grad "earth" (gradient (log (+ (&& (lerp (PHY) #(-0.080 0.408 0.254) (RAD)) (RAD)) #(-0.098 -0.277 -0.840))))) • (color_noise (mod (warped_color_noise (X) -0.003 -0.296 #(-0.359 0.020 -0.790)) (Y)) (color_noise (X) (invert (Y)))) 5
Generating new images using Genetic Algorithms • (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (X)) (RAD) (bw_noise (PHY) (log 0.529))) • (triwave (- (^^ (&& (RAD) (PHY)) (PHY)) #(-0.176 0.738 -0.928))) • (mynoise (triwave (|| (RAD) (PHY))) (RAD)) 6
Generating new images using Genetic Algorithms • (lerp (* (X) (X)) (/ (/ #(0.204 0.166 0.711) (RAD)) (RAD)) (bw_noise (RAD) (RAD))) • (triwave (lerp (min (lerp (PHY) (lerp (PHY) 0.033 (RAD)) #(0.050 0.137 -0.586)) (PHY)) (IRAD) (RAD))) • (color_noise (cos (+ (bw_noise (mod (X) (Y)) #(-0.419 -0.415 0.673)) (Y))) 0.296) 7
Generating image transitions using Genetic Algorithms • The program can also generate smooth transitions between any images generated in the program. • The user selects and source and the target images, and the program finds the frames which will transform the source image in the target image. • If the formulas used to compute the images have nothing in common, the transition will be a pure melt • If the formulas have some similarities, we obtain an interesting transition of forms. 8
Generating image transitions using Genetic Algorithms Source: (triwave (abs (RAD))) Target: (triwave (abs (X))) 9
Generating image transitions using Genetic Algorithms Source: (triwave (abs (X))) Target: (triwave (&& (PHY) (PHY))) 10
Generating image transitions using Genetic Algorithms Source: (/ (& (Y) (X)) (RAD)) Target: (/ (& (Y) (RAD)) (RAD)) 11
2 Genotype • Gaia codes every image generated with a formula and a domain. • Both elements are the genotype of the solutions. • The formulas are mathematical expressions built from a set of operators and constanst. • The expression is stored in lisp format, and tells Gaia how the image should be evaluated. • The domain tells Gaia where the formula should be evaluated. • The domain is simply a region of the real plane specified as the limits in both directions. • The default domain used is [-1..1] x [-1..1]. 12
2 Genotype • The formulas can be as long as desired, they have no fixed length. • The formulas are frequently shown in tree representation, showing how the expression is evaluated. Every node in the expression has one branch for each argument it needs, where it hangs more nodes. The operators with no arguments are the leafs of the tree, while the root node is that with no parent: the last one begin evaluated. Lisp Expression Domain (+ (+ (X) (Y)) (Y)) [0..1] x [0..1] (gradient (invert (- (0.5) (RAD)))) [-1..1] x [-1..1] (abs (lerp (mynoise (/ (triwave (mod (PHY) -0.190)) (RAD)) (min (RAD) -0.873)) (Y) (X))) [-1..1] x [-1..1] 13
3 Formulas & operators • Formulas are sequences of operators and functions arranged in tree form. • There are five classes of operators: • Domain operators: Those like X,Y,RAD,PHY which depends on the domain where the formula is being evaluated. X, Y Returns an image which is directly the values of the domain in the X-axis or Y-axis directions. So the resulting images are horizontal and vertical ramps of luminance. These images where obtained with the domain [0..1] x [0..1] 14
3 Formulas & operators RAD The resulting image depends directly on the domain where it is evaluated. The luminance of each pixel in the image is directly the distance from the coordinates of the pixel in the domain to the origin, typically at the center. IRAD Similar to the operador RAD, the luminance of each pixel is the distance measured to the nearest odd integer in the domain. The black corners represents the four integer coords (-1,-1) (-1,1) (1,1) (1,-1) of the domain where the image was rendered. 15
3 Formulas & operators PHY The luminance of the resulting image represents the angular coordinate of the pixel, with the zero heading down.There where the value is greater than 1 a white pixel is used. 16
3 Formulas & operators 2. One argument functions: From a single image, returns a new image after applying some function to the pixels values of the argument. Some examples of functions are compute his cos, sin, normalize, gradient, abs, round, triwave, ... TRIWAVE This operator is important because it is bounded in the interval 0..1 for all the source values. This means that we can feed this operator with any temporal image with any pixel values, that the resulting image will be limited between 0 and 1. 17
3 Formulas & operators 3. Simply operators: Combines two images to return the result of the operator. The combination operator can be as easy as add the images, substract, multitply, or combine logically at pixel level. 4. Complex operators: Need more than two images as arguments and returns a more complex combination of them. They usually use some of them as parameters of the combination. Examples are Lerp, noise functions, color grad, ... 5. Misc operators: Here are included the operators which uses external images imported from other programs, those which uses the templates of the program, and the real o vector numbers like #(0.098 0.277 0.840) 18
3 Formulas & operators LERP It takes three images as arguments: A,B,C. and computes the resulting image as A+(B-A)*Triwave(C). So it makes a linear interpolation from A to B, using C as weight of the interpolation. The operator Triwave is included to limit C in the interval 0..1. 19
4 Mutation Evolution is achived by two methods: Mutation and Combination. Mutations • A mutation can be though as a modification of the genotype. From a starting genotype (with his fenotype: the image) we can generate similar images performing changes in the genotype and evaluating the new formulas. If we are using a long formula to obtain an image, and make some little changes in the formula, the final image will be in general quite similar but different. • Mutations are normally applied on a single node, and affects this node and maybe nodes in lower levels. This node is choosed randomly in the tree. 20
4 Mutation • New Node:The selected node is substituted by a new simply random node. This type of mutation can achieve important changes when the original tree is big, and the node is near the root of the tree. • Adjust Node: The node is modified by another one with the same number of parameters. The tree maintains the same structure, but instead of computing the cos (maybe) we will compute the sin, or maybe we will change a constant with value 0.7 by a new one with value 0.5. This type of mutation makes light changes. • Node as arg: The selected node becomes the argument of a new random node which is placed in his previous location in the tree, and the old node hanging from it. If the new node needs more arguments they are generated randomly. 21
4 Mutation • Arg as node: The inverse operation. One of the arguments of the node substitutes the node itself. The new tree is simply than the original. If the original node got more arguments, they are discarted. • Node as uncle: The node is substituted by a copy of a node in upper levels of the tree. • Reorder arguments:This operator changes the order of the arguments of the selected node (assuming it has more than two). This is useful in some parametrized operators. It can be very different to compute X / RAD or RAD / X. The more mutations we apply, the more differences we get between parent and child images. The default number of mutations applied is one, but when formulas become bigger, it is preferable to use two o three. In Gaia, there is only one parent. Children are obtained by performing different types of mutations in different nodes of the same parent. 22
4 Mutation examples • Parent expression: (triwave (mod (triwave (PHY)) (RAD))) • Domain: [-1..1] x [-1..1] 23
4 Mutation examples • (triwave (mod (lerp (PHY) (Y) (Y)) (RAD))) • (mod (triwave (PHY)) (RAD)) • (triwave (mod (^^ (triwave (PHY)) (X)) (RAD))) • (triwave (mod (PHY) (RAD))) • (triwave (lerp (mod (triwave (PHY)) (RAD)) (Y) (RAD))) • (triwave (mod (max (triwave (PHY)) (RAD)) (RAD))) • (triwave (sin (triwave (PHY)))) • (triwave (mod (+ (triwave (PHY)) (RAD)) (RAD))) • (triwave (mod (triwave (PHY)) (warped_color_noise (RAD) #(0.653 0.865 -0.369) #(0.683 0.337 -0.625) (X)))) • (mod (mod (triwave (PHY)) (RAD)) (PHY)) • (triwave (mod (RAD) (triwave (PHY)))) • (triwave (rotate (mod (triwave (PHY)) (RAD)) (RAD))) • (triwave (mod (gradient (triwave (PHY))) (RAD))) • (invert (triwave (mod (triwave (PHY)) (RAD)))) • (triwave (* (mod (triwave (PHY)) (RAD)) (X))) • (triwave (mod (abs (PHY)) (RAD))) 24
5 Combination • With this type of evolution we want to find a method to generate images that share characteristics of two previously generated images. • To accomplish this we will rely on the fact that the relation between genotype and fenotype (formulas and images) is basically associated to the sequences of operators used in the genotypes, rather than the individual operators used. So we will try to maintain sequences of nodes of the parent and mother in the new genotypes. 26
5 Combination The steps to generate new offspring by combining two genotypes are: • Select one random node in the parent tree, and one random node in the mother tree. • Break the trees by this points. Parent is divided in P1 and P2, and mother in M1 and M2. • Interchange the parts of the genders. Create two new expressions C1=P1+M2 and C2=M1+P2 • Repeat the process choosing other nodes in the parent and mother trees. 27
5 Combination Mother Parent Child 1 Child 2 28
5 Combination examples Parent expression: (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (X)) (RAD) (bw_noise (PHY) (log 0.529))) Domain: [-1..1] x [-1..1] Mother expression: (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) Domain: [-1..1] x [-1..1] 29
5 Combination examples • (lerp (triwave (RAD)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (bw_noise (PHY) (log 0.529)) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod #(-0.468 -0.375 -0.624) (RAD)))) (RAD)) • (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (X)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (- (triwave (RAD)) 0.590)) (RAD) (bw_noise (PHY) (log 0.529))) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (bw_noise (PHY) (log 0.529))) • (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY))) (RAD)) (X)) (RAD) (bw_noise (PHY) (log 0.529))) 31
5 Combination examples • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (RAD) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (* (vector (PHY) (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (X)) (RAD) (bw_noise (PHY) (log 0.529))) • (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (X)) (RAD) (bw_noise (PHY) (log (RAD)))) • (lerp (triwave (PHY)) (log 0.529) (RAD)) • (lerp (* (vector -0.422 (warped_bw_noise (RAD) #(-0.468 -0.375 -0.624) (Y) (RAD)) (RAD)) (X)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) • (lerp (triwave (PHY)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (bw_noise (PHY) (log 0.529)))) (RAD)) • (lerp (triwave #(-0.468 -0.375 -0.624)) (* (lerp #(0.524 0.389 -0.394) (- (triwave (RAD)) 0.590) (PHY)) (triwave (mod (triwave (PHY)) (RAD)))) (RAD)) 32
6 Image transitions • The software is able to make smooth transitions between images generated elsewhere in the program. This means that the user selects one image as the source, and another as the target, and Gaia will make automatically a smooth transition between both images. • Depending on the semblance of both source and target, the transition will be a simply melt if the genotypes have nothing in common, but it can be an interesting transition if there are some similarities in the genotypes. • We say that two genotypes are similar if they share the upper nodes of the genotypes (the nodes near the root node). • The more nodes they share, the more similar they are. If both expressions have different root nodes, the expressions have nothing in common. 33
6 Image transitions • To obtain the animation, Gaia generates a new genotype parametrized in time. • This new genotype has the common nodes of source and target images, and interpolates in time the nodes which are different. • This means that when the parameter t = 0, the genotype is equivalent to the source genotype, while when t = 1, the genotype match the target. Between, the genotype is a proportional mix of both genotypes. • What it is important here is to note that in fact we are making a simply melt of the different nodes, but when apply the common operators, the melt is visualized as a transformation of forms in the image, and not just as an interpolation of colors. 34
6 Image transitions Source genotype Target genotype New genotype The blue nodes are common to both source and target genotypes, while the red and green nodes are different. The gray nodes are inserted to interpolate in time the different nodes. The program inserts this special join node every time the nodes of source and target differs. 35
6 Image transitions Source genotype Target genotype New genotype Source genotype: (+ (X) (* (RAD) (Y))) X + RAD*Y Target genotype: (+ (X) (* (RAD) (Cos (* (2) (PHY))))) X + RAD*(Cos(2*PHY)) New genotype: (+ (X) (* (RAD) (+ (* (1-t) (Y)) (* (t) (Cos (* (2) (PHY))))))) X + RAD* ((1-t)*(Y) + (t)*(Cos(2*PHY))) 36
6 Image transitions • Another type of animation is acomplished when the domains are different. • In this case, the intermediate domains is also a proportional mix of the source and target domains, finding an easy way to get zooms (magnification and reduction) and pannings (displacements) of the images. 37
6 Image transitions Source: (triwave (abs (X))) Target: (triwave (&& (PHY) (PHY))) 38
6 Image transitions Source: (lerp (/ (gradient (& (PHY) (RAD))) (RAD)) (Y) #(0.545 0.495 0.981)) Target: (lerp (Y) #(0.545 0.495 0.981) (/ (gradient (& (PHY) (RAD))) (RAD))) 39