Answers.Body: <blockquote>
    <p>Does artificial intelligence write its own code and then execute it? If so, does it create separate functions for each purpose?</p>
  </blockquote>
  
  <p>Artificial intelligence comprises more than one technique. Here you seem to be asking about <a href="https://en.wikipedia.org/wiki/Machine_learning" rel="nofollow noreferrer">machine learning</a>, a subfield of AI which itself contains many subfields.</p>
  
  <p>Self-modifying code <em>is</em> one of the (many) techniques used in some applications of artificial intelligence.  So, in that sense, the answer to your question is "yes".</p>
  
  <p>In <a href="https://en.wikipedia.org/wiki/Neural_network" rel="nofollow noreferrer">Neural Networks</a> (NNs), for example, the NN isn't writing any code, it is just running through an optimization algorithm (and back-propagation) that incrementally changes some weights (or coefficients) such that when you enter a certain input, you get an output that's close to the desired output.  </p>
  
  <p>You also have <a href="https://en.wikipedia.org/wiki/Genetic_algorithm" rel="nofollow noreferrer">genetic algorithms</a> (GA) to evolve the weights of a neural network, as opposed to using back-propagation. GAs are closer to the idea of "an AI coding itself" (although not exactly).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-03-11T23:25:22.243Z
Answers.LastActivityDate: 2019-06-25T17:09:31.800Z
Answers.LastEditDate: 2019-06-25T17:09:31.800Z
Answers.UserDisplayName: mindcrime
Answers.Body: <p>Computers are able to write their own code <em>without</em> needing any intelligence -- see the Wikipedia entries for <a href="https://en.wikipedia.org/wiki/Self-modifying_code" rel="nofollow noreferrer">self-modifying code</a> and <a href="https://en.wikipedia.org/wiki/Metaprogramming" rel="nofollow noreferrer">metaprogramming</a>. You do have to write the instructions for how the computer should program itself, and there's a stigma against doing this because (a) it makes it hard to reason about what your program is doing when it's changing its source code, and (b) the solution is usually slower than just hardcoding in what you want the program to do in the first place. But it <em>is</em> possible, and programmers have done it (usually for maintainability or aesthetic reasons).</p>
  <p>Some AI researchers are interested in <a href="https://en.wikipedia.org/wiki/Genetic_programming" rel="nofollow noreferrer">Genetic Programming</a> though. Genetic Programming is a subset of <a href="https://en.wikipedia.org/wiki/Evolutionary_algorithm" rel="nofollow noreferrer">evolutionary algorithms</a> and Wikipedia provides a good summary of how they usually work:</p>
  <blockquote>
  <p>Step One: Generate the initial population of individuals randomly. (First generation)</p>
  <p>Step Two: Evaluate the fitness of each individual in that population</p>
  <p>Step Three: Repeat the following regenerational steps until termination (time limit, sufficient fitness achieved, etc.):</p>
  <ul>
  <li><p>Select the best-fit individuals for reproduction. (Parents)</p>
  </li>
  <li><p>Breed new individuals through crossover and mutation operations to give birth to offspring.</p>
  </li>
  <li><p>Evaluate the individual fitness of new individuals.</p>
  </li>
  <li><p>Replace least-fit population with new individuals.</p>
  </li>
  </ul>
  </blockquote>
  <p>The &quot;individuals&quot; in this case are randomly-generated computer programs, which are then tested against a fitness function.</p>
  <p>The Wikipedia page for Genetic Programming claimed that these programs are usually represented by tree structures, though there has been some experiments in using non-tree structures as well.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-11T14:50:00.113Z
Answers.LastActivityDate: 2017-03-11T14:55:05.720Z
Answers.LastEditDate: 2020-06-17T09:57:20.710Z
Answers.UserDisplayName: Left SE On 10_6_19
Answers.Body: <p>I think the answer is most likely no, not in the most notable examples of AI programs (such as Machine Learning). There <em>is</em> a set of AI techniques which involve <a href="https://en.wikipedia.org/wiki/Automatic_programming" rel="nofollow noreferrer">automatic programming</a>, but in that scenario, we have a computer program which automatically codes another program (we can call it "target program"). But the target program is not the program which perfoms the coding; so technically speaking, no, it does not write its <em>own</em> code. This is an important difference; the programmer still has the task to write the code-generator.</p>
  
  <p>If you are interested in automated coding, though, the most notable example is <a href="https://en.wikipedia.org/wiki/Genetic_programming" rel="nofollow noreferrer">Genetic Programming</a>, a technique which uses an evolutionary algorithm to breed computer programs. As you can see, we have an AI which produces as a result a computer program (which may be or not an AI program); it is not interacting with its own code.</p>
  
  <p>As a final remark, note that automatic coding is a pretty vague term and not all techniques are AI-related (for instance, back then, the first compilers were seen as a form of automatic programming). The most relevant techinque to your question is probably <a href="https://en.wikipedia.org/wiki/Program_synthesis" rel="nofollow noreferrer">Program Synthesis</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-25T17:27:31.040Z
