[3] | 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> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </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> |
---|
| 10 | 00002 <span class="preprocessor">#include "nonstd.h"</span> <span class="comment">//randomN, rnd01</span> |
---|
| 11 | 00003 |
---|
| 12 | 00004 <span class="preprocessor">#define FIELDSTRUCT Geno_ftest</span> |
---|
| 13 | 00005 <span class="preprocessor"></span><span class="keyword">static</span> ParamEntry GENOtestparam_tab[]= <span class="comment">//external access to ftest genetic parameters</span> |
---|
| 14 | 00006 { |
---|
| 15 | 00007 {<span class="stringliteral">"Genetics: ftest"</span>,1,1,}, |
---|
| 16 | 00008 {<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>,}, |
---|
| 17 | 00009 {0,}, |
---|
| 18 | 00010 }; |
---|
| 19 | 00011 <span class="preprocessor">#undef FIELDSTRUCT</span> |
---|
| 20 | 00012 <span class="preprocessor"></span> |
---|
| 21 | 00013 Geno_ftest::Geno_ftest() : par(GENOtestparam_tab,this) |
---|
| 22 | 00014 { |
---|
| 23 | 00015 prob=0.1; |
---|
| 24 | 00016 } |
---|
| 25 | 00017 |
---|
| 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) |
---|
| 27 | 00020 { |
---|
| 28 | 00021 <span class="keywordflow">if</span> (!gene[0]) <span class="keywordflow">return</span> 1; <span class="comment">//empty is not valid</span> |
---|
| 29 | 00022 <span class="keywordtype">bool</span> ok=<span class="keyword">true</span>; |
---|
| 30 | 00023 <span class="keywordtype">int</span> i; |
---|
| 31 | 00024 <span class="keywordflow">for</span>(i=0;i<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>;} |
---|
| 32 | 00025 <span class="keywordflow">return</span> ok ? <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a> : i+1; |
---|
| 33 | 00026 } |
---|
| 34 | 00027 |
---|
| 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> *&gene) |
---|
| 36 | 00030 { |
---|
| 37 | 00031 SString validated; <span class="comment">//new genotype (everything except ATGC is skipped)</span> |
---|
| 38 | 00032 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i<strlen(gene);i++) |
---|
| 39 | 00033 <span class="keywordflow">if</span> (strchr(<span class="stringliteral">"ATGC"</span>,gene[i])) validated+=gene[i]; <span class="comment">//validated contains only ATGC</span> |
---|
| 40 | 00034 free(gene); |
---|
| 41 | 00035 gene=strdup(validated); <span class="comment">//reallocate</span> |
---|
| 42 | 00036 <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>; |
---|
| 43 | 00037 } |
---|
| 44 | 00038 |
---|
| 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> *&gene,<span class="keywordtype">float</span> &chg) |
---|
| 46 | 00041 { |
---|
| 47 | 00042 <span class="keyword">static</span> <span class="keywordtype">char</span> a[]=<span class="stringliteral">"ATGC"</span>; |
---|
| 48 | 00043 <span class="keywordtype">int</span> changes=0,len=strlen(gene); |
---|
| 49 | 00044 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i<len;i++) |
---|
| 50 | 00045 <span class="keywordflow">if</span> (rnd01<prob) <span class="comment">//normalize prob with length of genotype</span> |
---|
| 51 | 00046 {gene[i]=a[randomN(4)]; changes++;} |
---|
| 52 | 00047 chg=(float)changes/len; |
---|
| 53 | 00048 <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>; |
---|
| 54 | 00049 } |
---|
| 55 | 00050 |
---|
| 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> *&g1,<span class="keywordtype">char</span> *&g2,<span class="keywordtype">float</span>& chg1,<span class="keywordtype">float</span>& chg2) |
---|
| 57 | 00053 { |
---|
| 58 | 00054 <span class="keywordtype">int</span> len1=strlen(g1),len2=strlen(g2); |
---|
| 59 | 00055 <span class="keywordtype">int</span> p1=randomN(len1); <span class="comment">//random cut point for first genotype</span> |
---|
| 60 | 00056 <span class="keywordtype">int</span> p2=randomN(len2); <span class="comment">//random cut point for second genotype</span> |
---|
| 61 | 00057 <span class="keywordtype">char</span> *child1=(<span class="keywordtype">char</span>*)malloc(p1+len2-p2+1); |
---|
| 62 | 00058 <span class="keywordtype">char</span> *child2=(<span class="keywordtype">char</span>*)malloc(p2+len1-p1+1); |
---|
| 63 | 00059 strncpy(child1,g1,p1); strcpy(child1+p1,g2+p2); |
---|
| 64 | 00060 strncpy(child2,g2,p2); strcpy(child2+p2,g1+p1); |
---|
| 65 | 00061 free(g1); g1=child1; |
---|
| 66 | 00062 free(g2); g2=child2; |
---|
| 67 | 00063 chg1=(float)p1/strlen(child1); |
---|
| 68 | 00064 chg2=(float)p2/strlen(child2); |
---|
| 69 | 00065 <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>; |
---|
| 70 | 00066 } |
---|
| 71 | 00067 |
---|
| 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) |
---|
| 73 | 00070 { |
---|
| 74 | 00071 <span class="keywordtype">char</span> ch=g[pos]; |
---|
| 75 | 00072 <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> |
---|
| 76 | 00073 <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>); |
---|
| 77 | 00074 <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>); |
---|
| 78 | 00075 <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>); |
---|
| 79 | 00076 <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>); |
---|
| 80 | 00077 <span class="keywordflow">return</span> <a class="code" href="classGeno__ftest.html#a5">style</a>; |
---|
| 81 | 00078 } |
---|
| 82 | 00079 |
---|
| 83 | 00080 |
---|
| 84 | 00081 <span class="comment">//#ifdef GENO_FTEST_APPL ///<define this macro to compile a simple testing main function</span> |
---|
| 85 | 00082 |
---|
| 86 | 00083 <a class="code" href="classGeno__ftest.html">Geno_ftest</a> gft; |
---|
| 87 | 00084 |
---|
| 88 | 00085 <span class="keywordtype">void</span> main() |
---|
| 89 | 00086 { |
---|
| 90 | 00087 <span class="keywordtype">float</span> chg; |
---|
| 91 | 00088 <span class="keywordtype">char</span> *g=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>()); |
---|
| 92 | 00089 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i<10;i++) |
---|
| 93 | 00090 { |
---|
| 94 | 00091 <span class="keywordtype">int</span> result=gft.<a class="code" href="classGeno__ftest.html#a3">mutate</a>(g,chg); |
---|
| 95 | 00092 printf(<span class="stringliteral">"%s [mutated %.1f%%]\n"</span>,g,chg*100); |
---|
| 96 | 00093 } |
---|
| 97 | 00094 |
---|
| 98 | 00095 <span class="keywordtype">char</span> *g2=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>()); |
---|
| 99 | 00096 <span class="keywordtype">float</span> chg2; |
---|
| 100 | 00097 printf(<span class="stringliteral">"\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n"</span>,g,g2); |
---|
| 101 | 00098 gft.<a class="code" href="classGeno__ftest.html#a4">crossOver</a>(g,g2,chg,chg2); |
---|
| 102 | 00099 printf(<span class="stringliteral">"Offspring 1:\n\t%s (%.1f%% genes from parent1)\n"</span>,g,chg*100); |
---|
| 103 | 00100 printf(<span class="stringliteral">"Offspring 2:\n\t%s (%.1f%% genes from parent2)\n"</span>,g2,chg2*100); |
---|
| 104 | 00101 free(g); |
---|
| 105 | 00102 free(g2); |
---|
| 106 | 00103 |
---|
| 107 | 00104 g=strdup(<span class="stringliteral">"ATGsomethingCG"</span>); |
---|
| 108 | 00105 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)); |
---|
| 109 | 00106 gft.<a class="code" href="classGeno__ftest.html#a2">validate</a>(g); |
---|
| 110 | 00107 printf(<span class="stringliteral">"After validation:\n\t%s\n"</span>,g); |
---|
| 111 | 00108 free(g); |
---|
| 112 | 00109 printf(<span class="stringliteral">"...and is YOUR genotype O.K.?\n\n"</span>); |
---|
| 113 | 00110 } |
---|
| 114 | 00111 |
---|
| 115 | 00112 <span class="comment">//#endif</span> |
---|
| 116 | 00113 |
---|
| 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 |
---|
| 120 | width=110 height=53></a>1.2.17 </small></address> |
---|
| 121 | </body> |
---|
| 122 | </html> |
---|