# Quadratic optimization subject to boundary constraints

Hello, I was wondering if Sagemath can be used to optimize a quadratic function

y = b_0 + b_1x_1 + b_11x_1^2 + b_2x_2 + b_22x_2^2 + ... + b_kx_k + b_kkx_kk^2

subject to boundary constraints

1 <= x_i <= 9, i = 1, ..., k

Thanks, T.S. Lim

edit retag close merge delete

Sort by » oldest newest most voted

Assuming that in your problem the $b$'s have given numerical values, let's try minimize_constrained:

# number of variables
k = 5

# generate some random data
b_linear = random_vector(RDF, k)
b_const = random_vector(RDF, 1)


Then, minimize_constrained takes essentially three arguments: the objective function (symbolic function or standard python function), a list with constraints in $\geq 0$ form (or tuple(s) of lower/upper bounds as in this case), and an initial point.

# optimization variables
x = [SR.var('x' + str(i)) for i in range(k)]

# constraints
cons = [(1, 9)] * k

# cost function
func = b_const + sum([b_linear[i]*x[i] for i in range(k)]) \
+ sum([b_quadratic[i]*x[i]^2 for i in range(k)])

# initial point
x0 = *k

# solve
xopt = minimize_constrained(func, cons, x0, algorithm='l-bfgs-b')

# show optimal value and optimal point
func({x[i] : xopt[i] for i in range(k)}), xopt


The output looks like: (3.4200707906802412, (1.0, 1.0, 4.3222000559500335, 1.0, 1.0)).

more

You can use cvxopt,wich shipped with Sage, see

more