~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/math/mathNum_genalgio.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  *
  6  * Redistribution and use in source and binary forms, with or without modification,
  7  * are permitted provided that the following conditions are met:
  8  *
  9  *   . Redistributions of source code must retain the above copyright notice,
 10  *     this list of conditions and the following disclaimer.
 11  *
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  *
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this
 18  *     software without specific prior written permission.
 19  * 
 20  *
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :
 37  * Date    :
 38  * Version :
 39  * CVS Id  :
 40  *
 41  * Author  :  paul.bromiley@man.ac.uk
 42  *
 43  * Notes : ASCII input/ouput functions for the TINA genetic algorithm code.
 44  *
 45  *********
 46 */
 47 /**
 48  *  @file
 49  *  @brief    ASCII input/ouput functions for the TINA genetic algorithm code.
 50  *            PAB 7 / 2 / 2002
 51  *
 52  *
 53  *
 54 */
 55 
 56 #include "mathNum_genalgio.h"
 57 
 58 #if HAVE_CONFIG_H
 59 #include <config.h>
 60 #endif
 61 
 62 #include <tina/sys/sysDef.h>
 63 #include <tina/sys/sysPro.h>
 64 #include <tina/math/math_NumDef.h>
 65 
 66 /*
 67 
 68 #include <sys/param.h>
 69 #include <string.h>
 70 #include <math.h>
 71 #include <stdio.h>
 72 #include <stdlib.h>
 73 #include <time.h>
 74 #include <tina/sys.h>
 75 #include <tina/sysfuncs.h>
 76 #include <tina/vision.h>
 77 #include <tina/visionfuncs.h>
 78 #include <tina/math.h>
 79 #include <tina/mathfuncs.h>
 80 #include <tina/genetic_algorithm.h>
 81 #include <tina/ga_io.h>
 82 */
 83 
 84 /* Global variables for lengths of fvectors in genomes */
 85 
 86 static int dim = 0;
 87 static int no_objectives = 0;
 88 static int no_mutators = 0;
 89 static int no_monitors = 0;
 90 
 91 
 92 void init_io_coords(int dimensionality, int no_objective_functions, int no_mutation_probabilities, int no_mons)
 93 {
 94         /* Init the globals in this file which set the lengths of the fvectors in the genomes */
 95 
 96         dim = dimensionality;
 97         no_objectives = no_objective_functions;
 98         no_mutators = no_mutation_probabilities;
 99         no_monitors = no_mons;
100 }
101 
102 
103 void genome_header_writer(Genome *genome, FILE *f_out_ptr)
104 {
105         /* Write the part of the genome that is not the point list */
106 
107         int i, *search_direction=NULL, *monitor_vector=NULL;
108         double *cost_vector=NULL, *mutator_vector=NULL;
109 
110         fprintf(f_out_ptr, "No_points %d \n", genome->no_points);
111         fprintf(f_out_ptr, "Age %d \n", genome->age);
112         fprintf(f_out_ptr, "Parent_age %d \n", genome->parent_age);
113         fprintf(f_out_ptr, "Extraction_point %d \n", genome->extraction_point);
114 
115         mutator_vector = (double *)genome->mutation_prob_vector;
116         for(i=0; i<no_mutators; i++)
117         {
118                 fprintf(f_out_ptr, "Mutation_prob %f \n", mutator_vector[i]);
119         }
120 
121         cost_vector = (double *)genome->cost_vec;
122         for(i=0; i<no_objectives; i++)
123         {
124                 fprintf(f_out_ptr, "Objective_score %f \n", cost_vector[i]);
125         }
126 
127         search_direction = (int *)genome->search_direction;
128         for(i=0; i<no_objectives; i++)
129         {
130                 fprintf(f_out_ptr, "Search_direction %d \n", search_direction[i]);
131         }
132 
133         monitor_vector = (int *)genome->monitor_vector;
134         for(i=0; i<no_monitors; i++)
135         {
136                 fprintf(f_out_ptr, "Monitor_vector %d \n", monitor_vector[i]);
137         }
138 }
139 
140 
141 void genome_point_list_writer(Genome *genome, FILE *f_out_ptr)
142 {
143         /* Write the point list part of the genome. The weights are not written: this leaves them available
144         for storage by the cost functions. */
145 
146         List *plist=NULL;
147         Match *nptr=NULL;
148         double *point=NULL;
149         int i;
150 
151         for(plist = (List *)genome->point_list; plist!=NULL; plist=plist->next)
152         {
153                 nptr = (Match *)plist->to;
154                 point = (double *)nptr->to1;
155 
156                 fprintf(f_out_ptr, "Point ");
157                 for(i=0; i<(dim+1); i++)
158                 {
159                         fprintf(f_out_ptr, " %f ", point[i]);
160                 }
161                 fprintf(f_out_ptr, " \n");
162         }
163 }
164 
165 
166 void genome_list_writer(List *genome_list, FILE *f_out_ptr)
167 {
168         /* Generic genome list writer: handles lists of any length and so should
169         be able to write both backups of population and extracted set */
170 
171         List *plist=NULL;
172         Genome *genome=NULL;
173         int no_genomes=0;
174 
175         for(plist = genome_list; plist!=NULL; plist = plist->next) no_genomes++;
176 
177         fprintf(f_out_ptr, "No_genomes %d \n", no_genomes);
178         for(plist = genome_list; plist!=NULL; plist = plist->next)
179         {
180                 genome = plist->to;
181                 genome_header_writer(genome, f_out_ptr);
182                 genome_point_list_writer(genome, f_out_ptr);
183         }
184 }
185 
186 
187 void genome_header_reader(Genome *genome, FILE *f_in_ptr)
188 {
189         /* Read the part of the genome that is not the point list */
190 
191         int i, *search_direction = NULL, *monitor_vector=NULL;
192         double *cost_vector=NULL, *mutator_vector=NULL;
193 
194         fscanf(f_in_ptr, "%*s %d", &(genome->no_points));
195         fscanf(f_in_ptr, "%*s %d", &(genome->age));
196         fscanf(f_in_ptr, "%*s %d", &(genome->parent_age));
197         fscanf(f_in_ptr, "%*s %d", &(genome->extraction_point));
198 
199         mutator_vector = dvector_alloc(0, no_mutators);
200         for(i=0; i<no_mutators; i++)
201         {
202                 fscanf(f_in_ptr, "%*s %lf", &(mutator_vector[i]));
203         }
204         genome->mutation_prob_vector = mutator_vector;
205 
206         cost_vector = dvector_alloc(0, no_objectives);
207         for(i=0; i<no_objectives; i++)
208         {
209                 fscanf(f_in_ptr, "%*s %lf", &(cost_vector[i]));
210         }
211         genome->cost_vec = cost_vector;
212 
213         search_direction = ivector_alloc(0, no_objectives);
214         for(i=0; i<no_objectives; i++)
215         {
216                 fscanf(f_in_ptr, "%*s %d", &(search_direction[i]));
217         }
218         genome->search_direction = search_direction;
219 
220         monitor_vector = ivector_alloc(0, no_monitors);
221         for(i=0; i<no_monitors; i++)
222         {
223                 fscanf(f_in_ptr, "%*s %d", &(monitor_vector[i]));
224         }
225         genome->monitor_vector = monitor_vector;
226 }
227 
228 
229 void genome_point_list_reader(Genome *genome, FILE *f_in_ptr)
230 {
231         /* Read the point list part of the genome */
232 
233         List *point_list = NULL;
234         Match *nptr=NULL;
235         double *point=NULL;
236         int i, j;
237 
238         for(i=0; i<(genome->no_points); i++)
239         {
240                 fscanf(f_in_ptr, "%*s");
241                 nptr = match_alloc(i);
242                 point = dvector_alloc(0, (dim+1));
243                 for(j=0; j<(dim+1); j++)
244                 {
245                         fscanf(f_in_ptr, "%lf", &(point[j]));
246                 }
247                 nptr->to1 = point;
248                 nptr->to2 = fvector_alloc(0, 2);
249                 point_list = dd_list_addtoend(point_list, link_alloc(nptr, 1001));
250         }
251         genome->point_list = point_list;
252 }
253 
254 
255 
256 List *genome_list_reader(List *genome_list, FILE *f_in_ptr)
257 {
258         /* Generic genome list reader */
259 
260         int no_genomes;
261         Genome *genome=NULL;
262         int i;
263 
264         fscanf(f_in_ptr, "%*s %d", &no_genomes);
265         for(i=0; i<no_genomes; i++)
266         {
267                 genome = talloc(Genome);
268                 genome_header_reader(genome, f_in_ptr);
269                 genome_point_list_reader(genome, f_in_ptr);
270                 genome_list = dd_list_addtoend(genome_list, link_alloc(genome, 1002));
271         }
272         return genome_list;
273 }
274 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.