Java Simulated Annealing from Pseudo-Code

I am currently working on a project (TSP) and am trying to convert some simulated annealing pseudo code to Java. I have been successful in the past when converting pseudocode to Java code, however I cannot successfully convert it.

Pseudocode:

T0(T and a lowercase 0) Starting temperature Iter Number of iterations Ξ» The cooling rate 1. Set T = T0 (T and a lowercase 0) 2. Let x = a random solution 3. For i = 0 to Iter-1 4. Let f = fitness of x 5. Make a small change to x to make x' 6. Let f' = fitness of new point 7. If f' is worse than f then 8. Let p = PR(f', f, Ti (T with a lowercase i)) 9. If p > UR(0,1) then 10. Undo change (x and f) 11. Else 12. Let x = x' 13. End if 14. Let Ti(T with a lowercase i) + 1 = Ξ»Ti(Ξ» and T with a lowercase i) 15. End for Output: The solution x 

If anyone could show me the basic Java inscription, I would be extremely grateful - I just can't figure it out!

I work through several classes, using a number of functions (which I will not list, since it does not matter for what I ask). I already have a smallChange() method and a fitness function - can there be a chance that I will need to create several different versions of the specified methods? For example, I have something like:

 public static ArrayList<Integer> smallChange(ArrayList<Integer> solution){ //Code is here. } 

Maybe I need another version of this method that takes different parameters? Something like:

 public static double smallChange(double d){ //Code is here. } 

All I need is a basic idea of ​​what it will look like when writing in Java - I can adapt it to my code when I know how it will look in the correct syntax, but I can not get past this special let.

Thanks.

Mick

+6
java algorithm artificial-intelligence simulated-annealing pseudocode
source share
3 answers

The main code should look like this:

 public class YourClass { public static Solution doYourStuff(double startingTemperature, int numberOfIterations, double coolingRate) { double t = startingTemperature; Solution x = createRandomSolution(); double ti = t; for (int i = 0; i < numberOfIterations; i ++) { double f = calculateFitness(x); Solution mutatedX = mutate(x); double newF = calculateFitness(mutatedX); if (newF < f) { double p = PR(); // no idea what you're talking about here if (p > UR(0, 1)) { // likewise // then do nothing } else { x = mutatedX; } ti = t * coolingRate; } } return x; } static class Solution { // no idea what in here... } } 

Now, if you want the various versions of the smallChange () method to be fully executable, but you need to read the inheritance a bit

+5
source share

You can compare your answer with the code provided for the tutorial.
Artificial intelligence is a modern approach .

+4
source share

In addition, a Java approach is used here to teach simulated annealing (with sample code):

Neller, Todd. Teaching of stochastic local search , in the work of I. Russell and Z. Markov, ed. Proceedings of the 18th FLAIRS International Conference (FLAIRS-2005), Clearwater Beach, Florida, May 15-17, 2005, AAAI Press, pp. 8-13.

Related resources, links, and demos are here: http://cs.gettysburg.edu/~tneller/resources/sls/index.html

+3
source share

All Articles