1#include "../ECF_base.h"
2#include "../FitnessMax.h"
3#include "../ECF_macro.h"
14Classifier::Classifier(XCSParamsP xcsParams,
unsigned long long int time, IndividualP ind, StateP state){
16 this->xcsParams = xcsParams;
17 params = boost::dynamic_pointer_cast<ClassifierParams> (ind->getGenotype(3));
20 this->ind->fitness =
static_cast<FitnessP
> (
new FitnessMax);
21 setFitness(xcsParams->initF_);
25 BitStringP dontCareBits = getDontCareBitString();
26 for (uint i = 0; i < dontCareBits->bits.size(); ++i){
27 double rnd = state->getRandomizer()->getRandomDouble();
28 if (rnd < xcsParams->pdc_)
29 dontCareBits->bits[i] =
true;
31 dontCareBits->bits[i] =
false;
36Classifier::Classifier(ClassifierP cl){
38 xcsParams = cl->xcsParams;
39 ind =
static_cast<IndividualP
> (cl->ind->copy());
40 params = boost::dynamic_pointer_cast<ClassifierParams> (ind->getGenotype(3));
45bool Classifier::checkState(
const StateP state) {
48 if(state->getGenotypes().size() < 3) {
49 ECF_LOG_ERROR(state,
"Error: XCS algorithm requires min. 3 genotypes!");
55 if(state->getGenotypes()[0]->getName() != bitstring->getName()
56 || state->getGenotypes()[1]->getName() != bitstring->getName()) {
57 ECF_LOG_ERROR(state,
"Error: XCS algorithm accepts only BitString::BitString genotype as input!");
65bool Classifier::doesMatch(
const GenotypeP inp) {
67 BitStringP input = boost::static_pointer_cast<BitString::BitString> (inp);
69 BitStringP ruleBits = boost::static_pointer_cast<BitString::BitString> (ind->getGenotype(0));
70 BitStringP dontCareBits = boost::static_pointer_cast<BitString::BitString> (ind->getGenotype(1));
72 for (uint i = 0; i < input->bits.size(); i++){
73 if (dontCareBits->bits[i] )
continue;
74 if (ruleBits->bits[i] != input->bits[i])
return false;
79void Classifier::cover (std::set<int> actions,
const GenotypeP input, StateP state){
80 ind->at(0) =
static_cast<GenotypeP
> (input->copy());
81 BitStringP action = boost::dynamic_pointer_cast<BitString::BitString> (getAction());
84 action->initialize(state);
85 }
while (actions.find(getActionId()) != actions.end());
90void Classifier::print(){
92 std::cout <<
" [" << ind->index <<
"] ";
93 printRuleString (getRuleBitString(),getDontCareBitString());
96 printBitString (boost::dynamic_pointer_cast<BitString::BitString> (getAction()) );
98 std::cout <<
" ("<< getError() <<
", " << getPrediction() <<
", "<< getFitness() <<
")";
99 std::cout <<
"\t[ " << getNumerosity() <<
", " << getActSetSize() <<
", " << getExperience() <<
", " << getTimeStamp() <<
" ]";
100 std::cout << std::endl;
103void Classifier::printRuleString (
const BitStringP bString,
const BitStringP hashString) {
106 for (uint i = 0; i < bString->bits.size(); i++){
107 if (hashString != NULL && hashString->bits[i]) sign =
'#';
108 else sign = bString->bits[i] ?
'1':
'0';
114void Classifier::printBitString (
const BitStringP bString) {
116 for (uint i = 0; i < bString->bits.size(); i++){
117 std::cout << bString->bits[i] ?
'1':
'0';
125int Classifier::getActionId(){
128 BitStringP bstring = boost::dynamic_pointer_cast<BitString::BitString> (ind->getGenotype(2));
136 for (uint i =0; i < bstring->bits.size(); ++i){
137 if (bstring->bits[i]) ret += add;
144GenotypeP Classifier::getAction(){
145 return ind->getGenotype(2);
147void Classifier::setAction(GenotypeP action){
148 ind->at(2) =
static_cast<GenotypeP
> (action->copy());
152#pragma region Parameters setters
154void Classifier::setError(
double eps){
158void Classifier::setPrediction(
double p){
162void Classifier::setFitness(
double F){
163 ind->fitness->setValue(F);
166void Classifier::setTimeStamp(
unsigned long long int ts) {
170void Classifier::setNumerosity(
int num) {
174void Classifier::setActSetSize(
double as) {
178void Classifier::setExperience(
double exp) {
184#pragma region Parameters getters
186double Classifier::getError(){
190double Classifier::getPrediction(){
194double Classifier::getFitness(){
195 return ind->fitness->getValue();
198unsigned long long int Classifier::getTimeStamp() {
202int Classifier::getNumerosity(){
206double Classifier::getActSetSize(){
210double Classifier::getExperience(){
216double Classifier::getDeletionVote(
double avFit){
217 double vote = getActSetSize() * getNumerosity();
218 if (getExperience() > xcsParams->thresholdDel_ &&
219 getFitness() / getNumerosity() < xcsParams->delta_ * avFit){
220 vote = vote * avFit / (getFitness() / getNumerosity());
224bool Classifier::couldSubsume(){
225 double exp = getExperience();
226 double eps = getError();
227 if (exp > xcsParams->thresholdSub_ && eps < xcsParams->eps0_){
233int Classifier::numOfDCBits(){
235 BitStringP dcbstring = getDontCareBitString();
236 for (uint i = 0; i < dcbstring->bits.size(); ++i){
237 if (dcbstring->bits[i]) ++num;
241bool Classifier::isMoreGeneral(ClassifierP cl){
242 if (numOfDCBits() <= cl->numOfDCBits())
return false;
244 BitStringP dcGen = getDontCareBitString();
245 BitStringP ruleGen = getRuleBitString();
246 BitStringP ruleSpec = cl->getRuleBitString();
247 int bitsSize = (int) ruleGen->bits.size();
251 if (!dcGen->bits[i] && ruleGen->bits[i] != ruleSpec->bits[i])
254 }
while(i < bitsSize);
259bool Classifier::doesSubsume(ClassifierP cl){
261 if (cl->getActionId() == getActionId()){
263 if (isMoreGeneral(cl)){
272BitStringP Classifier::getRuleBitString(){
273 return boost::dynamic_pointer_cast<BitString::BitString> (ind->getGenotype(0));
276BitStringP Classifier::getDontCareBitString(){
277 return boost::dynamic_pointer_cast<BitString::BitString> (ind->getGenotype(1));
280void Classifier::mutateRule(GenotypeP genInput, StateP state) {
282 double rnd = state->getRandomizer()->getRandomDouble();
283 BitStringP input = boost::dynamic_pointer_cast<BitString::BitString> (genInput);
285 for (uint i= 0; i < input->bits.size(); i++){
286 if (rnd < xcsParams->pMutation_){
287 BitStringP ruleBits = getRuleBitString();
288 BitStringP dontCareBits = getDontCareBitString();
290 if (dontCareBits->bits[i] ==
true){
291 dontCareBits->bits[i] =
false;
292 ruleBits->bits[i] = input->bits[i];
294 dontCareBits->bits[i] =
true;
299void Classifier::mutateAction(StateP state) {
300 double rnd = state->getRandomizer()->getRandomDouble();
301 if (rnd < xcsParams->pMutation_){
302 BitStringP actionBits = boost::dynamic_pointer_cast<BitString::BitString> (getAction());
303 actionBits->initialize(state);
BitString class - implements genotype as a series of bits.
Fitness for maximization problems.