Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MohsenZandi/b81e9b682486bd4f4088ead8c4d5ee16 to your computer and use it in GitHub Desktop.
Save MohsenZandi/b81e9b682486bd4f4088ead8c4d5ee16 to your computer and use it in GitHub Desktop.
<component name="ArtifactManager">
<artifact type="jar" name="Simulated Annealing:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Simulated_Annealing_jar</output-path>
<root id="archive" name="Simulated Annealing.jar">
<element id="module-output" name="Simulated Annealing" />
</root>
</artifact>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Simulated Annealing.iml" filepath="$PROJECT_DIR$/Simulated Annealing.iml" />
</modules>
</component>
</project>
Manifest-Version: 1.0
Main-Class: SimulatedAnnealing
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SimulatedAnnealing extends JFrame {
double[] best = new double[3];
JLabel label = new JLabel("");
public SimulatedAnnealing() {
super();
this.setTitle("Simulated Annealing");
this.setSize(700, 100);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
this.add(label);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
SimulatedAnnealing sa = new SimulatedAnnealing();
double result=50000;
while(true){
sa.algorithm();
if(sa.best[2]<result){ result=sa.best[2];
sa.label.setText("Minimum of Ackley function: x= "+sa.best[0]+", y= "+sa.best[1]+", f(x,y) "+sa.best[2]);
}
}
}
public void algorithm() {
double[] current = {1.814320e-5,-2.0075423e-5, 0};
current[2] = Ackley(current);
double[] next = {current[0], current[1], current[2]};
for (int k = 0; k < 3; k++) {
best[k] = current[k];
}
double e = 0.00000001;
double t = 1000000;
double a = 0.9999;
double deltaE = 0;
double s=0;
while (t > 0) {
double r = Math.random();
if (0.25 > r) {
next[0] = current[0] + e;
} else if (0.5 > r) {
next[0] = current[0] - e;
} else if (0.75 > r) {
next[1] = current[1] + e;
} else {
next[1] = current[1] - e;
}
next[2] = Ackley(next);
deltaE = next[2] - current[2];
if (deltaE <= 0) {
for (int k = 0; k < 3; k++) {
current[k] = next[k];
}
} else if (Math.random() <= Math.exp(deltaE / t)) {
for (int k = 0; k < 3; k++) {
current[k] = next[k];
}
}
if (current[2] < best[2]) {
for (int k = 0; k < 3; k++) {
best[k] = current[k];
}
}
s = a * t;//scheduling t
if (s==t) t=t-e;
else t=s;
}
}
public double Ackley(double[] x) {
double a = 20;
double b = 0.2;
double c = 2 * Math.PI;
double s1 = 0;
double s2 = 0;
for (int i = 0; i < (x.length - 1); i++) {
s1 = s1 + x[i] * x[i];
s2 = s2 + Math.cos(c * x[i]);
}
return -a * Math.exp(-1.0*b * Math.sqrt((1.0 / (x.length - 1.0)) * s1)) - Math.exp((1.0 / (x.length - 1.0)) * s2) + a + Math.exp(1.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment