Ask Your Question
2

Quadratic optimization subject to boundary constraints

asked 2017-05-23 11:12:40 +0100

T.S. Lim gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-05-24 07:46:23 +0100

mforets gravatar image

updated 2017-05-24 08:47:58 +0100

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_quadratic = 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[0] + 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 = [5]*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)).

edit flag offensive delete link more
1

answered 2017-05-24 01:02:44 +0100

tmonteil gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-05-23 11:12:40 +0100

Seen: 894 times

Last updated: May 24 '17