468,513 Members | 941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

simulated annealing

28
Stil about simulated programming,,, Here is the code I have. Firstly, it must generate a path using number 1 to 6 to represent the city. Then it will calculate the distance (using the coordinate). But, in fact, the output of the distance doesn't appropriate with the coordinate of the randomed path. The output of the distance always 129.08, which is the distance city 1 to city 6. every time the code is deployed, it always prints 6 cities, randomed, but the distance isn't change. please help me.

Expand|Select|Wrap|Line Numbers
  1. int NC=6; // the amount of cities
  2. float X[6]={10.00,30.00,15.00,25.00,10.00,30.00}; // the coordinate of city
  3. float Y[6]={0.00,0.00,20.00,20.00,30.00,30.00};//the coordinate of city
  4. double panjang3=0;
  5.  
  6. void pjgJalur()
  7. {
  8. double panjang2=0;
  9. double panjang4=0;
  10.  
  11. int i;
  12. //printf("%2.2f",X[2]);
  13.  
  14. for (i=0;i<NC-1;i++)
  15. {
  16. panjang2=sqrt(pow((X[i+1]-X[i]),2)+pow((Y[i+1]-Y[i]),2));
  17. panjang3=panjang3+panjang2;
  18. }
  19. panjang4=sqrt(pow((X[5]-X[0]),2)+pow((Y[5]-Y[0]),2));
  20. panjang3=panjang3+panjang4;
  21.  
  22. }
  23.  
  24. void random1()
  25. {
  26. int b;
  27.      randomize();
  28.      ulang2:
  29.      int a=random (6);
  30.      a=a+1;
  31.      if (a==6)
  32.      {
  33.      goto ulang2;
  34.      }
  35.      else{
  36.      printf("R1: %d\n",a);
  37.  
  38.      ulang:
  39.      b=random (6);
  40.      b=b+1;
  41. //     printf("R2a: %d\n",b);
  42.      if (a<b)
  43.      {
  44.      printf("R2: %d\n",b);
  45.  
  46.      }
  47.      else{
  48.  
  49.      goto ulang;
  50.      }
  51.      }
  52. }
  53.  
  54. void random2()
  55. {
  56. randomize();
  57.      for (int a=0;a<10;a++)
  58.      {
  59.      float x=random (99);
  60.      x=x/100;
  61.      printf("Random number in the 0,1-0,99 range: %2.2f\n",x);}
  62. }
  63.  
  64. int x[6]={0,0,0,0,0,0};
  65. void random3()
  66. {
  67.  
  68.      int y,a;
  69.      int b=0;
  70.      int c=0;
  71.  
  72.      randomize();
  73.  
  74.      y= random (6);
  75.      y=y+1;
  76.      printf("Kota 1: %d\n",y);
  77.      x[0]=y;
  78.  
  79.      for (a=1;a<6;a++)
  80.      {
  81.         ulang:
  82.         c=0;b=0;
  83.         x[a]= random (7);
  84.         x[a]=x[a]+1;
  85.  
  86.  
  87.         cek:
  88.         if(a!=b)
  89.         {
  90.             if (x[a]==x[b])
  91.             {
  92.                 goto ulang;
  93.             }
  94.             else
  95.             {
  96.                     if(c==6){
  97.                                 goto lanjut;}
  98.                     else {b++;
  99.                              c++;goto cek;}
  100.             }
  101.         }
  102.         if(a==b) {b++;c++;goto cek;}
  103.         lanjut:
  104.  
  105.         printf("Kota %d: %d\n",a+1,x[a]);
  106.  
  107.      }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void T0()
  114. {
  115. float LMax=0;
  116.  
  117. }
  118.  
  119. void JalurBaru()
  120. {
  121. int a,b,x[6];
  122. int depan[6],tengah[6],belakang[6];
  123. random1();
  124. }
  125.  
  126. void main()
  127. {
  128.  
  129. random3();
  130. JalurBaru();
  131.  
  132. pjgJalur();
  133. printf("%2.2f",panjang3);
  134.  
  135. }
  136.  
thank you!!
Mar 11 '08 #1
1 4217
MACKTEK
40
Your code is not organized.
So, I have to admit, following the logic is not very easy for me.

Try making some changes:
Instead of having individual function names for each city... put each city in an enumeration like this:
enum city { New_York, Dallas, Charlotte, Atlanta };

This will give the value of 0 to New York, 1 to Dallas, 2 to Charlotte.... etc.
Then create an array called CityName. CityName[0]='New York' etc...
(you have different names of course). This will store the actual string name for later use.
That way no matter what city you choose, you will have a number associated with it, and from that number access the name.

I notice you are only using 1 random number in some places, and then adding 1 for the next city.

It seems to me you should make 2 random numbers.
do {
city1 = random #
city 2 = random#
} while (city1==city2) // keep looping until both are different.

You can call the function citydistance (city1 city2) which would return a floating variable. Ie float dist=citydistance(city1, city2);

in that function you would use your pythagorean formula to determine the distance, and return the answer.

Then you would use something like
Console::WriteLine("Traveling from {0} to {1} is {2} kilometers",CityName[city1],CityName[city2], distance);

That will reduce a lot of extra coding, and make the whole thing easier to understand.
Mar 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by HI-Lab * the Social Technology | last post: by
6 posts views Thread by tc | last post: by
3 posts views Thread by Jochus | last post: by
reply views Thread by Sam | last post: by
reply views Thread by polen.t2006 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.