Runtime problem

This is a discussion on Runtime problem within the C Programming forums, part of the General Programming Boards category; Hi All, I have written a piece of code (simulated Annealing) whose outline is as follows: Code: 97 while(temp > ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    [SOLVED] Runtime problem

    Hi All,
    I have written a piece of code (simulated Annealing) whose outline is as follows:
    Code:
     97   while(temp > 0.3 * TEMP) {
     98     while (j < 20) {
     99       printf("Calculating a new solution:\t");
    100       srand((unsigned) time(NULL));
    101       newSoln = neighbour(oldSoln);
    102       printf("%d: Calling the cost fn\t", j);
    103       newCost = cost(newSoln);
    104       deltaCost = newCost - oldCost;
    105       tempRn = rand();
    106       printf("rand num: %d\t", (int) tempRn);
    107       rn = 1 / (double)tempRn;
    108       deltaFn = exp( -((double)deltaCost/temp));
    109       if ((deltaCost < 0) || (rn < deltaFn)) {
    110         printf ("Accepting (%d) %d %f %f\t", newCost, deltaCost, rn, deltaFn);
    111         oldSoln = newSoln;
    112         oldCost = newCost;
    113       }
    114       j++;
    115       printf("%d: Done with the cost fn\t", j);
    116       printf("Hi\n");
    117     }
    118     printf("Hi Again\n");
    119  }
    The inner loop is expected to run 20 times for each outer loop iteration. What I am observing is this is not so. During run time, "Hi" from line 116 is being printed but neither "Hi again" from line 118 or "Calculating ....." from line 99 are printed. I have ensured that the loop conditions are still true. It just hangs for example the below is the output
    Calculating a new solution: 0: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 1: Done with the cost fn Hi
    Calculating a new solution: 1: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 2: Done with the cost fn Hi
    Calculating a new solution: 2: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 3: Done with the cost fn Hi
    Calculating a new solution: 3: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 4: Done with the cost fn Hi
    Calculating a new solution: 4: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 5: Done with the cost fn Hi
    Calculating a new solution: 5: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 6: Done with the cost fn Hi
    Calculating a new solution: 6: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 7: Done with the cost fn Hi
    Calculating a new solution: 7: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 8: Done with the cost fn Hi
    Calculating a new solution: 8: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 9: Done with the cost fn Hi
    Calculating a new solution: 9: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 10: Done with the cost fn Hi
    Calculating a new solution: 10: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 11: Done with the cost fn Hi
    Calculating a new solution: 11: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 12: Done with the cost fn Hi
    Calculating a new solution: 12: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 13: Done with the cost fn Hi
    Calculating a new solution: 13: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 14: Done with the cost fn Hi
    Calculating a new solution: 14: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 15: Done with the cost fn Hi
    Calculating a new solution: 15: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 16: Done with the cost fn Hi
    Calculating a new solution: 16: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 17: Done with the cost fn Hi
    Calculating a new solution: 17: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 18: Done with the cost fn Hi
    Calculating a new solution: 18: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 19: Done with the cost fn Hi
    Calculating a new solution: 19: Calling the cost fn rand num: 5208 Accepting (97) 0 0.000192 1.000000 20: Done with the cost fn Hi
    Hi Again
    The cost after swapping is 97. The temperature is 1.350851
    Partition A: 7 11 17 15 10 12 14 16 18 20
    Partition B: 1 8 3 6 4 9 13 5 2 19
    Next Iteration:
    Calculating a new solution: 0: Calling the cost fn rand num: 9511 Accepting (97) 0 0.000105 1.000000 1: Done with the cost fn Hi
    Calculating a new solution: 1: Calling the cost fn rand num: 10872 Accepting (95) -2 0.000092 4.395353 2: Done with the cost fn Hi
    Calculating a new solution: 2: Calling the cost fn rand num: 13196 Accepting (95) 0 0.000076 1.000000 3: Done with the cost fn Hi

    <HANG!!!!>
    Very clearly as the value of j which is 3 here is less that 20, should it go to top of the loop and continue, but it doesnot. It is stuck there forever and I need to kill the a.out. The j value where it hangs is random. I am not able to figure out the cause of the hang.
    Also note that the function cost (line 103) opens and closes a file, uses malloc for storing 200 arrays (of 10 integers each) everytime it is called.Many of you might frown at this, but I have just begun writing C programs and so there is very little planning that went into this script. I have used free() to free the malloc'd memory. So that should ease some nerves.

    Any ideas why these random hangs are occuring.? I have observed that CPU is running at 100% during these hangs. So I know something is fishy but not what.

    If any of you has waded thorough this long post I am sincerely thankful and would be glad to get any ideas/hints/suggestion or best solutions.

    Thanks a ton
    Santosh
    Last edited by SantoshN; 10-12-2010 at 03:43 PM. Reason: Solved the problem

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > srand((unsigned) time(NULL));
    time() is a CONSTANT in a short-lived program loop. Every call to rand() is going to return the same number.
    Call srand() ONCE at the start of main.


    Since you seem to be on a Unix/Linux platform, run the code in the debugger.
    gdb ./a.out
    run


    When it seems to hang, just press ctrl-c and then use the 'bt' command to show you exactly where it has stopped.

    You need to post your code that contains the malloc/free code. This is a frequent area for problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Lightbulb [Solved] Runtime problem

    Hi Salem,

    Thanks for the suggestions. Let me first mention that I managed to debug the problem. I was using lfind() to search for an element in an array (function call: neighbour) and it was behaving nastily for some strange reason. I then replace it with a traditional loop to search and now it runs smooth.
    The biggest cause of my inability to debug was:
    Code:
    printf("Some string\t");
    printf doesnot print till it encounters '\n' I was unaware of this and was adding printf statements with '\t' to debug. None of them were printed. So I was stuck. Once I realised this. it was a piece of cake.

    I am novice at gdb. Will have to learn it one of these days.

    Thanks
    Santosh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21