<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xavier Llorà &#187; code</title>
	<atom:link href="http://www.xavierllora.net/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xavierllora.net</link>
	<description>A notebook on data-intensive computing, genetics-based machine learning &#38; more.</description>
	<lastBuildDate>Sun, 08 Jan 2012 19:39:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fast mutation implementation for genetic algorithms in Python</title>
		<link>http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/</link>
		<comments>http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 05:31:52 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[genetic algorithms]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.xavierllora.net/?p=312</guid>
		<description><![CDATA[The other day I was playing to see how much I could squeeze out of a genetic algorithm written in Python. The code below shows the example I used. The first part implements a simple two loop version of a traditional allele random mutation. The second part is coded using numpy 2D arrays. The code [...]
Related posts:<ol>
<li><a href='http://www.xavierllora.net/2008/07/01/efficient-storage-for-python/' rel='bookmark' title='Efficient storage for Python'>Efficient storage for Python</a></li>
<li><a href='http://www.xavierllora.net/2008/01/10/profiling-python-code/' rel='bookmark' title='Profiling Python Code'>Profiling Python Code</a></li>
<li><a href='http://www.xavierllora.net/2005/11/24/genetic-algorithms-tools/' rel='bookmark' title='Genetic algorithms tools'>Genetic algorithms tools</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The other day I was playing to see how much I could squeeze out of a genetic algorithm written in Python. The code below shows the example I used. The first part implements a simple two loop version of a traditional allele random mutation. The second part is coded using <code>numpy</code> 2D arrays. The code also measures the time spent on both implementations using <code>cProfile</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> numpy <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
pop_size = <span style="color: #ff4500;">2000</span>
l = <span style="color: #ff4500;">200</span>
z = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>pop_size,l<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> mutate <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>pop_size<span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span> :
                        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">0.5</span> :
                                z<span style="color: black;">&#91;</span>i,j<span style="color: black;">&#93;</span> = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> cProfile
cProfile.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'mutate()'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> mutate_matrix <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :
        r = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span>size=<span style="color: black;">&#40;</span>pop_size,l<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">0.5</span>
        v = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span>size=<span style="color: black;">&#40;</span>pop_size,l<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        k = r<span style="color: #66cc66;">*</span>v + logical_not<span style="color: black;">&#40;</span>r<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>z
&nbsp;
cProfile.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'mutate_matrix()'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>If you run the code listed above you may get something similar to</p>
<pre>
$ python scan.py
         599933 function calls in 0.857 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.857    0.857 <string>:1(<module>)
        1    0.615    0.615    0.857    0.857 scan.py:7(mutate)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
   599930    0.242    0.000    0.242    0.000 {method 'random_sample' of 'mtrand.RandomState' objects}

         3 function calls in 0.082 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.002    0.002    0.082    0.082 <string>:1(<module>)
        1    0.080    0.080    0.080    0.080 scan.py:16(mutate_matrix)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
</pre>
<p>Also if you make the simple math, the numpy-based version is 10.45 times faster than a simple loop-based implementation. Yup, sometimes the easy way out is not the best, and giving it some thought helps <img src='http://www.xavierllora.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Related posts:<ol>
<li><a href='http://www.xavierllora.net/2008/07/01/efficient-storage-for-python/' rel='bookmark' title='Efficient storage for Python'>Efficient storage for Python</a></li>
<li><a href='http://www.xavierllora.net/2008/01/10/profiling-python-code/' rel='bookmark' title='Profiling Python Code'>Profiling Python Code</a></li>
<li><a href='http://www.xavierllora.net/2005/11/24/genetic-algorithms-tools/' rel='bookmark' title='Genetic algorithms tools'>Genetic algorithms tools</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking for code examples?</title>
		<link>http://www.xavierllora.net/2008/06/02/looking-for-code-examples/</link>
		<comments>http://www.xavierllora.net/2008/06/02/looking-for-code-examples/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 14:13:03 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.xavierllora.net/?p=233</guid>
		<description><![CDATA[Are you looking for some code sniped? Are you looking for some freely available code available on the net written on a specific language? If the answer is yes, you may want to take a look at Google Code Search. I just canned a few examples below. Genetic algorithm in Python Genetic algorithm in C [...]
Related posts:<ol>
<li><a href='http://www.xavierllora.net/2008/01/10/profiling-python-code/' rel='bookmark' title='Profiling Python Code'>Profiling Python Code</a></li>
<li><a href='http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/' rel='bookmark' title='Fast mutation implementation for genetic algorithms in Python'>Fast mutation implementation for genetic algorithms in Python</a></li>
<li><a href='http://www.xavierllora.net/2008/01/16/generic-looping-in-python/' rel='bookmark' title='Generic looping in Python'>Generic looping in Python</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Are you looking for some code sniped? Are you looking for some freely available code available on the net written on a specific language? If the answer is yes, you may want to take a look at <a title="Google code search" href="http://www.google.com/codesearch">Google Code Search</a>. I just canned a few examples below.</p>
<ul>
<li><a title="Genetic algorithm in Python" href="http://www.google.com/codesearch?as_q=genetic+algorithm&amp;btnG=Search+Code&amp;hl=en&amp;as_lang=python&amp;as_license_restrict=i&amp;as_license=&amp;as_package=&amp;as_filename=&amp;as_case=" target="_blank">Genetic algorithm in Python</a></li>
<li><a title="Genetic algorithm in C" href="http://www.google.com/codesearch?as_q=genetic+algorithm&amp;btnG=Search+Code&amp;hl=en&amp;as_lang=c&amp;as_license_restrict=i&amp;as_license=&amp;as_package=&amp;as_filename=&amp;as_case=" target="_blank">Genetic algorithm in C</a></li>
<li><a title="Genetic algorithm in Java" href="http://www.google.com/codesearch?as_q=genetic+algorithm&amp;btnG=Search+Code&amp;hl=en&amp;as_lang=java&amp;as_license_restrict=i&amp;as_license=&amp;as_package=&amp;as_filename=&amp;as_case=" target="_blank">Genetic algorithm in Java</a></li>
</ul>
<p>Related posts:<ol>
<li><a href='http://www.xavierllora.net/2008/01/10/profiling-python-code/' rel='bookmark' title='Profiling Python Code'>Profiling Python Code</a></li>
<li><a href='http://www.xavierllora.net/2008/11/13/fast-mutation-implementation-for-genetic-algorithms-in-python/' rel='bookmark' title='Fast mutation implementation for genetic algorithms in Python'>Fast mutation implementation for genetic algorithms in Python</a></li>
<li><a href='http://www.xavierllora.net/2008/01/16/generic-looping-in-python/' rel='bookmark' title='Generic looping in Python'>Generic looping in Python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.xavierllora.net/2008/06/02/looking-for-code-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

