Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any quadratic programming function that can have both lower and upper bounds - Python

Normally I have been using GNU Octave to solve quadratic programming problems.

I solve problems like

x = 1/2x'Qx + c'x

With subject to

A*x <= b
lb <= x <= ub

Where lb and ub are lower bounds and upper bounds, e.g limits for x

My Octave code looks like this when I solve. Just one simple line

U = quadprog(Q, c, A, b, [], [], lb, ub);

The square brackets [] are empty because I don't need the equality constraints

Aeq*x = beq,

So my question is: Is there a easy to use quadratic solver in Python for solving problems

x = 1/2x'Qx + c'x

With subject to

A*x <= b
lb <= x <= ub

Or subject to

b_lb <= A*x <= b_ub
lb <= x <= ub
like image 954
Nazi Bhattacharya Avatar asked Apr 22 '19 20:04

Nazi Bhattacharya


People also ask

How do you write a quadratic equation in Python?

A quadratic equation is a second-degree equation. The standard form of the quadratic equation in python is written as px² + qx + r = 0. The coefficients in the above equation are p, q, r.

Which method belongs to quadratic programming?

augmented Lagrangian, conjugate gradient, gradient projection, extensions of the simplex algorithm.

What is the quadratic programming problem?

A quadratic programming (QP) problem has a quadratic cost function and linear constraints. Such problems are encountered in many real-world applications. In addition, many general nonlinear programming algorithms require solution of a quadratic programming subproblem at each iteration. As seen in Eqs.

What is integer quadratic programming?

Mixed-integer quadratic programming (MIQP) is the problem of optimizing a quadratic function over points in a polyhedral set where some of the components are restricted to be integral.

How to solve quadratic equations in Python?

Python program to solve quadratic equation. Given a quadratic equation the task is solve the equation or find out the roots of the equation. Standard form of quadratic equation is –. ax 2 + bx + c where, a, b, and c are coefficient and real numbers and also a ≠ 0. If a is equal to 0 that equation is not valid quadratic equation.

What is quadratic programming?

Specifically, one seeks to optimize (minimize or maximize) a multivariate quadratic function subject to linear constraints on the variables. Quadratic programming is a type of nonlinear programming . "Programming" in this context refers to a formal procedure for solving mathematical problems.

What is lower and upper bound theory in Computer Science?

Lower and Upper Bound Theory. The Lower and Upper Bound Theory provides a way to find the lowest complexity algorithm to solve a problem. Before understanding the theory, first lets have a brief look on what actually Lower and Upper bounds are. Lower Bound –.

What are equality constraints in quadratic programming?

Equality constraints. Quadratic programming is particularly simple when Q is positive definite and there are only equality constraints; specifically, the solution process is linear.


1 Answers

You can write your own solver based scipy.optimize, here is a small example on how to code your custom python quadprog():

# python3
import numpy as np
from scipy import optimize

class quadprog(object):

    def __init__(self, H, f, A, b, x0, lb, ub):
        self.H    = H
        self.f    = f
        self.A    = A
        self.b    = b
        self.x0   = x0
        self.bnds = tuple([(lb, ub) for x in x0])
        # call solver
        self.result = self.solver()

    def objective_function(self, x):
        return 0.5*np.dot(np.dot(x.T, self.H), x) + np.dot(self.f.T, x)

    def solver(self):
        cons = ({'type': 'ineq', 'fun': lambda x: self.b - np.dot(self.A, x)})
        optimum = optimize.minimize(self.objective_function, 
                                    x0          = self.x0.T,
                                    bounds      = self.bnds,
                                    constraints = cons, 
                                    tol         = 10**-3)
        return optimum

Here is how to use this, using the same variables from the first example provided in matlab-quadprog:

# init vars
H  = np.array([[ 1, -1],
               [-1,  2]])

f  = np.array([-2, -6]).T

A  = np.array([[ 1, 1],
               [-1, 2],
               [ 2, 1]])

b  = np.array([2, 2, 3]).T
x0 = np.array([1, 2])
lb = 0
ub = 2

# call custom quadprog
quadprog  = quadprog(H, f, A, b, x0, lb, ub)
print(quadprog.result)

The output of this short snippet is:

     fun: -8.222222222222083
     jac: array([-2.66666675, -4.        ])
 message: 'Optimization terminated successfully.'
    nfev: 8
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([0.66666667, 1.33333333])

For more information on how to use scipy.optimize.minimize please refer to the docs.

like image 175
SuperKogito Avatar answered Sep 22 '22 00:09

SuperKogito