How can I minimize a function in Python, without using gradients, and using constraints and ranges?
<p><strong><em>EDIT: looks like this was already answered before <a href="https://stackoverflow.com/questions/21670080/how-to-find-global-minimum-in-python-optimization-with-bounds">here</a></em></strong></p>

<p>It didn't show up in my searches because I didn't know the right nomenclature. I'll leave the question here for now in case someone arrives here because of the constraints. </p>

<hr>

<p>I'm trying to optimize a function which is flat on almost all points ("steps function", but in a higher dimension). </p>

<p>The objective is to optimize a set of weights, that must sum to one, and are the parameters of a function which I need to minimize.  </p>

<p>The problem is that, as the function is flat at most points, gradient techniques fail because they immediately converge on the starting "guess". </p>

<p>My hypothesis is that this could be solved with (a) Annealing or (b) Genetic Algos. Scipy sends me to <a href="http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.basinhopping.html" rel="nofollow noreferrer">basinhopping</a>. However, I cannot find any way to use the constraint (the weights must sum to 1) or ranges (weights must be between 0 and 1) using scipy. </p>

<p>Actual question: How can I solve a minimization problem without gradients, and also use constraints and ranges for the input variables?  </p>

<hr>

<p>The following is a toy example (evidently this one could be solved using the gradient):  </p>

<pre><code># import minimize 
from scipy.optimize import minimize

# define a toy function to minimize 
def my_small_func(g): 
    x = g[0]
    y = g[1]
    return x**2 - 2*y + 1

# define the starting guess 
start_guess = [.5,.5]

# define the acceptable ranges (for [g1, g2] repectively)
my_ranges = ((0,1),(0,1))

# define the constraint (they must always sum to 1)
def constraint(g): 
    return g[0] + g[1] - 1 
cons = {'type':'eq', 'fun': constraint}

# minimize 
minimize(my_small_func, x0=start_guess, method='SLSQP',
         bounds=rranges, constraints=cons)
</code></pre>