e2nIEE / pandapower
1
# Copyright (c) 1996-2015 PSERC. All rights reserved.
2
# Use of this source code is governed by a BSD-style
3
# license that can be found in the LICENSE file.
4

5 1
"""Quadratic Program Solver for PYPOWER.
6
"""
7

8 1
import sys
9

10 1
from pandapower.pypower.qps_pips import qps_pips
11
#from pandapower.pypower.qps_ipopt import qps_ipopt
12
#from pandapower.pypower.qps_cplex import qps_cplex
13
#from pandapower.pypower.qps_mosek import qps_mosek
14
#from pandapower.pypower.qps_gurobi import qps_gurobi
15

16 1
from pandapower.pypower.util import have_fcn
17

18

19 1
def qps_pypower(H, c=None, A=None, l=None, u=None, xmin=None, xmax=None,
20
                x0=None, opt=None):
21
    """Quadratic Program Solver for PYPOWER.
22

23
    A common wrapper function for various QP solvers.
24
    Solves the following QP (quadratic programming) problem::
25

26
        min 1/2 x'*H*x + c'*x
27
         x
28

29
    subject to::
30

31
        l <= A*x <= u       (linear constraints)
32
        xmin <= x <= xmax   (variable bounds)
33

34
    Inputs (all optional except C{H}, C{c}, C{A} and C{l}):
35
        - C{H} : matrix (possibly sparse) of quadratic cost coefficients
36
        - C{c} : vector of linear cost coefficients
37
        - C{A, l, u} : define the optional linear constraints. Default
38
        values for the elements of C{l} and C{u} are -Inf and Inf,
39
        respectively.
40
        - C{xmin}, C{xmax} : optional lower and upper bounds on the
41
        C{x} variables, defaults are -Inf and Inf, respectively.
42
        - C{x0} : optional starting value of optimization vector C{x}
43
        - C{opt} : optional options structure with the following fields,
44
        all of which are also optional (default values shown in parentheses)
45
            - C{alg} (0) - determines which solver to use
46
                -   0 = automatic, first available of BPMPD_MEX, CPLEX,
47
                        Gurobi, PIPS
48
                - 100 = BPMPD_MEX
49
                - 200 = PIPS, Python Interior Point Solver
50
                pure Python implementation of a primal-dual
51
                interior point method
52
                - 250 = PIPS-sc, a step controlled variant of PIPS
53
                - 300 = Optimization Toolbox, QUADPROG or LINPROG
54
                - 400 = IPOPT
55
                - 500 = CPLEX
56
                - 600 = MOSEK
57
                - 700 = Gurobi
58
            - C{verbose} (0) - controls level of progress output displayed
59
                - 0 = no progress output
60
                - 1 = some progress output
61
                - 2 = verbose progress output
62
            - C{max_it} (0) - maximum number of iterations allowed
63
                - 0 = use algorithm default
64
            - C{bp_opt} - options vector for BP
65
            - C{cplex_opt} - options dict for CPLEX
66
            - C{grb_opt}   - options dict for gurobipy
67
            - C{ipopt_opt} - options dict for IPOPT
68
            - C{pips_opt}  - options dict for L{qps_pips}
69
            - C{mosek_opt} - options dict for MOSEK
70
            - C{ot_opt}    - options dict for QUADPROG/LINPROG
71
        - C{problem} : The inputs can alternatively be supplied in a single
72
        C{problem} dict with fields corresponding to the input arguments
73
        described above: C{H, c, A, l, u, xmin, xmax, x0, opt}
74

75
    Outputs:
76
        - C{x} : solution vector
77
        - C{f} : final objective function value
78
        - C{exitflag} : exit flag
79
            - 1 = converged
80
            - 0 or negative values = algorithm specific failure codes
81
        - C{output} : output struct with the following fields:
82
            - C{alg} - algorithm code of solver used
83
            - (others) - algorithm specific fields
84
        - C{lmbda} : dict containing the Langrange and Kuhn-Tucker
85
        multipliers on the constraints, with fields:
86
            - C{mu_l} - lower (left-hand) limit on linear constraints
87
            - C{mu_u} - upper (right-hand) limit on linear constraints
88
            - C{lower} - lower bound on optimization variables
89
            - C{upper} - upper bound on optimization variables
90

91

92
    Example from U{http://www.uc.edu/sashtml/iml/chap8/sect12.htm}:
93

94
        >>> from numpy import array, zeros, Inf
95
        >>> from scipy.sparse import csr_matrix
96
        >>> H = csr_matrix(array([[1003.1,  4.3,     6.3,     5.9],
97
        ...                       [4.3,     2.2,     2.1,     3.9],
98
        ...                       [6.3,     2.1,     3.5,     4.8],
99
        ...                       [5.9,     3.9,     4.8,     10 ]]))
100
        >>> c = zeros(4)
101
        >>> A = csr_matrix(array([[1,       1,       1,       1   ],
102
        ...                       [0.17,    0.11,    0.10,    0.18]]))
103
        >>> l = array([1, 0.10])
104
        >>> u = array([1, Inf])
105
        >>> xmin = zeros(4)
106
        >>> xmax = None
107
        >>> x0 = array([1, 0, 0, 1])
108
        >>> solution = qps_pips(H, c, A, l, u, xmin, xmax, x0)
109
        >>> round(solution["f"], 11) == 1.09666678128
110
        True
111
        >>> solution["converged"]
112
        True
113
        >>> solution["output"]["iterations"]
114
        10
115

116
    @author: Ray Zimmerman (PSERC Cornell)
117
    """
