Help - it doesn't work ...

3 отговора 394 прегледа Заключена
#1

Help - it doesn't work ...

______________________________________________________________________
annapp.cpp

/*************************************************************
Name: Full-connected ANN with GA
Author: Todor Balabanov, F19952, INF333
Date: 20.12.2004 10:20
E-mail: teodorgig@mail.ru
Compiler: Borland C++ v3.1 for DOS

New Bulgarian University, Sofia, 2004
**************************************************************/


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>


class Examples{
private:
double **inputs;
double **outputs;
double **calculates;
long eAmount;
long iAmount;
long oAmount;
long cAmount;
public:
Examples();
~Examples();

void load(const char *);
void save(const char *) const;

long getExamplesAmount() const;
long getInputSize() const;
long getOutputSize() const;
const double* getInput(long) const;
const double* getOutput(long) const;
void setCalculated(const double *, long);
};

Examples::Examples()
{
inputs = NULL;
outputs = NULL;
calculates = NULL;
eAmount = 0;
iAmount = 0;
oAmount = 0;
cAmount = 0;
}

Examples::~Examples()
{
if(calculates != NULL)
delete calculates;
if(outputs != NULL)
delete outputs;
if(inputs != NULL)
delete inputs;

eAmount = 0;
iAmount = 0;
oAmount = 0;
cAmount = 0;
}

void Examples::load(const char *name)
{
this->~Examples();

ifstream fin(name, ios::in);

fin>>eAmount;
fin>>iAmount;
fin>>oAmount;
fin>>cAmount;

inputs = new double*[ eAmount ];
outputs = new double*[ eAmount ];
calculates = new double*[ eAmount ];
for(int i=0; i<eAmount; i++)
inputs[ i ] = new double[ iAmount ];
for(i=0; i<eAmount; i++)
outputs[ i ] = new double[ oAmount ];
for(i=0; i<eAmount; i++)
calculates[ i ] = new double[ cAmount ];

for(i=0; i<eAmount; i++)
{
for(int j=0; j<iAmount; j++)
fin>>inputs[ i ][ j ];
for(j=0; j<oAmount; j++)
fin>>outputs[ i ][ j ];
for(j=0; j<cAmount; j++)
fin>>calculates[ i ][ j ];
}

fin.close();
}

void Examples::save(const char *name) const
{
ofstream fout(name, ios::out);

fout<<eAmount<<" ";
fout<<iAmount<<" ";
fout<<oAmount<<" ";
fout<<cAmount<<endl;
for(int i=0; i<eAmount; i++)
{
for(int j=0; j<iAmount; j++)
fout<<inputs[ i ][ j ]<<" ";
fout<<"\t";

for(j=0; j<oAmount; j++)
fout<<outputs[ i ][ j ]<<" ";
fout<<"\t";

for(j=0; j<cAmount; j++)
fout<<calculates[ i ][ j ]<<" ";
fout<<endl;
}

fout.close();
}

long Examples::getExamplesAmount() const
{
return( eAmount );
}

long Examples::getInputSize() const
{
return( iAmount );
}

long Examples::getOutputSize() const
{
return( oAmount );
}

const double* Examples::getInput(long idx) const
{
return( inputs[idx] );
}

const double* Examples::getOutput(long idx) const
{
return( outputs[idx] );
}

void Examples::setCalculated(const double *calcs, long idx)
{
for(int i=0; i<cAmount; i++)
calculates[ idx ][ i ] = calcs[ i ];
}


class GA{
private:
double **population;
double *goodness;
long pSize;
long hLen;

double* rndhrom(double *, const long);
int equals(const double *, const double *, long);
void sort();
public:
GA();
GA(long, long);
~GA();

void load(const char *);
void save(const char *) const;

long getPopulSize() const;

long getHromLen() const;
const double* getHrom(const long) const;
void setHrom(const double *, const long);

double getGoodness(const long) const;
void setGoodness(const double, const long);

void crossover();
void mutate();
void select();
};

double* GA::rndhrom(double *hrom, const long size)
{
const double TOLER = 3.0;
const double PREC = 1000;
for(int i=0; i<size; i++)
hrom[ i ] = random(PREC+1)*(2*TOLER)/PREC-TOLER;

return( hrom );
}

int GA::equals(const double *a, const double *b, long size)
{
for(int i=0; i<size; i++)
if(a != b)
return( 0 );

return( 1 );
}

