Function Minimisation using Simulated Annealing Algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starxxx
    New Member
    • Feb 2008
    • 4

    Function Minimisation using Simulated Annealing Algorithm

    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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What do you mean by 'the cooling schedule'? As long as you get the temperature
    T down at each iteration it will be fine theoretically. Have a look here.

    kind regards,

    Jos

    Comment

    • starxxx
      New Member
      • Feb 2008
      • 4

      #3
      Thank You...

      Yes I know about the temperature change. I have not done the f(x) functions for along time and have forgotten how they work and how to implement them and the connection between the temperature and the graph. It needs to be coded in Java to find the optimal point.

      Regards

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by starxxx
        Thank You...

        Yes I know about the temperature change. I have not done the f(x) functions for along time and have forgotten how they work and how to implement them and the connection between the temperature and the graph. It needs to be coded in Java to find the optimal point.

        Regards
        You have to come up with a value x' for a given value x. if f(x') > f(x) you've found
        a better approximation so you accept it.

        If deltaE = f(x)-f(x') > 0 then you only accept the worse step x' over x iff p*exp(deltaE/T)
        is smaller than a pseudo random value q in [0,1]. Note that p is in [0,1]
        as well and represents the probability of taking an 'uphill' step at T == infinite.

        This is basically the metropolis algorithm.

        For the java source code check the API documentation for the Math class and
        its static functions. We don't hand out boiler plate Java source code; we are
        willing to help you to solve your problem(s) though but you have to give it a try first.

        kind regards,

        Jos
        Last edited by JosAH; Feb 23 '08, 07:35 PM. Reason: corrected a typo.

        Comment

        • starxxx
          New Member
          • Feb 2008
          • 4

          #5
          Thank you

          I have come up with the following code but it seems to be wrong.


          [CODE=java] import java.io.*;
          import java.lang.*;
          import java.awt.*;
          import java.awt.event. *;
          import java.util.*;
          import java.io.Seriali zable;

          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.prin tln("\nx[i] values");
          //System.out.prin tln("========== ========");

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

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

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

          {

          r[i] = a;

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

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

          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.prin tln(+k);
          // System.out.prin tln("Addition formula out come");
          }

          k = 10*10+k;

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

          {

          double r1[]=new double[10];

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

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

          a = Math.random();

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

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

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

          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.prin tln(+k);

          }

          k = 10*10+k;

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


          //Source code: http://www.heatonresea rch.com/articles/9/page4.html / http://www.heatonresea rch.com/code/64/SimulatedAnneal ing.zip

          {

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

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

          temperature = k;
          // System.out.prin tln("\nBetterVa lue = "+k + "k");
          //System.out.prin tln(temp);
          System.out.prin tln(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.prin tln(l);
          }
          }
          }[/CODE]
          Last edited by r035198x; Feb 25 '08, 07:57 AM. Reason: added code tags, please don't forget them next time

          Comment

          • starxxx
            New Member
            • Feb 2008
            • 4

            #6
            Need help ASAP........... ........plz help me......

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by starxxx
              Need help ASAP........... ........plz help me......
              What exactly DOES happen? You say it's going wrong, but what is it, that goes wrong?

              Greetings,
              Nepomuk

              Comment

              Working...