1 /* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar'
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 1, or (at your option) any later
6 * version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 #include "fitness.hpp"
19 #include "error.hpp"
20 using namespace std;
21
22 namespace { // anonymous
23
24 const char
25 *CFitness_RESULTS[CFitness::RESULT_LAST] = {
26 "win","tie","lose"},
27 *CFitness_CATS[CFitness::CAT_LAST] = {
28 "attack","defence"};
29
30 } // end of anonymous namespace
31
32 /***** CFitness class implementation ******************/
33
read_ini(INIFile & ini)34 void CFitness::read_ini(INIFile &ini) { /* loads fitness values; values are REQUIRED */
35 int cat, res;
36 string key;
37 KeyValuePair *kvp;
38 for(cat=0; cat<CAT_LAST; ++cat) {
39 for(res=0; res<RESULT_LAST; ++res) {
40 // prepare key name
41 key = MNEMONIC_RESULT((RESULT)res);
42 key += '_';
43 key += MNEMONIC_CAT((CAT)cat);
44 // get it
45 kvp = ini.get(key);
46 if(0 == kvp)
47 PANIC(MISC,"fitness weighting expected",NULL);
48 // set it
49 _data[(CAT)cat][(RESULT)res] = kvp->getValueAsFloat();
50 }
51 }
52 }
53
read_override_ini(INIFile & ini)54 CFitness *CFitness::read_override_ini(INIFile &ini) /* loads fitness values; if any fitness values are
55 specified, then a copy of this fitness will be made; all unspecified values in the new
56 class will be "inherited" from this one, and any specified values will be overriden. If nothing
57 is specified, then this will be returned */ {
58 CFitness *ret = this; // assume it is
59 int cat, res;
60 string key;
61 KeyValuePair *kvp;
62 for(cat=0; cat<CAT_LAST; ++cat) {
63 for(res=0; res<RESULT_LAST; ++res) {
64 // prepare key name
65 key = MNEMONIC_RESULT((RESULT)res);
66 key += '_';
67 key += MNEMONIC_CAT((CAT)cat);
68 // get it
69 kvp = ini.get(key);
70 if(0 == kvp)
71 continue; // quite ok
72 // first time to override?
73 if(this == ret) {
74 ret = new CFitness(*this); // copy it so that all unoverriden values are 'inherited'
75 }
76 // set it
77 ret->_data[(CAT)cat][(RESULT)res] = kvp->getValueAsFloat();
78 }
79 }
80 return ret;
81 }
82
write_ini(ostream & os)83 void CFitness::write_ini(ostream &os) {
84 int cat, res;
85 for(cat=0; cat<CAT_LAST; ++cat) {
86 for(res=0; res<RESULT_LAST; ++res) {
87 os << MNEMONIC_RESULT((RESULT)res) << '_' << MNEMONIC_CAT((CAT)cat)
88 << '=' << _data[(CAT)cat][(RESULT)res] << endl;
89 }
90 }
91 }
92
write_override_ini(ostream & os,const CFitness * parent)93 void CFitness::write_override_ini(ostream &os,const CFitness *parent) {
94 if(this == parent) // nothing overriden?
95 return;
96 int cat, res;
97 for(cat=0; cat<CAT_LAST; ++cat) {
98 for(res=0; res<RESULT_LAST; ++res) {
99 if(parent->_data[(CAT)cat][(RESULT)res] != _data[(CAT)cat][(RESULT)res]) { // overriden?
100 os << MNEMONIC_RESULT((RESULT)res) << '_' << MNEMONIC_CAT((CAT)cat)
101 << '=' << _data[(CAT)cat][(RESULT)res] << endl;
102 }
103 }
104 }
105 }
106
MNEMONIC_RESULT(const CFitness::RESULT res)107 const char *CFitness::MNEMONIC_RESULT(const CFitness::RESULT res) {
108 if(0 >= res < CFitness::RESULT_LAST)
109 return CFitness_RESULTS[res];
110 return 0; // bad
111 }
112
MNEMONIC_CAT(const CFitness::CAT cat)113 const char *CFitness::MNEMONIC_CAT(const CFitness::CAT cat) {
114 if(0 >= cat < CFitness::CAT_LAST)
115 return CFitness_CATS[cat];
116 return 0; // bad
117 }
118