473,387 Members | 1,791 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,387 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 4595
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.