[galib] Help- Showing the best individual with Borland C++ Builder 6.0

opb brunstad at online.no
Sun Mar 13 16:22:32 EST 2005


I am a novice trying to implement GAlib in Borland Builder6.0.

I have built the lib and all examples with the makefile.bcc.
It all works fine, out-of-the-box, in commandline-version.

Then I tried to make a winApp - with a memo, and a button to run the
algorithm in example 1.  I had hoped for the memo to display the result as
it does in dos-window.

However, I allways get
"raised exception class std::bad_cast with message 'Exception Object Address 
when I run the program.
There seems to be a problem with the genome writing out to the ostingstream.

Does anybody have a clue to solve my problem?

Thanks in advance!
Ole Petter Brunstad

The code is (somewhat abbrevated) as follows:


#include <vcl.h>
#pragma hdrstop

#include "TestBCB6.h"
#include <sstream>
#include <ga/GASimpleGA.h> // we're going to use the simple GA
#include <ga/GA2DBinStrGenome.h> // and the 2D binary string genome
#include <ga/std_stream.h>

using namespace std;
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
// This is the objective function.
float Objective(GAGenome& g) {
  GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)g;
// The code goes here
void __fastcall TForm1::Button1Click(TObject *Sender)
 ostringstream ostr;
 ostr << "Example 1\n\n";
 ostr << "This program tries to fill a 2DBinaryStringGenome with\n";
 ostr << "alternating 1s and 0s using a SimpleGA\n\n";
// Inserted code from main() here:
// Declare variables for the GA parameters and set them to some default 
// ...

// Now the best genome that the GA found.
ostr << ga.statistics().bestIndividual();
AnsiString besttxt = ostr.str().c_str();

//then you can add received text to memo:
/* Solucao1->*/

Memo1->Lines->Add(AnsiString("best individual is: ") + besttxt);

More information about the galib mailing list