Answers.LastActivityDate: 2019-06-25T17:33:18.607Z
Answers.LastEditDate: 2019-06-25T17:33:18.607Z
Answers.UserDisplayName: olinarr
Body: <p>Does artificial intelligence write its own code and then execute it? If so, does it create separate functions for each purpose?</p>
  
Tags: <machine-learning><evolutionary-algorithms>
Title: Does artificial intelligence write its own code?
UserDisplayName: Vedant
ThreadText: # Does artificial intelligence write its own code?
  
  (Asked by: username_0 on 2017-03-11)
  
  Does artificial intelligence write its own code and then execute it? If so, does it create separate functions for each purpose?
  
  **Tags:** machine-learning, evolutionary-algorithms
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-03-11. Score: 2
  
  > Does artificial intelligence write its own code and then execute it? If so, does it create separate functions for each purpose?
  
  Artificial intelligence comprises more than one technique. Here you seem to be asking about [machine learning](https://en.wikipedia.org/wiki/Machine_learning), a subfield of AI which itself contains many subfields.
  
  Self-modifying code *is* one of the (many) techniques used in some applications of artificial intelligence. So, in that sense, the answer to your question is "yes".
  
  In [Neural Networks](https://en.wikipedia.org/wiki/Neural_network) (NNs), for example, the NN isn't writing any code, it is just running through an optimization algorithm (and back-propagation) that incrementally changes some weights (or coefficients) such that when you enter a certain input, you get an output that's close to the desired output.
  
  You also have [genetic algorithms](https://en.wikipedia.org/wiki/Genetic_algorithm) (GA) to evolve the weights of a neural network, as opposed to using back-propagation. GAs are closer to the idea of "an AI coding itself" (although not exactly).
  
  
  ---
  
  ### Answer by username_2 on 2017-03-11. Score: 1
  
  Computers are able to write their own code *without* needing any intelligence -- see the Wikipedia entries for [self-modifying code](https://en.wikipedia.org/wiki/Self-modifying_code) and [metaprogramming](https://en.wikipedia.org/wiki/Metaprogramming). You do have to write the instructions for how the computer should program itself, and there's a stigma against doing this because (a) it makes it hard to reason about what your program is doing when it's changing its source code, and (b) the solution is usually slower than just hardcoding in what you want the program to do in the first place. But it *is* possible, and programmers have done it (usually for maintainability or aesthetic reasons).
  
  Some AI researchers are interested in [Genetic Programming](https://en.wikipedia.org/wiki/Genetic_programming) though. Genetic Programming is a subset of [evolutionary algorithms](https://en.wikipedia.org/wiki/Evolutionary_algorithm) and Wikipedia provides a good summary of how they usually work:
  
  > Step One: Generate the initial population of individuals randomly. (First generation)
  >
  > Step Two: Evaluate the fitness of each individual in that population
  >
  > Step Three: Repeat the following regenerational steps until termination (time limit, sufficient fitness achieved, etc.):
  >
  > * Select the best-fit individuals for reproduction. (Parents)
  > * Breed new individuals through crossover and mutation operations to give birth to offspring.
  > * Evaluate the individual fitness of new individuals.
  > * Replace least-fit population with new individuals.
  
  The "individuals" in this case are randomly-generated computer programs, which are then tested against a fitness function.
  
  The Wikipedia page for Genetic Programming claimed that these programs are usually represented by tree structures, though there has been some experiments in using non-tree structures as well.
  
  
  ---
  
  ### Answer by username_3 on 2019-06-25. Score: 1
  
  I think the answer is most likely no, not in the most notable examples of AI programs (such as Machine Learning). There *is* a set of AI techniques which involve [automatic programming](https://en.wikipedia.org/wiki/Automatic_programming), but in that scenario, we have a computer program which automatically codes another program (we can call it "target program"). But the target program is not the program which perfoms the coding; so technically speaking, no, it does not write its *own* code. This is an important difference; the programmer still has the task to write the code-generator.
  
  If you are interested in automated coding, though, the most notable example is [Genetic Programming](https://en.wikipedia.org/wiki/Genetic_programming), a technique which uses an evolutionary algorithm to breed computer programs. As you can see, we have an AI which produces as a result a computer program (which may be or not an AI program); it is not interacting with its own code.
  
  As a final remark, note that automatic coding is a pretty vague term and not all techniques are AI-related (for instance, back then, the first compilers were seen as a form of automatic programming). The most relevant techinque to your question is probably [Program Synthesis](https://en.wikipedia.org/wiki/Program_synthesis).
  
  
  ---
  
  