Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Tue Jul 22, 2014 11:21 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: AI : genetic programming
PostPosted: Wed Jul 09, 2014 11:19 am 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 12
Location: zaanstad NL
this is a try at GP

GP what :
- trying to create a function out of a tabel

GP how :
- [ 1 ] - write a array of programs [ writeprog ]
- [ 2 ] - let programs produce output [ runprog ]
- [ 3 ] - sort programs on fitnes
- [ 4 ] - create child programs from best [ mixprog ]
- [ 5 ] - mutate some child programs [ mutateprog ]
- [ 6 ] - if besterror > wanted and generation > max goto [ 2 ]

it has error's jet
debuging help wil be nice

? :
- how do i save / load prog()
- how do the code sees that the file of prog() exsists

Code:
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <math.h>

using namespace std ;

void split( string& s , char c , vector<string>& v )
{
  string::size_type i = 0 ;
  string::size_type j = s.find( c ) ;

  while ( j != string::npos )
  {
   v.push_back( s.substr( i , j - i ) ) ;
   i = ++j ;
   j = s.find( c , j ) ;
    if ( j == string::npos )
   {
     v.push_back( s.substr( i , s.length() ) ) ;
   }
  }
}


const string _add      = "[ + # # # ]" ;
const string _subtract = "[ - # # # ]" ;
const string _divide   = "[ / # # # ]" ;
const string _multiply = "[ * # # # ]" ;

const string letter[] = { "x" , "y" , "z"
, "d" , "e" , "f" , "g" , "h" , "i" , "j" } ;

int numberPower = 10 ;
unsigned int progLenMax = 200 ;
unsigned int progLenMin = 40 ;
int mutateRate = 50 ;
int groeiRate = 50 ;
int population = 1000 ;
int parents = 100 ;
double in[ 10 ] ;

void wait_key()
{
  string in ;
  cout << "[ push return ]" ;
  cin >> in ;
}
void printSeting() {
  cout << "Setings :\n" ;
  cout << "population = " << population ;
  cout << "parents = " << parents << endl ;
  cout << "number power = " << numberPower << endl ;
  cout << "prog len max = " << progLenMax << endl ;
  cout << "prog len min = " << progLenMin << endl ;
  cout << "mutate rate % = " << mutateRate << endl ;
  cout << "growth rate % = " << groeiRate << endl ;
  wait_key() ;
}
bool isGen( string x )
{
  return x[ 0 ] == '[' ;
}
bool isInput( string x )
{
  int i ;
  for ( i = 0 ; i < 10 ; i++ )
  {
   if ( letter[ i ] == x ) return true ;
  }
  return false ;
}
int input( string x )
{
  int i = 0 ;
  while ( letter[ i ] != x ) i++ ;
  return i ;
}
bool isNumber( string x )
{
  string n = "0123456789" ;
  return n.find( x[ 0 ] ) != string.npos ;
}
string STR( double x )
{
  stringstream ss ( stringstream::in | stringstream::out ) ;
  ss << x ;
  return ss.str() ;
}
double VAL( string x )
{
   return (double)atof( x.c_str() ) ;
}
class CGP {
public :
  CGP() ;
  ~CGP() ;

  string runProg( string prog ) ;
  string writeProg( int hooks ) ;
  string mixProg( string a , string b ) ;
  string mutateProg( string prog ) ;
  void use( string gen ) ;
  void intList() ;
  void dblList() ;
  void setInputMax( unsigned int max ) ;
private :
  string hekjes( string prog ) ;
  string groei( string prog ) ;
  int m_inputMax ;
  string* m_gen ;
  int m_genTel ;
  int m_numberMode ;
} ;
CGP::CGP()
{
  srand( static_cast<unsigned int>( clock() ) ) ;
  m_genTel = 0 ;
  m_gen = new string[ 200 ] ;
}
CGP::~CGP()
{
   delete [] m_gen ;
}
void CGP::intList()
{
  double i ;
  for ( i = 0 ; i < numberPower ; i++ )
  {
   use( STR( floor( pow( 2 , i ) ) ) ) ;
   use( STR( floor( -pow( 2 , i ) ) ) ) ;
  }
}
void CGP::dblList() {
  double i ;
  for ( i = -numberPower ; i < numberPower ; i++ )
  {
   use( STR( pow( 2 , i ) ) ) ;
   use( STR( -pow( 2 , i ) ) ) ;
  }
}
void CGP::use( string gen ) {
  if ( m_genTel < 200 )
  {
   m_gen[ m_genTel ] = gen ;
   m_genTel++ ;
  }
}
string CGP::writeProg( int hooks )
{
  srand( static_cast<unsigned int>( clock() ) ) ;
  string prog = " " , left , right , gen ;
  int dice , hook = 0 , plek ;
  while ( ! isGen( prog ) )
  {
   dice = rand() % m_genTel ;
   prog = m_gen[ dice ] ;
  }
  while ( prog.find( "#" ) != string::npos
  && hook < hooks )
  {
   plek = prog.find( "#" ) ;
    left = prog.substr( 0 , plek ) ;
   right = prog.substr( plek + 1 , prog.length() ) ;
   gen = m_gen[ rand() % m_genTel ] ;
   if ( isGen( gen ) )
   {
     hook++ ;
   }
   left.append( gen ) ;
   left.append( right ) ;
   prog = left ;
  }
  return hekjes( prog ) ;
}
string CGP::hekjes( string prog ) {
  string gen , left , right ;
  int plek ;
  while ( prog.find( "#" ) != string::npos )
  {
   while ( isGen( gen ) )
   {
     gen = m_gen[ rand() % m_genTel ] ;
   }
   plek = prog.find( "#" ) ;
    left = prog.substr( 0 , plek ) ;
   right = prog.substr( plek + 1 , prog.length() ) ;
   left.append( gen ) ;
   left.append( right ) ;
   prog = left ;
  }
  return prog ;
}
string CGP::groei( string prog )
{
  int dice = rand() % m_genTel ;
  while ( ! isGen( m_gen[ dice ] ) )
  {
   dice = rand() % m_genTel ;
  }
  string gen = m_gen[ dice ] ;
  vector<string> vstr ;
  split( prog , ' ' , vstr ) ;
  dice = rand() % vstr.size() ;
  while ( ! isInput( vstr[ dice ] )
  || ! isNumber( vstr[ dice ] ) )
  {
    dice = rand() % vstr.size() ;
  }
  unsigned int i ;
  prog = "" ;
  for ( i = 0 ; i < vstr.size() ; i++ )
  {
   if ( i == dice )
   {
     prog.append( gen ) ;
   }
   else
   {
     prog.append( vstr[ i ] ) ;
   }
   prog.append( " " ) ;
  }
  return hekjes( prog ) ;
}
string CGP::runProg( string prog )
{
  if ( prog == "" )
  {
   return "error" ;
  }
  if ( prog.length() > progLenMax )
  {
   return "error" ;
  }
  int begin , end ;
  bool found ;
  string left , right , part ;
  string as , bs , cs ;
  double a , b , c , ab ;
  vector<string> vstr ;
  try {
  while ( prog.find( ']' ) != string::npos )
  {
   end = prog.find( ']' ) ;
   begin = end ;
   while ( ( prog[ begin ] != '[' )
       && ( begin > 0 ) )
   {
     begin-- ;
   }
   part = prog.substr( begin , end - 3 ) ;
   split( part , ' ' , vstr ) ;
   as = vstr[ 2 ]  ;
   bs = vstr[ 3 ]  ;
   cs = vstr[ 4 ]  ;
   if ( isNumber( as ) )
   {
     a = VAL( as ) ;
   }
   else
   {
     if ( isInput( as ) )
     {
      a = in[ input( as ) ] ;
     }
     else
     {
      return "error" ;
     }
   }
   if ( isNumber( bs ) )
   {
     b = VAL( bs ) ;
   }
   else
   {
     if ( isInput( bs ) )
     {
      b = in[ input( bs ) ] ;
     }
     else
     {
      return "error" ;
     }
   }
   if ( isNumber( cs ) )
   {
     c = VAL( cs ) ;
   }
   else
   {
     if ( isInput( cs ) )
     {
      c = in[ input( cs ) ] ;
     }
     else
     {
      return "error" ;
     }
   }
   found = false ;
   if ( vstr[ 1 ] == "+" )
   {
     found = true ;
     ab = a + b ;
   }
   if ( vstr[ 1 ] == "-" )
   {
     found = true ;
     ab = a - b ;
   }
   if ( vstr[ 1 ] == "/" )
   {
     if ( b == 0 )
     {
      return "error" ;
     }
     else
     {
      found = true ;
      ab = a / b ;
     }
   }
   if ( vstr[ 1 ] == "*" )
   {
     found = true ;
     ab = a * b ;
   }
   if ( ! found )
   {
     return "error" ;
   }
   left = prog.substr( 0 , begin ) ;
   right = prog.substr( end + 1 , prog.length() ) ;
   left.append( STR( ab ) ) ;
   left.append( right ) ;
   prog = left ;
  }
  }
  catch(...)
  {
    return "error" ;
  }
  return prog ;
}
void CGP::setInputMax( unsigned  int max )
{
  if ( max < 0 || max > 10 )
  {
   return  ;
  }
  unsigned int i ;
  for ( i = 0 ; i < max ; i++ ) {
    use( letter[ i ] ) ;
  }
}
string CGP::mixProg( string a , string b )
{
  string h ;
  if ( rand() % 2 < 1 )
  {
    h = a ;
    a = b ;
    b = h ;
  }
  vector<int> ha , hb ;
  unsigned int i ;
  for ( i = 0 ; i < a.length() ; i++ )
  {
    if ( a[ i ] == '[' )
   {
      ha.push_back( i ) ;
    }
  }
  for ( i = 0 ; i < b.length() ; i++ )
  {
    if ( b[ i ] == '[' )
   {
      hb.push_back( i ) ;
    }
  }
  int begina , einda , beginb , eindb , fl ;
  begina = ha[ rand() % ha.size() ] ;
  fl = 1 ;
  i = begina ;
  while ( fl > 0 )
  {
    i++ ;
    if ( a[ i ] == '[' ) fl++ ;
    if ( a[ i ] == ']' ) fl-- ;
  }
  einda = i ;
  beginb = hb[ rand() % hb.size() ] ;
  fl = 1 ;
  i = beginb ;
  while ( fl > 0 )
  {
    i++ ;
    if ( b[ i ] == '[' ) fl++ ;
    if ( b[ i ] == ']' ) fl-- ;
  }
  eindb = i ;
  string uit = a.substr( 0 , begina ) ;
  uit.append( b.substr( beginb , eindb ) ) ;
  uit.append( a.substr( einda , a.length() ) ) ;
 
  return uit ;
}
string CGP::mutateProg( string a )
{
  vector<string> vstr ;
  split( a , ' ' , vstr ) ;
  int dice = rand() % vstr.size() ;
  unsigned int i ;
  while ( vstr[ dice ] == "[" || vstr[ dice ] == "]" )
  {
    dice = rand() % vstr.size() ;
  }
  if ( isInput( vstr[ dice ] ) )
  {
    if ( ( rand() % 100 ) < 90 )
   {
      vstr[ dice ] = letter[ rand() % m_inputMax ] ;
    }
   else
   {
      if ( m_numberMode == 1 )
     {
        vstr[ dice ] =
        STR( pow( 2 , (double)(rand() % numberPower ) ) ) ;
      }
     else
     {
        vstr[ dice ] =
        STR( pow( 2
        , (double)(rand() % numberPower * 2 - numberPower ) ) ) ;
      }
    }
  }
  else
  {
    if ( isNumber( vstr[ dice ] ) )
   {
      if ( m_numberMode == 1 )
     {
        double no = VAL( vstr[ dice ] ) ;
        if ( ( rand() % 100 ) < 50 )
      {
          no -= pow( 2
        , (double)(rand() % numberPower ) ) ;
        }
      else
      {
          no += pow( 2
        , (double)(rand() % numberPower ) ) ;
        }
        vstr[ dice ] = STR( no ) ;
      }
     else
     {
        double no = VAL( vstr[ dice ] ) ;
        if ( ( rand() % 100 ) < 50 )
      {
          no -= pow( 2
          , (double)(rand() % numberPower * 2 - numberPower ) ) ;
        }
      else
      {
          no += pow( 2
          , (double)(rand() % numberPower * 2 - numberPower ) ) ;
        }
        vstr[ dice ] = STR( no ) ;
      }
      if ( m_inputMax > 0 )
     {
        if ( ( rand() % 100 ) < 10 )
      {
          vstr[ dice ] = letter[ rand() % m_inputMax ] ;
        }
      }
    }
   else
   {
      i = rand() % m_genTel ;
      while ( ! isGen( m_gen[ i ] ) )
     {
        i = rand() % m_genTel ;
      }
      vector<string> v ;
      split( m_gen[ i ] , ' ' , v ) ;
      vstr[ dice ] = v[ 1 ] ;
    }
  }
  a = "" ;
  for ( i = 0 ; i < vstr.size() ; i++ )
  {
    a.append( vstr[ i ] ) ;
    a.append( " " ) ;
  }
  if ( ( rand() % 100 ) < groeiRate )
  {
    a = groei( a ) ;
  }
  return a ;
}
string prog[ 1000 ] ;
double fout[ 1000 ] ;
void evaluate( CGP gp )
{
  int h , l ;
  double d ;
  string s ;
  for ( h = 1 ; h < 1000 ; h++ )
  {
   for ( l = 0 ; l < h ; l++ )
   {
     if ( fout[ l ] > fout[ h ] )
     {
       d = fout[ l ] ;
      fout[ l ] = fout[ h ] ;
      fout[ h ] = d ;
      s = prog[ l ] ;
      prog[ l ] = prog[ h ] ;
      prog[ h ] = s ;
     }
   }
  }
  cout << prog[ 0 ] << endl ;
  cout << fout[ 0 ] << endl ;
  string a , b ;
  for ( l = parents ; l < 1000 ; l++ )
  {
    a = prog[ rand() % parents ] ;
   b = prog[ rand() % parents ] ;
   prog[ l ] = gp.mixProg( a , b ) ;
   if ((rand() % 100 ) < mutateRate )
   {
     prog[ l ] = gp.mutateProg( prog[ l ] ) ;
   }
  }
}
int main()
{
   CGP gp ;
   string a = "[ + 1 [ - 2 3 4 ] 5 ]" ;
   string b = "[ * 6 [ / 7 8 9 ] 10 ]" ;
   string c , prog ;
   int i ;
   
   cout << "testing mix :" ;
   cout << "a = " << a << endl ;
   cout << "b = " << b << endl ;
   for ( i = 0 ; i < 10 ; i ++ )
   {
      c = gp.mixProg( a , b ) ;
      cout << "mix a b = " << c << endl ;
      cout << "run mix a b = "
      << gp.runProg( c ) << endl ;
   }

   wait_key() ;
   
   gp.intList() ;
   gp.use( _add ) ;
   gp.use( _subtract ) ;
   gp.use( _divide ) ;
   gp.use( _multiply ) ;

   cout << "testing mutate :\n" ;
   cout << "operators = { + - * / }\n" ;
   for ( i = 0 ; i < 10 ; i++ )
   {
      c = gp.mutateProg( a ) ;
      cout << "mutate a = " << c << endl ;
      cout << "run mutate a = " << gp.runProg( c ) ;
   }

   wait_key() ;

   cout << "testing write : \n" ;
   cout << "operators = { + - * / } \n" ;
   for ( i = 0 ; i < 10 ; i++ )
   {
      prog = gp.writeProg( 2 ) ;
      cout << "prog = " << prog << endl;
      cout << "run prog = "
         << gp.runProg( prog ) << endl ;
   }

   wait_key() ;
}

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Top
 Profile  
 
