\n", " Sketch #30:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "// Create the space for the full gene:\n", "float [] gene = new float[350];\n", "\n", "// Set each location to a number between 0 and 1:\n", "for (int i = 0; i < 350; i++) {\n", " gene[i] = random();\n", "}\n", "\n", "// Create a place to draw the creature (the Phenotype):\n", "PGraphics pg = createGraphics(100, 100);\n", "\n", "// Now we draw it:\n", "for (int i = 0; i < 350; i += 7) {\n", " r = gene[i + 0] * 255;\n", " g = gene[i + 1] * 255;\n", " b = gene[i + 2] * 255;\n", " a = gene[i + 3] * 255;\n", " radius = gene[i + 4] * 100/2;\n", " x = gene[i + 5] * 100;\n", " y = gene[i + 6] * 100;\n", " pg.fill(r, g, b, a);\n", " pg.noStroke();\n", " pg.ellipse(x, y, radius, radius);\n", "}\n", "// Finally, show the creature:\n", "image(pg, 0, 0); " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That doesn't look very much like Taylor Swift. How off is it?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Fitness" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We give a phenotype a fitness value by comparing each pixel of the generated image with our target image. To compare two pixels, we sum the difference of each red, green, blue, and alpha components." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #29:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "