void perturb_emissions( double amount=0.2 ) { assert(amount <= 1.0); assert(amount >= 0.0); typedef typename Algo::probability_type P; typedef LogspaceDouble<P> logspace_t; const size_t N = num_states(*_model); const size_t S = num_symbols(*_model); for( size_t state=0; state < N; ++state) { if( _model->is_silent(state) ) continue; for( size_t symbol=0; symbol<S; ++symbol ) { const P orig = logspace_t(_model->e(state, symbol), true); const P random = _random(); const P perturbed = orig * (1-amount) + random * amount; _model->SetEmissionProbability(state, _model->get_symbol(symbol), perturbed); // accepts probability in real space } } _model->normalize_emissions(); #ifndef NO_TESTS tests::test_emissions_valid_pdf<Algo>(*_model); #endif //NO_TESTS }
int main( int argc , char *argv[] ) { rng_type rng; trainings_data_type c = gpcxx::generate_evenly_spaced_test_data< 3 >( -5.0 , 5.0 + 0.1 , 0.4 , []( double x1 , double x2 , double x3 ) { return 1.0 / ( 1.0 + pow( x1 , -4.0 ) ) + 1.0 / ( 1.0 + pow( x2 , -4.0 ) ) + 1.0 / ( 1.0 + pow( x3 , -4.0 ) ); } ); gpcxx::normalize( c.y ); std::ofstream fout1( "testdata.dat" ); for( size_t i=0 ; i<c.x[0].size() ; ++i ) fout1 << c.y[i] << " " << c.x[0][i] << " " << c.x[1][i] << " " << c.x[2][i] << "\n"; fout1.close(); auto eval = gpcxx::make_static_eval< value_type , symbol_type , eval_context_type >( fusion::make_vector( fusion::make_vector( 'x' , []( eval_context_type const& t ) { return t[0]; } ) , fusion::make_vector( 'y' , []( eval_context_type const& t ) { return t[1]; } ) , fusion::make_vector( 'z' , []( eval_context_type const& t ) { return t[2]; } ) ) , fusion::make_vector( fusion::make_vector( 's' , []( double v ) -> double { return std::sin( v ); } ) , fusion::make_vector( 'c' , []( double v ) -> double { return std::cos( v ); } ) , fusion::make_vector( 'e' , []( double v ) -> double { return std::exp( v ); } ) , fusion::make_vector( 'l' , []( double v ) -> double { return ( std::abs( v ) < 1.0e-20 ) ? log( 1.0e-20 ) : std::log( std::abs( v ) ); } ) ) , fusion::make_vector( fusion::make_vector( '+' , std::plus< double >() ) , fusion::make_vector( '-' , std::minus< double >() ) , fusion::make_vector( '*' , std::multiplies< double >() ) , fusion::make_vector( '/' , std::divides< double >() ) ) ); typedef decltype( eval ) eval_type; typedef eval_type::node_attribute_type node_attribute_type; typedef gpcxx::basic_tree< node_attribute_type > tree_type; typedef std::vector< tree_type > population_type; typedef gpcxx::static_pipeline< population_type , fitness_type , rng_type > evolver_type; size_t population_size = 1000; size_t generation_size = 20; double number_elite = 1; double mutation_rate = 0.0; double crossover_rate = 0.6; double reproduction_rate = 0.3; size_t min_tree_height = 8 , max_tree_height = 8; size_t tournament_size = 15; // generators< rng_type > gen( rng ); auto terminal_gen = eval.get_terminal_symbol_distribution(); auto unary_gen = eval.get_unary_symbol_distribution(); auto binary_gen = eval.get_binary_symbol_distribution(); gpcxx::node_generator< node_attribute_type , rng_type , 3 > node_generator { { 2.0 * double( terminal_gen.num_symbols() ) , 0 , terminal_gen } , { double( unary_gen.num_symbols() ) , 1 , unary_gen } , { double( binary_gen.num_symbols() ) , 2 , binary_gen } }; auto tree_generator = gpcxx::make_ramp( rng , node_generator , min_tree_height , max_tree_height , 0.5 ); evolver_type evolver( number_elite , mutation_rate , crossover_rate , reproduction_rate , rng ); std::vector< double > fitness( population_size , 0.0 ); std::vector< tree_type > population( population_size ); auto fitness_f = gpcxx::regression_fitness< eval_type >( eval ); evolver.mutation_function() = gpcxx::make_mutation( gpcxx::make_simple_mutation_strategy( rng , node_generator ) , gpcxx::make_tournament_selector( rng , tournament_size ) ); evolver.crossover_function() = gpcxx::make_crossover( gpcxx::make_one_point_crossover_strategy( rng , max_tree_height ) , gpcxx::make_tournament_selector( rng , tournament_size ) ); evolver.reproduction_function() = gpcxx::make_reproduce( gpcxx::make_tournament_selector( rng , tournament_size ) ); gpcxx::timer timer; // initialize population with random trees and evaluate fitness timer.restart(); for( size_t i=0 ; i<population.size() ; ++i ) { tree_generator( population[i] ); fitness[i] = fitness_f( population[i] , c ); } std::cout << gpcxx::indent( 0 ) << "Generation time " << timer.seconds() << std::endl; std::cout << gpcxx::indent( 1 ) << "Best individuals" << std::endl << gpcxx::best_individuals( population , fitness , 1 , 10 ) << std::endl; std::cout << gpcxx::indent( 1 ) << "Statistics : " << gpcxx::calc_population_statistics( population ) << std::endl; std::cout << gpcxx::indent( 1 ) << std::endl << std::endl; timer.restart(); for( size_t generation=1 ; generation<=generation_size ; ++generation ) { gpcxx::timer iteration_timer; iteration_timer.restart(); evolver.next_generation( population , fitness ); double evolve_time = iteration_timer.seconds(); iteration_timer.restart(); std::transform( population.begin() , population.end() , fitness.begin() , [&]( tree_type const &t ) { return fitness_f( t , c ); } ); double eval_time = iteration_timer.seconds(); std::cout << gpcxx::indent( 0 ) << "Generation " << generation << std::endl; std::cout << gpcxx::indent( 1 ) << "Evolve time " << evolve_time << std::endl; std::cout << gpcxx::indent( 1 ) << "Eval time " << eval_time << std::endl; std::cout << gpcxx::indent( 1 ) << "Best individuals" << std::endl << gpcxx::best_individuals( population , fitness , 2 , 10 ) << std::endl; std::cout << gpcxx::indent( 1 ) << "Statistics : " << gpcxx::calc_population_statistics( population ) << std::endl << std::endl; } std::cout << "Overall time : " << timer.seconds() << std::endl; return 0; }
std::string direct_modem<G>::description() const { std::ostringstream sout; sout << "GF(" << num_symbols() << ") Modulation"; return sout.str(); }
int main(int argc, char *argv[]) { if(argc < 3) { fprintf(stderr, "usage: dyldsymboltool obj1 [obj2 ...] outfile\n"); exit(-1); } symbols_dylib_t dylib; symbolList_t* symbols = NULL; uint32_t start_addr = 0; int i; for(i = 1; i < argc-1; i++) { char line[256]; char* command = malloc(strlen(argv[1]) + sizeof("nm -g ")); FILE *fpipe; // Parse boot.sys (arg1) to get symtab sprintf(command, "nm -g %s", argv[i]); // TODO: read boot.sym directly, no need for nm if ( !(fpipe = (FILE*)popen(command,"r")) ) { // If fpipe is NULL perror("Problems with pipe"); exit(1); } while ( fgets( line, sizeof line, fpipe)) { if((strlen(line) < strlen(argv[i]) || strncmp(line, argv[i], strlen(argv[i])) != 0) && line[0] != ' ') { uint32_t address = 0; char* addr = strtok(line, " "); strtok(NULL, " "); char* name = strtok(NULL, " "); name[strlen(name)-1] = 0; // remove newline sscanf(addr, "%x", &address); if(strcmp(name, VOID_SYMBOL) == 0) { start_addr = address; } if(strcmp(name, START_SYMBOL) == 0) { if(!start_addr) start_addr = address; } else add_symbol(&symbols, name, address); } } pclose(fpipe); free(command); } if(start_addr == 0) { fprintf(stderr, "Unable to locate Symbol.dylib start function\n"); //exit(1); } else { add_symbol(&symbols, START_SYMBOL, start_addr); } /* Header command info */ dylib.header.ncmds = 2; dylib.header.sizeofcmds = sizeof(dylib) - sizeof(struct mach_header);// + dylib.symtab.nsyms * sizeof(struct nlist) + dylib.symtab.strsize; dylib.header.magic = MH_MAGIC; dylib.header.cputype = CPU_TYPE_X86; dylib.header.cpusubtype = /*CPUSUBTYPE_I386*/ 3; dylib.header.filetype = MH_DYLIB; dylib.header.flags = MH_NOUNDEFS | MH_DYLDLINK | MH_NO_REEXPORTED_DYLIBS; /* Load Commands - dylib id */ dylib.dylib_info.cmd = LC_ID_DYLIB; dylib.dylib_info.cmdsize = sizeof(struct dylib_command) + sizeof(dylib.module_name); // todo: verify dylib.dylib_info.dylib.name.offset = sizeof(struct dylib_command); dylib.dylib_info.dylib.timestamp = 0; // TODO: populate with time dylib.dylib_info.dylib.current_version = 0; // TODO dylib.dylib_info.dylib.compatibility_version = 0; // TODO //int offset = dylib.dylib_info.cmdsize%4 ? 4 - (dylib.dylib_info.cmdsize % 4) : 0; //dylib.dylib_info.cmdsize += offset; //dylib.header.sizeofcmds += offset; sprintf(dylib.module_name, "%s", DYLIB_NAME); /* Load Commands - Symtable */ dylib.symtab.cmd = LC_SYMTAB; dylib.symtab.symoff = sizeof(dylib); dylib.symtab.nsyms = num_symbols(symbols); dylib.symtab.stroff = sizeof(dylib) + dylib.symtab.nsyms * sizeof(struct nlist); dylib.symtab.strsize = string_size(symbols); dylib.symtab.cmdsize = sizeof(struct symtab_command); FILE* outfile = fopen(argv[argc-1], "w"); fwrite(&dylib, sizeof(dylib) /* Sizeof header + module name */ , 1, outfile); char* symtab = malloc(dylib.symtab.stroff + dylib.symtab.strsize - sizeof(dylib) + 1); // Add extra 1 for last symbol bzero(symtab, dylib.symtab.nsyms * sizeof(struct nlist) + dylib.symtab.strsize + 1); char* orig = symtab; //symtab += offset; while(symbols) { ((struct nlist*)symtab)->n_un.n_strx = symbols->pos; ((struct nlist*)symtab)->n_type = 0xF; // TODO: read from boot.sys ((struct nlist*)symtab)->n_sect = 0; ((struct nlist*)symtab)->n_desc = REFERENCE_FLAG_DEFINED; ((struct nlist*)symtab)->n_value = (uint32_t)symbols->addr; symtab+= sizeof(struct nlist); strcpy(orig + dylib.symtab.stroff - sizeof(dylib) + symbols->pos, symbols->name); symbols = symbols->next; } fwrite(orig, dylib.symtab.stroff + // Sizeof symbol nlists dylib.symtab.strsize - sizeof(dylib) + 1 // sizeof symbol strings , 1, outfile); fclose(outfile); exit(0); }