PostPosted: Wed Jul 09, 2014 8:21 pm 
Bearer of the Golden Keyboard
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 238
It's very hard to understand you, sorry and theres just too much code, I'm not sure what do you want from us.

Did you write that code all alone? all of it?

You are clearly using C++, why are you not using objects more? the code is a mess :confused

blua tigro wrote:
- how do i save / load prog()
- how do the code sees that the file of prog() exsists


1. are you asking how to save and load a program created with your GP program ?
2. what? are you asking how to check if a file exists or what exactly do you want to know?

blua tigro wrote:
it has error's jet
debuging help wil be nice


What errors are you asking about? Is the program acutally throwing an exception or are you asking if we can help you make the code better? In either case, there is too much code to go through and it's not commented at all. You can't expect someone to go through that code and waste time trying to understand everything you wrote. If there is an exception please point only the relevant functions and Line numbers and if possible a call Trace

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Thu Jul 10, 2014 11:29 am 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 12
Location: zaanstad NL
i dont know how to do this whit trees
so i used strings
vc++2008 reports that there is a error in line +-770
and there are no more then +-660 lines

update :
- i added some REMs

if you need more ask

i want to know how to store/read the programs on/from disk
i have no experiance whit file io in c++
in that case the program has to know if the file whit programs already exsits
and if not create one

