1

I want to create a program that uses an stochastic optimization algorithm to evolve mathematical functions, taking two constant numbers as inputs and evolving a function in order to produce a specific answer from those two numbers.

So say the inputs are 4 and 7, and I want the answer to be 230, the program would generate a function that produces this answer from the original inputs.

I'm capable of producing evolutionary algorithms, but I'm just not sure how to store the function information, randomly mutate it, and also allow that mutated function to be executed by the java program. Is this what is called meta programming? Is it possible in java? I know I can store constants that might be used in the function as variables. Thats easy. But how could I store operators, and functions such as sine cosine and tan, as variables that can be changed?

Alex Lorimer
  • 477
  • 5
  • 9
  • Is Java a must for you? can you consider using another JVM language such as Groovy? – Dror Bereznitsky Dec 05 '13 at 14:57
  • @Vallentin Considering they are apparently capable of producing "evolutionary algorithms" and such, I doubt `if` statements are the major road block to their solution. Also, there are methods of evaluating expressions without if statements as pointed out by Ali and StackOverflowException. – Paul Richter Dec 05 '13 at 15:11
  • 1
    @Teeg What I meant was that he doesn't need to "alter the source code" this could be done with the use of `if-statements`. – vallentin Dec 05 '13 at 15:20
  • 1
    @Vallentin: But that would be awful or require more than ifs... – tilpner Dec 05 '13 at 15:22
  • @StackOverflowException Still not the point. The points was YOU DO NOT NEED TO ALTER THE CODE. – vallentin Dec 05 '13 at 15:23

3 Answers3

2

Please, dont alter the programs sourcecode and recompile at runtime.

There are tons of ways to evaluate functions, some are:

You should store the equation in a tree (done for you by many libraries), alter that tree in your genetic algorithm, and evaluate it.

You can store functions easily via interfaces or subclasses... It would be even easier with reflection.

But: You might find more efficient ways to determine that function. We can help you more if you give us the function the program should create.

Community
  • 1
  • 1
tilpner
  • 4,351
  • 2
  • 22
  • 45
2

This might help

You can create "real" plugins with java. This thread talks about it:

How can I write my own plugin loader in java?

Basically, create a main program that allows you to compose and execute chains of plugins. Where plugins are your functions like sin, cos... Since you can load real plugins on the fly; you can create new random plugins and add them to your chain.

Community
  • 1
  • 1
0

Generally Java does not support meta programming. But This will help you.

Community
  • 1
  • 1
Ali
  • 931
  • 10
  • 22