void GA::sort()
{
for(int i=0; i<2*pSize; i++)
{
long idx = i;
for(int j=idx; j<2*pSize; j++)
if(goodness[j] < goodness[idx])
idx = j;

double temp = goodness[ i ];
goodness[ i ] = goodness[ idx ];
goodness[ idx ] = temp;

double *buff = new double[ hLen ];
for(j=0; j<hLen; j++)
buff[ j ] = population[ i ][ j ];
for(j=0; j<hLen; j++)
population[ i ][ j ] = population[ idx ][ j ];
for(j=0; j<hLen; j++)
population[ idx ][ j ] = buff[ j ];
delete buff;
}
}

GA::GA()
{
population = NULL;
goodness = NULL;
pSize = 0;
hLen = 0;
}

GA::GA(long pSize, long hLen)
{
population = new double*[ 2*pSize ];
for(int i=0; i<2*pSize; i++)
population[ i ] = NULL;

for(i=0; i<2*pSize; i++)
population[ i ] = new double[ hLen ];

goodness = new double[ 2*pSize ];

this->pSize = pSize;
this->hLen = hLen;
}

GA::~GA()
{
if(population != NULL)
{
for(int i=0; i<2*pSize; i++)
if(population != NULL)
delete population[ i ];

delete population;
}

if(goodness != NULL)
delete goodness;


pSize = 0;
hLen = 0;
}

void GA::load(const char *name)
{
this->~GA();

ifstream fin(name, ios::in);

fin>>pSize;
fin>>hLen;

population = new double*[ 2*pSize ];
for(int i=0; i<2*pSize; i++)
population[ i ] = NULL;

for(i=0; i<2*pSize; i++)
population[ i ] = new double[ hLen ];

goodness = new double[ 2*pSize ];

for(i=0; i<pSize; i++)
{
fin>>goodness[ i ];

for(int j=0; j<hLen; j++)
fin>>population[ i ][ j ];
}

for(i=pSize; i<2*pSize; i++)
{
goodness[ i ] = 0.0;

for(int j=0; j<hLen; j++)
population[ i ][ j ] = 0.0;
}

fin.close();
}

void GA::save(const char *name) const
{
ofstream fout(name, ios::out);

fout<<pSize<<" ";
fout<<hLen<<endl;

for(int i=0; i<pSize; i++)
{
fout<<goodness[ i ]<<"\t";

for(int j=0; j<hLen; j++)
fout<<population[ i ][ j ]<<" ";

fout<<endl;
}

fout.close();
}


long GA::getPopulSize() const
{
return( pSize );
}

long GA::getHromLen() const
{
return( hLen );
}

const double* GA::getHrom(const long idx) const
{
return( population[idx] );
}

void GA::setHrom(const double *hrom, const long idx)
{
for(int i=0; i<hLen; i++)
population[ idx ][ i ] = hrom[ i ];
}

double GA::getGoodness(const long idx) const
{
return( goodness[idx] );
}

void GA::setGoodness(const double goodness, const long idx)
{
this->goodness[ idx ] = goodness;
}

void GA::crossover()
{
for(int i=0; i<pSize; i++)
{
long idx = random( pSize );
long brk = random( hLen );

for(int j=0; j<hLen; j++)
if(j < brk)
population[ pSize+i ][ j ] = population[ i ][ j ];
else
population[ pSize+i ][ j ] = population[ idx ][ j ];
}
}

void GA::mutate()
{
for(int i=0; i<2*pSize; i++)
{
double hi, low;

hi = low = population[ i ][ 0 ];
for(int j=0; j<hLen; j++)
{
if(population[j] > hi)
hi = population[ i ][ j ];
if(population[j] < low)
low = population[ i ][ j ];
}

const double PREC = 1000.0;
const double PERC = 3.0;
hi *= PERC;
low *= PERC;
population[ i ][ random(hLen) ] = random(PREC+1)*(hi-low)/PREC+low;
}

}

void GA::select()
{
sort();

for(int i=0; i<pSize-1; i++)
for(int j=i+1; j<pSize; j++)
if(goodness == goodness[j])
{
rndhrom(population[j], hLen);
goodness[ j ] = 0.0;
}
}


