Jump to content

automatic recombination of code-snippets

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Does a program exist that is able to combine code-snippets to generate other programms? I need to find methods to do that to create an AI I'm planning.

It would need to know at what places in the code-snippet what other snippets may be inserted.

examples for snippets I have in mind (I don't know how they usually look like):
<Type> <variable_name>;
<variable_name> = <value>; : where TypeOf(<value>) == TypeOf(<variable_name>)
while(<bool_value>){<executable_code>}

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Why wouldn't you use delegates (.NET) or function pointers (C/C++)? If you have logic which determines what to do, then just invoke appropriate function on the fly.

#3
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
That would work, but it might be too inefficient for the finished thing. The frequently used delegates would be called continuously from several threads at once. I don't think that will be fast enough (then again, I know little about processing efficiency, as I have no formal IT education, yet), but it will suffice for a prototype, if I find the time to write one. Thanks for the input, but now I feel stupid for not thinking of this workaround myself :(

#4
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Are you trying to do genetic programming? Other options would be to have templates, combine them they way you want; then generate the program. You could create stuff on the fly (c# & java support this), or you could have an interpretted language which takes your snippets and runs the code.

Depends really on what your goal is.

#5
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
It's supposed to be a hybrid of different AI approaches. Genetic programming is one of them. It's a private project to learn and test some theories.

Your idea with the templates sounds like what I want. The problem is, I don't want them to be combined in a way I choose, but more or less randomly. I'm looking for algorithms that can combine templates randomly without creating faulty code that results in syntax errors or something. It should, for example, understand that a while() loop may only have a parameter of type boolean in the () bracket and should contain executable code in the {} bracket.

I'm mostly just looking for good templates, the algorithms for recombination I may be able to make myself. That will be good practice.

#6
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Beware of random code snippets in AI. If you fall so low to combine methods arbitrarily then you're doing something seriously wrong. Everything in AI is well sounded and all theorems can be proved. My suggestion to you is not to apply random methods but rather to learn how to apply each of them correctly so to maximize its capabilities.

#7
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts

anotheruser said:

It's supposed to be a hybrid of different AI approaches. Genetic programming is one of them. It's a private project to learn and test some theories.

Your idea with the templates sounds like what I want. The problem is, I don't want them to be combined in a way I choose, but more or less randomly. I'm looking for algorithms that can combine templates randomly without creating faulty code that results in syntax errors or something. It should, for example, understand that a while() loop may only have a parameter of type boolean in the () bracket and should contain executable code in the {} bracket.

I'm mostly just looking for good templates, the algorithms for recombination I may be able to make myself. That will be good practice.

Ic, ic. I would suggest starting very simply. So maybe first have variable declarations and some arithmetic operators first.

What you are really doing is building a data-flow, a bit like what compilers do with the source code. It's this flow which can be manipfulated to optimise etc... Then spat out as whatever language they want.

But anyway, I would suggest somthing like this (i'm being a bit loose with the terminology here):,

store a set of variables, which is initially empty. Each var has an associated type (for typed vars.)
have a number of expression templates, each has a parameter set, containing it's requirements, and has a generator set, of which contains vars to add to the set.
so for example, expression "int a" has a parameter set {} and generator set {A}
then "let var=5" might be {A,B}, generator {VAR}
then maybe "add" has a parameter set {A,B} and generator set {A,B,C}
etc...

So for each expression then the parameter set must be a subset of the current variable set; those parameters are removed from that set, and replaced with the generatred set. So as you select code at random you can tell if valid if you can satisify the parameter set of an expression. And if so you know you can use it.

It's an interesting problem, might have some more thoughts on the matter later, hopefuly that is helpful and you can start doing somthing from it.

#8
Excited

Excited

    Newbie

  • Members
  • PipPip
  • 27 posts
You will need a dynamic language, I suggest you take a look at a Lisp or perhaps Python.

#9
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
I would suggest that the language is pretty independant here; the AI doesn't have to use the syntax of our programming languages. I think it's an easier task to solve at the data flow level (which can always be spat back out as whatever syntax you want) rather then attempting to take syntax templates and doing string replacement on them.

#10
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts

abzero said:

store a set of variables, which is initially empty. Each var has an associated type (for typed vars.)
have a number of expression templates, each has a parameter set, containing it's requirements, and has a generator set, of which contains vars to add to the set.
so for example, expression "int a" has a parameter set {} and generator set {A}
then "let var=5" might be {A,B}, generator {VAR}
then maybe "add" has a parameter set {A,B} and generator set {A,B,C}
etc...

So for each expression then the parameter set must be a subset of the current variable set; those parameters are removed from that set, and replaced with the generatred set. So as you select code at random you can tell if valid if you can satisify the parameter set of an expression. And if so you know you can use it.


That's exactly what I want. Do you know if something like this exists already, so I don't have to write all of that myself?

#11
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
I would have thought someone has done somthing like this before. Although I think some genetic based programming uses a different approach (using bit-strings to represent stuff, so they can randomly flip them and combine etc, might be worth a search?)

but alas I don't know if anything like this exists. But good luck with your project.

Edit: Genetic programming - Wikipedia, the free encyclopedia contains a number of references; maybe somthing in there might help?

#12
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
I would recommend writing it yourself anyway. First of all, it's a very specific program, so you will encounter many limits, if you use someone else's program, and secondly writing it will give you good experience for the future. :)