Thread: MAZE Problem

  1. #16
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Okay, I noticed in your original code that the food data was kept in the matrix_data array, but that the print_matrix function used the trace_matrix for it's output. I assumed there was something we weren't seeing where you copied the matrix_data into the trace_matrix, tho that's not shown in the code snippet.

    In other words, of course you don't see what we've done because our code filled matrix_data and your code prints trace_matrix. You've got so many matrix variables, is it possible you're confusing yourself? (And what in the world did you show us print_program function for?)

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Code:
    int    seed;
    
    int intrnd () /* 1<=seed<=m */
    {
      double const a    = 16807;      
      double const m    = 2147483647; 
      double temp = seed * a;
      seed = (int) (temp - m * floor ( temp / m ));
      return seed;
    }
    
    
    double rand_0to1()
    { return ( intrnd() / 2147483647.0 ); }
    
    
    double random_number()
    {
      double r;
      while ((r = rand_0to1() ) >= 1.0 ) ; 
    
      return ( r );
    
    } /* end random_number()*/
    
    #define Randint(UPPER) ((int)(random_number()*(UPPER)))
    
    enum tokens {DUMMY, LEFT, RIGHT, MOVE, IF_FOOD_AHEAD, PROGN2, PROGN3};
    
    struct token_entry
    {
       char *name;
       int arity_sub1;
       // function
    };
    
    struct token_entry token_table [] =
         { {"# ", -1},     // String end. Arity must be -1 for sub-string search
           {"LEFT ", -1},
           {"RIGHT ", -1},
           {"MOVE ", -1},
           {"IF_FOOD_AHEAD ", 1},
           {"PROGN2 ", 1},
           {"PROGN3 ", 2}
         };
    
    
    struct prog
    {
      char code[MAX_PROG_SIZE+1];    // allow for string terminator
      int proglen;
      int fitness;
    };
    
    struct prog population [POPULATION_SIZE];
    
    int best_prog;
    int smallest_proglen;
    int best_fitness;
    
    
    int correct; int trace=FALSE;
    
    //(AG) File globals (run used in filename)
    int run;
    char filename[16];
    FILE *filep;
    
    char *nodep;
    int row, col, next_row, next_col, heading, steps, pellets;
    enum {NORTH, EAST, SOUTH, WEST};
    
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE];
    
    void randomfood (void)
    {
        int cfood, nofood;
        for (nofood = 0; nofood < 924; nofood++) matrix_data[rand()&#37;MATRIX_SIZE][RAND()%MATRIC_SIZE] = 0
        for (cfood = 0; cfood < 100; cfood++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1
        }
          
          
    char trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
          
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      char token;
    
      while (token = *codep++)
         fputs(token_table[token].name, stdout);
      putchar('\n');
    }
    
    
    void print_matrix(void)
    {  int i, j, matrix_data ; char symbs[] = " OX";
    
       for (i=0; i < MATRIX_SIZE; i++)
       {  for (j=0; j < MATRIX_SIZE; j++)
           for (matrix_data=0; matrix_data < 500; matrix_data++)
               
              putchar(symbs[trace_matrix[i][j]]);
          putchar('\n');
       }
    }
    
    
    void skip_arg(void)
    {  int count = 0;
       while ((count += token_table[*nodep++].arity_sub1) >= 0)   ;
    }
    Really going no where ..................

    thanks every 1

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    I have followed what guesset given, but i really confused ..

    Cheers

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    double temp = seed * a;
    And where is the seed initialized?

    do you have any main? I do not see what is the flow of your program
    do you need all this scaring global vars appearing here and there?

    do you know how to comment your code to bring a little light into the dark room?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Sorry Have attaced the file...
    Coz too large to place here.
    hope you have time take a look.

    I am really really thanks for your helping.

  6. #21
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Code:
    int    seed;
    
    int intrnd () /* 1<=seed<=m */
    {
      double const a    = 16807;      
      double const m    = 2147483647; 
      double temp = seed * a;
      seed = (int) (temp - m * floor ( temp / m ));
      return seed;
    }
    
    
    double rand_0to1()
    { return ( intrnd() / 2147483647.0 ); }
    
    
    double random_number()
    {
      double r;
      while ((r = rand_0to1() ) >= 1.0 ) ; 
    
      return ( r );
    
    } /* end random_number()*/
    
    #define Randint(UPPER) ((int)(random_number()*(UPPER)))
    
    enum tokens {DUMMY, LEFT, RIGHT, MOVE, IF_FOOD_AHEAD, PROGN2, PROGN3};
    
    struct token_entry
    {
       char *name;
       int arity_sub1;
       // function
    };
    
    struct token_entry token_table [] =
         { {"# ", -1},     // String end. Arity must be -1 for sub-string search
           {"LEFT ", -1},
           {"RIGHT ", -1},
           {"MOVE ", -1},
           {"IF_FOOD_AHEAD ", 1},
           {"PROGN2 ", 1},
           {"PROGN3 ", 2}
         };
    
    
    struct prog
    {
      char code[MAX_PROG_SIZE+1];    // allow for string terminator
      int proglen;
      int fitness;
    };
    
    struct prog population [POPULATION_SIZE];
    
    int best_prog;
    int smallest_proglen;
    int best_fitness;
    
    
    int correct; int trace=FALSE;
    
    //(AG) File globals (run used in filename)
    int run;
    char filename[16];
    FILE *filep;
    
    char *nodep;
    int row, col, next_row, next_col, heading, steps, pellets;
    enum {NORTH, EAST, SOUTH, WEST};
    
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE];
    
    void randomfood (void)
    {
        int cfood, nofood;
        for (nofood = 0; nofood < 924; nofood++) matrix_data[rand()&#37;MATRIX_SIZE][RAND()%MATRIX_SIZE] = 0;
        for (cfood = 0; cfood < 100; cfood++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
        }
    
        char trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
        
     /////////////////Test////////////////////////////
     
    void intimatrinx( char matrix_data[MATRIX_SIZE][MATRIX_SIZE])
    {
    int i, j;
    srand((unsigned int)time(NULL));
    for(j=0;j<MATRIX_SIZE;j++
            for(i=o;i<MATRIX_SIZE;i++)
            {   
                int res = rand()%5;
                matrix_data[i][j] = (res ==0);
            }
        }
        
    //char trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
          
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      char token;
    
      while (token = *codep++)
         fputs(token_table[token].name, stdout);
      putchar('\n');
    }
    
    
    void print_matrix(void)
    {  int i, j, matrix_data ; char symbs[] = " OX";
    
       for (i=0; i < MATRIX_SIZE; i++)
       {  for (j=0; j < MATRIX_SIZE; j++)
           for (matrix_data=0; matrix_data < 500; matrix_data++)
               
              putchar(symbs[trace_matrix[i][j]]);
          putchar('\n');
       }
    }


    I have modify the code, but it is........
    "::><::"

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    1) What is your problem?

    2) I looked at your code and you have one of the most aggravating coding styles I've ever seen.

    I'm assuming that you're still trying to fill your matrix with random data. I looked at your code, and NOWHERE is either matrix_data or trace_matrix referenced by anything useful. There doesn't even seem to be an attempt to fill either of them with random data. So I guess I'm a little confused on what you want help with...

  8. #23
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    oooo I think i was too sleep and depress so just put some bull ........ in....

    Really Sorry

    If you have chance too look at the original code, it is working already and able to compile and see the result...


    But now I am trying to randomly fill 1s and 0s on the map.

    but i will specificy 100 food is being place, So do i still need the srand()?
    Code:
    for (c = 0; c < 100; c++) matrix_data[rand()&#37;MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
    how do i expand this code??
    thanks
    thanks evey1

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by peacealida View Post
    But now I am trying to randomly fill 1s and 0s on the map.

    but i will specificy 100 food is being place, So do i still need the srand()?
    Code:
    for (c = 0; c < 100; c++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
    how do i expand this code??
    thanks
    thanks evey1
    Even my Hat is unable to guess what you think you mean by "expand this code". It does just what it says it will: every time through the loop, it will set matrix_data[some random row][some random column] to 1. The only reason you might not be getting 100 pieces of food is if you happen to pick the same row and column in loop number 84 as you did in loop number 37. If you want to be sure there are 100 pieces of food there, you need to check if there was food there already (quick and dirty way: increment your matrix_data element, and if it's bigger than 1, decrement it back down and decrement c as well).

  10. #25
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    wt you mean is food is already therer? but just not sure the mouse eat that or not??

    If i can successfuly computer the random array does it affect the XML tree or i need to do the XML tree again??

    Thanks thanks

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    What really amazes me is that you have 700 lines of genetic algorithm code, but you can't fill an array with random values. Something just doesn't seem right...

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by peacealida View Post
    wt you mean is food is already therer? but just not sure the mouse eat that or not??
    You have to finish making the grid before you can do anything else. You start with 0's everywhere, and you (claim you) want to put 100 1's in the grid. There is no mouse, or XML, or anything else, in the problem.

    Quote Originally Posted by peacealida View Post
    If i can successfuly computer the random array does it affect the XML tree or i need to do the XML tree again??

    Thanks thanks
    What XML tree? I don't even think the phrase "XML tree" makes sense. If you mean the XML markup that you print your data in, of course the XML markup doesn't change.

  13. #28
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    the code is not from me, but i need to modifiy it to something similr.
    First i need to change it to random food map.
    Secondly i need to change the GUI

    and find a way to show it by SVG in IE

    HAAHAH thanks 700Line of code of course is not from me. I won't say it is mine.

    tabstop I really thanks for your help.

    So i need to leave the array in all 0's? and put 1 in from the function??

    thanks

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by peacealida View Post
    So i need to leave the array in all 0's? and put 1 in from the function??

    thanks
    If by "function" you mean "that one line of code upthread that had a for-loop in it", yes. Initialize to 0's instead of the pattern you have, and use the for loop to put 1s in it.

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    and find a way to show it by SVG in IE
    Make sure you have a plugin, then. Firefox can display SVG images by itself, but Internet Explorer needs a plugin. http://wiki.svg.org/Internet_Explorer

    By the way, you can initialize every element to zero with
    Code:
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}};
    because any unspecified elements are set to zero. Or you could just leave it, because global variables are initialized to all zeros by default. That's probably not a good idea, though. Try the code I have just above.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM