{"id":15027975,"url":"https://github.com/primaryobjects/ai-programmer","last_synced_at":"2025-05-16T14:02:58.943Z","repository":{"id":6537971,"uuid":"7779270","full_name":"primaryobjects/AI-Programmer","owner":"primaryobjects","description":"Using artificial intelligence and genetic algorithms to automatically write programs. Tutorial: http://www.primaryobjects.com/cms/article149","archived":false,"fork":false,"pushed_at":"2023-01-16T13:42:32.000Z","size":6787,"stargazers_count":1093,"open_issues_count":0,"forks_count":264,"subscribers_count":159,"default_branch":"master","last_synced_at":"2025-04-03T09:09:22.841Z","etag":null,"topics":["ai","artificial-intelligence","c-sharp","fitness-score","genetic-algorithm","genetic-algorithms","genetic-programming","genome","program-synthesis","programming-language"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/primaryobjects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["primaryobjects"]}},"created_at":"2013-01-23T16:59:29.000Z","updated_at":"2025-03-26T13:58:28.000Z","dependencies_parsed_at":"2023-02-10T03:45:52.619Z","dependency_job_id":null,"html_url":"https://github.com/primaryobjects/AI-Programmer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primaryobjects%2FAI-Programmer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primaryobjects%2FAI-Programmer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primaryobjects%2FAI-Programmer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primaryobjects%2FAI-Programmer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/primaryobjects","download_url":"https://codeload.github.com/primaryobjects/AI-Programmer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248544271,"owners_count":21121921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","artificial-intelligence","c-sharp","fitness-score","genetic-algorithm","genetic-algorithms","genetic-programming","genome","program-synthesis","programming-language"],"created_at":"2024-09-24T20:07:23.824Z","updated_at":"2025-04-12T09:22:29.704Z","avatar_url":"https://github.com/primaryobjects.png","language":"C#","readme":"AI-Programmer\n=========\n\nRead the full article [Using Artificial Intelligence to Write Self-Modifying/Improving Programs](http://www.primaryobjects.com/2013/01/27/using-artificial-intelligence-to-write-self-modifying-improving-programs/)\n\nRead the research paper [AI Programmer: Autonomously Creating\nSoftware Programs Using Genetic Algorithms](https://arxiv.org/pdf/1709.05703.pdf \"AI Programmer: Autonomously Creating\nSoftware Programs Using Genetic Algorithms\").\n\nAI-Programmer is an experiment with using artificial intelligence and genetic algorithms to automatically generate programs. Successfully created [programs](https://github.com/primaryobjects/AI-Programmer/tree/master/Results) by the AI include: hello world, hello \u003cname\u003e, addition, subtraction, reversing a string, fibonnaci sequence, 99 bottles of beer on the wall, and more. It's getting smarter. In short, it's an AI genetic algorithm implementation with self modifying code.\n\n## Motivation\n\nIs it possible for a computer to write its own programs? Need a word processor? Let the computer create one for you. Need a screen capture tool? Let the computer create one for you. Take it a step further, and let the computer create programs that simplify your life, that you didn't even know you needed!\n\nThis is the idea behind the AI-Programmer experiment. The goal is to ultimately create a computer program that can write its own computer programs to solve specific computational problems. While the capability of a computer deciding what type of program to write is beyond our current means, we can still have a computer generate programs to solve very specific tasks, such as outputting the text, \"Hello World\". AI Programmer uses an esoteric programming language for generating software programs.\n\n## Details\n\nThe underlying programming language consists of only 8 instructions, while being Turing complete. Theoretically, it is capable of solving any computational problem. This makes it easy to develop an interpreter, capable of running the AI-generated programs in a simulated environment. In this manner, each generated program may be executed and its performance ranked as a fitness score. Since the AI is using a Turing complete programming language, the AI itself, is also theoretically capable of solving any computational problem. However, for this experiment, the AI will focus on outputting a simple string to the console.\n\n## How It Works\n\nAI-Programmer works as follows:\n\n- A genome consists of an array of doubles.\n- Each gene corresponds to an instruction in the programming language.\n- Start with a population of random genomes.\n- Decode each genome into a resulting program by converting each double into its corresponding instruction and execute the program.\n- Get each program's fitness score, based upon the output it writes to the console (if any), and rank them.\n- Mate the best genomes together using roulette selection, crossover, and mutation to produce a new generation.\n- Repeat the process with the new generation until the target fitness score is achieved.\n\n## The Fitness Method\n\nThe fitness method works by scoring the output of the generated program. The score is calculated by looking at each character output by the program and subtracting its value from the desired character:\n\n```\nfitness += 256 - Math.Abs(console[i] - targetString[i]);\n```\n\n## Interpreter Instruction Set\n\n```\n\u003e \tIncrement the pointer.\n\u003c \tDecrement the pointer.\n+ \tIncrement the byte at the pointer.\n- \tDecrement the byte at the pointer.\n. \tOutput the byte at the pointer.\n, \tInput a byte and store it in the byte at the pointer.\n[ \tJump forward past the matching ] if the byte at the pointer is zero.\n] \tJump backward to the matching [ unless the byte at the pointer is zero.\n```\n\n## Results?\n\nKeep in mind, this is a proof of concept. So far, the program has successfully written several programs in its target programming language. You can view screenshots of all the results in the [/Results](https://github.com/primaryobjects/AI-Programmer/tree/master/Results) folder. These tests were ran on an Intel Core 2 Quad 2.5GHz.\n\n## hi\n\nThe AI successfully wrote a program to output \"hi\" after 5,700 generations in about 1 minute. It produced the following code:\n\n```\n+[+++++-+\u003e++\u003e++-++++++\u003c\u003c]\u003e++.[+.]-.,-#\u003e\u003e]\u003c]\n```\n\nWhile the above code contains parsing errors, such as non-matching brackets, our simulation interpreter computes the result up until the program fails, so in the above case, the syntax error (which is later on in the code, after a solution is found) doesn't impact the fitness.\n\nYou can try pasting the above code into an online [interpreter](http://www.iamcal.com/misc/bf_debug/). Click \"Start Debugger\", ignore the warnings, then click Run To Breakpoint. Note the output.\n\nIf we trim off the excess code, we see the following syntactically-valid code:\n\n```\n+[+++++-+\u003e++\u003e++-++++++\u003c\u003c]\u003e++.[+.]\n```\n\n## hello\n\nThe AI successfully wrote a program to output \"hello\" after 252,0000 generations in about 29 minutes. It produced the following code:\n\n```\n+-+-+\u003e-\u003c[++++\u003e+++++\u003c+\u003c\u003e++]\u003e[-[---.--[[-.++++[+++..].+]],]\u003c-+\u003c+,.+\u003e[[.,],+\u003c.+-\u003c,--+.]],+][[[.+.,,+].-\n```\n\nDuring the generation process, the AI came pretty close to a solution, but a couple letters were bound to each other, within a loop. The AI was able to overcome this by creating an inner-loop, within the problematic one, that successfully output the correct character, and continued processing.\n\n## Hi!\n\nIn another example, the AI successfully wrote a program to output \"Hi!\" after 1,219,400 generations in about 2 hours and 7 minutes. It produced the following code:\n\n```\n\u003e-----------\u003c++[[++\u003e++\u003c+][]\u003e-.+[+++++++++++++++++++++++++++++\u003e\u003c+++.\u003c\u003e\u003c--\u003e\u003e\u003e+].]\n```\n\n## I love all humans\n\nThe AI successfully wrote a program to output \"I love all humans\" after 6,057,200 generations in about 10 hours. It produced the following code:\n\n```\n+[\u003e+\u003c+++]+\u003e------------.+\u003c+++++++++++++++++++++++++++++++.\u003e++++++++++++++++++++++++++++++++++.+++.+++++++.-----------------.--\u003c.\u003e--.+++++++++++..---\u003c.\u003e-.+++++++++++++.--------.------------.+++++++++++++.+++++.\n```\n\nMore complex programs could likely be generated while using faster PCs. Next steps include attempting to accept user input and process results.\n\n## Quick-Start Guide to Using the Code\n\nBy default, the code is configured to use the Classic instruction set and to write a program to output a string. To change the string that is generated, simply expand the \"Private Variables\" section and change the text for TargetString to your desired value.\n\n```\nprivate static TargetParams _targetParams = new TargetParams { TargetString = \"hello world\" };\n```\n\nTo change the type of program that the AI writes, change the fitness method inside GetFitnessMethod().\n\n```\nprivate static IFitness GetFitnessMethod()\n{\n\treturn new StringStrictFitness(_ga, _maxIterationCount, _targetParams.TargetString, _appendCode);\n}\n```\n\nYou can change this to any class within the AI.Programmer.Fitness/Concrete project. Examples:\n\n```\nreturn new StringStrictFitness(_ga, _maxIterationCount, _targetParams.TargetString, _appendCode);\nreturn new AddFitness(_ga, _maxIterationCount);\nreturn new SubtractFitness(_ga, _maxIterationCount);\nreturn new ReverseStringFitness(_ga, _maxIterationCount);\nreturn new HelloUserFitness(_ga, _maxIterationCount, _targetString);\n```\n\nTo use sub-routines, you'll need to enable functions. This will let the AI produce programs much faster. Uncomment the code for the functionGenerator, as follows:\n\n```\nprivate static IFunction _functionGenerator = new StringFunction(() =\u003e GetFitnessMethod(), _bestStatus, fitnessFunction, OnGeneration, _crossoverRate, _mutationRate, _genomeSize, _targetParams);\n```\n\nSet the App.config to use BrainPlus, so the AI has access to sub-routine instructions:\n\n```\n\u003cappSettings\u003e\n\t\u003cadd key=\"BrainfuckVersion\" value=\"2\"/\u003e \u003c!-- 1 = BF Classic, 2 = BrainPlus (BF Extended Type 3, Functions, Faster) --\u003e\n\u003c/appSettings\u003e\n```\n\nWhen using sub-routines, less code is required. So, you can use a smaller genomeSize for speed. Expand the Genetic Algorithm Settings section and change the _genomeSize to a smaller value:\n\n```\nprivate static int _genomeSize = 50;\n```\n\nExperiment and have fun!\n\n## Author\n\nKory Becker\nhttp://www.primaryobjects.com/kory-becker\n\n[Using Artificial Intelligence to Write Self-Modifying/Improving Programs](http://www.primaryobjects.com/CMS/Article149)\n\n[Pushing the Limits of Self-Programming Artificial Intelligence](http://www.primaryobjects.com/CMS/Article150)\n\n[Self-Programming Artificial Intelligence Learns to Use Functions](http://www.primaryobjects.com/CMS/Article163)\n\n[BF-Programmer: A Counterintuitive Approach to Autonomously Building Simplistic Programs Using Genetic Algorithms](http://www.primaryobjects.com/bf-programmer-2017.pdf)\n\nView @ GitHub\nhttps://github.com/primaryobjects/ai-programmer\n\n## Copyright\n\nCopyright (c) 2018 Kory Becker http://primaryobjects.com/kory-becker\n","funding_links":["https://github.com/sponsors/primaryobjects"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimaryobjects%2Fai-programmer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprimaryobjects%2Fai-programmer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimaryobjects%2Fai-programmer/lists"}