118 1
    if opt is None:
119 0
        opt = {}
120
#    if x0 is None:
121
#        x0 = array([])
122
#    if xmax is None:
123
#        xmax = array([])
124
#    if xmin is None:
125
#        xmin = array([])
126

127
    ## default options
128 1
    if 'alg' in opt:
129 1
        alg = opt['alg']
130
    else:
131 0
        alg = 0
132

133 1
    if 'verbose' in opt:
134 1
        verbose = opt['verbose']
135
    else:
136 0
        verbose = 0
137

138
    ##----- call the appropriate solver  -----
139
    # if alg == 0 or alg == 200 or alg == 250:    ## use MIPS or sc-MIPS
140
    ## set up options
141 1
    if 'pips_opt' in opt:
142 1
        pips_opt = opt['pips_opt']
143
    else:
144 0
        pips_opt = {}
145

146 1
    if 'max_it' in opt:
147 0
        pips_opt['max_it'] = opt['max_it']
148

149 1
    if alg == 200:
150 1
        pips_opt['step_control'] = False
151
    else:
152 0
        pips_opt['step_control'] = True
153

154 1
    pips_opt['verbose'] = verbose
155

156
    ## call solver
157 1
    x, f, eflag, output, lmbda = \
158
        qps_pips(H, c, A, l, u, xmin, xmax, x0, pips_opt)
159
#    elif alg == 400:                    ## use IPOPT
160
#        x, f, eflag, output, lmbda = \
161
#            qps_ipopt(H, c, A, l, u, xmin, xmax, x0, opt)
162
#    elif alg == 500:                    ## use CPLEX
163
#        x, f, eflag, output, lmbda = \
164
#            qps_cplex(H, c, A, l, u, xmin, xmax, x0, opt)
165
#    elif alg == 600:                    ## use MOSEK
166
#        x, f, eflag, output, lmbda = \
167
#            qps_mosek(H, c, A, l, u, xmin, xmax, x0, opt)
168
#    elif 700:                           ## use Gurobi
169
#        x, f, eflag, output, lmbda = \
170
#            qps_gurobi(H, c, A, l, u, xmin, xmax, x0, opt)
171
#     else:
172
#         print('qps_pypower: {} is not a valid algorithm code\n'.format(alg))
173

174 1
    if 'alg' not in output:
175 1
        output['alg'] = alg
176

177 1
    return x, f, eflag, output, lmbda

Read our documentation on viewing source code .

Loading