class ANN{
private:
double *inputs;
double *weights;
double *neurons;
double *outputs;
long iAmount;
long wAmount;
long nAmount;
long oAmount;
public:
ANN();
~ANN();

void load(const char *);
void save(const char *) const;
void calculate();

long getInputSize() const;
const double* getInput() const;
void setInput(const double *);

long getWeightsAmount() const;
const double* getWeights() const;
void setWeights(const double *);

long getNeuronsAmount() const;
const double* getNeurons() const;
void setNeurons(const double *);

long getOutputSize() const;
const double* getOutput() const;
void setOutput(const double *);

void add(int);
void remove(int);
};

ANN::ANN()
{
inputs = NULL;
weights = NULL;
neurons = NULL;
outputs = NULL;
iAmount = 0;
wAmount = 0;
nAmount = 0;
oAmount = 0;
}

ANN::~ANN()
{
if(outputs != NULL)
delete outputs;
if(neurons != NULL)
delete neurons;
if(weights != NULL)
delete weights;
if(inputs != NULL)
delete inputs;

iAmount = 0;
wAmount = 0;
nAmount = 0;
oAmount = 0;
}

void ANN::load(const char *name)
{
this->~ANN();

ifstream fin(name, ios::in);

fin>>iAmount;
inputs = new double[ iAmount ];
for(int i=0; i<iAmount; i++)
fin>>inputs[ i ];

fin>>oAmount;
outputs = new double[ oAmount ];
for(i=0; i<oAmount; i++)
fin>>outputs[ i ];

fin>>wAmount;
weights = new double[ wAmount ];
for(i=0; i<wAmount; i++)
fin>>weights[ i ];

fin>>nAmount;
neurons = new double[ nAmount ];
for(i=0; i<nAmount; i++)
fin>>neurons[ i ];

fin.close();
}

void ANN::save(const char *name) const
{
ofstream fout(name, ios::out);

fout<<iAmount<<" ";
for(int i=0; i<iAmount; i++)
fout<<inputs[ i ]<<" ";
fout<<"\t";

fout<<oAmount<<" ";
for(i=0; i<oAmount; i++)
fout<<outputs[ i ]<<" ";
fout<<"\t";

fout<<wAmount<<" ";
for(i=0; i<wAmount; i++)
fout<<weights[ i ]<<" ";
fout<<"\t";

fout<<nAmount<<" ";
for(i=0; i<nAmount; i++)
fout<<neurons[ i ]<<" ";
fout<<endl;

fout.close();
}

void ANN::calculate()
{
double *temp = new double[ nAmount ];

for(int i=0; i<nAmount; i++)
{
double sum = 0.0;

for(int j=0; j<iAmount; j++)
sum += inputs[ j ]*weights[ i*(iAmount+nAmount+1)+j ];
for(j=0; j<nAmount; j++)
sum += neurons[ j ]*weights[ i*(iAmount+nAmount+1)+iAmount+j ];
sum += 1.0*weights[ i*(iAmount+nAmount+1)+iAmount+nAmount ];

/*
temp[ i ] = 1.0/(1.0 + exp(-sum));
/**/

/**/
if(sum < -2.7)
neurons[ i ] = 0.0;
else if(sum >= 2.7)
neurons[ i ] = 1.0;
else
{
neurons[ i ] = 0.185185185185185*sum + 0.5;
}
/**/
}

for(i=0; i<nAmount; i++)
neurons[ i ] = temp[ i ];

for(i=0; i<oAmount; i++)
outputs[ i ] = neurons[ i ];

delete temp;
}

long ANN::getInputSize() const
{
return( iAmount );
}

const double* ANN::getInput() const
{
return( inputs );
}

void ANN::setInput(const double *inputs)
{
for(int i=0; i<iAmount; i++)
this->inputs[ i ] = inputs[ i ];
}

long ANN::getWeightsAmount() const
{
return( wAmount );
}

const double* ANN::getWeights() const
{
return( weights );
}

void ANN::setWeights(const double *weights)
{
for(int i=0; i<wAmount; i++)
this->weights[ i ] = weights[ i ];
}

long ANN::getNeuronsAmount() const
{
return( nAmount );
}

const double* ANN::getNeurons() const
{
return( neurons );
}

void ANN::setNeurons(const double *neurons)
{
for(int i=0; i<nAmount; i++)
this->neurons[ i ] = neurons[ i ];
}

long ANN::getOutputSize() const
{
return( oAmount );
}

const double* ANN::getOutput() const
{
return( outputs );
}

void ANN::setOutput(const double *outputs)
{
for(int i=0; i<oAmount; i++)
this->outputs[ i ] = outputs[ i ];
}

