473,385 Members | 1,593 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,385 software developers and data experts.

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
Feb 23 '08 #1
6 3753
JosAH
11,448 Expert 8TB
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
Feb 23 '08 #2
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
Feb 23 '08 #3
JosAH
11,448 Expert 8TB
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
Feb 23 '08 #4
Thank you

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


Expand|Select|Wrap|Line Numbers
  1.  import java.io.*;
  2. import java.lang.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import java.io.Serializable;
  7.  
  8. public class sim
  9.  
  10. {
  11. // Initialize variables
  12.           Random random = new Random(); // set up random number generator
  13.     protected static double temperature = 0.95; // annealing adjustments
  14.            protected static double a,y = 0,k = 0,temp = 0; // Variables
  15.     protected static double minx = 0; // Constrain of x
  16.     protected static double maxx = 40; // Constrain of x
  17.     //public boolean anneal(double d);
  18.  
  19. public static void main( String args[] )
  20.  
  21.  { 
  22.  
  23. double r[]=new double[10];
  24. Random random = new Random(); // How are the inputs will be randomized
  25.  
  26. // Generate initial x[i] values 
  27.  
  28.         //System.out.println("\nx[i] values");
  29.         //System.out.println("==================");
  30.  
  31.        for(int i=0;i<10;i++)
  32.        {
  33.  
  34.        a = random.nextDouble(); // How are the inputs will be randomized
  35.  
  36.        if((a<maxx) || (a>minx))
  37.  
  38.        {
  39.  
  40.          r[i] = a;  
  41.  
  42.        }
  43.         //    System.out.println(""+r[i]);
  44.         //    System.out.println("Random number printing");
  45.        } 
  46.  
  47.     //    System.out.println("\nf(x) values");
  48.     //    System.out.println("==================");
  49.  
  50.       for(int i = 0;i<10;i++)
  51.     {
  52.  
  53.     //y = r[i]*r[i]-10*Math.cos(2*3.142*r[i]); // Calculate probabilities
  54.       y = Math.sin(0.15*r[i])+ Math.cos(r[i]);   
  55.     //f(x)=sin(0.15*x)+cos(x)          
  56.     k = k + y;
  57.  
  58.          //    System.out.println(+k);
  59.         //   System.out.println("Addition formula out come");
  60.     }
  61.  
  62.     k = 10*10+k;
  63.  
  64.     //    System.out.println("\nf(x) + 10 values returns");
  65.     //         System.out.println("\n"+k);
  66. //
  67.    int l = 1;
  68.    while(l != 4) // loop
  69.  
  70.    {
  71.  
  72. double r1[]=new double[10];
  73.  
  74.     //    System.out.println("\nx[i] values");
  75. //        System.out.println("==================");
  76.  
  77.        for(int i = 0;i<10;i++)
  78.        {
  79.  
  80.        a = Math.random();
  81.  
  82.        if((a<maxx) || (a>minx))
  83.        {
  84.     //       System.out.println("<" + maxx+"Maxx"+" > " + minx + "minxx");
  85.          r[i] = a;
  86.  
  87.        }
  88.     //        System.out.println(""+r[i]);
  89.        } 
  90.  
  91. //        System.out.println("\nf(x) values");
  92. //        System.out.println("==================");
  93.  
  94.       for(int i = 0;i<10;i++)
  95.     {
  96.  
  97.     //y = r[i]*r[i]-10*Math.cos(2*3.142*r[i]); // Calculate probabilities
  98.       y = Math.sin(0.15*r[i])+ Math.cos(r[i]);   
  99.     //f(x)=sin(0.15*x)+cos(x)
  100.  
  101.     k = k + y;
  102. //               System.out.println(+k);
  103.  
  104.     }
  105.  
  106.     k = 10*10+k;
  107.  
  108. //        System.out.println("\nf(x) + 10 values returns");
  109.  //            System.out.println("\n"+k);
  110.  
  111.  
  112. //Source code: http://www.heatonresearch.com/articles/9/page4.html / http://www.heatonresearch.com/code/64/SimulatedAnnealing.zip
  113.  
  114. {
  115.  
  116. if (temperature < 1.0E-4)
  117.          {
  118.            if (k > 0.0)
  119.             {
  120.               temperature = k;
  121.             //  System.out.println("\nBetter Value = "+k);
  122.           //    System.out.println(1.0E-4);
  123.            }   
  124.            }
  125.  
  126.            if (Math.random() < Math.exp(k / temperature))
  127.               {
  128.    // System.out.println(Math.exp(k / temperature)+ " Some equation");           
  129.  
  130.                 temperature = k;
  131.              // System.out.println("\nBetterValue = "+k + "k");
  132. //System.out.println(temp);
  133. System.out.println(temperature + "   Temparature");
  134.  
  135. // ====== 
  136. //1) Choose a high starting temperature T and a random starting point x. T <- T0, x <- x0 
  137. //2) Calculate the function value of the starting point. E <- f (x) 
  138. //3) For each iteration k, k = 1 ... kf and while T is sufficiently large, do the following: 
  139. //a) Choose a new point x', using a generating function. x' <- g(x) 
  140. //b) Calculate the function value of x'. E' <- f (x') 
  141. //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. 
  142. //4) Return x and E as the optimal point and the optimal function value. 
  143. // ======              
  144.  
  145.              }
  146.              }  
  147.  
  148. l++;
  149.  
  150. //System.out.println(l);
  151.     }
  152. }
  153. }
Feb 24 '08 #5
Need help ASAP...................plz help me......
Feb 25 '08 #6
Nepomuk
3,112 Expert 2GB
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
Feb 26 '08 #7

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

Similar topics

10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
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...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
17
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find...
4
by: java.koder | last post by:
Hello all, I am making a simple msAccess aplication for some construction calculus, I need however to calculate a parameter from an equation and I am told this can be made with the solver...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
2
by: Bernard Dhooghe | last post by:
The information center writes: "Encryption Algorithm: The internal encryption algorithm used is RC2 block cipher with padding, the 128-bit secret key is derived from the password using a MD2...
5
by: Daniel Rudy | last post by:
Hello Group, I've looked at this backwards and forwards and I can't seem to find the problem here. What happens is I have a multidimensional array of char that has 3 dimensions with a...
17
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.