Go Down

Topic: Genetic algoritam empty serial monitor display (Read 605 times) previous topic - next topic

Frod

Hey guys, can u tell me why I don't get nothing on serial monitor? Here is the code if u can change it and reply. Tnx

jremington

That is not an Arduino program. Run it on a PC or Mac instead.

Frod

my bad wrong file check this out

zoomkat

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

PaulS

Which Arduino are you trying to run that on? How much SRAM does the class instance use?

jurs

Hey guys, can u tell me why I don't get nothing on serial monitor? Here is the code if u can change it and reply. Tnx
Your code is NOT for Arduino:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#define NUMBER_ORGANISMS 100
#define NUMBER_GENES     20
#define ALLELES          4
#define MUTATION_RATE    0.001

#define MAXIMUM_FITNESS NUMBER_GENES
#define FALSE 0
#define TRUE  1


// global variables
char **currentGeneration, **nextGeneration;
char *modelOrganism;
int *organismsFitnesses;
int totalOfFitnesses;


// function declarations
void AllocateMemory(void);
int DoOneRun(void);
  void InitializeOrganisms(void);
  int EvaluateOrganisms(void);
  void ProduceNextGeneration(void);
    int SelectOneOrganism(void);

// functions
int main(){
  int finalGeneration;
  AllocateMemory();
  finalGeneration = DoOneRun();
  printf("The final generation was: %d\n",
         finalGeneration);
        
}


Output to Serial with Arduino is made by calling "Serial.print()" and not "printf()".

Besides of that, all the code for the declared functions AllocateMemory(void), DoOneRun(void), InitializeOrganisms(void), EvaluateOrganisms(void), ProduceNextGeneration(void) and SelectOneOrganism(void) IS MISSING!

And: Many Arduino boards only provide little RAM, and your code might want to allocate more RAM for the simulation than your Arduino board provides (might depend on the board you use).


Frod

I am using Arduino uno, I have java code too, it works with eclipse but I don't know how to make communication between java and arduino

jurs

#7
Dec 07, 2015, 04:01 pm Last Edit: Dec 07, 2015, 04:03 pm by jurs
I am using Arduino uno, I have java code too, it works with eclipse but I don't know how to make communication between java and arduino
If you have one device which can communicate through a serial port (i.e. a "personal computer") and you have an Arduino board, you can establish a serial commonication over Arduino "Serial".

Here is a small example sketch for Arduino which will do two things:
- each 10 seconds ==> send "Hello world" to the PC
- echo back every character received from the PC back to the PC
Code: [Select]

void setup()
{
  Serial.begin(9600); // initialize Serial @9600 Baud
}

unsigned long lastHelloTime;

void loop()
{
  // Sending loop: Sends "Hello world" each 10 seconds
  if (millis()-lastHelloTime>=10000) // 10000 milliseconds have passed
  {
    lastHelloTime+=10000;
    Serial.println("Hello world");
  }

  // Echo loop: Echo everything received back to sender
  if (Serial.available())
  {
    Serial.write(Serial.read());
  }
}


You can test this sketch with the Arduino "serial monitor" set to 9600 Baud and it will communicate with your PC.

Frod

Yes it works, but how can I connect java eclipse with Arduino?

jurs

#9
Dec 07, 2015, 04:49 pm Last Edit: Dec 07, 2015, 04:54 pm by jurs
Yes it works, but how can I connect java eclipse with Arduino?
If you want to run a Java software on your PC, sending data to the Arduino back and forth, you will have to look up how to establish a "serial connection" with your individually programmed Java application on the PC.

This is not forum to teach you about Java programming. Go to a Java forum and ask about serial port programming with Java, if you want to know more about programming in Java!

Arduino boards are to be programmed in the C/C++ programming language by using the Arduino core libraries and the AVR LIBC standard library.

If you just want to run a software on the Arduino board and send data to the PC, you can use a "serial monitor" or "serial terminal" software on the PC. This requires no special programming on the PC as the "serial monitor" is part of the Arduino-IDE and "serial terminal" programs are "standard software" which you just need to download and install to show incoming data (text only, no graphical diagrams!) on a PC monitor.



Frod

I just want 1 of my codes for GA to work on Arduino I have lot of but none of them works, if some1 have GA code witch work on Arduino please send. Or if u can to convert 1 of this code to work on Arduino ill be grateful tnx http://cswww.essex.ac.uk/staff/sml/gecco/results/TinyGP/entries/   this is all code in C.

jurs

Or if u can to convert 1 of this code to work on Arduino ill be grateful tnx
I've converted the code you posted above to Arduino, here it is:
Code: [Select]

#include <stdio.h>
#include <stdlib.h>