i dont plan my code
i build it part for part
i build first some code whit output
if the output is wat i want
- i extent it
else
- i try to find the mistake whit experimenting

i use a lot of cut and paste to resufle my code
so it looks not orded

Code:
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <math.h>

using namespace std ;

void split( string& s , char c , vector<string>& v )
//split a sentence string into words
{
  string::size_type i = 0 ;
  string::size_type j = s.find( c ) ;

  while ( j != string::npos )
  {
     v.push_back( s.substr( i , j - i ) ) ;
     i = ++j ;
     j = s.find( c , j ) ;
    if ( j == string::npos )
     {
     v.push_back( s.substr( i , s.length() ) ) ;
     }
  }
}

//genes strings for writing | mutating programs
const string _add      = "[ + # # # ]" ;
const string _subtract = "[ - # # # ]" ;
const string _divide   = "[ / # # # ]" ;
const string _multiply = "[ * # # # ]" ;

//variable strings for writing | mutating programs
const string letter[] = { "x" , "y" , "z"
, "d" , "e" , "f" , "g" , "h" , "i" , "j" } ;

int numberPower = 10 ;
unsigned int progLenMax = 200 ;
unsigned int progLenMin = 40 ;
int mutateRate = 50 ;
int groeiRate = 50 ;
int population = 1000 ;
int parents = 100 ;
double in[ 10 ] ;

