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. Please help me im supposed to generate random numbers between 0 and 40 and Substitute in f(x)=sin(0.15*x)+cos(x) then what should i do next.......

Regards

Recommended Answers

All 6 Replies

Write some code would be my advice on what to do next.

I dont understand the concept after the substitution of fx and there where i need the help if i understand then i can code it........

Do you not have any class notes on simulated annealing? Why are you trying to write an algorithm you do not understand?

I have tried searching on internet for some information but end up getting the information on the Traveling Salesperson Problem. The formulas and explanation from class is not good.

I have come up with the following code. Thats what i could do please help me where i am going wrong

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.Serializable;



public class sim

{
// Initialize variables
        Random random = new Random(); // set up random number generator
    protected static double temperature = 0.95; // annealing adjustments
        protected static double a,y = 0,k = 0,temp = 0; // Variables
    protected static double minx = 0; // Constrain of x
    protected static double maxx = 40; // Constrain of x
    //public boolean anneal(double d);


public static void main( String args[] )

 { 

double r[]=new double[10];
Random random = new Random(); // How are the inputs will be randomized

// Generate initial x[i] values 

        //System.out.println("\nx[i] values");
        //System.out.println("==================");

       for(int i=0;i<10;i++)
       {

       a = random.nextDouble(); // How are the inputs will be randomized

       if((a<maxx) || (a>minx))

       {

         r[i] = a;  

       }
        //  System.out.println(""+r[i]);
        //  System.out.println("Random number printing");
       } 

    //  System.out.println("\nf(x) values");
    //  System.out.println("==================");

      for(int i = 0;i<10;i++)
    {

    //y = r[i]*r[i]-10*Math.cos(2*3.142*r[i]); // Calculate probabilities
      y = Math.sin(0.15*r[i])+ Math.cos(r[i]);   
    //f(x)=sin(0.15*x)+cos(x)          
    k = k + y;

        //  System.out.println(+k);
        //   System.out.println("Addition formula out come");
    }

    k = 10*10+k;

    //  System.out.println("\nf(x) + 10 values returns");
    //      System.out.println("\n"+k);
//
   int l = 1;
   while(l != 4) // loop

   {

double r1[]=new double[10];

    //  System.out.println("\nx[i] values");
//      System.out.println("==================");

       for(int i = 0;i<10;i++)
       {

       a = Math.random();

       if((a<maxx) || (a>minx))
       {
    //      System.out.println("<" + maxx+"Maxx"+" > " + minx + "minxx");
         r[i] = a;

       }
    //      System.out.println(""+r[i]);
       } 

//      System.out.println("\nf(x) values");
//      System.out.println("==================");

      for(int i = 0;i<10;i++)
    {

    //y = r[i]*r[i]-10*Math.cos(2*3.142*r[i]); // Calculate probabilities
      y = Math.sin(0.15*r[i])+ Math.cos(r[i]);   
    //f(x)=sin(0.15*x)+cos(x)

    k = k + y;
//              System.out.println(+k);

    }

    k = 10*10+k;

//      System.out.println("\nf(x) + 10 values returns");
 //         System.out.println("\n"+k);


//Source code: [url]http://www.heatonresearch.com/articles/9/page4.html[/url] / [url]http://www.heatonresearch.com/code/64/SimulatedAnnealing.zip[/url]

{

if (temperature < 1.0E-4)
         {
           if (k > 0.0)
            {
              temperature = k;
            //  System.out.println("\nBetter Value = "+k);
          //    System.out.println(1.0E-4);
           }   
           }

           if (Math.random() < Math.exp(k / temperature))
              {
   // System.out.println(Math.exp(k / temperature)+ " Some equation");           

                temperature = k;
             // System.out.println("\nBetterValue = "+k + "k");
//System.out.println(temp);
System.out.println(temperature + "   Temparature");

// ====== 
//1) Choose a high starting temperature T and a random starting point x. T <- T0, x <- x0 
//2) Calculate the function value of the starting point. E <- f (x) 
//3) For each iteration k, k = 1 ... kf and while T is sufficiently large, do the following: 
//a) Choose a new point x', using a generating function. x' <- g(x) 
//b) Calculate the function value of x'. E' <- f (x') 
//c) Set x<-x'and E<-E'with probability determined by the acceptance function h(). //d) Reduce the temperature T by //annealing, e.g. T(k+1) <- c*T(k), 0 < c < 1. 
//4) Return x and E as the optimal point and the optimal function value. 
// ======              

             }
             }  

l++;

//System.out.println(l);
    }
}
}

Please help me im stuck..................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.