#define NUMBER_ORGANISMS 100
#define NUMBER_GENES     20
#define ALLELES          4
#define MUTATION_RATE    0.001

#define MAXIMUM_FITNESS NUMBER_GENES
#define FALSE 0
#define TRUE  1


// global variables
char **currentGeneration, **nextGeneration;
char *modelOrganism;
int *organismsFitnesses;
int totalOfFitnesses;


// function declarations
void AllocateMemory(void);
int DoOneRun(void);
  void InitializeOrganisms(void);
  int EvaluateOrganisms(void);
  void ProduceNextGeneration(void);
    int SelectOneOrganism(void);

// functions
void setup()
{
  Serial.begin(9600);
  Serial.println(F("Program is running..."));
}

void loop()
{
  int finalGeneration;
  AllocateMemory();
  finalGeneration = DoOneRun();
  Serial.print(F("The final generation was: "));
  Serial.println(finalGeneration);
}


void ErrorOutOfMemory(int errorCode)
{
  if (errorCode==1) Serial.println(F("Out of RAM in malloc() currentGeneration"));
  else if (errorCode==2) Serial.println(F("Out of RAM in malloc() nextGeneration" ));
  else if (errorCode==3) Serial.println(F("Out of RAM in malloc() modelOrganism" ));
  else if (errorCode==4) Serial.println(F("Out of RAM in malloc() organismsFitnesses" ));
  else if (errorCode==5) Serial.println(F("Out of RAM in malloc() currentGeneration[organism]/nextGeneration[organism]" ));
  else Serial.println(F("Unknown Error code"));
  while(1); // finish with endless loop
  
}

void AllocateMemory(void){
  int organism;

  currentGeneration =  (char**)malloc(sizeof(char*) * NUMBER_ORGANISMS);
  if (currentGeneration==NULL) ErrorOutOfMemory(1);
  
  nextGeneration =  (char**)malloc(sizeof(char*) * NUMBER_ORGANISMS);
  if (nextGeneration==NULL) ErrorOutOfMemory(2);

  modelOrganism = (char*)malloc(sizeof(char) * NUMBER_GENES);
  if (modelOrganism ==NULL) ErrorOutOfMemory(3);
  
  organismsFitnesses = (int*)malloc(sizeof(int) * NUMBER_ORGANISMS);
  if (modelOrganism ==NULL) ErrorOutOfMemory(4);

  for(organism=0; organism<NUMBER_ORGANISMS; ++organism)
  {
    currentGeneration[organism] = (char*)malloc(sizeof(char) * NUMBER_GENES);
    nextGeneration[organism] = (char*)malloc(sizeof(char) * NUMBER_GENES);
    if (currentGeneration[organism]==NULL || nextGeneration[organism]==NULL) ErrorOutOfMemory(5);
  }
}


int DoOneRun(void){
  int generations = 1;
  int perfectGeneration = FALSE;

  InitializeOrganisms();

  while(TRUE)
  {
    Serial.print(F("Generation: "));Serial.println(generations);
    perfectGeneration = EvaluateOrganisms();
    if( perfectGeneration==TRUE ) return generations;
    ProduceNextGeneration();
    ++generations;
  }
}


void InitializeOrganisms(void){
  int organism;
  int gene;

  // initialize the normal organisms
  for(organism=0; organism<NUMBER_ORGANISMS; ++organism){
    for(gene=0; gene<NUMBER_GENES; ++gene){
      currentGeneration[organism][gene] = rand()%ALLELES;
    }
  }

  // initialize the model organism
  for(gene=0; gene<NUMBER_GENES; ++gene){
    modelOrganism[gene] = rand()%ALLELES;
  }
}


int EvaluateOrganisms(void){
  int organism;
  int gene;
  int currentOrganismsFitnessTally;

  totalOfFitnesses = 0;

  for(organism=0; organism<NUMBER_ORGANISMS; ++organism){
    currentOrganismsFitnessTally = 0;

    // tally up the current organism's fitness
    for(gene=0; gene<NUMBER_GENES; ++gene){
      if( currentGeneration[organism][gene]
          == modelOrganism[gene] ){
        ++currentOrganismsFitnessTally;
      }
    }

    // save the tally in the fitnesses data structure
    // and add its fitness to the generation's total
    organismsFitnesses[organism] =
      currentOrganismsFitnessTally;
    totalOfFitnesses += currentOrganismsFitnessTally;

    // check if we have a perfect generation
    if( currentOrganismsFitnessTally == MAXIMUM_FITNESS ){
      return TRUE;
    }
  }
  return FALSE;
}


