1 2 #ifndef KINGDOM_HPP 3 #define KINGDOM_HPP 4 5 /* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar' 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 1, or (at your option) any later 10 * version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 #include "species.hpp" 23 24 class CKingdomFile { 25 friend class CKingdom; 26 public: 27 CKingdomFile(); 28 protected: 29 std::string _filename; 30 FILE *_f; 31 void writeKingdom(const CGeneration::NUM generation,const CWarrior::TUid uid); 32 void readKingdom(CGeneration::NUM &generation,CWarrior::TUid &uid); 33 }; 34 35 class CKingdom { 36 public: 37 CKingdom(); 38 ~CKingdom(); 39 void load(const bool runnable = true); // if false, a lazy load is allowed 40 void save(); 41 void clear(); /* clears all the genera */ in_experiment() const42 bool in_experiment() const { return _in_experiment; } size() const43 int size() const { return _num_genera; } coresize() const44 int coresize() const { return _coresize; } processes() const45 int processes() const { return _processes; } cycles() const46 int cycles() const { return _cycles; } minsep() const47 int minsep() const { return _minsep; } rounds() const48 int rounds() const { return _rounds; } pspacesize() const49 int pspacesize() const { return _pspacesize; } TUid_NEXT()50 CWarrior::TUid TUid_NEXT() { return ++_uid; } TUid_CUR() const51 CWarrior::TUid TUid_CUR() const { return _uid; } generation() const52 CGeneration::NUM generation() const { return _generation; } 53 void set_generation(const CGeneration::NUM num); num_genera() const54 int num_genera() const { return _num_genera; } 55 int num_evolving_species() const; 56 int num_evolved_warriors() const; 57 int num_warriors() const; /* returns a trivia number of warriors 'live' in the kingdom at this time */ 58 void fight(); /* this will actually do all the fights for a generation */ fitness() const59 CFitness *fitness() const { return _fitness; } length() const60 CLength *length() const { return _length; } freq() const61 CInstGenerator *freq() const { return _freq; } operands() const62 COperand *operands() const { return _operands; } reproduction() const63 CReproduction *reproduction() const { return _reproduction; } benchmark() const64 CSpecies *benchmark() const { return _benchmark_species; } benchmark_n() const65 float benchmark_n() const { return _benchmark_n; } benchmark_candidate() const66 int benchmark_candidate() const { return _benchmark_candidate; } 67 void core_check() const; hill() const68 const std::string &hill() const { return _hill; } author() const69 const std::string &author() const { return _author; } email() const70 const std::string &email() const { return _email; } 71 CWarrior *warrior(const CWarrior::TUid uid) const; /* fetch a particular warrior from this kingdom; NULL if not present */ 72 // array index accessors 73 CGenus &genus(unsigned index) const; 74 protected: 75 bool _in_experiment; 76 CFitness *_fitness; 77 CLength *_length; 78 CInstGenerator *_freq; 79 COperand *_operands; 80 CReproduction *_reproduction; GEN_NEXT()81 int GEN_NEXT() { return ++_generation; } 82 CGeneration::NUM _generation; 83 CWarrior::TUid _uid; 84 int _num_genera; 85 CGenus **_genus, 86 *_benchmark_genus; 87 CSpecies *_benchmark_species; 88 int _benchmark_candidate; 89 float _benchmark_n; 90 std::string _hill, 91 _author, 92 _email; 93 bool _mars_prepared; 94 void alloc_mars(); 95 void dealloc_mars(); 96 insn_t *_core; /* the actual mars core */ 97 int /* key core metrics */ 98 _coresize, /* size of core */ 99 _processes, /* max. number of processes per warrior */ 100 _cycles, /* cycles until tie */ 101 _rounds, /* rounds per match */ 102 _minsep, /* minimum separation between warriors */ 103 _pspacesize; /* size of pspace */ 104 CKingdomFile *_kingdom_file; 105 void read_ini(INIFile &ini); 106 void write_ini(std::ostream &os); 107 }; 108 109 #endif // ifdef KINGDOM_HPP 110