/* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar' * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 1, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "inst_gen_markov.hpp" #include "inst_gen_markov_2.hpp" #include "operand_table.hpp" #include #include #include using namespace std; /******* main code ********************************************/ int Operand(int argc,const char **args) { try { COperandTable *table; const char *lookup_fname; unsigned coresize; if(argc != 2 && argc < 5) { cout << "Usage to analyse rc files:" << endl << " build_freq_tables Operand lookup_filename CORESIZE [file1] .. [filen]" << endl << "Usage to list contents of a lookup file:" << endl << " build_freq_tables Operand lookup_filename" << endl; return 1; } lookup_fname = args[1]; if(argc == 2) { table = new COperandTable(lookup_fname); table->dump(cerr); } else { coresize = atoi(args[2]); if(coresize <= 0 || coresize > 0xFFFF) PANIC(MISC,"invalid coresize",NULL); table = new COperandTable(coresize); for(int i=3; ilearn(args[i]); } table->dump(cerr); table->save(lookup_fname); } } catch(CSpeciesError *se) { cerr << se->desc() << endl; return 1; } return 0; } int Markov2(int argc,const char **args) { try { CInstGeneratorMarkov2 *markov; const char *lookup_fname; unsigned coresize, threshold; if(argc != 2 && argc < 5) { cout << "Usage to analyse rc files:" << endl << " build_freq_tables Markov2 lookup_filename CORESIZE THRESHOLD [file1] .. [filen]" << endl << "Usage to list contents of a lookup file:" << endl << " build_freq_tables Markov2 lookup_filename" << endl; return 1; } lookup_fname = args[1]; if(argc == 2) { markov = new CInstGeneratorMarkov2(lookup_fname); markov->dump(cerr); } else { coresize = atoi(args[2]); if(coresize <= 0 || coresize > 0xFFFF) PANIC(MISC,"invalid coresize",NULL); threshold = atoi(args[3]); if(threshold <= 0 || threshold > 0xFF) PANIC(MISC,"invalid threshold",NULL); markov = new CInstGeneratorMarkov2(coresize); for(int i=4; ilearn(args[i]); } markov->cull(threshold); markov->prepare(); markov->dump(cerr); markov->save(lookup_fname); } } catch(CSpeciesError *se) { cerr << se->desc() << endl; return 1; } return 0; } int Markov(int argc,const char **args) { CInstGeneratorMarkov freq; try { if(argc == 2) { freq.load(args[1]); freq.dump(cout); return 0; } if(argc < 4) { cout << "Usage to analyse rc files:" << endl << " build_freq_tables Markov lookup_filename CORESIZE [file1] .. [filen]" << endl << "Usage to list contents of a lookup file:" << endl << " build_freq_tables Markov lookup_filename" << endl; return 1; } freq.init(atoi(args[2])); for(int i=3; idesc() << endl; return 1; } return 0; } int main(int argc,const char **args) { cout << "Species frequency lookup builder" << endl << "(c) Will \'Varfar\', 2003; this is GPL software" << endl; int ret = 1; if(argc == 1) { cout << "*** mode not specified ***" << endl; ret = 1; } else if(strcasecmp(args[1],"markov")==0) { cout << "*** Markov mode ***" << endl; ret = Markov(argc-1,&args[1]); } else if(strcasecmp(args[1],"markov2")==0) { cout << "*** Markov2 mode ***" << endl; ret = Markov2(argc-1,&args[1]); } else if(strcasecmp(args[1],"operand")==0) { cout << "*** Operand mode ***" << endl; ret = Operand(argc-1,&args[1]); } else { cout << "*** unknown mode \"" << args[1] << "\" ***" << endl; ret = 1; } if(0 != ret) { cout << "Generic usage:" << endl << " build_freq_tables mode" << endl << " where mode is one of:" << endl << " Markov, Markov2, Operand" << endl << " (each mode has mode-specific help if you specify no other arguments)" << endl; } return ret; }