1 #ifndef __CReproduction_HPP__
2 #define __CReproduction_HPP__
3 
4 /* "Species" - a CoreWars evolver.  Copyright (C) 2003 'Varfar'
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 1, or (at your option) any later
9  * version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "ini.hpp"
22 #include <iostream>
23 
24 // a forward
25 class CWarrior;
26 class CChromosome;
27 
28 class CReproduction { /* records the stats used for during breeding */
29 	//TODO: subclasses with factories?
30 	public:
31 		typedef float NUM;
32 		CReproduction();
33 		void read_ini(INIFile &ini);
34 		CReproduction *read_override_ini(INIFile &ini); /* loads values; if any values are
35 			specified, then a copy of this reproduction will be made; all unspecified values in the new
36 			class will be "inherited" from this one, and any specified values will be overriden.  If nothing
37 			is specified, then this will be returned */
38 		void write_ini(std::ostream &os);
39 		void write_override_ini(std::ostream &os,const CReproduction *parent);
40 		bool survive() const; // should a member of the current generation survive? prob controlled by _survive
41 		/* generates a completely random warrior using instruction biases in genus */
42 		void generateCompletelyRandom(CChromosome &chromosome);
43 		/* generates a new chromosome based upon two parents; despite the param naming, warriors are not sexed.
44 		   chromosome stub must have been created (but contents are wiped) */
45 		void breed(CChromosome &chromosome,const CChromosome &mother,const CChromosome &father);
46 		static bool fit_enough(CWarrior &warrior);
47 	protected:
48 		enum CROSSOVER { // what type of crossover are we using?
49 			CROSSOVER_RANDOM,
50 			CROSSOVER_LAST
51 		} _crossover; // what type of crossover strategy are we using?
52 		enum MUTATION { // where should mutation occur?
53 			MUT_NONE,
54 			MUT_OPCODE,
55 			MUT_MODIFIER,
56 			MUT_ADDRMODE_A,
57 			MUT_ADDRMODE_B,
58 			MUT_OPERAND_A,
59 			MUT_OPERAND_B,
60 			MUTATION_LAST
61 		};
62 		enum RANDOM_COMPONENT {
63 			RND_NONE,
64 			RND_CROSSOVER,
65 			RND_INSERT,
66 			RND_REPLACE,
67 			RND_DELETE,
68 			RANDOM_COMPONENT_LAST
69 		};
70 		NUM	_crossover_random[RANDOM_COMPONENT_LAST], // probability of crossover (used if CROSSOVER_RANDOM)
71 			_crossover_random_sum, // used if CROSSOVER_RANDOM
72 			_dat_padding, // percentatge of warrior that can be dat padding (used if CROSSOVER_BRANCH)
73 			_survive, // probability that a warrior will survive
74 			_mutation[MUTATION_LAST], // roulette-wheel of where mutation will occur
75 			_mutation_sum; // sum mutation
76 		static const char
77 			*MUTATION_DESCS[MUTATION_LAST],
78 			*mutation_desc(const MUTATION mutation),
79 			*RANDOM_COMPONENT_DESCS[RANDOM_COMPONENT_LAST],
80 			*random_component_desc(const RANDOM_COMPONENT component),
81 			*CROSSOVER_DESCS[CROSSOVER_LAST],
82 			*crossover_desc(const CROSSOVER crossover);
83 		bool _fitness_bias,
84 			_adjust_addressing; // used by crossover_random to insert instructions smoothly
85 		// lookups
86 		static CROSSOVER crossover(const char *desc);
87 		// prob checks
88 		RANDOM_COMPONENT crossover_random() const; // should there be something? prob controlled by _crossover_random[]
89 		MUTATION mutation() const; // should there be mutation? prob controlled _mutation[]
90 		// shared routines used by breeding algorithms
91 		void mutate(CChromosome &chromosome,const int i) const; // apply mutation to an instruction in a gene
92 		// actual crossover implementations
93 		void breed_random(CChromosome &chromosome,const CChromosome &mother,const CChromosome &father);
94 };
95 
96 #endif // __CReproduction_HPP__
97 
98