@@ -0,0 +1,1519 @@
+'''%0A%0AThis is my Python version of a Latin-Hypercube Search algorithm %0Anot presented in%0A%0A    %22Clever Algorithms: Nature-Inspired Programming Recipes%22.%0A%0Abut based on the Random Search algorithm.%0A%0AFind the book and the Ruby source codes on GitHub: %0A  %0A   https://github.com/jbrownlee/CleverAlgorithm:s%0A'''%0Aimport random%0A%0Adef objective_function(vector):%0A   return sum(%5Bx ** 2.0 for x in vector%5D)%0A%0Adef latin_hypercube(minmax,max_iter):%0A    segmentSize = float(len(minmax)) / float(max_iter)%0A    pos = %5B%5D%0A    for j in range(max_iter):%0A        segmentMin = j * segmentSize%0A        point = segmentMin + (random.random() * segmentSize)%0A        pointValue = %5B(point * (minmax%5Bi%5D%5B1%5D - minmax%5Bi%5D%5B0%5D)) + minmax%5Bi%5D%5B0%5D for i in range(len(minmax))%5D%0A        pos.append(pointValue)%0A    return pos%0A#    return %5Bminmax%5Bi%5D%5B0%5D + ((minmax%5Bi%5D%5B1%5D - minmax%5Bi%5D%5B0%5D) * random.random()) for i in range(len(minmax))%5D%0A%0Adef search(search_space, max_iter):%0A    best = None%0A    lhs = latin_hypercube(search_space,max_iter)%0A    for i in range(max_iter):%0A        candidate = %7B%7D%0A        candidate%5B%22pos%22%5D = lhs%5Bi%5D%0A        candidate%5B%22cost%22%5D = objective_function(candidate%5B%22pos%22%5D)%0A        if best is None or candidate%5B%22cost%22%5D %3C best%5B%22cost%22%5D: best = candidate%0A        print %22 %3E iteration %25i, best=%25.4g%22 %25 (i, best%5B%22cost%22%5D)%0A    return best%0A%0Aif __name__ == %22__main__%22:%0A%0A    search_space = %5B%5B-5,5%5D,%5B-5,5%5D%5D%0A    problem_size = 2%0A    max_iter = 100%0A    best = search(search_space, max_iter)%0A    print  %22Done! Best solution: f = %25.4g, v =%22 %25 best%5B%22cost%22%5D, best%5B%22pos%22%5D%0A

add latin-hypercube search algorithm