// ///////////////////////////////////////////////////////////////////////////////////////// // BRAIN FUNCTIONS // TODO: implementar esses métodos em brainActions(). public void train() { for (int i = 0; i < repeat_teaching; i++) { float r = p.random(1); if (r < 0.25f) { brain.setInput(0, p.random(0.5f, 1f)); brain.setInput(1, p.random(0.5f, 1f)); brain.setDesiredOutput(0, 1f); brain.setDesiredOutput(1, 0f); brain.feedForward(); brain.backPropagate(); } else if (r >= 0.25f && r < 0.5f) { brain.setInput(0, p.random(0f, 0.5f)); brain.setInput(1, p.random(0f, 0.5f)); brain.setDesiredOutput(0, 0.5f); brain.setDesiredOutput(1, 0.5f); brain.feedForward(); brain.backPropagate(); } else if (r >= 0.5f && r < 0.75f) { brain.setInput(0, p.random(0.5f, 1f)); brain.setInput(1, p.random(0f, 0.5f)); brain.setDesiredOutput(0, 0.5f); brain.setDesiredOutput(1, 0.5f); brain.feedForward(); brain.backPropagate(); } else { brain.setInput(0, p.random(0f, 0.5f)); brain.setInput(1, p.random(0f, 0.5f)); brain.setDesiredOutput(0, 0f); brain.setDesiredOutput(1, 0f); brain.feedForward(); brain.backPropagate(); } } }
Stripe(PApplet p) { parent = p; x = 0; // All stripes start at 0 speed = parent.random(1); // All stripes have a random positive speed w = parent.random(10, 30); mouse = false; }
// //////////// // /////////// // ////////// // COM DNA VVVVVVVV// public Creature( PApplet _p, DNA _dna, int _id, PVector _loc, boolean _mouth, boolean _eye, boolean _ear, boolean _nose, boolean _hand, boolean _leg, boolean _reproductive, boolean _isPredator) { myHash = this.hashCode(); p = _p; id = _id; isPredator = _isPredator; loc = _loc; acc = new PVector(0, 0); vel = new PVector(p.random(-1, 1), p.random(-1, 1)); dna = _dna; // numero fixos de neurons dna.setGene(0, .02f); // in dna.setGene(1, .03f); // hidden dna.setGene(2, .01f); // out dna.setGene(3, .25f); // learn brainInput = (int) dna.getGene(0) * 100; brainHidden = (int) dna.getGene(1) * 100; brainOutput = (int) dna.getGene(2) * 100; // println("input: " + brainHidden); // muitas das capacidades do cérebro são herdadas pelo dna // UTILIZAR MAIS DE UM "BRAIN" PARA SEPARAR FUNCIONALIDADES brain = new Brain(dna); // movementCortex = new Brain(dna); // socialCortex = new Brain(dna); // enviromentCortex = new Brain(dna); haveMouth = _mouth; haveEye = _eye; haveEar = _ear; haveNose = _nose; haveHand = _hand; haveLeg = _leg; haveReproductive = _reproductive; // haveShape = false; decision = new Decision(); // //toda criatura tem um corpo físico bodyCopy = new Body(p, dna, loc, vel, acc, myHash); creatureDrawer = new CreatureDrawer(p, dna, loc, vel); }
// MÉTODOS DE PROCRIAÇÃO // TODO: métodos de geração (por gestação, ovos etc) public Creature copulate(ArrayList<Creature> creatures) { Creature c = null; for (Creature other : creatures) { DNA partner = other.dna; if (relativeness(creatures, 3, 297, 391) == true) { DNA childDNA = dna.mate(partner); childDNA.mutate(0.01f); // mudar (isPredator) c = new Creature( childDNA, p.round(p.random(9999999)), loc, true, true, true, true, true, true, true, false); } } return c; }
// joku randomilla tämän vierestä // tylsä bruteforcemenetelmä hajotusfiiliksen tuotoksena int[] getadj(PApplet pa, int x, int y, int z, boolean[] visited) { // {-x, x, -y, y, -z, z} int[][] dirs = { {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1} }; boolean ok = true; int[] dir; do { int i = (int) (pa.random(0, 6)); dir = dirs[i]; ok = true; if (x == 0 && i == 0) ok = false; if (y == 0 && i == 2) ok = false; if (z == 0 && i == 4) ok = false; if (x == size - 1 && i == 1) ok = false; if (y == size - 1 && i == 3) ok = false; if (z == size - 1 && i == 5) ok = false; } while (!ok || visited[world.at(x + dir[0], y + dir[1], z + dir[2])]); return dir; }
// TODO: método melhor de briga public void fight(Creature other) { float r = p.random(0, 1); if (r < 5) { // substituir por stregth other.health -= 1; } else { health -= 1; } }
@SuppressWarnings("static-access") public int getColor() { if (_colorList.size() <= 0) return 0; PApplet app = H.app(); int index = app.round(app.random(_colorList.size() - 1)); return _colorList.get(index); }
public static ArrayList<BChar> buildString(PApplet app, String str, float wander) { ArrayList<BChar> bchars = new ArrayList<BChar>(str.length()); for (int i = 0, n = str.length(); i < n; i++) { bchars.add(new BChar(str.substring(i, i + 1), app.random(-wander, wander))); if (i > 0) bchars.get(i - 1).addChild(bchars.get(i)); } return bchars; }
/** * Randomize the list order using the random() function from the specified sketch, allowing * shuffle() to use its current randomSeed() setting. */ public void shuffle(PApplet sketch) { int num = count; while (num > 1) { int value = (int) sketch.random(num); num--; float temp = data[num]; data[num] = data[value]; data[value] = temp; } }
/** * Calculates the new height to assign to a vertex and stores that value in our array of vertex * heights. * * @param inpVertexIdx The index of this vertex in our arrays. * @param inpPrevAudioFrameLvlNbr The level number of the previous audio frame. * @param inpCurrAudioFrameLvlNbr The level number of the current audio frame. * @return The height at which to draw the vertex. */ private float calcAndStoreNewVertexHghtNbr( int inpVertexIdx, float inpPrevAudioFrameLvlNbr, float inpCurrAudioFrameLvlNbr) { float rtnVertexHghtNbr = 0; rtnVertexHghtNbr = _modelRadiusNbr + _parApp.random( 0, AMPLITUDE_SCALE_NBR * PApplet.lerp( inpPrevAudioFrameLvlNbr, inpCurrAudioFrameLvlNbr, HGHT_INTERPOLATION_NBR)); _radiusLenNbrs[inpVertexIdx] = rtnVertexHghtNbr; return rtnVertexHghtNbr; }
// Constructor Box(PApplet parent, PBox2D box2d, float x, float y, float w_, float h_, boolean lock) { this.parent = parent; this.box2d = box2d; w = w_; h = h_; // Define and create the body BodyDef bd = new BodyDef(); bd.position.set(box2d.coordPixelsToWorld(new Vec2(x, y))); if (lock) bd.type = BodyType.STATIC; else bd.type = BodyType.DYNAMIC; body = box2d.createBody(bd); // Define the shape -- a (this is what we use for a rectangle) PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w / 2); float box2dH = box2d.scalarPixelsToWorld(h / 2); sd.setAsBox(box2dW, box2dH); // Define a fixture FixtureDef fd = new FixtureDef(); fd.shape = sd; // Parameters that affect physics fd.density = 1; fd.friction = 0.3f; fd.restitution = 0.5f; body.createFixture(fd); // Give it some initial random velocity body.setLinearVelocity(new Vec2(parent.random(-5, 5), parent.random(2, 5))); body.setAngularVelocity(parent.random(-5, 5)); }
private int getColor() { // Setup color float colorPart1 = sumOfColorIntensity; float colorPart2 = sumOfColorIntensity; // Get random intervals while (colorPart1 >= colorPart2 || colorPart1 > 255f || colorPart2 - colorPart1 > 255f || sumOfColorIntensity - colorPart2 > 255f) { colorPart1 = p.random(sumOfColorIntensity); colorPart2 = p.random(sumOfColorIntensity); } // float red = colorPart1; float green = colorPart2 - colorPart1; float blue = sumOfColorIntensity - colorPart2; return p.color(red, green, blue); }
public void update() { // Update movement m.update(); // If distance from first element if too big if (m.getPosition().dist(elements[0]) > elementChangeDist) { // Change values of all elements for (int i = elements.length - 1; i > 0; i--) { elements[i].set(elements[i - 1]); elementColors[i] = elementColors[i - 1]; } // Set position and color of first element elementColors[0] = getColor(); elements[0].set(m.getPosition()); elementChangeDist = p.random(elementMinDist, elementMaxDist); } }
public void setArbitraryPos(PApplet app, PGraphics context) { arbitraryPos.set(app.random(0, context.width), app.random(0, context.height), 0); }
void generate(PApplet pa) { int space = World.space; // sz^3 pisteitä, joiden välillä space tyhjää (tyhjä 0: kuutio) // eli (sz + (sz - 1) * space) ^ 3 tileä // visited ~ V_new wikipedian algossa boolean[] visited = new boolean[size3]; int[] visitorder = new int[size3]; // indeksoidaan järjestysnumerolla // aluksi size on pisteiden määrä, newsize sitten kun niiden väliin on venytetty kulkuväyliä int newsz = size + (size - 1) * space; int newsz3 = newsz * newsz * newsz; map = new int[newsz3]; // lähtöpaikka visitorder[0] = 0; visited[0] = true; map[0] = 0xffffffff; // käydään kaikki alkuperäiset pisteet läpi, jokaiseen mennään jotenkin for (int count = 1; count < size3; count++) { // - arvo tiili reunalta // (randomilla saattaa tulla sellainenkin joka ei ole reunalla, ei väliä kun ei jättikarttoja) // vois tietty pitää listaa sellaisista joista ei vielä pääse kaikkialle... // - arvo sille suunta // - visitoi se. int vidx; do { vidx = (int) (pa.random(0, count)); // monesko jo visitoitu leviää. } while (full(visited, visitorder[vidx])); int idx = visitorder[vidx]; int z = idx / (size * size), y = idx / size % size, x = idx % size; // joku vierestä, dir on akselin suuntainen yksikkövektori int[] dir = getadj(pa, x, y, z, visited); int nx = x + dir[0], ny = y + dir[1], nz = z + dir[2]; int newidx = world.at(nx, ny, nz); visitorder[count] = newidx; visited[newidx] = true; // nykykohta venytetyssä maailmassa int i = x * (space + 1), j = y * (space + 1), k = z * (space + 1); // nurkkien välillä debugväreillä for (int a = 0; a < space; a++) { i += dir[0]; j += dir[1]; k += dir[2]; // vihreä kasvaa iteraatioiden kasvaessa, sininen taas z:n mukaan map[world.at(i, j, k, newsz)] = pa.color(1, (int) ((float) count / size3 * 255), (int) ((float) k / newsz * 255)); } // verkon kärkipisteet punaisia map[world.at(i + dir[0], j + dir[1], k + dir[2], newsz)] = pa.color(255, 0, 0); } world.size = newsz; world.size3 = newsz3; world.map = map; }
public void applyRandom() { apply = PApplet.floor(parent.random(CNT)); }
public AnimatedParticle(PApplet p) { this.p = p; timerDiff = p.random(0.01f, 0.04f); color = p.color(p.random(255), p.random(255), 255); }
private void setSpots() { for (int i = 0; i < numberOfSpots; ++i) { spots[i] = new Vec2D(pa.random(pa.width - 100) + 50, pa.random(pa.height - 400) + 200); } }
// FUNCTION THAT WILL BE CALLED IN PARENT CLASS public void createbg() { PImage canvas; // addational value float r = 1; // MASKE erstellen (Vignette) // begindraw and endraw are needed for pgraphics to work properly mask.beginDraw(); // make a black background mask.background(0); // center the anchorpoint mask.shapeMode(PConstants.CENTER); // make a white ellipse half as large as the canvas mask.fill(255); mask.ellipse(width / 2, height / 2, width / 1.3f, height / 1.3f); // resize for better blur compability (prevent crashes) mask.resize(width / 5, height / 5); mask.filter(PConstants.BLUR, 19); // restire full size. this will blur the image even further mask.resize(width / 2, height / 2); mask.endDraw(); // CREATE Picture pimg.beginDraw(); // make a black background pimg.background(0); // this will generate bubbles specified by the image (NOT FINISHED) for (int i = 0; i < img.width; i += parent.random(200)) { for (int j = 0; j < img.height; j += parent.random(200)) { pimg.fill(img.get(i, j)); pimg.noStroke(); pimg.shapeMode(PConstants.CENTER); pimg.ellipse(i + 1, j + 1, r, r); r = parent.random(0.2f, 200); } r = parent.random(0.2f, 200); } // resize for better blur compability (prevent crashes) pimg.resize(width / 5, height / 5); pimg.filter(PConstants.BLUR, 9); // create a temp canvas to use te mask canvas = pimg.get(); canvas.filter(PConstants.BLUR, 15); canvas.resize(width / 2, height / 2); // apply mask on the generated image pimg.resize(width / 2, height / 2); pimg.image(canvas, 0, 0); pimg.mask(mask.get()); pimg.endDraw(); // set the transparent image on a black background // save the vignetted image stage.beginDraw(); stage.image(pimg.get(), 0, 0); stage.endDraw(); stage.save(output_path + output_data); }