473,226 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 4586
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

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

Similar topics

0
by: HI-Lab * the Social Technology | last post by:
I'm searching for a script or an application (if possible free) to create: - a business plan on-line or - a simulated enterprise for educational purposes. I found on Sourceforge "Simuland"...
2
by: Sean Berry | last post by:
I want to debug a script that has a line like the following: if len(os.environ) > 0: How do I debug this from the command line. I tried QUERY_STRING='sized=72&shaped=3' ./aqua_getShapes But...
6
by: tc | last post by:
Does anyone know of a module for python which simulates a samba server (windows fileshare). Instead of sharing a Phisical Device I'd like to share a database based filesystem which is spread over...
3
by: Jochus | last post by:
Hi! I'm study Industrial Enginering Informatics (second year) and we have to write a a program that writes out HTML timetables of classes (school classes, I mean ...). First we have to read...
0
by: Sam | last post by:
I'm using Python 2.3.5 with pygtk 2.4.1, and I'm using the second threading approach from pygtk's FAQ 20.6 - invoking "gtk.gdk.threads_init()", and wrapping all gtk/gdk function calls with...
2
by: mnmgirl | last post by:
hi im new here.. does anyone know simulated annealing algortihm? im using SA for my final year project but i don't understand the algorithm.. can someone please help me out.. thanx..<^_^>
4
by: mserkank | last post by:
i have a project. in my project i need to cofigure a network and upload my own MIBs and run them. I have just one computer in my lab. therefore i m creating my network with simulating program which...
0
by: polen.t2006 | last post by:
Hi, in the past it worked well to send from own program a simulated Key- press of the Cursor-up key Taste to another program, that in the other prgram the selection of an item in a listfield...
6
by: starxxx | last post by:
I need help n writing a simulated annealing algorithm that is able to maximize f(x)=sin(0.15*x)+cos(x) defined on the interval 0<= x <= 40 using the cooling schedule
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.