0

I'm trying to make some kind of AI that will have acess to his own code and can modify it during the execution to be able to evolve

for example, during the runtime I want it to be able to change it's code from :

if (a>1){doSomething();}

to :

if (a>1){doSomethingElse();}

It is possible to do such a thing ?

If it is not possible, is it possible instead of writting it in a function, to have a String containing the algorithm like this :

String code = "if (a>1){doSomethingElse();}";

and running the string as code like that :

run(code);

And again, if that's not possible in java, is there a programming language where it can be possible ?

5
  • For the first case, you need to share more details to be able to tell you. You can definitely design your code to change the behaviour depending on data collected, but your example is too generic. About the second question yes, it is possible. In java you can write source code programmatically, compile it on the fly and execute it at your taste. Commented Sep 21, 2022 at 21:31
  • You can check ClassLoader and it's hard to give an exact answer Commented Sep 21, 2022 at 21:43
  • Let's focus on the second part. I'm not sure if it was clear that I want to run a code that will be able to modify some variables of my current program. If we were talking of the same thing, I would be happy to see a tutorial about writing source code programmatically Commented Sep 21, 2022 at 21:48
  • thanks, I will try to check ClassLoader Commented Sep 21, 2022 at 21:51
  • Lookup BeanShell. It allows scripting using Java. beanshell.github.io Commented Sep 21, 2022 at 22:09

1 Answer 1

0

You can change class files and their code at runtime using BCEL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.