def main(): #take 3 command line arguments for random seed number, number of dimensions, and number of gaussians seed = int(sys.argv[1]) dims = int(sys.argv[2]) ncenters = int(sys.argv[3]) max_val = 0 np.random.seed(seed) sog = SG.SumofGaussians(dims, ncenters) epsilon = 1e-8 # Data #Get Random Location current_location = np.random.ranf(dims) * 10 next_location = 0 final_location = 0 final_val = 0 uniform = np.random.uniform(-0.01, 0.01, dims) T = 10000000 Tmin = 0.000000000000000001 #max_val = sog.Eval(current_location) print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) counter = 0 while T > Tmin or counter < 2000: next_location = current_location + np.random.uniform(-0.01, 0.01, dims) if (sog.Eval(next_location) > sog.Eval(current_location)): current_location = next_location max_val = sog.Eval(current_location) if (sog.Eval(current_location) > sog.Eval(final_location)): final_location = next_location final_val = sog.Eval(current_location) print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) elif (math.exp( (sog.Eval(next_location) - sog.Eval(current_location)) / T) > np.random.random_sample()): current_location = next_location max_val = sog.Eval(current_location) print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) if (T > Tmin): T = T - (T * 0.09) counter += 1 print(" ".join(["%.8f" % (x) for x in final_location], ), end=" ") print("%.8f" % final_val)
def main(): #take 3 command line arguments for random seed number, number of dimensions, and number of gaussians seed = int(sys.argv[1]) dims = int(sys.argv[2]) ncenters = int(sys.argv[3]) max_val = 0 step_size = 0.0 inc = True np.random.seed(seed) sog = SG.SumofGaussians(dims, ncenters) epsilon = 1e-8 # Data #data_input = np.loadtxt(sys.stdin) current_location = np.random.ranf(dims) current_location = current_location * 10 max_val = 0 temp_val = 0 step_size = 0 inc = True location = 0 counter = 0 temp_loc = current_location step_size = 0.01 * sog.Deriv(current_location) x_left = temp_loc - step_size x_right = temp_loc + step_size right_val = sog.Eval(x_right) left_val = sog.Eval(x_left) if (right_val > left_val): current_location = x_right while inc == True and counter < 10000: current_location = current_location + step_size temp_val = sog.Eval(current_location) if "%.8f" % temp_val > "%.8f" % max_val: max_val = temp_val step_size = 0.01 * sog.Deriv(current_location) print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) counter += 1 else: print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) inc = False #x_right += step_size else: current_location = x_left while inc == True and counter < 10000: current_location = current_location - step_size temp_val = sog.Eval(current_location) if "%.8f" % temp_val > "%.8f" % max_val: max_val = temp_val step_size = 0.01 * sog.Deriv(current_location) print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) counter += 1 else: print(" ".join(["%.8f" % (x) for x in current_location], ), end=" ") print("%.8f" % max_val) inc = False
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import SumofGaussians as SG import numpy as np, sys import math seed = int(sys.argv[1]) dims = int(sys.argv[2]) ncenters = int(sys.argv[3]) np.random.seed(seed) sog = SG.SumofGaussians(dims, ncenters) # Initialize the sog function with random position x1 = np.random.ranf(dims) * 10 y1 = sog.Eval(x1) x2 = np.empty(dims) print(" ".join(["%.8f" % (x) for x in x1]), end=' ') print("%.8f" % y1) # Terminate at maximum of 100000 iterations for i in range(100000): random_val = (np.random.ranf(dims) * 0.1) - 0.05 for i in range(dims): if 0 <= x1[i] + random_val[i] <= 10: x2[i] = x1[i] + random_val[i] else: x2[i] = x1[i] - random_val[i] y2 = sog.Eval(x2)
# This program usses Simulated Annealing to find the closest maximum to # the starting location of the sum of gaussian function given generated based # on the random seed number, number of dimensions, and number of centers (hills) import SumofGaussians as SG import numpy as np, sys np.set_printoptions(formatter={'float': lambda x: "{0:0.8f}".format(x) }) # set printing format seed = int(sys.argv[1]) #random number seed dims = int(sys.argv[2]) #number of dimensions ncenters = int(sys.argv[3]) #number of hills np.random.seed(seed) #seed the numpy random number generator sog = SG.SumofGaussians(dims, ncenters) #create the sum of gaussian function iterations = 1 # starting iteration temperature = 1.0 # starting temperature alpha = 0.9999 # alpha used for decreasing the temperature start = 10.0 * np.random.ranf(dims) #starting point # perform simulated annealing current = start # while the tempearture is above the minimum tolerance and the # iterations are less than the maximum number of iterations # keep searching while iterations <= 100000: #print the current location and the value of its point print(
def __init__(self, dims, ncenters): self.sog = SG.SumofGaussians(dims, ncenters) self.dims = dims self.loc = r.ranf(dims) * 10 self.eval = self.sog.Eval(self.loc) self.delta = 1