void wait_key()
{
  string in ;
  cout << "[ push return ]" ;
  cin >> in ;
}
void printSeting()
{
  cout << "Setings :\n" ;
  cout << "population = " << population ;
  cout << "parents = " << parents << endl ;
  cout << "number power = " << numberPower << endl ;
  cout << "prog len max = " << progLenMax << endl ;
  cout << "prog len min = " << progLenMin << endl ;
  cout << "mutate rate % = " << mutateRate << endl ;
  cout << "growth rate % = " << groeiRate << endl ;
  wait_key() ;
}
bool isGen( string x )
{
  return x[ 0 ] == '[' ;
}
bool isInput( string x )
//detect if string is a variable
{
  int i ;
  for ( i = 0 ; i < 10 ; i++ )
  {
     if ( letter[ i ] == x ) return true ;
  }
  return false ;
}
int input( string x )
//witch input is the variable
{
  int i = 0 ;
  while ( letter[ i ] != x ) i++ ;
  return i ;
}
bool isNumber( string x )
//detect if string is a number
{
  string n = "0123456789" ;
  return n.find( x[ 0 ] ) != string.npos ;
}
string STR( double x )
//double to string
{
  stringstream ss ( stringstream::in | stringstream::out ) ;
  ss << x ;
  return ss.str() ;
}
double VAL( string x )
//string to double
{
   return (double)atof( x.c_str() ) ;
}
class CGP {
public :
  CGP() ;
  ~CGP() ;

