crossoverbindata.cc

  1 const char *help = "\
  2 progname: crossoverbindata.cc\n\
  3 code2html: This program performs crossing-over on a bindata files.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "DiskXFile.h"
  8 #include "CmdLine.h"
  9 
 10 using namespace Torch;
 11 
 12 int main(int argc, char *argv[])
 13 {
 14 	char *filename_in;
 15 	char *filename_out;
 16 	int cut;
 17 	bool verbose;
 18 	
 19 	CmdLine cmd;
 20 	cmd.setBOption("write log", false);
 21 
 22 	cmd.info(help);
 23 	cmd.addText("\nArguments:");
 24 	cmd.addSCmdArg("filename in", &filename_in, "input bindata filename");
 25 	cmd.addSCmdArg("filename out", &filename_out, "output bindata filename");
 26 	cmd.addText("\nArguments:");
 27 	cmd.addICmdOption("-cut", &cut, 2, "cut");
 28 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 29 	cmd.read(argc, argv);
 30 
 31 	DiskXFile *file = NULL;
 32 	DiskXFile *fileout = NULL;
 33   
 34   	// Reading headers
 35 	int n_patterns;
 36 	int n_inputs;
 37 
 38 	file = new DiskXFile(filename_in, "r");
 39 		
 40 	file->read(&n_patterns, sizeof(int), 1);
 41 	file->read(&n_inputs, sizeof(int), 1);
 42 
 43 	if(verbose)
 44 	{
 45 		print("Reading bindata file (%s)\n", filename_in);
 46 		print("   n_inputs = %d\n", n_inputs);
 47 		print("   n_patterns = %d\n", n_patterns);  
 48 	}
 49 
 50 	if(n_patterns != 2)
 51 		error("n_patterns != 2");
 52 	
 53   	fileout = new DiskXFile(filename_out, "w");
 54   	if(fileout == NULL)
 55 	{
 56       		error("Opening BinData file %s", filename_out);
 57       
 58       		return 1;
 59 	}
 60 
 61 	fileout->write(&n_patterns, sizeof(int), 1);
 62 	fileout->write(&n_inputs, sizeof(int), 1);
 63 
 64 	if(verbose)
 65 	{
 66   		print("\n");  
 67   		print("Writing bindata file :\n");  
 68   		print("   n_inputs : %d\n", n_inputs);
 69   		print("   n_patterns : %d\n", n_patterns);  
 70         }
 71 
 72 	real *input1 = new real [n_inputs];
 73 	real *input2 = new real [n_inputs];
 74 
 75 	real *output1 = new real [n_inputs];
 76 	real *output2 = new real [n_inputs];
 77 
 78 	file->read(input1, sizeof(real), n_inputs);
 79 	file->read(input2, sizeof(real), n_inputs);
 80 
 81 	for(int i = 0 ; i < n_inputs ; i++)
 82 	{
 83 		output1[i] = 0.0;
 84 		output2[i] = 0.0;
 85 	}
 86 	
 87 	for(int i = 0 ; i < cut ; i++)
 88 	{
 89 		output1[i] = input1[i];
 90 		output2[i] = input2[i];
 91 	}
 92 	
 93 	for(int i = cut ; i < n_inputs ; i++)
 94 	{
 95 		output1[i] = input2[i];
 96 		output2[i] = input1[i];
 97 	}
 98 	
 99 	fileout->write(output1, sizeof(real), n_inputs);
100 	fileout->write(output2, sizeof(real), n_inputs);
101 
102 	delete [] input1;
103 	delete [] input2;
104 	delete [] output1;
105 	delete [] output2;
106 	delete fileout;
107 	delete file;
108 
109 	return 0;
110 }
111