Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Genetic algorithm implementation in c/c++ (http://www.go4expert.com/forums/genetic-algorithm-implementation-c-cpp-t27097/)

balbeer 6Nov2011 16:36

Genetic algorithm implementation in c/c++
 
How to implement a c/c++ code to find shortest path using genetic algorithm.

Can anyone help me out?

xpi0t0s 7Nov2011 20:00

Re: Genetic algorithm implementation in c/c++
 
Where are you stuck?

balbeer 7Nov2011 20:15

Re: Genetic algorithm implementation in c/c++
 
I am unable to save different possible paths.what type of data structure i should use to save different paths..as i have to further use these paths..

xpi0t0s 7Nov2011 21:30

Re: Genetic algorithm implementation in c/c++
 
You could use a tree structure, or just save everything locally and recurse. Depends if you're using a depth-first or breadth-first algorithm.

balbeer 7Nov2011 23:25

Re: Genetic algorithm implementation in c/c++
 
Code:

#include<stdio.h>
#include<conio.h>

static struct {
  int value1;
  int value2;
  int used;
} data[] = {
  { 1, 2 },
  { 1, 6 },
  { 2, 1 },
  { 2, 3 },
  { 3, 2 },
  { 3, 4 },
  { 3, 5 },
  { 4, 3 },
  { 4, 5 },
  { 4, 8 },
  {5,3},
  {5,4},
  {5,6},
  {5,12},
  {6,1},
  {6,5},
  {7,1},{7,11},{8,4},{8,9},{8,10},{9,8},{9,14},{9,15},{10,8},{10,12},
  //{10,16},{11,7},{11,12},{12,5},{12,10},{12,11},{12,13},{13,12},{13,16},{14,9},{14,16},{15,9},{15,16},{16,10},{16,13},{16,14},{16,15}
};

enum { DATA_SIZE = sizeof data / sizeof *data };

static int output[DATA_SIZE];

int traverse(int from, int to, int depth) {
  output[depth++] = from;
  int len;
  int i;
  if (from == to) {
    for (i = 0; i < depth; i++) {
      if (i) {
        printf("-");
      }
      printf("%d", output[i]);
      if(len>depth){
      len=depth;
      //len=len+1;
  }   
    }
    len=depth;
    printf("\n");
   
  } else {
    for (i = 0; i < DATA_SIZE; i++) {
      if (!data[i].used) {
        data[i].used = 1;

        if (from == data[i].value1) {
          traverse(data[i].value2, to, depth);
        } else if (from == data[i].value2) {
          traverse(data[i].value1, to, depth);
        }

        data[i].used = 0;
      }
    }
  }
  return len;
}

int main() {
  int len=traverse(9, 4, 0);
  printf("Shortest length path :%d",len);
  getch();
}


how can i save the paths in an array so that i can use them further..?


All times are GMT +5.5. The time now is 05:56.