void ANN::add(int type)
{
//Add neuron, input, output ...
if(type == 0)
{
}
else if(type == 1)
{
}
else if(type == 2)
{
}
}

void ANN::remove(int type)
{
//Remove neuron, input, output ...
if(type == 0)
{
}
else if(type == 1)
{
}
else if(type == 2)
{
}
}


class Trainer{
private:
ANN *ann;
GA *ga;
Examples *ex;

void load();
void save() const;
public:
Trainer();
~Trainer();

void train();
};

void Trainer::load()
{
ann->load( "netl.txt" );
ex->load( "exmpl.txt" );
ga->load( "gal.txt" );
}

void Trainer::save() const
{
ga->save( "gas.txt" );
ex->save( "exmps.txt" );
ann->save( "nets.txt" );
}


Trainer::Trainer()
{
ann = new ANN();
ga = new GA();
ex = new Examples();
}

Trainer::~Trainer()
{
if(ex != NULL)
delete ex;
if(ga != NULL)
delete ga;
if(ann != NULL)
delete ann;
}

void Trainer::train()
{
const double PERC = 0.1;
load();

if(ex->getInputSize()==ann->getInputSize() && ex->getOutputSize()==ann->getOutputSize())
{
do
{
for(int i=0; i<2*ga->getPopulSize(); i++)
{
double goodness = 0.0;
int *used = new int[ ex->getExamplesAmount() ];
for(int l=0; l<ex->getExamplesAmount(); l++)
used[ l ] = 0;

for(int j=0; j<ex->getExamplesAmount(); j++)
{
int idx = 0;
do
idx = random( ex->getExamplesAmount() );
while(used[idx] == 1);
used[ idx ] = 1;

ann->setInput( ex->getInput(idx) );
ann->setWeights( ga->getHrom(i) );
ann->calculate();
const double *calced = ann->getOutput();
const double *input = ex->getInput( idx );
const double *output = ex->getOutput( idx );
/**/
//gotoxy(1, 1);
for(l=0; l<ex->getInputSize(); l++)
cout<<input[ l ]<<" ";
cout<<endl;
for(l=0; l<ex->getOutputSize(); l++)
cout<<output[ l ]<<" ";
cout<<endl;
for(l=0; l<ex->getOutputSize(); l++)
cout<<calced[ l ]<<" ";
cout<<endl<<endl;
/**/

double err = 0.0;
for(int k=0; k<ann->getOutputSize()&&k<ex->getOutputSize(); k++)
err += pow(calced[k]-output[k], 2.0);
err = sqrt( err );

goodness += err;
}

goodness /= ex->getExamplesAmount();
ga->setGoodness(goodness, i);

delete used;
}

ga->crossover();
ga->mutate();
ga->select();
}while(ga->getGoodness(0)>PERC && !kbhit());
}

save();
}


class ANNApp{
private:
Trainer *tr;
public:
ANNApp();
~ANNApp();

void run();
};

ANNApp::ANNApp()
{
tr = new Trainer();
}

ANNApp::~ANNApp()
{
if(tr != NULL)
delete tr;
}

void ANNApp::run()
{
tr->train();
}


int main()
{
randomize();
clrscr();

ANNApp *theApp = new ANNApp();
theApp->run();
delete theApp;

cout<<"Press ENTER to exit ..."<<endl;
while(getch() != 13)
;

return( 0 );
}


______________________________________________________________________
exmpl.txt

4 2 1 1
0.0 0.0 0.0 0.0
0.0 1.0 1.0 0.0
1.0 0.0 1.0 0.0
1.0 1.0 0.0 0.0


______________________________________________________________________
gal.txt

18 18
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.0 -1.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 -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 0.0 -1.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 -1.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 -1.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.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 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0

Отговор
в отговор на Todor Balabanov #2

Re: Help - it doesn't work ...

ti nared li si be pich, kvo sia iskash da pusna kompilatora i da ti proveria programata shto ne raboti li, XA XA XA, stooped.

Отговор
в отговор на Todor Balabanov #3

тря'а да си много...

наивен, за да си мислиш, че някой ще ти дебъгва програмата.

Отговор
в отговор на Smile #4

При това

не е сложена в таг 'пре' и i-тата са станали на италик :-)
Темата си е направо за коша. Даже не за катинар. Дай да не я вдигаме повече.

System Doctor Error:
Your girlfriend is pregnant.
(A)bort, (M)arry, (I)gnore?_
Отговор