void ProduceNextGeneration(void){
  int organism;
  int gene;
  int parentOne;
  int parentTwo;
  int crossoverPoint;
  int mutateThisGene;

  // fill the nextGeneration data structure with the
  // children
  for(organism=0; organism<NUMBER_ORGANISMS; ++organism){
    parentOne = SelectOneOrganism();
    parentTwo = SelectOneOrganism();
    crossoverPoint  =  rand() % NUMBER_GENES;

    for(gene=0; gene<NUMBER_GENES; ++gene){

      // copy over a single gene
      mutateThisGene = rand() % (int)(1.0 / MUTATION_RATE);
      if(mutateThisGene == 0){

        // we decided to make this gene a mutation
        nextGeneration[organism][gene] = rand() % ALLELES;
      } else {
        // we decided to copy this gene from a parent
        if (gene < crossoverPoint){
          nextGeneration[organism][gene] =
            currentGeneration[parentOne][gene];
        } else {
          nextGeneration[organism][gene] =
            currentGeneration[parentTwo][gene];
        }
      }
    }
  }

  // copy the children in nextGeneration into
  // currentGeneration
  for(organism=0; organism<NUMBER_ORGANISMS; ++organism){
    for(gene=0; gene<NUMBER_GENES; ++gene){
      currentGeneration[organism][gene] =
        nextGeneration[organism][gene];
    }
  }
}


int SelectOneOrganism(void){
  int organism;
  int runningTotal;
  int randomSelectPoint;

  runningTotal = 0;
  randomSelectPoint = rand() % (totalOfFitnesses + 1);

  for(organism=0; organism<NUMBER_ORGANISMS; ++organism){
    runningTotal += organismsFitnesses[organism];
    if(runningTotal >= randomSelectPoint) return organism;
  
  }
}


What I have done is: Added error checking for dynamically allocating RAM.
On an Arduino UNO it looks as if you are trying to use MUCH more memory than an UNO has built-in.

If you use less organisms and define a much smaller number like:
Code: [Select]
#define NUMBER_ORGANISMS 10
at least the Arduino memory is enough to run the code.

I've also added Serial output for the number of generations created already.

But as it seems, the number of generations is very high, this will lead to strange results after generation 32767:
Code: [Select]
int generations = 1;
Integers on 8-bit Arduino microcontrollers are 16-bit integers with limited range.

I don't know where else in your code integer overflows might cause problems, too.

RAM is very limited on microcontrollers.
That's why those little thingies are named "microcontrollers" and not "personal computers" or "super computers".

Frod

Tnx a lot. I just needed to show that this GA code work on Arduino. I made

#define NUMBER_ORGANISMS 20
#define NUMBER_GENES     4
#define ALLELES          4
#define MUTATION_RATE    0.001

and get this https://scontent-vie1-1.xx.fbcdn.net/hphotos-xfp1/v/t35.0-12/12351812_1212796268737187_19596516_o.jpg?oh=fcab323d9aa45df5d8b61c0129bb14ed&oe=5668AEB1

jurs

#13
Dec 07, 2015, 06:02 pm Last Edit: Dec 07, 2015, 06:06 pm by jurs
Tnx a lot. I just needed to show that this GA code work on Arduino. I made

#define NUMBER_ORGANISMS 20
#define NUMBER_GENES     4
#define ALLELES          4
#define MUTATION_RATE    0.001

and get this https://scontent-vie1-1.xx.fbcdn.net/hphotos-xfp1/v/t35.0-12/12351812_1212796268737187_19596516_o.jpg?oh=fcab323d9aa45df5d8b61c0129bb14ed&oe=5668AEB1
Perhaps you'd better finish the loop() function with entering an endless while-loop after the calculations are done:
Code: [Select]
void loop()
{
  int finalGeneration;
  AllocateMemory();
  finalGeneration = DoOneRun();
  Serial.print(F("The final generation was: "));
  Serial.println(finalGeneration);
  while(1); // finish with endless loop
}


Otherwise the loop() function will restart automatically, allocate new memory and create new generations with new random values.

Or if you want to run "DoOneRun(void);" in an endless loop, just move "AllocateMemory();" from the loop() to the setup() function like that:
Code: [Select]
void setup()
{
  Serial.begin(9600);
  Serial.println(F("Program is running..."));
  AllocateMemory();
}

void loop()
{
  int finalGeneration=0;
  finalGeneration = DoOneRun();
  Serial.print(F("The final generation was: "));
  Serial.println(finalGeneration);
}



BTW "random" values: Arduino has built in a pseudo-random-number generator, and after each reset this is reset to the same starting value, so all your "random" values generated are absolutely the same than after the last reset of the microcontroller.

If you want to create "true random numbers" instead of "pseudo random numbers", you will have to initialize the random number generator with a true random "seed" value:
https://www.arduino.cc/en/Reference/RandomSeed
Otherwise you get the same result(s) after each reset.

Go Up