Most viewed recipes tagged "genetic_algorithms"http://code.activestate.com/recipes/tags/genetic_algorithms/views/2012-10-02T16:18:36-07:00ActiveState Code RecipesPartition Problem and natural selection (Python)
2009-10-27T00:27:14-07:00Jai Vikram Singh Vermahttp://code.activestate.com/recipes/users/4171450/http://code.activestate.com/recipes/576937-partition-problem-and-natural-selection/
<p style="color: grey">
Python
recipe 576937
by <a href="/recipes/users/4171450/">Jai Vikram Singh Verma</a>
(<a href="/recipes/tags/approximate_solution/">approximate_solution</a>, <a href="/recipes/tags/complete/">complete</a>, <a href="/recipes/tags/crossover/">crossover</a>, <a href="/recipes/tags/easiest_hard_problem/">easiest_hard_problem</a>, <a href="/recipes/tags/generations/">generations</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/natural_selection/">natural_selection</a>, <a href="/recipes/tags/np/">np</a>, <a href="/recipes/tags/np_complete/">np_complete</a>, <a href="/recipes/tags/np_hard/">np_hard</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/partition_problem/">partition_problem</a>).
Revision 4.
</p>
<p>Partition problem
From Wikipedia, the free encyclopedia</p>
<p>In computer science, the partition problem is an NP-complete problem. The problem is to decide whether a given multiset of integers can be partitioned into two "halves" that have the same sum. More precisely, given a multiset S of integers, is there a way to partition S into two subsets S1 and S2 such that the sum of the numbers in S1 equals the sum of the numbers in S2? The subsets S1 and S2 must form a partition in the sense that they are disjoint and they cover S. The optimization version asks for the "best" partition, and can be stated as: Find a partition into two subsets S1,S2 such that max(sum(S_1), sum(S_2)) is minimized (sometimes with the additional constraint that the sizes of the two sets in the partition must be equal, or differ by at most 1).</p>
<p>The partition problem is equivalent to the following special case of the subset sum problem: given a set S of integers, is there a subset S1 of S that sums to exactly t /2 where t is the sum of all elements of S? (The equivalence can be seen by defining S2 to be the difference S − S1.) Therefore, the pseudo-polynomial time dynamic programming solution to subset sum applies to the partition problem as well.</p>
<p>Although the partition problem is NP-complete, there are heuristics that solve the problem in many instances, either optimally or approximately. For this reason, it has been called the "The Easiest Hard Problem" by Brian Hayes.</p>
Artificial Neuroglial Network (ANGN) (Python)
2012-10-02T16:18:36-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578242-artificial-neuroglial-network-angn/
<p style="color: grey">
Python
recipe 578242
by <a href="/recipes/users/4182015/">David Adler</a>
(<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/genetic_algorithm/">genetic_algorithm</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_networks/">neural_networks</a>).
Revision 5.
</p>
<p>This is an attempt at emulating the algorithm from these scientific articles:</p>
<ol>
<li><a href="http://www.hindawi.com/journals/cmmm/2012/476324/">2011 - Artificial Astrocytes Improve Neural Network Performance</a></li>
<li><a href="http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0019109">2012 - Computational Models of Neuron-Astrocyte Interactions Lead to Improved Efficacy in the Performance of Neural Networks</a></li>
</ol>
<p>The objective of the program is to train a neural network to classify the four inputs (the dimensions of a flower) into one of three categories (three species of flower), (taken from the <a href="http://archive.ics.uci.edu/ml/datasets/Iris">Iris Data Set</a> from the UCI Machine Learning Repository). This program has two learning phases: the first is a genetic algorithm (supervised), the second is a neuroglial algorithm (unsupervised). This ANGN is a development of a previous program only consisting of a genetic algorithm which can be found <a href="http://code.activestate.com/recipes/578241-genetic-algorithm-neural-network-in-python-source-/">here</a>.</p>
<p>The second phase aims to emulate astrocytic interaction with neurons in the brain. The algorithm is based on two axioms: a) astrocytes are activated by persistent neuronal activity b) astrocytic effects occur over a longer time-scale than neurons. Each neuron has an associated astrocyte which counts the number of times its associated neuron fires (+1 for active -1 for inactive). If the counter reaches its threshold (defined as <code>Athresh</code>) the astrocyte is activated and for the next x iterations (defined as <code>Adur</code>) the astrocyte modifies the incoming weights to that particular neuron. If the counter reached a maximum due to persistent firing the incoming weights are increase by 25% for the proceeding <code>Adur</code> iterations; conversely if the counter reached a minimum due to persistent lack of firing the weights are decreased by 50% for the following <code>Adur</code> iterations). For a detailed description of the algorithm see the linked articles. For a general understanding of how this program was coded look at the pseudo-code/schematic <a href="http://commons.wikimedia.org/wiki/File:ANGN_schematic.png">here</a>.</p>
<p>Any comments for improvements are welcome. There are several issues in this program which require addressing, please scroll down below code to read about these issues.</p>
Evolutionary Algorithm (Generation of Prime Numbers) (Python)
2011-11-27T06:45:00-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577964-evolutionary-algorithm-generation-of-prime-numbers/
<p style="color: grey">
Python
recipe 577964
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/genetic_algorithm/">genetic_algorithm</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>).
</p>
<p>This is an evolutionary algorithm that returns a random list of prime numbers. This code is highly inefficient for a reason. This algorithm is more of a proof of concept that if a prime was a heritable trait, it would not be a desired one. </p>
<p>Parameters:</p>
<p>isPrime --> n: number to check if it is prime
allPrimes --> n: size of list of random primes, m: the primes in the list will be between 0 and m</p>