1 #ifndef __INST_GEN_WEIGHTED_RANDOM_HPP__
2 #define __INST_GEN_WEIGHTED_RANDOM_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 "inst_gen.hpp"
22 
23 class CInstGeneratorWeightedRandom: public CInstGenerator { /* manages instruction frequency */
24 	public:
25 		CInstGeneratorWeightedRandom(); /* constructor creates default values */
26 		float get_freq_hint(const OPCODE opcode);
27 		void set_freq_hint(const OPCODE opcode,const float val);
28 		// actual query points
29 		virtual void suggest_instruction(insn_t &instruction,const MODE mode); /* this uses the freq_hint table to pick a random opcode */
30 		// and saving
31 		virtual void write_ini(std::ostream &os);
32 		virtual void write_override_ini(std::ostream &os,const CInstGenerator *parent);
33 	protected:
34 		static const int
35 			OPCODE_FREQ_HINT_DEFAULT = 1,
36 			BRANCH_PIVOT_DEFAULT = 1;
37 		bool _branch_bias; /* do we have a bias towards branches being not too adjacent? */
38 		int _branch_pivot; /* this is the number of non-branch instructions at which branch strength is exact */
39 		float _freq_hint[OPCODE_LAST], /* a hint to creating random warriors, on the freq of opcodes expected in this genus */
40 			_freq_hint_sum; /* sum total of all hints in freq_hint table, precalculated as it is used quite often */
41 		// from CInstGenerator
42 		virtual void read_ini_impl(INIFile &ini);
43 		virtual CInstGenerator *read_override_ini_impl(INIFile &ini); /* loads freq values; if any freq values are
44 			specified, then a copy of this freq will be made; all unspecified values in the new
45 			class will be "inherited" from this one, and any specified values will be overriden.  If nothing
46 			is specified, then this will be returned */
47 };
48 
49 #endif // __INST_GEN_WEIGHTED_RANDOM_HPP__
50