Code:
// Learning Classifier Systems YCS on Protein secondary structure
#define POPSize 27000
#define AAlgh 186
#define PWild 50 // 90% to high 80-85 works
#include <time.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int Pay = 10, Err = 10, Niche = 10;
int Initialize(int POPinit[POPSize][AAlgh])
{
// create a Population of classifiers representing 9 amino acid units
// each unit is 20 int long to represent the 20 different amino acids
// eg: [00001000000000000000] = AA 5
// each classifier contains a Payoff prediction, Error prediction, Niche size
// estimate and fitness tagged on to the end.
// Fill Pop
int i; int j; int AminoAcid;
char check = 'Y';
for (i = 0; i < POPSize; i++)
{
for ( j = 0; j < AAlgh-6; j++)
{
if (check != 'N')
{
AminoAcid = (j + rand()%20); // random number 0-19, pick amino acid
check = 'N';
for (j; j < AminoAcid; j++) // fill up to AA
{
POPinit[i][j] = 7;
}
POPinit[i][j] = 1;
}
else
{
POPinit[i][j] = 0;
}
if (j == 19 || j == 39 || j == 59 || j == 79 || j == 99 || j == 119
|| j == 139 || j == 159 || j == 179)
// Let rule fill up with 0's until it reachs end
{
check = 'Y';
}
else
{
//do nothing
}
}
check = 'Y';
}
// edit each rule with generality '#' also add Parameter initialisation
for ( i = 0; i < POPSize; i++)
{
for (j = 0; j < AAlgh-6; j++)
{
if ( POPinit[i][j] != 1)
{
if (rand()%100 > PWild)
{
POPinit[i][j] = 4;
}
}
cout << POPinit[i][j];
}
cout << endl;
// AAlgh -6 is nothing so far
POPinit[i][AAlgh-5] = rand()%3+1; // Action
POPinit[i][AAlgh-4] = 10; // Payoff
POPinit[i][AAlgh-3] = 10; // Error
POPinit[i][AAlgh-2] = 10; // Niche
POPinit[i][AAlgh-1] = 0; // Fitness
}
cin.get();
}
// PERFORMANCE
int MatchList(int POPinit[POPSize][AAlgh], int Mlist[100][AAlgh], int &M)
{
// Get input from environment then scan population and form matchlist
int i, j, l, c = 0;
int matched;
char covercheck = 'Y';
int Env[180] = {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0};
for (i = 0; i < POPSize; i++)
{
matched = 0;
for (j = 0; j < AAlgh-6; j++) // only need to scan Amino Acids
{
if (Env[j] == 1 && (POPinit[i][j] == 1 || POPinit[i][j] == 4))
{
matched++;
if (matched == 9) // make sure all nine AA in condition match
{
for (l = 0; l < AAlgh; l++) // need to copy whole rule
{
Mlist[M][c] = POPinit[i][l];
c++;
covercheck = 'N';
}
M++;
c = 0;
matched = 0; // clear match for next run
}
}
else
{
// do nothing
}
}
}
// Covering operator if no match to input, new classifier for each Output ie 3
if (covercheck != 'N')
{
for (i = 0; i < 3; i++)
{
for ( j = 0; j < AAlgh-6; j++) // only need to fill amino acid
{
if(rand()%100 > PWild)
{
if (Env[j] != 1)
{
Mlist[0][j] = 4; // Add # to string
}
else
{
Mlist[0][j] = 1;
}
}
else
{
Mlist[0][j] = Env[j];
}
}
// add action + other parameters
Mlist[0][AAlgh-5] = 1; // add action
Mlist[1][AAlgh-5] = 2; // add action
Mlist[2][AAlgh-5] = 3; // add action
Mlist[i][AAlgh-4] = 10; // Payoff
Mlist[i][AAlgh-3] = 10; // Error
Mlist[i][AAlgh-2] = 10; // Niche
Mlist[i][AAlgh-1] = 0; // Fitness
M++;
}
}
cout << "filled"<< endl;
cout << M;
cin.get();
for (i = 0; i < M; i++)
{
for ( j = 0; j < AAlgh; j++)
{
cout << Mlist[i][j];
}
cout << endl;
}
cout << Mlist[0][AAlgh-5] << endl;
cout << Mlist[0][AAlgh-4] << endl;
cout << Mlist[0][AAlgh-3] << endl;
cout << Mlist[0][AAlgh-2] << endl;
cout << Mlist[0][AAlgh-1] << endl;
cout << "Mlist" << endl;
cin.get();
}
int ActionSelection(int &ActSel) // explore/exploit scheme
{
cout << "Action Selection" << endl;
cin.get();
//Explore
if (ActSel == 1)
{
ActSel = 0;
}
else // Exploit
{
ActSel = 1;
}
}
int ActionList(int &M, int &A, int Alist[100][AAlgh], int Mlist[100][AAlgh], int &ActSel, int &ActChoice)
{
int index, i, j;
int PopSort[AAlgh];
A = 0;
cout << "Action List" << endl;
cin.get();
cout << Mlist[0][AAlgh-5] << endl;
cout << Mlist[0][AAlgh-4] << endl;
cout << Mlist[0][AAlgh-3] << endl;
cout << Mlist[0][AAlgh-2] << endl;
cout << Mlist[0][AAlgh-1] << endl;
cin.get();
if (ActSel == 1)
{
ActChoice = rand()%3+1; // ie 1-3
}
else // choose highest payoff using Bubble sort
{
for (i = 0; i < M; i++)
{
for (index = 0; index < M-1; index++)
{
if (Mlist[index][AAlgh-4] > Mlist[index+1][AAlgh-4]) // payoff
{
for ( j = 0; j < AAlgh; j++)
{
PopSort[j] = Mlist[index+1][j];
Mlist[index+1][j] = Mlist[index][j];
Mlist[index][j] = PopSort[j];
}
}
}
}
ActChoice = Mlist[M][AAlgh-5]; // pick classifier with highest payoff
}
cout << "Choice made: " << ActChoice;
for (index = 0; index < M; index++) // check through whole matchlist
{
cout << "for loop" << endl;
cout << Mlist[index][AAlgh-5] << endl;
cout << Mlist[index][AAlgh-4] << endl;
cout << Mlist[index][AAlgh-3] << endl;
cout << Mlist[index][AAlgh-2] << endl;
cout << Mlist[index][AAlgh-1] << endl;
cin.get();
switch (Mlist[index][AAlgh-5])
{
case 1: // Scan Mlist and copy all classifiers with action 1
{
if ( ActChoice == 1)
{
for (j = 0; j < AAlgh; j++)
{
Alist[A][j] = Mlist[index][j];
cout << Alist[A][j];
}
A++; // so we know size of Action List
cout << endl;
}
else
{
// do nothing
}
break;
}
case 2: // Scan Mlist and copy all classifiers with action 2
{
if ( ActChoice == 2)
{
for (j = 0; j < AAlgh; j++)
{
Alist[A][j] = Mlist[index][j];
cout << Alist[A][j];
}
A++;
cout << endl;
}
else
{
// do nothing
}
break;
}
case 3: // Scan Mlist and copy all classifiers with action 3
{
if ( ActChoice == 3)
{
for (j = 0; j < AAlgh; j++)
{
Alist[A][j] = Mlist[index][j];
cout << Alist[A][j];
}
A++;
cout << endl;
}
else
{
// do nothing
}
break;
default:
break;
}
}
}
}
int main()
{
srand ( time(NULL) );
static int POPinit[POPSize][AAlgh]; // 180 for AA, 1 for action, 5 for Parameters etc
int Env[AAlgh];
int Mlist[100][AAlgh];
int Alist[100][AAlgh];
int ActSel = 0;
int ActChoice;
int M = 0, A = 0;
Initialize(POPinit);
MatchList(POPinit, Mlist, M);
ActionSelection(ActSel);
ActionList(M, A, Mlist, Alist, ActSel, ActChoice);
cin.get();
}