source: cpp/geno_fx/html/geno__ftest_8cpp-source.html @ 109

Last change on this file since 109 was 3, checked in by Maciej Komosinski, 15 years ago

added geno_fx, a base class for genetic operations on genotypes

File size: 9.8 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3<title>geno_ftest.cpp Source File</title>
4<link href="doxygen.css" rel="stylesheet" type="text/css">
5</head><body>
6<!-- Generated by Doxygen 1.2.17 -->
7<center>
8<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center>
9<hr><h1>geno_ftest.cpp</h1><div class="fragment"><pre>00001 <span class="preprocessor">#include "<a class="code" href="geno__ftest_8h.html">geno_ftest.h</a>"</span>
1000002 <span class="preprocessor">#include "nonstd.h"</span> <span class="comment">//randomN, rnd01</span>
1100003
1200004 <span class="preprocessor">#define FIELDSTRUCT Geno_ftest</span>
1300005 <span class="preprocessor"></span><span class="keyword">static</span> ParamEntry GENOtestparam_tab[]=   <span class="comment">//external access to ftest genetic parameters</span>
1400006 {
1500007 {<span class="stringliteral">"Genetics: ftest"</span>,1,1,},
1600008 {<span class="stringliteral">"ftest_mut"</span>,0,0,<span class="stringliteral">"Mutation probability"</span>,<span class="stringliteral">"f 0 1"</span>,FIELD(prob),<span class="stringliteral">"How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)"</span>,},
1700009 {0,},
1800010 };
1900011 <span class="preprocessor">#undef FIELDSTRUCT</span>
2000012 <span class="preprocessor"></span>
2100013 Geno_ftest::Geno_ftest() : par(GENOtestparam_tab,this)
2200014 {
2300015    prob=0.1;
2400016 }
2500017
26<a name="l00019"></a><a class="code" href="classGeno__ftest.html#a1">00019</a> <span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a1">Geno_ftest::check</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* gene)
2700020 {
2800021    <span class="keywordflow">if</span> (!gene[0]) <span class="keywordflow">return</span> 1; <span class="comment">//empty is not valid</span>
2900022    <span class="keywordtype">bool</span> ok=<span class="keyword">true</span>;
3000023    <span class="keywordtype">int</span> i;
3100024    <span class="keywordflow">for</span>(i=0;i&lt;strlen(gene);i++) <span class="keywordflow">if</span> (!strchr(<span class="stringliteral">"ATGC"</span>,gene[i])) {ok=<span class="keyword">false</span>; <span class="keywordflow">break</span>;}
3200025    <span class="keywordflow">return</span> ok ? <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a> : i+1;
3300026 }
3400027
35<a name="l00029"></a><a class="code" href="classGeno__ftest.html#a2">00029</a> <span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a2">Geno_ftest::validate</a>(<span class="keywordtype">char</span> *&amp;gene)
3600030 {
3700031    SString validated; <span class="comment">//new genotype (everything except ATGC is skipped)</span>
3800032    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;strlen(gene);i++)
3900033       <span class="keywordflow">if</span> (strchr(<span class="stringliteral">"ATGC"</span>,gene[i])) validated+=gene[i];  <span class="comment">//validated contains only ATGC</span>
4000034    free(gene);
4100035    gene=strdup(validated); <span class="comment">//reallocate</span>
4200036    <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
4300037 }
4400038
45<a name="l00040"></a><a class="code" href="classGeno__ftest.html#a3">00040</a> <span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a3">Geno_ftest::mutate</a>(<span class="keywordtype">char</span> *&amp;gene,<span class="keywordtype">float</span> &amp;chg)
4600041 {
4700042    <span class="keyword">static</span> <span class="keywordtype">char</span> a[]=<span class="stringliteral">"ATGC"</span>;
4800043    <span class="keywordtype">int</span> changes=0,len=strlen(gene);
4900044    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;len;i++)
5000045       <span class="keywordflow">if</span> (rnd01&lt;prob) <span class="comment">//normalize prob with length of genotype</span>
5100046          {gene[i]=a[randomN(4)]; changes++;}
5200047    chg=(float)changes/len;
5300048    <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
5400049 }
5500050
56<a name="l00052"></a><a class="code" href="classGeno__ftest.html#a4">00052</a> <span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a4">Geno_ftest::crossOver</a>(<span class="keywordtype">char</span> *&amp;g1,<span class="keywordtype">char</span> *&amp;g2,<span class="keywordtype">float</span>&amp; chg1,<span class="keywordtype">float</span>&amp; chg2)
5700053 {
5800054    <span class="keywordtype">int</span> len1=strlen(g1),len2=strlen(g2);
5900055    <span class="keywordtype">int</span> p1=randomN(len1);  <span class="comment">//random cut point for first genotype</span>
6000056    <span class="keywordtype">int</span> p2=randomN(len2);  <span class="comment">//random cut point for second genotype</span>
6100057    <span class="keywordtype">char</span> *child1=(<span class="keywordtype">char</span>*)malloc(p1+len2-p2+1);
6200058    <span class="keywordtype">char</span> *child2=(<span class="keywordtype">char</span>*)malloc(p2+len1-p1+1);
6300059    strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
6400060    strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
6500061    free(g1); g1=child1;
6600062    free(g2); g2=child2;
6700063    chg1=(float)p1/strlen(child1);
6800064    chg2=(float)p2/strlen(child2);
6900065    <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
7000066 }
7100067
72<a name="l00069"></a><a class="code" href="classGeno__ftest.html#a5">00069</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="classGeno__ftest.html#a5">Geno_ftest::style</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *g, <span class="keywordtype">int</span> pos)
7300070 {
7400071    <span class="keywordtype">char</span> ch=g[pos];
7500072    <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="classGeno__ftest.html#a5">style</a>=GENSTYLE_CS(0,<a class="code" href="geno__fx_8h.html#a5">GENSTYLE_INVALID</a>); <span class="comment">//default, should be changed below</span>
7600073    <span class="keywordflow">if</span> (ch==<span class="charliteral">'A'</span>) <a class="code" href="classGeno__ftest.html#a5">style</a>=GENSTYLE_RGBS(200,0,0,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
7700074    <span class="keywordflow">if</span> (ch==<span class="charliteral">'T'</span>) <a class="code" href="classGeno__ftest.html#a5">style</a>=GENSTYLE_RGBS(0,200,0,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
7800075    <span class="keywordflow">if</span> (ch==<span class="charliteral">'G'</span>) <a class="code" href="classGeno__ftest.html#a5">style</a>=GENSTYLE_RGBS(0,0,200,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
7900076    <span class="keywordflow">if</span> (ch==<span class="charliteral">'C'</span>) <a class="code" href="classGeno__ftest.html#a5">style</a>=GENSTYLE_RGBS(200,200,0,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
8000077    <span class="keywordflow">return</span> <a class="code" href="classGeno__ftest.html#a5">style</a>;
8100078 }
8200079
8300080
8400081 <span class="comment">//#ifdef GENO_FTEST_APPL ///&lt;define this macro to compile a simple testing main function</span>
8500082
8600083 <a class="code" href="classGeno__ftest.html">Geno_ftest</a> gft;
8700084
8800085 <span class="keywordtype">void</span> main()
8900086 {
9000087    <span class="keywordtype">float</span> chg;
9100088    <span class="keywordtype">char</span> *g=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>());
9200089    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;10;i++)
9300090    {
9400091       <span class="keywordtype">int</span> result=gft.<a class="code" href="classGeno__ftest.html#a3">mutate</a>(g,chg);
9500092       printf(<span class="stringliteral">"%s   [mutated %.1f%%]\n"</span>,g,chg*100);
9600093    }
9700094
9800095    <span class="keywordtype">char</span> *g2=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>());
9900096    <span class="keywordtype">float</span> chg2;
10000097    printf(<span class="stringliteral">"\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n"</span>,g,g2);
10100098    gft.<a class="code" href="classGeno__ftest.html#a4">crossOver</a>(g,g2,chg,chg2);
10200099    printf(<span class="stringliteral">"Offspring 1:\n\t%s  (%.1f%% genes from parent1)\n"</span>,g,chg*100);
10300100    printf(<span class="stringliteral">"Offspring 2:\n\t%s  (%.1f%% genes from parent2)\n"</span>,g2,chg2*100);
10400101    free(g);
10500102    free(g2);
10600103
10700104    g=strdup(<span class="stringliteral">"ATGsomethingCG"</span>);
10800105    printf(<span class="stringliteral">"\nChecking genotype:\n\t%s... error at position %d.\n"</span>,g,gft.<a class="code" href="classGeno__ftest.html#a1">check</a>(g));
10900106    gft.<a class="code" href="classGeno__ftest.html#a2">validate</a>(g);
11000107    printf(<span class="stringliteral">"After validation:\n\t%s\n"</span>,g);
11100108    free(g);
11200109    printf(<span class="stringliteral">"...and is YOUR genotype O.K.?\n\n"</span>);
11300110 }
11400111
11500112 <span class="comment">//#endif</span>
11600113
117</pre></div><hr><address style="align: right;"><small>Generated on Tue Jul 30 23:21:19 2002 for Framsticks GenoFX by
118<a href="http://www.doxygen.org/index.html">
119<img src="doxygen.png" alt="doxygen" align="middle" border=0
120width=110 height=53></a>1.2.17 </small></address>
121</body>
122</html>
Note: See TracBrowser for help on using the repository browser.