Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Apache Tomcat
| Search | Today's Posts | Mark Forums Read
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2008, 03:16 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Function Minimisation using Simulated Annealing Al

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. In Java.

 
Old February 24th, 2008, 08:04 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i have comeup with the following now need help as its giving the wrong output

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: http://www.heatonresearch.com/articles/9/page4.html / http://www.heatonresearch.com/code/6...dAnnealing.zip

{

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);
    }
}
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
file uploader with simulated progressbar kunchala_rakesh Ajax 1 September 13th, 2007 10:11 PM
Hello al!! abhjos Linux 1 December 28th, 2005 09:53 AM
footers et. al. in css cat Dreamweaver (all versions) 2 August 22nd, 2004 12:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.