Genetic algorithm implementation in c/c++

Discussion in 'C++' started by balbeer, Nov 6, 2011.

  1. balbeer

    balbeer New Member

    Joined:
    Nov 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    How to implement a c/c++ code to find shortest path using genetic algorithm.

    Can anyone help me out?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Where are you stuck?
     
  3. balbeer

    balbeer New Member

    Joined:
    Nov 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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..
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  5. balbeer

    balbeer New Member

    Joined:
    Nov 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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..?
     
    Last edited by a moderator: Nov 8, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice