# Random functions from processing API for genetic algorithm?

**URL:** <https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294>\
**Category:** Coding Questions\
**Created:** [November 7, 2018, 6:33am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294 "2018-11-07T06:33:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hankg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hankg/32/2250_2.png) [@Hankg](https://discourse.processing.org/u/Hankg)\
**Post date:** [November 7, 2018, 6:33am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/1 "2018-11-07T06:33:57Z")

</div>

Hi! I want to build a program in which a class is constructed and then randomly adds, to itself, one of the various functions available in processing’s API. The result would be drawn to the screen (if in fact it calls a function that draws anything at all, and is filled with the proper variables…), and then some fitness algorithm (maybe based on pixel contrast or something) would decide if that class is mutated (by again adding a new function, or switching values in its current function, etc), or if it dies. The idea would be that, over time, /something/, however boring, would develop on screen based on the contingent mutation of the class.

So, my question- is there a way, on each draw loop, to randomly integrate a function in the Processing API into a class ? I’m very much a beginner when it comes to genetic stuff, so forgive me if I’m not using any language properly. I’m certainly not fully aware of how API’s are structured, so even some conceptual help on how to approach this would get me well on my way.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:43am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/2 "2018-11-07T06:43:23Z")

</div>

I’m surely the last person that should say this, as i planned to do the exact same thing some months ago, but i can tell you that it is quite… complicated… especially if you just started out. Though not impossible… But you would need many things.

First of all, you need to make sure that the resulting code does not cause any errors that would cause the program to freeze. This can be done by testing the result in a thread. (Though there still might be problems…)

Next, you need to add a method to your Base class… which i just looked up some days ago. Here’s a link : [https://stackoverflow.com/questions/1011443/extending-or-adding-new-classes-at-runtime-in-java](https://stackoverflow.com/questions/1011443/extending-or-adding-new-classes-at-runtime-in-java)

And after that… well, start with even getting a class to run a foreign method…

On the other hand, if you don’t want something fancy, you could just create some base Methods for your class and let them be executed in a random order/manner. Thats a lot easier and shouldn’t cause too many problems.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 7, 2018, 6:46am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/3 "2018-11-07T06:46:09Z")

</div>

Hi Hankg,

If you are a beginner, you might want to start by something simpler. Genetic algorithm by itself is quite a challenging topic to master.

The fitness function is probably the most important thing to work on and right now it is not clear what you really want to do with your program.

The gene that codes the behavior is also important and in most cases, not that obvious to construct.

On top of that you want the behavior to be complicated using random call to functions.

My advice would be to start with common exercice on the topic with a bunch of fish trying to eat food for example.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:48am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/4 "2018-11-07T06:48:34Z")

</div>

He’s basically trying to create a Class, composed of random functions… And then execute the class inside draw with some of its methods. If then something is drawn on the canvas, the fitness function would increase and the class is better than one without a drawn output. And then the better class gets additional methods and removes the ones unused, and also changes a bit which methods are drawn. Like you would expect from a generic algorithm… at least, thats what i understood…

---

<div class="post-metadata">

**Author:** ![Hankg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hankg/32/2250_2.png) [@Hankg](https://discourse.processing.org/u/Hankg)\
**Post date:** [November 7, 2018, 6:50am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/5 "2018-11-07T06:50:31Z")

</div>

Exactly. I think your suggestion of just constructing some methods at first might be the best way to feel out the project. Funny that we both arrived at the same idea- I was thinking of it in terms of processing being a ‘universe’ itself, and allowing it to randomly assemble as it will, over time.

The issue of crashing and the need to test in threads is what I imagined a more complex version of this might get into…yikes.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 7, 2018, 6:51am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/6 "2018-11-07T06:51:46Z")

</div>

If the fitness function is simply how many pixels are draw onto the screen I seriously doubt the end result will be interesting. Better use call to `random()` in a for loop to get the same result…

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:51am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/7 "2018-11-07T06:51:50Z")

</div>

Yup, there are already some who try to create such self developing programs. Some time ago i came across a program that constructs a game on its own… But i don’t remember the name.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:53am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/8 "2018-11-07T06:53:44Z")

</div>

Well, that sure is better to get the same result, but the underlying program and setup is more important than the result in this case. Like an AI that recognises if a number is 0 or 1 and a handwritten program that just selects which one is written is basically the same, but the approach is different. (might not be the best example, but you get the point^^)

---

<div class="post-metadata">

**Author:** ![Hankg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hankg/32/2250_2.png) [@Hankg](https://discourse.processing.org/u/Hankg)\
**Post date:** [November 7, 2018, 6:56am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/9 "2018-11-07T06:56:06Z")

</div>

I saw the one that makes it’s own game, it’s really cool stuff.  
Do you know any others off the top of your head in this vein?

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:58am UTC](https://discourse.processing.org/t/random-functions-from-processing-api-for-genetic-algorithm/5294/10 "2018-11-07T06:58:13Z")

</div>

You might wanna take a look at this wikipedia page. It talks about the concept itself of self-modifying code. Which can then be used to make such things as self improving games/codes in general.

> **[Self-modifying code](https://en.wikipedia.org/wiki/Self-modifying_code)**
>
> In computer science, self-modifying code is code that alters its own instructions while it is executing – usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance. Self-modification is an alternative to the method of "flag setting" and conditional program branching, used primarily to reduce the number of times a condition needs to be tested. The term is usually only applied to code where the self-mo...
