dmitri-bko commited on
Commit
d347290
1 Parent(s): 37cc072

initial commit

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import logging
3
+
4
+ from gradio_log import Log
5
+ from numpy import asarray
6
+ from numpy import exp
7
+ from numpy.random import randn
8
+ from numpy.random import rand
9
+ from numpy.random import seed
10
+ from matplotlib import pyplot
11
+
12
+ # setup logging
13
+
14
+ LOG_FILE = "sim-ann.log"
15
+ logging.basicConfig(
16
+ filename=LOG_FILE,
17
+ level=logging.INFO,
18
+ format="%(levelname)s: %(asctime)s %(message)s",
19
+ datefmt="%m/%d/%Y %I:%M:%S",
20
+ )
21
+
22
+ # clear the log file
23
+ with open(LOG_FILE, "w"):
24
+ pass
25
+
26
+
27
+ # Define the objective function
28
+ def objective(x):
29
+ return x[0] ** 2.0
30
+
31
+
32
+ # Simulated Annealing algorithm
33
+ def simulated_annealing(objective, bounds, n_iterations, step_size, temp):
34
+ # Generate an initial point
35
+ best = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])
36
+ # Evaluate the initial point
37
+ best_eval = objective(best)
38
+ # Current working solution
39
+ curr, curr_eval = best, best_eval
40
+ scores = list()
41
+ # Run the algorithm
42
+ for i in range(n_iterations):
43
+ # Take a step
44
+ candidate = curr + randn(len(bounds))
45
+ candidate = curr + randn(len(bounds)) * step_size
46
+ # Evaluate the candidate point
47
+ candidate_eval = objective(candidate)
48
+ # Check for a new best solution
49
+ if candidate_eval < best_eval:
50
+ # Store the new best point
51
+ best, best_eval = candidate, candidate_eval
52
+ # Keep track of scores
53
+ scores.append(best_eval)
54
+ # Report progress
55
+ logging.info(">%d f(%s) = %.10f" % (i, best, best_eval))
56
+ # Difference between candidate and current point evaluation
57
+ diff = candidate_eval - curr_eval
58
+ # Calculate temperature for the current epoch
59
+ t = temp / float(i + 1)
60
+ # Calculate Metropolis acceptance criterion
61
+ metropolis = exp(-diff / t)
62
+ # Check if we should keep the new point
63
+ if diff < 0 or rand() < metropolis:
64
+ # Store the new current point
65
+ curr, curr_eval = candidate, candidate_eval
66
+ return [best, best_eval, scores]
67
+
68
+
69
+ def run(s=1, l_bound=-0.5, u_bound=0.5, n_iterations=1000, step_size=0.1, temp=10):
70
+
71
+ # Seed the pseudorandom number generator
72
+ seed(s)
73
+ # Define the range for input
74
+ bounds = asarray([[l_bound, u_bound]])
75
+ # Perform the simulated annealing search
76
+ best, score, scores = simulated_annealing(
77
+ objective, bounds, n_iterations, step_size, temp
78
+ )
79
+ logging.info("Done!")
80
+ logging.info("f(%s) = %.10f" % (best, score))
81
+
82
+
83
+ with gr.Blocks(title="Simulated Annealing") as demo:
84
+ title = gr.Markdown("## Simulated Annealing")
85
+ with gr.Row():
86
+ with gr.Column():
87
+ s = gr.Number(label="Seed", value=1)
88
+ l_bound = gr.Number(label="Lower Bound", value=-0.5)
89
+ u_bound = gr.Number(label="Upper Bound", value=0.5)
90
+ with gr.Column():
91
+ n_iterations = gr.Number(label="# Iterations", value=1000)
92
+ step_size = gr.Number(label="Step Size", value=0.1)
93
+ temp = gr.Number(label="Temperature", value=10)
94
+ # output = gr.Textbox()
95
+ with gr.Row():
96
+ btn = gr.Button("Run")
97
+ btn.click(
98
+ fn=run,
99
+ inputs=[s, l_bound, u_bound, n_iterations, step_size, temp],
100
+ # outputs=[output],
101
+ )
102
+ Log(LOG_FILE, dark=True, xterm_font_size=12)
103
+
104
+ if __name__ == "__main__":
105
+ demo.launch(share=True)