simulated annealing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momogi
    New Member
    • Sep 2007
    • 28

    #1

    simulated annealing

    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.

    Code:
    int NC=6; // the amount of cities
    float X[6]={10.00,30.00,15.00,25.00,10.00,30.00}; // the coordinate of city
    float Y[6]={0.00,0.00,20.00,20.00,30.00,30.00};//the coordinate of city
    double panjang3=0;
    
    void pjgJalur()
    {
    double panjang2=0;
    double panjang4=0;
    
    int i;
    //printf("%2.2f",X[2]);
    
    for (i=0;i<NC-1;i++)
    {
    panjang2=sqrt(pow((X[i+1]-X[i]),2)+pow((Y[i+1]-Y[i]),2));
    panjang3=panjang3+panjang2;
    }
    panjang4=sqrt(pow((X[5]-X[0]),2)+pow((Y[5]-Y[0]),2));
    panjang3=panjang3+panjang4;
    
    }
    
    void random1()
    {
    int b;
    	 randomize();
    	 ulang2:
    	 int a=random (6);
    	 a=a+1;
    	 if (a==6)
    	 {
    	 goto ulang2;
    	 }
    	 else{
    	 printf("R1: %d\n",a);
    
    	 ulang:
    	 b=random (6);
    	 b=b+1;
    //	 printf("R2a: %d\n",b);
    	 if (a<b)
    	 {
    	 printf("R2: %d\n",b);
    
    	 }
    	 else{
    
    	 goto ulang;
    	 }
    	 }
    }
    
    void random2()
    {
    randomize();
    	 for (int a=0;a<10;a++)
    	 {
    	 float x=random (99);
    	 x=x/100;
    	 printf("Random number in the 0,1-0,99 range: %2.2f\n",x);}
    }
    
    int x[6]={0,0,0,0,0,0};
    void random3()
    {
    
    	 int y,a;
    	 int b=0;
    	 int c=0;
    
    	 randomize();
    
    	 y= random (6);
    	 y=y+1;
    	 printf("Kota 1: %d\n",y);
    	 x[0]=y;
    
    	 for (a=1;a<6;a++)
    	 {
    		ulang:
    		c=0;b=0;
    		x[a]= random (7);
    		x[a]=x[a]+1;
    
    
    		cek:
    		if(a!=b)
    		{
    			if (x[a]==x[b])
    			{
    				goto ulang;
    			}
    			else
    			{
    					if(c==6){
    								goto lanjut;}
    					else {b++;
    							 c++;goto cek;}
    			}
    		}
    		if(a==b) {b++;c++;goto cek;}
    		lanjut:
    
    		printf("Kota %d: %d\n",a+1,x[a]);
    
    	 }
    }
    
    
    
    
    void T0()
    {
    float LMax=0;
    
    }
    
    void JalurBaru()
    {
    int a,b,x[6];
    int depan[6],tengah[6],belakang[6];
    random1();
    }
    
    void main()
    {
    
    random3();
    JalurBaru();
    
    pjgJalur();
    printf("%2.2f",panjang3);
    
    }
    thank you!!
  • MACKTEK
    New Member
    • Mar 2008
    • 40

    #2
    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=citydistan ce(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::WriteL ine("Traveling from {0} to {1} is {2} kilometers",Cit yName[city1],CityName[city2], distance);

    That will reduce a lot of extra coding, and make the whole thing easier to understand.

    Comment

    Working...