Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

random_selection.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003-2005 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <time.h>
00013 #include <phission.h>
00014 
00015 #ifdef __cplusplus
00016 extern "C" {
00017 #endif
00018 
00019 #include <random_selection.h>
00020 
00021 /* ------------------------------------------------------------------------ */
00022 /* Create random indexes for the training and test sets.
00023  * Then create randomized index arrays for each member of the set.
00024  * The train set starts at [0] in rset_array and the test set starts
00025  * at [train_set_size] 
00026  * The rindex location also holds all the indecies into the train and
00027  * test sets,
00028  */
00029 /* ------------------------------------------------------------------------ */
00030 int random_selection_new( image_data_collection data,
00031                           uint32_t              train_set_size,
00032                           uint32_t              test_set_size,
00033                           random_selection_type **info )
00034 {
00035     phFUNCTION("random_selection_new")
00036         
00037     uint32_t i = 0;
00038     uint32_t j = 0;
00039 
00040     uint32_t        *random_flag_array  = NULL;
00041     uint32_t        random_flag_size    = 0;
00042     uint32_t        chosen_set          = 0;
00043     uint32_t        nsets               = 0;
00044     uint32_t        maxsets             = 0;
00045     image_data_set  img                 = NULL;
00046     uint32_t        nfiles              = 0;
00047     uint32_t        looped              = 0;
00048 
00049     if (info == NULL) return phFAIL;
00050 
00051     *info = (random_selection_type *)phCalloc(1,sizeof(random_selection_type));
00052     phCHECK_PTR(*info,"phCalloc","phCalloc failed.");
00053     
00054     nsets = idc_get_nsets( data );
00055 
00056     /* find the maximum number of files that any image set has */
00057     maxsets = 0;
00058     for (i = 0; i < nsets; i++)
00059     {
00060         img = idc_get_set( data, i );
00061         nfiles = ids_get_nfiles( img );
00062 
00063         if (nfiles > maxsets) maxsets = nfiles;
00064     }
00065     
00066     (*info)->total_sets         = train_set_size + test_set_size;
00067     (*info)->train_array_count  = train_set_size;
00068     (*info)->test_array_count   = test_set_size;
00069         
00070     /* allocate the set array */
00071     (*info)->rset_array_count = (*info)->total_sets;
00072     phDALLOC((*info)->rset_array,
00073              (*info)->array_size[0],
00074              (*info)->rset_array_count,
00075              uint32_t);
00076     phDALLOC((*info)->rindex_array_size,
00077              (*info)->array_size[1],
00078              (*info)->rset_array_count,
00079              uint32_t);
00080     phDALLOC((*info)->rindex_array_count,
00081              (*info)->array_size[2],
00082              (*info)->rset_array_count,
00083              uint32_t);
00084 
00085     /* allocate the index arrays */
00086     phDALLOC((*info)->rindex_array,
00087              (*info)->array_size[3],
00088              (*info)->total_sets,
00089              uint32_t *);
00090     
00091     srand(time(NULL));
00092 
00093     phDALLOC(random_flag_array,
00094              random_flag_size,
00095              maxsets,
00096              uint32_t);
00097 
00098     /* RANDOMLY CHOOSE THE FILE */
00099     
00100     /* Randomize the set/file selection */
00101     for (i = 0; i < (*info)->total_sets; i++ )
00102     {            
00103         /* choose a set that hasn't been chosen this time around*/
00104         chosen_set = ((uint32_t)rand()) % maxsets;
00105         looped = 0;
00106         while (random_flag_array[chosen_set])
00107         {
00108             chosen_set = ( chosen_set + 1 ) % maxsets;
00109             
00110             if (looped >= maxsets) 
00111             {
00112                 phPROGRESS("WARNING !!! No spaces in random flag array left: %d %d\n",
00113                         i,
00114                         (*info)->total_sets );
00115                 exit(1);
00116             }
00117             looped++;
00118         }
00119         /* mark the set as already chosen */
00120         random_flag_array[chosen_set] = 1;
00121 
00122         (*info)->rset_array[i] = chosen_set;
00123     }
00124 
00125     phDALLOC(random_flag_array,
00126              random_flag_size,
00127              nsets,
00128              uint32_t);
00129                  
00130     /* RANDOMLY CHOOSE THE SET array for each file */
00131     
00132     /* randomize the set index selections */
00133     for (i = 0; i < (*info)->total_sets; i++ )
00134     {
00135         srand(time(NULL));
00136 
00137         phMemset(random_flag_array,0,nsets*sizeof(uint32_t));
00138 
00139         /* allocate the array for the current set indecies */
00140         phDALLOC((*info)->rindex_array[i],
00141                  (*info)->array_size[4],
00142                  nsets,
00143                  uint32_t);
00144         
00145         for (j = 0; j < nsets; j++ )
00146         {
00147             /* choose a set that hasn't been chosen this time around*/
00148             chosen_set = ((uint32_t)rand()) % nsets;
00149             looped = 0;
00150             while (random_flag_array[chosen_set])
00151             {
00152                 chosen_set = ( chosen_set + 1 ) % nsets;
00153             
00154                 if (looped >= nsets) 
00155                 {
00156                     phPROGRESS("WARNING !!! No spaces in random flag array left\n");
00157                     exit(1);
00158                 }
00159                 looped++;
00160             }
00161             /* mark the set as already chosen */
00162             random_flag_array[chosen_set] = 1;
00163 
00164             (*info)->rindex_array[i][j] = chosen_set;
00165         }
00166     }
00167 
00168     phFree(random_flag_array);
00169     
00170     return phSUCCESS;
00171 error:
00172     phFree(random_flag_array);
00173     
00174     return phFAIL;
00175 } 
00176 
00177 /* ------------------------------------------------------------------------ */
00178 int random_selection_free( random_selection_type **info )
00179 {
00180     phFUNCTION("random_selection_free")
00181         
00182     uint32_t i = 0;
00183     uint32_t j = 0;
00184 
00185     if (info == NULL) return phFAIL;
00186     if (*info == NULL) return phSUCCESS;
00187 
00188     phFree((*info)->rset_array);
00189     
00190     for (i = 0; i < (*info)->total_sets; i++ )
00191     {
00192         phFree((*info)->rindex_array[i]);
00193     }
00194         
00195     phFree((*info)->rindex_array);
00196     phFree((*info)->rindex_array_size);
00197     phFree((*info)->rindex_array_count);
00198 
00199     phFree(*info);
00200 
00201     return phSUCCESS;
00202 } 
00203 
00204 #ifdef __cplusplus
00205 }
00206 #endif
00207 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:06 2007 for phission by  doxygen 1.4.4