source: cpp/geno_fx/html/geno__ftest_8cpp.html @ 97

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

added geno_fx, a base class for genetic operations on genotypes

File size: 10.5 KB
RevLine 
[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 File Reference</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 File Reference</h1><table border=0 cellpadding=0 cellspacing=0>
10<tr><td></td></tr>
11<tr><td colspan=2><br><h2>Defines</h2></td></tr>
12<tr><td nowrap align=right valign=top><a name="a0" doxytag="geno_ftest.cpp::FIELDSTRUCT"></a>
13#define&nbsp;</td><td valign=bottom><b>FIELDSTRUCT</b>&nbsp;&nbsp;&nbsp;<a class="el" href="classGeno__ftest.html">Geno_ftest</a></td></tr>
14</table>
15<hr><a name="_details"></a><h2>Detailed Description</h2>
16Sample output (simple examples of various genetic operations): <a name="geno_ftest_example"></a> <div class="fragment"><pre>GTTCCGATC   [mutated 11.1%]
17GATCCGATC   [mutated 33.3%]
18GATCCGAAC   [mutated 11.1%]
19GATCCGAAC   [mutated 0.0%]
20GATCCGAAC   [mutated 0.0%]
21GATCCGAAC   [mutated 0.0%]
22GATCCGAAC   [mutated 22.2%]
23GATGCGAAC   [mutated 11.1%]
24GATGCGAAC   [mutated 0.0%]
25GATGCGAAC   [mutated 11.1%]
26
27Crossing over the last mutant,
28        GATGCGAAC
29and the simplest genotype
30        GTTCAGATC
31:
32Offspring 1:
33        GATGCGAATTCAGATC  (50.0% genes from parent1)
34Offspring 2:
35        GC  (50.0% genes from parent2)
36
37Checking genotype:
38        ATGsomethingCG... error at position 4.
39After validation:
40        ATGCG
41...and is YOUR genotype O.K.?
42
43</pre></div> Produced by the source: <div class="fragment"><pre><span class="comment">// This file is a part of Framsticks GenoFX library.</span>
44<span class="comment">// Copyright (C) 2002  Maciej Komosinski.  See LICENSE.txt for details.</span>
45<span class="comment">// Refer to http://www.frams.alife.pl/ for further information.</span>
46
47<span class="preprocessor">#include "<a class="code" href="geno__ftest_8h.html">geno_ftest.h</a>"</span>
48<span class="preprocessor">#include "nonstd.h"</span> <span class="comment">//randomN, rnd01</span>
49
50<span class="preprocessor">#define FIELDSTRUCT Geno_ftest</span>
51<span class="preprocessor"></span><span class="keyword">static</span> ParamEntry GENOtestparam_tab[]=   <span class="comment">//external access to ftest genetic parameters</span>
52{
53{<span class="stringliteral">"Genetics: ftest"</span>,1,1,},
54{<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>,},
55{0,},
56};
57<span class="preprocessor">#undef FIELDSTRUCT</span>
58<span class="preprocessor"></span>
59Geno_ftest::Geno_ftest()
60{
61   par.setParamTab(GENOtestparam_tab);
62   par.select(<span class="keyword">this</span>);
63   <a class="code" href="classGeno__fx.html#m1">supported_format</a>=<span class="charliteral">'t'</span>;
64   prob=0.1;
65}
66
67<span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a1">Geno_ftest::checkValidity</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* gene)
68{
69   <span class="keywordflow">if</span> (!gene[0]) <span class="keywordflow">return</span> 1; <span class="comment">//empty is not valid</span>
70   <span class="keywordtype">bool</span> ok=<span class="keyword">true</span>;
71   <span class="keywordtype">int</span> i;
72   <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>;}
73   <span class="keywordflow">return</span> ok ? <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a> : i+1;
74}
75
76<span class="keywordtype">int</span> <a class="code" href="classGeno__ftest.html#a2">Geno_ftest::validate</a>(<span class="keywordtype">char</span> *&amp;gene)
77{
78   SString validated; <span class="comment">//new genotype (everything except ATGC is skipped)</span>
79   <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;strlen(gene);i++)
80      <span class="keywordflow">if</span> (strchr(<span class="stringliteral">"ATGC"</span>,gene[i])) validated+=gene[i];  <span class="comment">//validated contains only ATGC</span>
81   free(gene);
82   gene=strdup(validated); <span class="comment">//reallocate</span>
83   <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
84}
85
86<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)
87{
88   <span class="keyword">static</span> <span class="keywordtype">char</span> a[]=<span class="stringliteral">"ATGC"</span>;
89   <span class="keywordtype">int</span> changes=0,len=strlen(gene);
90   <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;len;i++)
91      <span class="keywordflow">if</span> (rnd01&lt;prob) <span class="comment">//normalize prob with length of genotype</span>
92         {gene[i]=a[randomN(4)]; changes++;}
93   chg=(float)changes/len;
94   <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
95}
96
97<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)
98{
99   <span class="keywordtype">int</span> len1=strlen(g1),len2=strlen(g2);
100   <span class="keywordtype">int</span> p1=randomN(len1);  <span class="comment">//random cut point for first genotype</span>
101   <span class="keywordtype">int</span> p2=randomN(len2);  <span class="comment">//random cut point for second genotype</span>
102   <span class="keywordtype">char</span> *child1=(<span class="keywordtype">char</span>*)malloc(p1+len2-p2+1);
103   <span class="keywordtype">char</span> *child2=(<span class="keywordtype">char</span>*)malloc(p2+len1-p1+1);
104   strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
105   strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
106   free(g1); g1=child1;
107   free(g2); g2=child2;
108   chg1=(float)p1/strlen(child1);
109   chg2=(float)p2/strlen(child2);
110   <span class="keywordflow">return</span> <a class="code" href="geno__fx_8h.html#a0">GENOPER_OK</a>;
111}
112
113<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)
114{
115   <span class="keywordtype">char</span> ch=g[pos];
116   <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="classGeno__fx.html#a6">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>
117   <span class="keywordflow">if</span> (ch==<span class="charliteral">'A'</span>) <a class="code" href="classGeno__fx.html#a6">style</a>=GENSTYLE_RGBS(200,0,0,<a class="code" href="geno__fx_8h.html#a6">GENSTYLE_BOLD</a>);
118   <span class="keywordflow">if</span> (ch==<span class="charliteral">'T'</span>) <a class="code" href="classGeno__fx.html#a6">style</a>=GENSTYLE_RGBS(0,200,0,<a class="code" href="geno__fx_8h.html#a6">GENSTYLE_BOLD</a>);
119   <span class="keywordflow">if</span> (ch==<span class="charliteral">'G'</span>) <a class="code" href="classGeno__fx.html#a6">style</a>=GENSTYLE_RGBS(0,0,200,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
120   <span class="keywordflow">if</span> (ch==<span class="charliteral">'C'</span>) <a class="code" href="classGeno__fx.html#a6">style</a>=GENSTYLE_RGBS(200,200,0,<a class="code" href="geno__fx_8h.html#a4">GENSTYLE_NONE</a>);
121   <span class="keywordflow">return</span> <a class="code" href="classGeno__fx.html#a6">style</a>;
122}
123
124
125<span class="preprocessor">#ifdef GENO_FTEST_APPL //define this macro to compile a simple testing main function</span>
126<span class="preprocessor"></span>
127<a class="code" href="classGeno__ftest.html">Geno_ftest</a> gft;
128
129<span class="keywordtype">void</span> main()
130{
131   <span class="keywordtype">float</span> chg;
132   <span class="keywordtype">char</span> *g=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>());
133   <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;10;i++)
134   {
135      <span class="keywordtype">int</span> result=gft.<a class="code" href="classGeno__ftest.html#a3">mutate</a>(g,chg);
136      printf(<span class="stringliteral">"%s   [mutated %.1f%%]\n"</span>,g,chg*100);
137   }
138
139   <span class="keywordtype">char</span> *g2=strdup(gft.<a class="code" href="classGeno__ftest.html#a6">getSimplest</a>());
140   <span class="keywordtype">float</span> chg2;
141   printf(<span class="stringliteral">"\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n"</span>,g,g2);
142   gft.<a class="code" href="classGeno__ftest.html#a4">crossOver</a>(g,g2,chg,chg2);
143   printf(<span class="stringliteral">"Offspring 1:\n\t%s  (%.1f%% genes from parent1)\n"</span>,g,chg*100);
144   printf(<span class="stringliteral">"Offspring 2:\n\t%s  (%.1f%% genes from parent2)\n"</span>,g2,chg2*100);
145   free(g);
146   free(g2);
147
148   g=strdup(<span class="stringliteral">"ATGsomethingCG"</span>);
149   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">checkValidity</a>(g));
150   gft.<a class="code" href="classGeno__ftest.html#a2">validate</a>(g);
151   printf(<span class="stringliteral">"After validation:\n\t%s\n"</span>,g);
152   free(g);
153   printf(<span class="stringliteral">"...and is YOUR genotype O.K.?\n\n"</span>);
154}
155
156<span class="preprocessor">#endif</span>
157<span class="preprocessor"></span>
158</pre></div>
159<p>
160<hr><address style="align: right;"><small>Generated on Sun Sep 15 00:58:40 2002 for Framsticks GenoFX by
161<a href="http://www.doxygen.org/index.html">
162<img src="doxygen.png" alt="doxygen" align="middle" border=0
163width=110 height=53></a>1.2.17 </small></address>
164</body>
165</html>
Note: See TracBrowser for help on using the repository browser.