  string runProg( string prog ) ;
  string writeProg( int hooks ) ;
  string mixProg( string a , string b ) ;
  string mutateProg( string prog ) ;
  void use( string gen ) ;
  void intList() ;
  void dblList() ;
  void setInputMax( unsigned int max ) ;
private :
  string hekjes( string prog ) ;
  string groei( string prog ) ;
  int m_inputMax ;
  string* m_gen ;
  int m_genTel ;
  int m_numberMode ;
} ;
CGP::CGP()
{
  srand( static_cast<unsigned int>( clock() ) ) ;
  m_genTel = 0 ;
  m_gen = new string[ 200 ] ;
}
CGP::~CGP()
{
   delete [] m_gen ;
}
void CGP::intList()
//use a set of integer genes
//and set mutating | writing on integers
{
  double i ;
  for ( i = 0 ; i < numberPower ; i++ )
  {
   use( STR( floor( pow( 2 , i ) ) ) ) ;
   use( STR( floor( -pow( 2 , i ) ) ) ) ;
  }
}
void CGP::dblList()
//use a set of double genes
//and set writing and mutating on doubles
{
  double i ;
  for ( i = -numberPower ; i < numberPower ; i++ )
  {
     use( STR( pow( 2 , i ) ) ) ;
     use( STR( -pow( 2 , i ) ) ) ;
  }
}
void CGP::use( string gen )
//activate a gene for writing
{
  if ( m_genTel < 200 )
  {
     m_gen[ m_genTel ] = gen ;
     m_genTel++ ;
  }
}
string CGP::writeProg( int hooks )
//create a progam whit hooks number of [
{
  srand( static_cast<unsigned int>( clock() ) ) ;
  string prog = " " , left , right , gen ;
  int dice , hook = 0 , plek ;
  //start writing : must be a function gene
  while ( ! isGen( prog ) )
  {
     dice = rand() % m_genTel ;
     prog = m_gen[ dice ] ;
  }
  //replace al # whit a gene
  //til number of [ >= hooks
  while ( prog.find( "#" ) != string::npos
  && hook < hooks )
  {
     plek = prog.find( "#" ) ;
    left = prog.substr( 0 , plek ) ;
     right = prog.substr( plek + 1 , prog.length() ) ;
     gen = m_gen[ rand() % m_genTel ] ;
     if ( isGen( gen ) )
     {
       hook++ ;
     }
     left.append( gen ) ;
     left.append( right ) ;
     prog = left ;
  }
  return hekjes( prog ) ;
}
string CGP::hekjes( string prog )
//replace all # whit a gene that isnt a function gene
{
  string gen , left , right ;
  int plek ;
  while ( prog.find( "#" ) != string::npos )
  {
    //gene cant be a function gene
     while ( isGen( gen ) )
     {
       gen = m_gen[ rand() % m_genTel ] ;
     }
     plek = prog.find( "#" ) ;
    left = prog.substr( 0 , plek ) ;
     right = prog.substr( plek + 1 , prog.length() ) ;
     left.append( gen ) ;
     left.append( right ) ;
     prog = left ;
  }
  return prog ;
}
string CGP::groei( string prog )
//replace a variable or a number whit a function gene
{
  int dice = rand() % m_genTel ;
  //gene must be a function gene
  while ( ! isGen( m_gen[ dice ] ) )
  {
     dice = rand() % m_genTel ;
  }
  string gen = m_gen[ dice ] ;
  vector<string> vstr ;
  split( prog , ' ' , vstr ) ;
  dice = rand() % vstr.size() ;
  //part of program to replace must be a number or variable
  while ( ! isInput( vstr[ dice ] )
  || ! isNumber( vstr[ dice ] ) )
  {
    dice = rand() % vstr.size() ;
  }
  unsigned int i ;
  prog = "" ;
  //replace stuf
  for ( i = 0 ; i < vstr.size() ; i++ )
  {
     if ( i == dice )
     {
       prog.append( gen ) ;
     }
     else
     {
       prog.append( vstr[ i ] ) ;
     }
     prog.append( " " ) ;
  }
  return hekjes( prog ) ;
}
string CGP::runProg( string prog )
//calculate output of a program
//output is a double in a string
//if there is a error in program return "error"
{
  if ( prog == "" )
  {
     return "error" ;
  }
  if ( prog.length() > progLenMax )
  {
     return "error" ;
  }
  int begin , end ;
  bool found ;
  string left , right , part ;
  string as , bs , cs ;
  double a , b , c , ab ;
  vector<string> vstr ;
  //run until there are no ] or error happens
  try {
  //find a end and a begin of a part
  //part = "[ function var|number var|number var|number ]"
  while ( prog.find( ']' ) != string::npos )
  {
     end = prog.find( ']' ) ;
     begin = end ;
     while ( ( prog[ begin ] != '[' )
         && ( begin > 0 ) )
   {
     begin-- ;
   }
   part = prog.substr( begin , end - 3 ) ;
   split( part , ' ' , vstr ) ;
   as = vstr[ 2 ]  ;
   bs = vstr[ 3 ]  ;
   cs = vstr[ 4 ]  ;
   if ( isNumber( as ) )
  //if as is a number convert of double
   {
     a = VAL( as ) ;
   }
   else
   {
     if ( isInput( as ) )
    //if as is a variable read input[]
     {
        a = in[ input( as ) ] ;
     }
    else
    //program has a error
     {
        return "error" ;
     }
   }
   if ( isNumber( bs ) )
   {
     b = VAL( bs ) ;
   }
   else
   {
     if ( isInput( bs ) )
     {
      b = in[ input( bs ) ] ;
     }
     else
     {
      return "error" ;
     }
   }
   if ( isNumber( cs ) )
   {
     c = VAL( cs ) ;
   }
   else
   {
     if ( isInput( cs ) )
     {
      c = in[ input( cs ) ] ;
     }
     else
     {
      return "error" ;
     }
   }
  //parse the function in part of program
   found = false ;
   if ( vstr[ 1 ] == "+" )
   {
     found = true ;
     ab = a + b ;
   }
   if ( vstr[ 1 ] == "-" )
   {
     found = true ;
     ab = a - b ;
   }
   if ( vstr[ 1 ] == "/" )
   {
     if ( b == 0 )
     {
        return "error" ;
     }
     else
     {
        found = true ;
        ab = a / b ;
     }
   }
   if ( vstr[ 1 ] == "*" )
   {
     found = true ;
     ab = a * b ;
   }
   if ( ! found )
   {
     return "error" ;
   }
   left = prog.substr( 0 , begin ) ;
   right = prog.substr( end + 1 , prog.length() ) ;
   left.append( STR( ab ) ) ;
   left.append( right ) ;
   prog = left ;
  }
  }
  catch(...)
  {
    return "error" ;
  }
  return prog ;
}
void CGP::setInputMax( unsigned int max )
//set number of variables
//to be used for mutation and writing
{
  if ( max < 0 || max > 10 )
  {
   return  ;
  }
  unsigned int i ;
  for ( i = 0 ; i < max ; i++ ) {
    use( letter[ i ] ) ;
  }
}
string CGP::mixProg( string a , string b )
//crossover 2 programes
//take a random part of a program
//and put in a place of a part of another program
{
  string h ;
  if ( rand() % 2 < 1 )
  {
    h = a ;
    a = b ;
    b = h ;
  }
  vector<int> ha , hb ;
  unsigned int i ;
  //look for al [ in program a and store there place
  for ( i = 0 ; i < a.length() ; i++ )
  {
    if ( a[ i ] == '[' )
     {
      ha.push_back( i ) ;
    }
  }
  //look for al [ in program b and store there place
  for ( i = 0 ; i < b.length() ; i++ )
  {
    if ( b[ i ] == '[' )
   {
      hb.push_back( i ) ;
    }
  }
  int begina , einda , beginb , eindb , fl ;
  //take a random place of a [ out program a
  begina = ha[ rand() % ha.size() ] ;
  fl = 1 ;
  i = begina ;
  //find matching ] of [ out of program a
  while ( fl > 0 )
  {
    i++ ;
    if ( a[ i ] == '[' ) fl++ ;
    if ( a[ i ] == ']' ) fl-- ;
  }
  einda = i ;
  //take a random place of a [ out program b
  beginb = hb[ rand() % hb.size() ] ;
  fl = 1 ;
  i = beginb ;
  //find matching ] of [ out of program b
  while ( fl > 0 )
  {
    i++ ;
    if ( b[ i ] == '[' ) fl++ ;
    if ( b[ i ] == ']' ) fl-- ;
  }
  eindb = i ;
  //create a.til [ + b.from [ b.to ] + a.from ]
  string uit = a.substr( 0 , begina ) ;
  uit.append( b.substr( beginb , eindb ) ) ;
  uit.append( a.substr( einda , a.length() ) ) ;
 
  return uit ;
}
string CGP::mutateProg( string a )
//change program randomly
{
  vector<string> vstr ;
  //split program into parts
  split( a , ' ' , vstr ) ;
  int dice = rand() % vstr.size() ;
  unsigned int i ;
  //part may not be a [ or ]
  while ( vstr[ dice ] == "[" || vstr[ dice ] == "]" )
  {
    dice = rand() % vstr.size() ;
  }
  if ( isInput( vstr[ dice ] ) )
  //part is a variable
  {
    if ( ( rand() % 100 ) < 90 )
    //variable changes into other variable
     {
      vstr[ dice ] = letter[ rand() % m_inputMax ] ;
    }
    else
    //variable changes into number
     {
      if ( m_numberMode == 1 )
      //program uses integers
       {
        vstr[ dice ] =
        STR( pow( 2 , (double)(rand() % numberPower ) ) ) ;
      }
      else
      //program uses doubles
       {
        vstr[ dice ] =
        STR( pow( 2
        , (double)(rand() % numberPower * 2 - numberPower ) ) ) ;
      }
    }
  }
  else
  {
    if ( isNumber( vstr[ dice ] ) )
    //part is a number
     {
      if ( m_numberMode == 1 )
      //program uses integers
       {
        double no = VAL( vstr[ dice ] ) ;
        if ( ( rand() % 100 ) < 50 )
          {
          no -= pow( 2
            , (double)(rand() % numberPower ) ) ;
        }
          else
          {
          no += pow( 2
            , (double)(rand() % numberPower ) ) ;
        }
        vstr[ dice ] = STR( no ) ;
      }
      else
      //program uses doubles
       {
        double no = VAL( vstr[ dice ] ) ;
        if ( ( rand() % 100 ) < 50 )
          {
          no -= pow( 2
          , (double)(rand() % numberPower * 2 - numberPower ) ) ;
        }
          else
          {
          no += pow( 2
          , (double)(rand() % numberPower * 2 - numberPower ) ) ;
        }
        vstr[ dice ] = STR( no ) ;
      }
      if ( m_inputMax > 0 )
      //program uses variables ?
       {
        if ( ( rand() % 100 ) < 10 )
          {
          vstr[ dice ] = letter[ rand() % m_inputMax ] ;
        }
      }
    }
    else
    //part is a function
     {
      i = rand() % m_genTel ;
      //take a random function gene
      while ( ! isGen( m_gen[ i ] ) )
       {
        i = rand() % m_genTel ;
      }
      vector<string> v ;
      split( m_gen[ i ] , ' ' , v ) ;
      //take funntion outof gene
      vstr[ dice ] = v[ 1 ] ;
    }
  }
  a = "" ;
  //create mutated program
  for ( i = 0 ; i < vstr.size() ; i++ )
  {
    a.append( vstr[ i ] ) ;
    a.append( " " ) ;
  }
  //make sometimes program longer
  if ( ( rand() % 100 ) < groeiRate )
  {
    a = groei( a ) ;
  }
  return a ;
}
string prog[ 1000 ] ;
double fout[ 1000 ] ;
void evaluate( CGP gp )
{
  int h , l ;
  double d ;
  string s ;
  //sort programs on fitnes
  for ( h = 1 ; h < 1000 ; h++ )
  {
     for ( l = 0 ; l < h ; l++ )
     {
       if ( fout[ l ] > fout[ h ] )
       {
         d = fout[ l ] ;
          fout[ l ] = fout[ h ] ;
          fout[ h ] = d ;
          s = prog[ l ] ;
          prog[ l ] = prog[ h ] ;
          prog[ h ] = s ;
       }
     }
  }
  cout << prog[ 0 ] << endl ;
  cout << fout[ 0 ] << endl ;
  string a , b ;
  //let best programs produce childeren
  for ( l = parents ; l < 1000 ; l++ )
  {
    a = prog[ rand() % parents ] ;
     b = prog[ rand() % parents ] ;
     prog[ l ] = gp.mixProg( a , b ) ;
    //sometimes mutate child
     if ((rand() % 100 ) < mutateRate )
     {
       prog[ l ] = gp.mutateProg( prog[ l ] ) ;
     }
  }
}
int main()
{
   CGP gp ;
   string a = "[ + 1 [ - 2 3 4 ] 5 ]" ;
   string b = "[ * 6 [ / 7 8 9 ] 10 ]" ;
   string c , prog ;
   int i ;
   
   cout << "testing mix :" ;
   cout << "a = " << a << endl ;
   cout << "b = " << b << endl ;
   for ( i = 0 ; i < 10 ; i ++ )
   {
      c = gp.mixProg( a , b ) ;
      cout << "mix a b = " << c << endl ;
      cout << "run mix a b = "
      << gp.runProg( c ) << endl ;
   }

   wait_key() ;
   
   gp.intList() ;
   gp.use( _add ) ;
   gp.use( _subtract ) ;
   gp.use( _divide ) ;
   gp.use( _multiply ) ;

   cout << "testing mutate :\n" ;
   cout << "operators = { + - * / }\n" ;
   for ( i = 0 ; i < 10 ; i++ )
   {
      c = gp.mutateProg( a ) ;
      cout << "mutate a = " << c << endl ;
      cout << "run mutate a = " << gp.runProg( c ) ;
   }

   wait_key() ;

   cout << "testing write : \n" ;
   cout << "operators = { + - * / } \n" ;
   for ( i = 0 ; i < 10 ; i++ )
   {
      prog = gp.writeProg( 2 ) ;
      cout << "prog = " << prog << endl;
      cout << "run prog = "
         << gp.runProg( prog ) << endl ;
   }

   wait_key() ;
}

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Top
 Profile  
 
PostPosted: Fri Jul 11, 2014 6:36 pm 
Bearer of the Golden Keyboard
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 238
blua tigro wrote:
vc++2008 reports that there is a error in line +-770


Maybe it's throwing the error in a different file? Perhaps in one of the included library files or something they include?

as for saving and reading files, did you use google? Since you are only using this to save/load files, you can use the services provided by the operating system, which are fopen() fprintf() fread() fclose() you can find all the info on these on http://www.cplusplus.com. If you want a more "C++" way look into fstream and ostream.

here are examples of both writing a file:

Code:
#include <cstdio>

using namespace std;

int main()
{
FILE * file = fopen("newfile.txt","w");
fprintf(file, "TESTING!!!");
fclose(file);
return 0;
}


and

Code:
#include <fstream>

int main()
{
std::ofstream file("newfile.txt");
file << "TESTING!!!";
return 0;
}

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group